summaryrefslogtreecommitdiff
path: root/Source/3rdParty/Luax/luax_state.inl
diff options
context:
space:
mode:
Diffstat (limited to 'Source/3rdParty/Luax/luax_state.inl')
-rw-r--r--Source/3rdParty/Luax/luax_state.inl110
1 files changed, 110 insertions, 0 deletions
diff --git a/Source/3rdParty/Luax/luax_state.inl b/Source/3rdParty/Luax/luax_state.inl
new file mode 100644
index 0000000..1515123
--- /dev/null
+++ b/Source/3rdParty/Luax/luax_state.inl
@@ -0,0 +1,110 @@
+
+// ͨ͵вͬע̣ͨLuaxStateRegister_ʵ
+
+// עṤΪעinterface tableclass tabletype nameΪƿռϡע׶βԪȵNewõʱŻᡣ
+
+template<typename T>
+void LuaxState::RegisterFactory()
+{
+ lua_State* L = mState;
+
+ int top = lua_gettop(L); // namespace table
+
+ const char* type = T::GetLuaxFactoryName();
+
+ // interface table.
+ lua_newtable(L);
+
+ int idx = AbsIndex(-1);
+
+ LuaxClass::RegisterLuaxInterface<T>(*this);
+ T::RegisterLuaxInterface(*this);
+
+ // TǷûעķ
+#define assertMethods(I, NAME)\
+ GetField(I, NAME);\
+ assert(IsType(-1, LUA_TFUNCTION));\
+ Pop();
+
+ assertMethods(idx, "New");
+
+#undef assertMethods
+
+ lua_settop(L, top);
+
+ // class table.
+ lua_newtable(L);
+
+ assert(lua_istable(L, -1));
+
+ lua_pushvalue(L, -1);
+
+ LuaxClass::RegisterLuaxClass<T>(*this);
+ LuaxClass::RegisterLuaxFactoryClass<T>(*this);
+ T::RegisterLuaxClass(*this);
+
+ SetField(top, type);
+
+ // reset top
+ lua_settop(L, top);
+
+}
+
+// עᵥ
+template<typename T>
+void LuaxState::RegisterSingleton()
+{
+ lua_State* L = mState;
+
+ int top = lua_gettop(L); // namespace table
+
+ const char* type = T::GetLuaxSingletonName();
+
+ // class table.
+ lua_newtable(L);
+
+ assert(lua_istable(L, -1));
+
+ lua_pushvalue(L, -1);
+
+ LuaxClass::RegisterLuaxClass<T>(*this);
+ LuaxClass::RegisterLuaxFactoryClass<T>(*this);
+ T::RegisterLuaxClass(*this);
+
+ SetField(top, type);
+
+ // reset top
+ lua_settop(L, top);
+
+}
+
+template<typename T>
+void LuaxState::SetField(int idx, cc8* key, T value)
+{
+ if (IsTableOrUserdata(idx))
+ {
+ idx = AbsIndex(idx);
+ this->Push(value);
+ lua_setfield(mState, idx, key);
+ }
+}
+
+template<typename T>
+T LuaxState::GetField(int idx, cc8* key, T value)
+{
+ GetField(idx, key);
+ T result = GetValue < T >(-1, value);
+ this->Pop();
+
+ return result;
+}
+
+template<typename T>
+T LuaxState::GetField(int idx, int key, T value)
+{
+ GetField(idx, key);
+ T result = GetValue < T >(-1, value);
+ Pop();
+
+ return result;
+}