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