Leviathan  0.8.0.0
Leviathan game engine
ScriptNotifiers.cpp
Go to the documentation of this file.
1 // ------------------------------------ //
2 #include "ScriptNotifiers.h"
3 
4 #include "Bindings/BindHelpers.h"
5 
6 #include "ScriptExecutor.h"
7 using namespace Leviathan;
8 using namespace std;
9 // ------------------------------------ //
10 
11 // ------------------ ScriptNotifier ------------------ //
12 DLLEXPORT Leviathan::ScriptNotifier::ScriptNotifier(asIScriptFunction* functiontocall) :
13  CallbackFunction(functiontocall)
14 {
15  // TODO: increase ref count? or does the caller do that
16 }
17 
19 
20  GUARD_LOCK();
21 
22  ReleaseChildHooks(guard);
23 
24  if(CallbackFunction)
25  CallbackFunction->Release();
26 
27  CallbackFunction = NULL;
28 }
29 // ------------------------------------ //
31  Lock &childlock)
32 {
33  ScriptRunningSetup params;
34  params.ErrorOnNonExistingFunction = true;
35 
36  auto result = ScriptExecutor::Get()->RunScript<void>(CallbackFunction, nullptr, params);
37 
38  if(result.Result != SCRIPT_RUN_RESULT::Success)
39  LOG_ERROR("ScriptNotifier: OnNotified: failed to call script callback");
40 }
41 // ------------------ ScriptNotifiable ------------------ //
42 DLLEXPORT Leviathan::ScriptNotifiable::ScriptNotifiable(asIScriptFunction* functiontocall) :
43  CallbackFunction(functiontocall)
44 {
45  // TODO: increase ref count? or does the caller do that
46 }
47 
49 
50  GUARD_LOCK();
51 
52  ReleaseParentHooks(guard);
53 
54  if(CallbackFunction)
55  CallbackFunction->Release();
56 
57  CallbackFunction = NULL;
58 }
59 // ------------------------------------ //
61  Lock &parentlock)
62 {
63  ScriptRunningSetup params;
64  params.ErrorOnNonExistingFunction = true;
65 
66  auto result = ScriptExecutor::Get()->RunScript<void>(CallbackFunction, nullptr, params);
67 
68  if(result.Result != SCRIPT_RUN_RESULT::Success)
69  LOG_ERROR("ScriptNotifiable: OnNotified: failed to call script callback");
70 }
71 // ------------------------------------ //
72 ScriptNotifier* ScriptNotifierFactory(asIScriptFunction* cb){
73 
74  return new ScriptNotifier(cb);
75 }
76 
77 ScriptNotifiable* ScriptNotifiableFactory(asIScriptFunction* cb){
78 
79  return new ScriptNotifiable(cb);
80 }
81 
83  obj->ConnectToNotifier(target);
84 
85  // Release the reference given to us //
86  target->Release();
87 }
88 
89 
91  obj->ConnectToNotifiable(target);
92 
93  // Release the reference given to us //
94  target->Release();
95 }
96 
97 
98 bool Leviathan::RegisterNotifiersWithAngelScript(asIScriptEngine* engine){
99 
100  if(engine->RegisterFuncdef("void NotifierCallback()") < 0)
101  {
103  }
104 
105  // Register the base notifiers //
106 
107 
108  if(engine->RegisterObjectType("BaseNotifiableAll", 0, asOBJ_REF | asOBJ_NOHANDLE) < 0){
110  }
111  if(engine->RegisterObjectType("BaseNotifierAll", 0, asOBJ_REF | asOBJ_NOHANDLE) < 0){
113  }
114 
115 
116  // ScriptNotifier //
118 
119  if(engine->RegisterObjectBehaviour("ScriptNotifier", asBEHAVE_FACTORY, "ScriptNotifier@ f(NotifierCallback @cb)", asFUNCTION(ScriptNotifierFactory), asCALL_CDECL) < 0){
121  }
122 
123 
124  // ScriptNotifiable //
125  ANGELSCRIPT_REGISTER_REF_TYPE("ScriptNotifiable", ScriptNotifiable);
126  if(engine->RegisterObjectBehaviour("ScriptNotifiable", asBEHAVE_FACTORY, "ScriptNotifiable@ f(NotifierCallback @cb)", asFUNCTION(ScriptNotifiableFactory), asCALL_CDECL) < 0){
128  }
129 
130 
131  // Notifier connecting //
132  if(engine->RegisterObjectMethod("BaseNotifiableAll", "void ConnectToNotifier(ScriptNotifier@ notifier)", asFUNCTION(NotifiableConnectProxy), asCALL_CDECL_OBJFIRST) < 0){
134  }
135  if(engine->RegisterObjectMethod("BaseNotifierAll", "void ConnectToNotifiable(ScriptNotifiable@ notifiable)", asFUNCTION(NotifierConnectProxy), asCALL_CDECL_OBJFIRST) < 0){
137  }
138 
139 
140 
141 
142  return true;
143 }
144 
145 
ScriptRunResult< ReturnT > RunScript(const std::shared_ptr< ScriptModule > &module, ScriptRunningSetup &parameters, Args &&... args)
Runs a function in a script.
#define LOG_ERROR(x)
Definition: Define.h:83
bool RegisterNotifiersWithAngelScript(asIScriptEngine *engine)
void NotifiableConnectProxy(BaseNotifiableAll *obj, ScriptNotifier *target)
#define ANGELSCRIPT_REGISTER_REF_TYPE(RegisterName, ClassName)
Definition: BindHelpers.h:90
BaseNotifier for use in scripts.
STL namespace.
ScriptNotifier * ScriptNotifierFactory(asIScriptFunction *cb)
Specialized class for accepting all parent/child objects.
Definition: BaseNotifier.h:103
virtual void OnNotified(Lock &ownlock, BaseNotifiableAll *child, Lock &childlock) override
Calls into the script engine.
DLLEXPORT ScriptNotifiable(asIScriptFunction *functiontocall)
virtual DLLEXPORT ~ScriptNotifiable()
virtual void OnNotified(Lock &ownlock, BaseNotifierAll *parent, Lock &parentlock) override
Calls into the script engine.
ScriptNotifiable * ScriptNotifiableFactory(asIScriptFunction *cb)
static DLLEXPORT ScriptExecutor * Get()
void NotifierConnectProxy(BaseNotifierAll *obj, ScriptNotifiable *target)
FORCE_INLINE void Release()
removes a reference and deletes the object if reference count reaches zero
Specialized class for accepting all parent/child objects.
DLLEXPORT ScriptNotifier(asIScriptFunction *functiontocall)
bool ConnectToNotifier(BaseNotifier< ParentType, ChildType > *owner)
Connects this to a notifier object calling all the needed functions.
BaseNotifiable for use in scripts.
FORCE_INLINE bool ConnectToNotifiable(BaseNotifiable< ParentType, ChildType > *child)
Connects this to a notifiable object for holding a reference to it.
Definition: BaseNotifier.h:26
#define ANGELSCRIPT_REGISTERFAIL
Definition: BindHelpers.h:10
#define DLLEXPORT
Definition: Include.h:115
The access mask controls which registered functions and classes a script sees.
Definition: GameModule.h:12
virtual DLLEXPORT ~ScriptNotifier()
#define GUARD_LOCK()
Definition: ThreadSafe.h:97
std::unique_lock< std::mutex > Lock
Definition: ThreadSafe.h:16