Phasor  01.00.10.059
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Manager.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <map>
4 #include <deque>
5 #include <list>
6 #include <array>
7 #include <memory>
8 #include <stack>
9 #include "Common/Common.h"
10 
11 typedef unsigned long DWORD;
12 
13 // Forward declare relevant classes
14 namespace Lua
15 {
16  class State;
17 }
18 
19 namespace Manager
20 {
21  class ScriptState;
22 }
23 
24 //typedef Manager::ScriptState ScriptState;
25 
26 // Namespace: Manager
27 // Provides an interface for managing scripts of different types.
28 // Currently only Lua is supported, but others can be integrated easily.
29 namespace Manager
30 {
31 
32  class Result;
33  class ScriptState;
34  class CallHandler;
35 
36  // nice article about aggregate types http://stackoverflow.com/questions/4178175/what-are-aggregates-and-pods-and-how-why-are-they-special
38  {
41  const char* name;
42  int minargs;
43  std::array<Common::obj_type, 10> fmt; // change max args as needed
44  };
45 
51 
53 
54  // --------------------------------------------------------------------
55 
56  // Creates a new script, doesn't load any files.
57  std::unique_ptr<ScriptState> CreateScript();
58 
59  void CloseScript(std::unique_ptr<ScriptState>& state);
60 
61  // Register the specified functions with the script
62  void RegisterFunctions(ScriptState& state, const ScriptCallback* funcs, size_t n);
63 
65  {
66  std::string func;
67  bool scriptInvoked; // false: called script, true script called
68 
69  ScriptCallstack(const std::string& func, bool scriptInvoked)
70  : func(func), scriptInvoked(scriptInvoked) {}
71  };
72 
73 #define __NO_RET
74  // Scripts should derive from this and implement the getters.
76  {
77  private:
78  int nargs;
79 
80  protected:
81 
83 
84  // Get the next argument for the function, if it's not of the
85  // expected type an error should be described and raised through
86  // RaiseError.
87  virtual std::unique_ptr<MObject> GetArgument(Common::obj_type expected) = 0;
88 
89  CallHandler(ScriptState& state, const ScriptCallback* cb, int nargs);
90  public:
91 
93 
94  // Invokes the call to the setup function
96 
97  // This function should propagate the error to the script's
98  // virtual machine. It should not return.
99  virtual void __NO_RET RaiseError(const std::string& err) = 0;
100  };
101 
102  // --------------------------------------------------------------------
103  // Class: ScriptState
104  // Classes compatible with this interface should inherit this class.
106  {
107  public:
108  virtual ~ScriptState(){}
109 
110  std::stack<std::unique_ptr<ScriptCallstack>> callstack;
111 
112  void PushCall(const std::string& func, bool scriptInvoked);
113  void PopCall();
114 
115  // Loads the specified file
116  virtual void DoFile(const char* file) = 0;
117 
118  // Checks if the specified function is defined in the script
119  virtual bool HasFunction(const char* name) = 0;
120 
121  // Registers a C function with the script
122  virtual void RegisterFunction(const ScriptCallback* cb) = 0;
123 
124  // Calls a function with an optional timeout
125  virtual MObject::unique_deque Call(const char* name,
126  const MObject::unique_list& args) = 0;
127  virtual MObject::unique_deque Call(const char* name) = 0;
128  };
129 
130  // --------------------------------------------------------------------
131  // Class: Result
132  // Provides an interface for retrieving values from scripts
133  class Result
134  {
135  protected:
136 
138 
139  // Copy the other result into this one
140  void SetData(const Result& other);
141 
142  public:
143  Result();
144  Result(const Result& other);
145  Result& operator=(const Result& rhs);
146  ~Result();
147 
148  // Returns number of items stored
149  size_t size() const;
150 
151  // Read the next result
152  template <class T>
153  T& ReadResult(size_t index) {
154  return static_cast<T&>(*result[index]);
155  }
156 
157  // Clear the current data
158  void Clear();
159 
160  MObject& ReadObject(size_t index) { return ReadResult<MObject>(index); }
161  MObjBool& ReadBool(size_t index) { return ReadResult<MObjBool>(index); }
162  MObjNumber& ReadNumber(size_t index) { return ReadResult<MObjNumber>(index); }
163  MObjString& ReadString(size_t index) { return ReadResult<MObjString>(index); }
164  MObjTable& ReadTable(size_t index) { return ReadResult<MObjTable>(index); }
165 
166  obj_type GetType(size_t index);
167  void Replace(size_t index, std::unique_ptr<Common::Object> obj);
168 
169  friend class Caller;
170  };
171 
172  // --------------------------------------------------------------------
173  // Class: Caller
174  // Provides an interface for passing parameters to scripts.
175  class Caller
176  {
177  protected:
179 
180  // Copy other into this object
181  void SetData(const Caller& other);
182 
183  public:
184  Caller();
185  Caller(const Caller& other);
186  ~Caller();
187  Caller& operator=(const Caller& rhs);
188 
189  // Adds an argument to the list, which is passed to the next function called
190  void AddArgNil();
191  void AddArg(bool b);
192  void AddArg(const std::string& str);
193  void AddArg(int value);
194  void AddArg(DWORD value);
195  void AddArg(float value);
196  void AddArg(double value);
197  void AddArg(const std::map<std::string, std::string>& table);
198  void AddArg(std::unique_ptr<Common::Object> obj);
199 
200  // Clears the current argument list
201  void Clear();
202 
203  // Calls the specified function on the specified script.
204  Result Call(ScriptState& state, const std::string& function, bool* found);
205  Result Call(ScriptState& state, const std::string& function);
206  };
207 }
208