My Project
Classes | Public Types | Public Member Functions | Public Attributes | Protected Member Functions | Friends | List of all members
NPL::CNPLRuntimeState Class Reference

A runtime state contains the scripting runtime stack and can be run in a single thread. More...

#include <NPLRuntimeState.h>

Inheritance diagram for NPL::CNPLRuntimeState:
ParaEngine::IAttributeFields NPL::INPLRuntimeState ParaScripting::CNPLScriptingState ParaEngine::IObject ParaEngine::CRefCounted

Classes

class  CCurrentMessage
 construct this to ensure matching calls to SetCurrentMessage(). More...
 

Public Types

typedef std::map< int, NPLTimer_ptr > NPLTimer_Pool_Type
 
typedef boost::signals2::signal< void(CNPLRuntimeState *pRuntimeState)> Signal_StateLoaded_t
 
- Public Types inherited from ParaEngine::IAttributeFields
typedef ParaEngine::weak_ptr< IObject, IAttributeFieldsWeakPtr_type
 
- Public Types inherited from ParaEngine::IObject
typedef ParaEngine::weak_ptr< IObjectWeakPtr_type
 
- Public Types inherited from ParaScripting::CNPLScriptingState
enum  NPL_MemAllocatorType { MEM_ALLOC_TYPE_SYS_MALLOC, MEM_ALLOC_TYPE_POOL_MALLOC, MEM_ALLOC_TYPE_DL_MALLOC }
 NPL runs in its own thread. More...
 

Public Member Functions

 CNPLRuntimeState (const string &name, NPLRuntimeStateType type_=NPLRuntimeStateType_NPL)
 a type must be provided. More...
 
 ATTRIBUTE_DEFINE_CLASS (CNPLRuntimeState)
 
virtual int InstallFields (ParaEngine::CAttributeClass *pClass, bool bOverride)
 this class should be implemented if one wants to add new attribute. More...
 
 ATTRIBUTE_METHOD1 (CNPLRuntimeState, GetProcessedMsgCount_s, int *)
 
 ATTRIBUTE_METHOD1 (CNPLRuntimeState, GetCurrentQueueSize_s, int *)
 
 ATTRIBUTE_METHOD1 (CNPLRuntimeState, GetTimerCount_s, int *)
 
 ATTRIBUTE_METHOD1 (CNPLRuntimeState, SetMsgQueueSize_s, int)
 
 ATTRIBUTE_METHOD1 (CNPLRuntimeState, GetMsgQueueSize_s, int *)
 
 ATTRIBUTE_METHOD1 (CNPLRuntimeState, HasDebugHook_s, bool *)
 
 ATTRIBUTE_METHOD1 (CNPLRuntimeState, IsPreemptive_s, bool *)
 
 ATTRIBUTE_METHOD1 (CNPLRuntimeState, PauseAllPreemptiveFunction_s, bool)
 
 ATTRIBUTE_METHOD1 (CNPLRuntimeState, IsAllPreemptiveFunctionPaused_s, bool *)
 
 ATTRIBUTE_METHOD1 (CNPLRuntimeState, GetFileName_s, const char **)
 
void Init ()
 call this function before calling anything else. More...
 
void Reset_Imp ()
 reset NPL state. More...
 
void Reset (const char *onResetScode=NULL)
 reset NPL state. More...
 
int Run_Async ()
 this function will create a worker thread to run Run(), and return immediately. More...
 
int Run ()
 this function does not return until the run time state is about to be destroyed. More...
 
int Process ()
 this function should be called whenever there are messages in the input_queue for processing. More...
 
virtual const string & GetName () const
 return the name of this runtime state. More...
 
int Stop ()
 stop the thread by sending the quit message to this runtime state. More...
 
int Stop_Async ()
 same as Stop(), except that it just send the exist message, but does not wait for termination of the thread. More...
 
int GetCurrentQueueSize ()
 Get the current number of messages in the input message queue. More...
 
int GetProcessedMsgCount ()
 the total number of message processed by this runtime state since start. More...
 
int GetMsgQueueSize ()
 get the message queue size. More...
 
void SetMsgQueueSize (int nSize=500)
 set the message queue size. More...
 
void WaitForMessage (int nMessageCount=-1)
 simply wait for the next message to arrive. More...
 
NPLMessage_ptr PeekMessage (int nIndex)
 
NPLMessage_ptr PopMessageAt (int nIndex)
 pop message at given index. More...
 
virtual const std::string & GetIdentifier ()
 
virtual int activate (const char *sNPLFilename, const char *sCode, int nCodeLength=0, int priority=2, int reliability=4)
 activate any runtime local or remote file. More...
 
template<typename StringType >
bool LoadFile_any (const StringType &filepath, bool bReload=false, lua_State *L=0, bool bNoReturn=false)
 load a file without running it in this runtime state. More...
 
template<typename StringType >
NPLReturnCode ActivateFile_any (const StringType &filepath, const char *code=NULL, int nLength=0)
 Activate a file(script or dll) in this runtime state. More...
 
virtual NPLReturnCode Activate_async (const string &filepath, const char *code=NULL, int nLength=0, int priority=0)
 activate the specified file in this runtime state. More...
 
virtual NPLReturnCode Loadfile_async (const string &filepath, int priority=0)
 
virtual NPLReturnCode Activate_async (NPLMessage_ptr &msg, int priority=0)
 same as Activate_async. More...
 
virtual NPLReturnCode ActivateLocal (const char *filepath, const char *code=NULL, int nLength=0, int priority=0)
 same as Activate_async, except that it is a short cut name. More...
 
virtual NPLReturnCode SendMessage (NPLMessage_ptr &msg, int priority=0)
 send a message to the current message queue. More...
 
virtual const char * GetCurrentMsg ()
 get a pointer to the current message. More...
 
virtual int GetCurrentMsgLength ()
 get length of the current message. More...
 
virtual INPLRuntimeGetNPLRuntime ()
 get the NPL runtime environment
 
virtual void WriteLog (const char *text, int nTextLen=0, int nLogType=0)
 write a log message More...
 
int GetTimerCount ()
 [thread safe]
 
virtual bool SetTimer (int nIDEvent, float fElapse, const char *sNeuronFile)
 creates a timer with the specified time-out value [thread safe] More...
 
virtual bool KillTimer (int nIDEvent)
 Destroys the specified timer [thread safe]. More...
 
virtual bool ChangeTimer (int nIDEvent, int dueTime, int period)
 Changes the start time and the interval between method invocations for a timer, using 32-bit signed integers to measure time intervals. More...
 
virtual INPLRuntimeStateGetNPLStateInterface ()
 get the INPLRuntimeState state interface. More...
 
virtual void RegisterFile (const char *sFilename, INPLActivationFile *pFileHandler=NULL)
 function to register the a file handler in the current NPL state, so that it is callable from NPL script or C++ More...
 
virtual void call (const char *sNPLFilename, const char *sCode, int nCodeLength=0)
 synchronous function call
 
std::string & GetStringBuffer (int nIndex=0)
 get string buffer by index. More...
 
int ProcessMsg (NPLMessage_ptr msg)
 function for processing a given msg More...
 
int SendTick ()
 any cross-frame pending messages are processed. More...
 
CNeuronFileStateGetNeuronFileState (const std::string &filename, bool bCreateIfNotExist=true)
 get neuron file state. More...
 
bool HasDebugHook ()
 whether there is already a debug hook. More...
 
bool IsPreemptive ()
 whether we are currently running in a preemptive activation function. More...
 
bool IsAllPreemptiveFunctionPaused () const
 whether all preemptive functions are paused for debugging purposes. More...
 
void PauseAllPreemptiveFunction (bool val)
 
template<typename StringType >
NPL::NPLReturnCode ActivateFile_any (const StringType &filepath, const char *code, int nLength)
 
- Public Member Functions inherited from ParaEngine::IAttributeFields
virtual int GetAttributeClassID ()
 attribute class ID should be identical, unless one knows how overriding rules work. More...
 
virtual const char * GetAttributeClassName ()
 a static string, describing the attribute class object's name
 
virtual const char * GetAttributeClassDescription ()
 a static string, describing the attribute class object
 
virtual int GetChildAttributeColumnCount ()
 we support multi-dimensional child object. More...
 
virtual int GetChildAttributeObjectCount (int nColumnIndex=0)
 get the number of child objects (row count) in the given column. More...
 
virtual IAttributeFieldsGetChildAttributeObject (int nRowIndex, int nColumnIndex=0)
 
virtual IAttributeFieldsGetChildAttributeObject (const std::string &sName)
 get attribute by child object. More...
 
 ATTRIBUTE_METHOD1 (IAttributeFields, GetName_s, const char **)
 
 ATTRIBUTE_METHOD1 (IAttributeFields, SetName_s, const char *)
 
 ATTRIBUTE_METHOD (IAttributeFields, PrintObject_s)
 
 ATTRIBUTE_METHOD (IAttributeFields, AddRef_s)
 
 ATTRIBUTE_METHOD1 (IAttributeFields, GetRefCount_s, int *)
 
 ATTRIBUTE_METHOD1 (IAttributeFields, SetTime_s, int)
 
 ATTRIBUTE_METHOD1 (IAttributeFields, GetTime_s, int *)
 
 ATTRIBUTE_METHOD (IAttributeFields, Release_s)
 
virtual void SetIdentifier (const std::string &sID)
 
virtual bool IsModified ()
 whether some of the fields are modified.It is up to the implementation class to provide this functionality if necessary. More...
 
virtual void SetModified (bool bModified)
 set whether any field has been modified. More...
 
virtual bool ValidateFields ()
 validate all fields and return true if validation passed. More...
 
virtual string GetValidationMessage ()
 get the recent validation message due to the most recent call to ValidateFields()
 
virtual bool ResetField (int nFieldID)
 Reset the field to its initial or default value. More...
 
virtual bool InvokeEditor (int nFieldID, const std::string &sParameters)
 Invoke an (external) editor for a given field. More...
 
virtual bool AddChildAttributeObject (IAttributeFields *pChild, int nRowIndex=-1, int nColumnIndex=0)
 add child object. More...
 
virtual void * QueryObjectByName (const std::string &sObjectType)
 convert to object of a given type. More...
 
virtual void * QueryObject (int nObjectType)
 convert to object of a given type. More...
 
virtual int GetTime ()
 get the current local time in case it is animated in milli seconds frames. More...
 
virtual void SetTime (int nTime)
 
virtual CDynamicAttributeFieldGetDynamicField (const std::string &sName)
 Get a dynamic field with a given name. More...
 
virtual CDynamicAttributeFieldGetDynamicField (int nIndex)
 Get a dynamic field with a given index. More...
 
virtual const char * GetDynamicFieldNameByIndex (int nIndex)
 get field name by index
 
virtual int GetDynamicFieldCount ()
 how many dynamic field this object currently have. More...
 
virtual int SetDynamicField (const std::string &sName, const CVariable &value)
 set a dynamic field with a given name. More...
 
virtual int AddDynamicField (const std::string &sName, ATTRIBUTE_FIELDTYPE dwType)
 add dynamic field and return field index
 
virtual void RemoveAllDynamicFields ()
 remove all dynamic fields
 
virtual int SaveDynamicFieldsToString (std::string &output)
 save only text dynamic fields to fieldname = value text strings. More...
 
virtual int LoadDynamicFieldsFromString (const std::string &input)
 load only text dynamic fields from string More...
 
CAttributeClassGetAttributeClass ()
 get the main attribute class object. More...
 
void PrintObject ()
 print the content of this object to a text file at temp/doc/[ClassName].txt. More...
 
- Public Member Functions inherited from ParaEngine::IObject
virtual void Clone (IObject *obj) const
 Clone the object's contains to a pointer. More...
 
virtual IObjectClone () const
 Clone the object's contains and return a pointer to the newly created object. More...
 
virtual bool Equals (const IObject *obj) const
 Compare the object with another object. More...
 
virtual const ITypeGetType () const
 
virtual std::string ToString () const
 
WeakPtr_typeGetWeakReference ()
 get weak reference object. More...
 
virtual int ProcessObjectEvent (const ObjectEvent &event)
 this function is only used to backward compatibility of ParaObject:AddEvent() function. More...
 
- Public Member Functions inherited from ParaEngine::CRefCounted
void addref () const
 add reference count of the object. More...
 
bool delref () const
 decrease reference count of the object. More...
 
int GetRefCount () const
 get the reference count
 
virtual int Release ()
 
CRefCountedAddToAutoReleasePool ()
 addref and releases the ownership sometime soon automatically (usually at the end of the current frame). More...
 
- Public Member Functions inherited from NPL::INPLRuntimeState
virtual NPLReturnCode Activate_async (const std::string &filepath, const char *code=NULL, int nLength=0, int priority=0)=0
 activate the specified file in this runtime state. More...
 
- Public Member Functions inherited from ParaScripting::CNPLScriptingState
 CNPLScriptingState (bool bCreateState=true)
 split registration to save compiling time. More...
 
int GetLastReturnValue () const
 get the last return value from lua_pcall. More...
 
void SetLastReturnValue (int val)
 
lua_StateGetLuaState ()
 get the lua state.
 
bool IsValid ()
 return true if the runtime state is valid
 
void LoadNPLLib ()
 load only NPL related functions. More...
 
void LoadParaLib ()
 load only Para related functions. More...
 
void LoadHAPI_Globals ()
 load functions for scene management
 
void LoadHAPI_SceneManager ()
 load functions for scene management
 
void LoadHAPI_ResourceManager ()
 load functions for resource or assets management
 
void LoadHAPI_UI ()
 load functions for GUI controls and sounds
 
void LoadHAPI_UI_Extension ()
 this function is called by LoadHAPI_UI()
 
void LoadHAPI_Audio ()
 load functions for Audio Engine More...
 
void LoadHAPI_Network ()
 load functions for NPL Network Layer
 
void LoadHAPI_AI ()
 load functions for AI
 
void LoadHAPI_NPL ()
 load functions for neural parallel language
 
void LoadHAPI_Jabber ()
 load jabber related functions
 
bool IsScriptFileLoaded (const string &filepath)
 whether a given script file is loaded. More...
 
bool LoadFile (const string &filePath, bool bReload, lua_State *L=0, bool bNoReturn=false)
 load a new NPL script file without running it. More...
 
int DoString (const char *sCode, int nLength=0, const char *sFileName=NULL, bool bPopReturnValue=true)
 do string in the current state. More...
 
NPL::NPLReturnCode ActivateFile (const string &filepath, const char *code=NULL, int nLength=0)
 Activate a local file. More...
 
bool BindFileActivateFunc (const object &funcActivate, const std::string &filename)
 bind the activation function. More...
 
const string & GetFileName ()
 get current file name which is being processed now. More...
 
const char * GetCurrentFileName (lua_State *L=0)
 get current file that is being loaded or where the current code is defined. More...
 
bool CreateSetState (lua_State *pLuaState=NULL)
 load or restore the runtime state no need to call LoadLuabind(), if the script runtime is created by this function More...
 
void SetOwnLuaState (bool bOwn)
 if true, we will delete the luastate when this class is destroyed. More...
 
int NPL_export (lua_State *L=0)
 set/get exported file module. More...
 
const std::map< std::string, int32 > & GetLoadedFiles ()
 NOT thread-safe: all loaded files mapping from filename to number of cached objects. More...
 

Public Attributes

Signal_StateLoaded_t StateLoaded
 signal: called when state is first loaded
 

Protected Member Functions

void LoadNPLState ()
 load all NPL related functions. More...
 
int TickTimers (DWORD nTickCount)
 this function is called often enough from the NPL runtime's main thread. More...
 
int FrameMoveTick ()
 
void SetCurrentMessage (const char *msg=NULL, int nLength=0)
 set the current message that is being processed. More...
 
IMonoScriptingStateGetMonoState ()
 get the mono scripting state. More...
 
void SetPreemptive (bool val)
 
- Protected Member Functions inherited from ParaEngine::IAttributeFields
virtual CAttributeClassCreateAttributeClass ()
 initialize fields
 
virtual CDynamicAttributesSetGetDynamicAttributes (bool bCreateIfNotExist=false)
 
- Protected Member Functions inherited from ParaScripting::CNPLScriptingState
void DestroyState ()
 destroy the runtime state careful with this function, it will make the state invalid.
 
void LoadLuabind ()
 the function must be called for the lua state before binding anything to it
 
void SetRuntimeState (NPL::NPLRuntimeState_ptr runtime_state)
 set the current runtime state. More...
 
void ProcessResult (int nResult, lua_State *L=0)
 Process return result after calling a function or loading a file in Lua. More...
 
int CacheFileModule (const std::string &filename, int nResult, lua_State *L=0)
 save nResult objects on stack to file modules More...
 
int PopFileModule (const std::string &filename, lua_State *L=0)
 pop file module to stack for a given file. More...
 
std::string GetModuleFilePath (const std::string &modulename, lua_State *L=0)
 get module file path by module name
 
int GetFileLoadStatus (const string &filepath)
 wrapping the m_loaded_files
 
void SetFileLoadStatus (const string &filepath, int nStatus)
 

Friends

class CNPLRuntime
 

Additional Inherited Members

- Static Public Member Functions inherited from ParaEngine::IAttributeFields
static HRESULT GetAttributeClassID_s (IAttributeFields *cls, int *p1)
 
static HRESULT GetAttributeClassName_s (IAttributeFields *cls, const char **p1)
 
static bool OpenWithDefaultEditor (const char *sFilename, bool bWaitOnReturn=false)
 Open a given file with the default registered editor in the game engine. More...
 
- Static Public Member Functions inherited from ParaScripting::CNPLScriptingState
static int GetNPLCodeFromFile (ParaEngine::CParaFile *pFile, char **pBuffer, int *pBufferSize)
 get the NPL code buffer and size according to a given file. More...
 
static void AddSearchPath (const char *sSearchPath, bool bIsAdding=true)
 static function: More...
 
static uint32 GetScriptDiskPath (const string &filePath, string &sFileName)
 we will first find if there is an up-to-date compiled version in the script/bin directory. More...
 
static NPL::NPLRuntimeState_ptr GetRuntimeStateFromLuaObject (const object &obj)
 get pointer to NPLRuntimeState from the lua state object. More...
 
static NPL::NPLRuntimeState_ptr GetRuntimeStateFromLuaState (lua_State *L)
 
- Protected Attributes inherited from ParaEngine::IObject
WeakPtr_type m_weak_reference
 
- Protected Attributes inherited from ParaEngine::CRefCounted
int m_refcount
 

Detailed Description

A runtime state contains the scripting runtime stack and can be run in a single thread.

An NPL runtime state is message driven, however we also support timer and several other event callbacks.

Constructor & Destructor Documentation

§ CNPLRuntimeState()

NPL::CNPLRuntimeState::CNPLRuntimeState ( const string &  name,
NPLRuntimeStateType  type_ = NPLRuntimeStateType_NPL 
)

a type must be provided.

it defaults to NPL runtime state.

Member Function Documentation

§ activate()

int NPL::CNPLRuntimeState::activate ( const char *  sNPLFilename,
const char *  sCode,
int  nCodeLength = 0,
int  priority = 2,
int  reliability = 4 
)
virtual

activate any runtime local or remote file.

This function is usually called by dll interface.

Implements NPL::INPLRuntimeState.

§ Activate_async() [1/2]

NPL::NPLReturnCode NPL::CNPLRuntimeState::Activate_async ( const string &  filepath,
const char *  code = NULL,
int  nLength = 0,
int  priority = 0 
)
virtual

activate the specified file in this runtime state.

the file can be script or DLL. The function will just insert the message into the message queue and return immediately.

Parameters
codeit is a chunk of code that should be executed in the destination neuron. this code usually set the values of POL global variables.
nLengththe code length. if this is 0, length is determined from code, however, it must not exceed 4096 bytes. if it is specified. it can be any code length
prioritybigger is higher. 0 is the default. if 1, it will be inserted to the front of the queue.
Returns
: NPLReturnCode

§ Activate_async() [2/2]

NPL::NPLReturnCode NPL::CNPLRuntimeState::Activate_async ( NPLMessage_ptr &  msg,
int  priority = 0 
)
virtual

same as Activate_async.

except that input are read from NPLMesage. e.g. NPLMessage_ptr msg(new NPLMessage()); return Activate_async(msg, priority);

Parameters
msgthe caller is should only new but never delete the NPLMessage_ptr. And that the message must be created in the same thread, usually just before calling this function

Implements NPL::INPLRuntimeState.

§ ActivateFile_any()

template<typename StringType >
NPLReturnCode NPL::CNPLRuntimeState::ActivateFile_any ( const StringType &  filepath,
const char *  code = NULL,
int  nLength = 0 
)

Activate a file(script or dll) in this runtime state.

The file should be loaded already.

Parameters
filepathpointer to the file path. it can be StringBuilder or std::string.
Returns
: NPLReturnCode

§ ActivateLocal()

NPL::NPLReturnCode NPL::CNPLRuntimeState::ActivateLocal ( const char *  filepath,
const char *  code = NULL,
int  nLength = 0,
int  priority = 0 
)
virtual

same as Activate_async, except that it is a short cut name.

and may be used by external dlls to activate a file on this local state asynchrounously.

Implements NPL::INPLRuntimeState.

§ ChangeTimer()

bool NPL::CNPLRuntimeState::ChangeTimer ( int  nIDEvent,
int  dueTime,
int  period 
)
virtual

Changes the start time and the interval between method invocations for a timer, using 32-bit signed integers to measure time intervals.

[thread safe]

Parameters
nIDEventSpecifies the timer to be destroyed.For nIDEvent<=0, they are reserved for internal uses can not be killed by this function. This value must be the same as the nIDEvent value passed to the SetTimer function that created the timer.
dueTimeThe amount of time to delay before the invoking the callback method specified when the Timer was constructed, in milliseconds. Specify zero (0) to restart the timer immediately. however, the current implementation does not accept dueTime that is larger than MAX_TIMER_DUE_TIME 10000000, which is 10000 seconds.
period:Thetime interval between invocations of the callback method specified when the Timer was constructed, in milliseconds.
Returns
: If the function succeeds, the return value is true

Implements NPL::INPLRuntimeState.

§ GetCurrentMsg()

const char * NPL::CNPLRuntimeState::GetCurrentMsg ( )
virtual

get a pointer to the current message.

This function is usually called by DLL plugin in the LibActivate function.

Implements NPL::INPLRuntimeState.

§ GetCurrentMsgLength()

int NPL::CNPLRuntimeState::GetCurrentMsgLength ( )
virtual

get length of the current message.

This function is usually called by DLL plugin in the LibActivate function.

Implements NPL::INPLRuntimeState.

§ GetCurrentQueueSize()

int NPL::CNPLRuntimeState::GetCurrentQueueSize ( )

Get the current number of messages in the input message queue.

Sometimes, a monitor or dispatcher logics may need to know the queue size of all NPL runtime states. and a dispatcher will usually need to add new messages to the NPL state with smallest queue size. This function will lock the message queue to retrieve the queue size, so do not call it very often, but use a timer to query it on some interval. [thread safe]

§ GetMonoState()

NPL::IMonoScriptingState * NPL::CNPLRuntimeState::GetMonoState ( )
protected

get the mono scripting state.

and create one from the NPLMono plugin, if one does not exist.

§ GetMsgQueueSize()

int NPL::CNPLRuntimeState::GetMsgQueueSize ( )

get the message queue size.

default to 500. For busy server side, we can set this to something like 5000 [thread safe]

§ GetName()

virtual const string& NPL::CNPLRuntimeState::GetName ( ) const
inlinevirtual

return the name of this runtime state.

if "", it is considered an anonymous name

Implements NPL::INPLRuntimeState.

§ GetNeuronFileState()

NPL::CNeuronFileState * NPL::CNPLRuntimeState::GetNeuronFileState ( const std::string &  filename,
bool  bCreateIfNotExist = true 
)

get neuron file state.

§ GetNPLStateInterface()

virtual INPLRuntimeState* NPL::CNPLRuntimeState::GetNPLStateInterface ( )
inlinevirtual

get the INPLRuntimeState state interface.

§ GetProcessedMsgCount()

int NPL::CNPLRuntimeState::GetProcessedMsgCount ( )

the total number of message processed by this runtime state since start.

If this number does not increase, perhaps the processing is blocking somewhere, and we should take actions. [Not thread safe] in most cases, it will return correct result even in multi-threaded environment, but since we do not use lock, unexpected result may return. This function is usually used for stat printing and monitoring.

§ GetStringBuffer()

std::string & NPL::CNPLRuntimeState::GetStringBuffer ( int  nIndex = 0)

get string buffer by index.

Internally it is an array of std::strings.

§ HasDebugHook()

bool NPL::CNPLRuntimeState::HasDebugHook ( )

whether there is already a debug hook.

Not thread-safe: can only be called from the current thread.

§ Init()

void NPL::CNPLRuntimeState::Init ( void  )

call this function before calling anything else.

It will load all NPL modules into the runtime state.

§ InstallFields()

int NPL::CNPLRuntimeState::InstallFields ( ParaEngine::CAttributeClass pClass,
bool  bOverride 
)
virtual

this class should be implemented if one wants to add new attribute.

This function is always called internally.

Reimplemented from ParaEngine::IAttributeFields.

§ IsAllPreemptiveFunctionPaused()

bool NPL::CNPLRuntimeState::IsAllPreemptiveFunctionPaused ( ) const

whether all preemptive functions are paused for debugging purposes.

§ IsPreemptive()

bool NPL::CNPLRuntimeState::IsPreemptive ( )

whether we are currently running in a preemptive activation function.

Not thread-safe: can only be called from the current thread.

§ KillTimer()

bool NPL::CNPLRuntimeState::KillTimer ( int  nIDEvent)
virtual

Destroys the specified timer [thread safe].

Parameters
nIDEventSpecifies the timer to be destroyed.For nIDEvent<=0, they are reserved for internal uses can not be killed by this function. This value must be the same as the nIDEvent value passed to the SetTimer function that created the timer.
Returns
: If the function succeeds, the return value is true

Implements NPL::INPLRuntimeState.

§ LoadFile_any()

template<typename StringType >
bool NPL::CNPLRuntimeState::LoadFile_any ( const StringType &  filepath,
bool  bReload = false,
lua_State L = 0,
bool  bNoReturn = false 
)

load a file without running it in this runtime state.

If the file is already loaded,it will not be loaded again. If the file extension is ".dll", it will be treated as a plug-in. if the filepath is "*.dll", it means all DLLs in that directory. IMPORTANT: unlike other activation functions, this is more like "include()", the function will be loaded where it is and returned to the original caller upon finishing.

Parameters
filePaththe local file path, it can be StringBuilder or std::string.
bReloadif true, the file will be reloaded even if it is already loaded. otherwise, the file will only be loaded if it is not loaded yet.
bNoReturngenerate no return on lua_state's stack.
Returns
: return true if file is loaded.

§ LoadNPLState()

void NPL::CNPLRuntimeState::LoadNPLState ( )
protected

load all NPL related functions.

This function must be called for all scripting based classes.

§ PeekMessage()

NPL::NPLMessage_ptr NPL::CNPLRuntimeState::PeekMessage ( int  nIndex)
Returns
: get a pointer to the object at given index, if exist, or NULL.
Note
this is thread safe, however the returned object may be invalid if it is popped by another thread when you use it.

§ PopMessageAt()

NPL::NPLMessage_ptr NPL::CNPLRuntimeState::PopMessageAt ( int  nIndex)

pop message at given index.

usually we need to call peek() first.

Returns
true if popped.

§ Process()

int NPL::CNPLRuntimeState::Process ( void  )

this function should be called whenever there are messages in the input_queue for processing.

one can call this function regularly using a timer(the main game thread does this), or wait on the m_semaphore object when input_queue has items (almost all other secondary runtime states use this).

Returns
: if -1, we should exit the runtime state and never call this function again. normally it returns 0.

§ ProcessMsg()

int NPL::CNPLRuntimeState::ProcessMsg ( NPLMessage_ptr  msg)

function for processing a given msg

Returns
: if -1, we should exit the runtime state. normally it returns 0.

§ RegisterFile()

void NPL::CNPLRuntimeState::RegisterFile ( const char *  sFilename,
INPLActivationFile pFileHandler = NULL 
)
virtual

function to register the a file handler in the current NPL state, so that it is callable from NPL script or C++

Parameters
sFilenameany name with cpp file extension can be used. usually it is "states.cpp". The name does not need to be same as the real cpp file.
pFileHandlerif NULL it will unregister. If not, it is the file handler pointer, the pointer must be always valid, it is usually a static singleton object.

Implements NPL::INPLRuntimeState.

§ Reset()

void NPL::CNPLRuntimeState::Reset ( const char *  onResetScode = NULL)

reset NPL state.

it will put MSG_TYPE_RESET to the input queue of this runtime process, and wait for the next cycle to call Reset_Imp(). all NPL and table memory will be released. This function is usually called in case of a soft ParaEngine restart.

Parameters
onResetScodethe code to be executed immediately after runtime state is reset. default to NULL.

§ Reset_Imp()

void NPL::CNPLRuntimeState::Reset_Imp ( )

reset NPL state.

This function must not be called by the NPL state itself. all NPL and table memory will be released. This function is usually called in case of a soft ParaEngine restart.

§ Run()

int NPL::CNPLRuntimeState::Run ( void  )

this function does not return until the run time state is about to be destroyed.

it wait on the m_semaphore object when input_queue has items, and process messages in input_queue.

§ Run_Async()

int NPL::CNPLRuntimeState::Run_Async ( )

this function will create a worker thread to run Run(), and return immediately.

This is the advised way to start the runtime state.

§ SendMessage()

NPL::NPLReturnCode NPL::CNPLRuntimeState::SendMessage ( NPLMessage_ptr &  msg,
int  priority = 0 
)
virtual

send a message to the current message queue.

This function is rarely needed to call directly, use Activate_async instead. e.g. NPLMessage_ptr msg(new NPLMessage()); return SendMessage(msg, priority);

Parameters
msgthe message to send. Please note that when the function returns, the msg will be reset to null.
Returns
may fail if message queue is full.

Implements NPL::INPLRuntimeState.

§ SendTick()

int NPL::CNPLRuntimeState::SendTick ( )

any cross-frame pending messages are processed.

§ SetCurrentMessage()

void NPL::CNPLRuntimeState::SetCurrentMessage ( const char *  msg = NULL,
int  nLength = 0 
)
protected

set the current message that is being processed.

§ SetMsgQueueSize()

void NPL::CNPLRuntimeState::SetMsgQueueSize ( int  nSize = 500)

set the message queue size.

default to 500. For busy server side, we can set this to something like 5000 [thread safe]

§ SetTimer()

bool NPL::CNPLRuntimeState::SetTimer ( int  nIDEvent,
float  fElapse,
const char *  sNeuronFile 
)
virtual

creates a timer with the specified time-out value [thread safe]

Parameters
nIDEventSpecifies a positive timer identifier. For nIDEvent<=0, they are reserved for internal uses. If the NPL runtime already has a timer with the value nIDEvent, then the existing timer is replaced by the new timer. When SetTimer replaces a timer, the timer is reset.
fElapseSpecifies the time-out value, in seconds. Please note that a timer will not be repeatedly activated if its timeout is shorter than the frame rate of the NPL simulation pipeline .
sNeuronFileThe NPL file to be activated when the time-out value elapses. For more information about the file name See NPL.activate().
Returns
: true if succeeds.An application can pass the value of the nIDEvent parameter to the NPL.KillTimer function to destroy the timer.

Implements NPL::INPLRuntimeState.

§ Stop()

int NPL::CNPLRuntimeState::Stop ( void  )

stop the thread by sending the quit message to this runtime state.

this function will only return when the thread is quit. so never call this function from the same thread, otherwise it is a deadlock. Usually this function is called from the main NPL runtime thread.

§ Stop_Async()

int NPL::CNPLRuntimeState::Stop_Async ( )

same as Stop(), except that it just send the exist message, but does not wait for termination of the thread.

§ TickTimers()

int NPL::CNPLRuntimeState::TickTimers ( DWORD  nTickCount)
protected

this function is called often enough from the NPL runtime's main thread.

[thread safe]

Parameters
nTickCountit should be ::GetTickCount() in millisecond. if 0, we will call the system ::GetTickCount() to get the current tick count.
Returns
the number of active timers

§ WaitForMessage()

void NPL::CNPLRuntimeState::WaitForMessage ( int  nMessageCount = -1)

simply wait for the next message to arrive.

Parameters
nMessageCountif not negative, this function will immediately return when the message queue size is bigger than this value.

§ WriteLog()

void NPL::CNPLRuntimeState::WriteLog ( const char *  text,
int  nTextLen = 0,
int  nLogType = 0 
)
virtual

write a log message

Parameters
textthe content of the log message.
nTextLenthe log text length in byte. if 0, text length will be determined automatically.
nLogTypeif this is 0, it is a normal log message. if this is 1, we will print current time, and runtime state name with the log message.

Implements NPL::INPLRuntimeState.


The documentation for this class was generated from the following files: