My Project
NPLRuntimeState.h
1 #pragma once
2 #include "NPLMessageQueue.h"
3 #include "NPLScriptingState.h"
4 #include "NPLCommon.h"
5 #include "NeuronFileState.h"
6 #include "INPLRuntimeState.h"
7 #include "INPLScriptingState.h"
8 #include "IAttributeFields.h"
9 #include "util/mutex.h"
10 #include "util/unordered_array.hpp"
11 #include <set>
12 
13 #include <boost/thread.hpp>
14 #include <boost/noncopyable.hpp>
15 #include <boost/shared_ptr.hpp>
16 #include <boost/enable_shared_from_this.hpp>
17 
18 
19 namespace ParaEngine
20 {
21  struct DLLPlugInEntity;
22 }
23 namespace NPL
24 {
25  using namespace std;
26  class INPLActivationFile;
27  class CNeuronFileState;
28 
29 
37  public INPLRuntimeState,
39  public boost::enable_shared_from_this<CNPLRuntimeState>,
40  private boost::noncopyable
41  {
42  public:
43  typedef std::map<int,NPLTimer_ptr> NPLTimer_Pool_Type;
44  typedef boost::signals2::signal<void(CNPLRuntimeState* pRuntimeState)> Signal_StateLoaded_t;
45 
47  CNPLRuntimeState(const string & name, NPLRuntimeStateType type_ = NPLRuntimeStateType_NPL);
48  virtual ~CNPLRuntimeState();
49 
50  ATTRIBUTE_DEFINE_CLASS(CNPLRuntimeState);
51 
53  virtual int InstallFields(ParaEngine::CAttributeClass* pClass, bool bOverride);
54 
55  ATTRIBUTE_METHOD1(CNPLRuntimeState, GetProcessedMsgCount_s, int*) { *p1 = cls->GetProcessedMsgCount(); return S_OK; }
56  ATTRIBUTE_METHOD1(CNPLRuntimeState, GetCurrentQueueSize_s, int*) { *p1 = cls->GetCurrentQueueSize(); return S_OK; }
57  ATTRIBUTE_METHOD1(CNPLRuntimeState, GetTimerCount_s, int*) { *p1 = cls->GetTimerCount(); return S_OK; }
58  ATTRIBUTE_METHOD1(CNPLRuntimeState, SetMsgQueueSize_s, int) { cls->SetMsgQueueSize(p1); return S_OK; }
59  ATTRIBUTE_METHOD1(CNPLRuntimeState, GetMsgQueueSize_s, int*) { *p1 = cls->GetMsgQueueSize(); return S_OK; }
60  ATTRIBUTE_METHOD1(CNPLRuntimeState, HasDebugHook_s, bool*) { *p1 = cls->HasDebugHook(); return S_OK; }
61  ATTRIBUTE_METHOD1(CNPLRuntimeState, IsPreemptive_s, bool*) { *p1 = cls->IsPreemptive(); return S_OK; }
62  ATTRIBUTE_METHOD1(CNPLRuntimeState, PauseAllPreemptiveFunction_s, bool) { cls->PauseAllPreemptiveFunction(p1); return S_OK; }
63  ATTRIBUTE_METHOD1(CNPLRuntimeState, IsAllPreemptiveFunctionPaused_s, bool*) { *p1 = cls->IsAllPreemptiveFunctionPaused(); return S_OK; }
64  ATTRIBUTE_METHOD1(CNPLRuntimeState, GetFileName_s, const char**) { *p1 = cls->GetCurrentFileName(); return S_OK; }
65 
67  void Init();
68 
72  void Reset_Imp();
73 
78  void Reset(const char* onResetScode = NULL);
79 
80 
83  int Run_Async();
84 
89  int Run();
90 
96  int Process();
97 
99  virtual const string& GetName() const {return m_name;}
100 
104  int Stop();
105 
107  int Stop_Async();
108 
114  int GetCurrentQueueSize();
115 
121  int GetProcessedMsgCount();
122 
126  int GetMsgQueueSize();
127 
131  void SetMsgQueueSize(int nSize = 500);
132 
133 
137  void WaitForMessage(int nMessageCount = -1);
138 
143  NPLMessage_ptr PeekMessage(int nIndex);
144 
148  NPLMessage_ptr PopMessageAt(int nIndex);
149 
150  public:
151  virtual const std::string& GetIdentifier();
152 
154  virtual int activate(const char * sNPLFilename, const char* sCode, int nCodeLength=0, int priority=2, int reliability=4);
155 
168  template <typename StringType>
169  bool LoadFile_any(const StringType & filepath, bool bReload = false, lua_State* L = 0, bool bNoReturn = false);
175  template <typename StringType>
176  NPLReturnCode ActivateFile_any(const StringType& filepath, const char * code = NULL, int nLength=0);
177 
186  virtual NPLReturnCode Activate_async(const string & filepath, const char * code = NULL,int nLength=0, int priority=0);
187  virtual NPLReturnCode Loadfile_async(const string & filepath, int priority = 0);
188 
195  virtual NPLReturnCode Activate_async(NPLMessage_ptr& msg, int priority=0);
196 
198  virtual NPLReturnCode ActivateLocal(const char* filepath, const char * code = NULL,int nLength=0, int priority=0);
199 
208  virtual NPLReturnCode SendMessage(NPLMessage_ptr& msg, int priority=0);
209 
210 
212  virtual const char* GetCurrentMsg();
213 
215  virtual int GetCurrentMsgLength();
216 
218  virtual INPLRuntime* GetNPLRuntime();
219 
225  virtual void WriteLog(const char* text, int nTextLen=0, int nLogType = 0);
226 
228  //
229  // Timer functions
230  //
232 
233  int GetTimerCount();
234 
246  virtual bool SetTimer(int nIDEvent, float fElapse, const char* sNeuronFile);
247 
255  virtual bool KillTimer(int nIDEvent);
256 
267  virtual bool ChangeTimer(int nIDEvent, int dueTime, int period);
268 
270  virtual INPLRuntimeState* GetNPLStateInterface() {return this;}
271 
276  virtual void RegisterFile(const char* sFilename, INPLActivationFile* pFileHandler = NULL);
277 
279  virtual void call(const char * sNPLFilename, const char* sCode, int nCodeLength = 0);;
280 
282  std::string& GetStringBuffer(int nIndex = 0);
283 
284 
286  Signal_StateLoaded_t StateLoaded;
287 
291  int ProcessMsg(NPLMessage_ptr msg);
292 
294  int SendTick();
295 
297  CNeuronFileState* GetNeuronFileState(const std::string& filename, bool bCreateIfNotExist = true);
298 
302  bool HasDebugHook();
303 
307  bool IsPreemptive();
308 
310  bool IsAllPreemptiveFunctionPaused() const;
311  void PauseAllPreemptiveFunction(bool val);
312 
313  protected:
314 
316  void LoadNPLState();
317 
323  int TickTimers(DWORD nTickCount);
324 
325  int FrameMoveTick();
326 
329  {
330  public:
331  CCurrentMessage(CNPLRuntimeState* pState, const char* msg, int nLength):m_pState(pState){
332  if(m_pState)
333  m_pState->SetCurrentMessage(msg,nLength);
334  }
335  ~CCurrentMessage(){
336  if(m_pState)
337  m_pState->SetCurrentMessage(NULL, 0);
338  }
339  CNPLRuntimeState* m_pState;
340  };
342  void SetCurrentMessage(const char* msg=NULL, int nLength = 0);
343 
345  IMonoScriptingState* GetMonoState();
346 
347  void SetPreemptive(bool val);
348  private:
349  typedef map<std::string, ParaEngine::DLLPlugInEntity*> DLL_Plugin_Map_Type;
350  typedef std::vector<NPLTimer_ptr> NPLTimer_TempPool_Type;
352  DLL_Plugin_Map_Type m_dll_plugins_map;
353 
355  const string m_name;
356 
358  NPLTimer_Pool_Type m_activeTimers;
359 
360  NPLTimer_TempPool_Type m_temp_timer_pool;
361 
363  const NPLRuntimeStateType m_type;
364 
366  CNPLMessageQueue m_input_queue;
367 
370 
372  ParaEngine::mutex m_mutex;
373 
375  boost::condition_variable m_semaphore;
376 
378  bool m_bUseMessageEvent;
379 
381  bool m_bIsProcessing;
382 
384  bool m_bIsPreemptive;
385 
387  bool m_bPauseAllPreemptiveFunction;
388 
390  const char* m_current_msg;
391 
393  int m_current_msg_length;
394 
398  int m_processed_msg_count;
399 
401  int m_nFrameMoveCount;
404  IMonoScriptingState* m_pMonoScriptingState;
405 
407  std::vector <std::string> m_string_buffers;
408 
410  std::map<std::string, INPLActivationFile*> m_act_files_cpp;
411 
415  std::map<std::string, CNeuronFileState*> m_neuron_files;
420 
421  friend class CNPLRuntime;
422  };
423 
426  {
427  bool operator()( const NPLRuntimeState_ptr & a, const NPLRuntimeState_ptr & b ) const
428  { return a.get() < b.get(); }
429  };
430 }
compare functions for runtime state ptr
Definition: NPLRuntimeState.h:425
an attribute class is a collection of attribute fields.
Definition: AttributeClass.h:10
virtual const string & GetName() const
return the name of this runtime state.
Definition: NPLRuntimeState.h:99
define this to enable debugging of NPL code in visual studio
Definition: INPL.h:9
virtual INPLRuntimeState * GetNPLStateInterface()
get the INPLRuntimeState state interface.
Definition: NPLRuntimeState.h:270
INPLRuntimeState interface for DLL interface.
Definition: INPLRuntimeState.h:27
different physics engine has different winding order.
Definition: EventBinding.h:32
Definition: unordered_array.hpp:9
A runtime state contains the scripting runtime stack and can be run in a single thread.
Definition: NPLRuntimeState.h:35
Signal_StateLoaded_t StateLoaded
signal: called when state is first loaded
Definition: NPLRuntimeState.h:286
Message queue implementation.
Definition: NPLMessageQueue.h:301
a NPL scripting state (wrapper of lua State), for binding c++ classes to lua.
Definition: NPLScriptingState.h:23
CNPLRuntime is a wrapper of the NPL application programming interface (NPL API).
Definition: NPLRuntime.h:64
Definition: minilua.c:461
A common interface for all classes implementing IAttributeFields By implementing this class&#39;s virtual...
Definition: IAttributeFields.h:59
Definition: inftrees.h:24
Definition: INPLAcitvationFile.h:18
construct this to ensure matching calls to SetCurrentMessage().
Definition: NPLRuntimeState.h:328
This is the NPLMono DLL plugin interface.
Definition: INPLScriptingState.h:90
cross platform mutex
Definition: mutex.h:95
NPL Runtime Environment interface.
Definition: INPLRuntime.h:30
each neuron file that is activated will have a CNeuronFileState structure kept.
Definition: NeuronFileState.h:14