bf8ec2069ecc9b4e30da6f67ee44c7bcb1a49f70
[scilab.git] / scilab / modules / core / includes / runner.hxx
1 /*
2  *  Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3  *  Copyright (C) 2010-2010 - DIGITEO - Bruno JOFRET
4  *  Copyright (C) 2014-2015 - Scilab Enterprises - Cedric Delamarre
5  *
6  * Copyright (C) 2012 - 2016 - Scilab Enterprises
7  *
8  * This file is hereby licensed under the terms of the GNU GPL v2.0,
9  * pursuant to article 5.3.4 of the CeCILL v.2.1.
10  * This file was originally licensed under the terms of the CeCILL v2.1,
11  * and continues to be available under such terms.
12  * For more information, see the COPYING file which you should have received
13  * along with this program.
14  *
15  */
16
17 #ifndef __RUNNER_HXX__
18 #define __RUNNER_HXX__
19
20 #include <atomic>
21 #include <memory>
22
23 #include "exp.hxx"
24 #include "runvisitor.hxx"
25
26 extern "C"
27 {
28 #include "dynlib_core.h"
29 #include "storeCommand.h" // command_origin_t
30 }
31
32 class CORE_IMPEXP Runner
33 {
34 public :
35     Runner(ast::Exp* _theProgram, ast::RunVisitor *_visitor) : m_theProgram(_theProgram), m_visitor(_visitor), m_iCommandOrigin(NONE), m_isInterruptible(true)
36     { }
37
38     Runner(ast::Exp* _theProgram, ast::RunVisitor *_visitor, command_origin_t _iCommandOrigin, bool _isInterruptible) : m_theProgram(_theProgram), m_visitor(_visitor), m_iCommandOrigin(_iCommandOrigin), m_isInterruptible(_isInterruptible)
39     { }
40
41     ~Runner()
42     {
43         delete m_theProgram;
44     }
45
46     ast::RunVisitor *getVisitor()
47     {
48         return m_visitor.get();
49     }
50
51     ast::Exp* getProgram()
52     {
53         return m_theProgram;
54     }
55
56     command_origin_t getCommandOrigin()
57     {
58         return m_iCommandOrigin;
59     }
60
61     void setCommandOrigin(command_origin_t _origin)
62     {
63         m_iCommandOrigin = _origin;
64     }
65
66     bool isInterruptible()
67     {
68         return m_isInterruptible;
69     }
70
71 private :
72     ast::Exp* m_theProgram;
73     std::unique_ptr<ast::RunVisitor> m_visitor;
74     command_origin_t m_iCommandOrigin;
75     bool m_isInterruptible;
76 };
77
78 // static members to manage execution
79 class StaticRunner
80 {
81 public:
82     static int launch(void);
83     static void setRunner(Runner* _RunMe);
84     static Runner* getRunner(void);
85     static bool isRunnerAvailable(void);
86     static bool isRunning(void);
87     static bool isInterruptibleCommand(void);
88     static command_origin_t getCommandOrigin();
89     static void execAndWait(ast::Exp* _theProgram, ast::RunVisitor *_visitor,
90                             bool _isInterruptible, bool _isPrioritary, command_origin_t _iCommandOrigin);
91     static bool exec(ast::Exp* _theProgram, ast::RunVisitor *_visitor);
92     static void sendExecDoneSignal();
93     static void setCommandOrigin(command_origin_t _origin);
94
95 private:
96     static std::atomic<Runner*> m_RunMe;
97     static std::atomic<Runner*> m_CurrentRunner;
98 };
99
100 extern "C"
101 {
102     void StaticRunner_launch(void);
103     int StaticRunner_isRunnerAvailable(void);
104     int StaticRunner_isInterruptibleCommand(void);
105     command_origin_t StaticRunner_getCommandOrigin(void);
106     void StaticRunner_setCommandOrigin(command_origin_t _origin);
107 }
108
109 #endif /* !__RUNNER_HXX__ */