]> git.tdb.fi Git - libs/core.git/blob - source/core/process.cpp
Add move semantics to Variant
[libs/core.git] / source / core / process.cpp
1 #include <msp/io/console.h>
2 #include "except.h"
3 #include "process.h"
4 #include "process_private.h"
5
6 using namespace std;
7
8 namespace Msp {
9
10 Process *Process::_self = nullptr;
11
12 Process::Process(const Private &p):
13         priv(new Private(p))
14 { }
15
16 Process::Process():
17         priv(new Private)
18 { }
19
20 Process &Process::self()
21 {
22         if(!_self)
23         {
24                 Private _priv;
25                 platform_get_self_info(_priv);
26                 _self = new Process(_priv);
27         }
28         return *_self;
29 }
30
31 void Process::set_working_directory(const FS::Path &d)
32 {
33         work_dir = d;
34 }
35
36 void Process::redirect_cin(IO::Base &io)
37 {
38         do_redirect(cin, io);
39 }
40
41 void Process::redirect_cout(IO::Base &io)
42 {
43         do_redirect(cout, io);
44 }
45
46 void Process::redirect_cerr(IO::Base &io)
47 {
48         do_redirect(cerr, io);
49 }
50
51 void Process::do_redirect(IO::Base *&ptr, IO::Base &io)
52 {
53         if(this==_self)
54         {
55                 if(&ptr==&cin)
56                         IO::cin.redirect(io);
57                 else if(&ptr==&cout)
58                         IO::cout.redirect(io);
59                 else if(&ptr==&cerr)
60                         IO::cerr.redirect(io);
61         }
62         else
63         {
64                 redirect = true;
65                 ptr = &io;
66         }
67 }
68
69 void Process::execute(const string &command, const Arguments &args)
70 {
71         execute(command, true, args);
72 }
73
74 void Process::execute(const FS::Path &command, const Arguments &args)
75 {
76         execute(command.str(), false, args);
77 }
78
79 unsigned Process::get_exit_code() const
80 {
81         if(!finished)
82                 throw invalid_state("not finished");
83         return exit_code;
84 }
85
86 } // namespace Msp