aboutsummaryrefslogtreecommitdiff
path: root/src/lua/common
diff options
context:
space:
mode:
Diffstat (limited to 'src/lua/common')
-rw-r--r--src/lua/common/je_lua_common.h1
-rw-r--r--src/lua/common/je_lua_proxy.h29
-rw-r--r--src/lua/common/je_lua_reference.h52
-rw-r--r--src/lua/common/je_lua_shared.hpp9
4 files changed, 74 insertions, 17 deletions
diff --git a/src/lua/common/je_lua_common.h b/src/lua/common/je_lua_common.h
index 82c550a..5b217a2 100644
--- a/src/lua/common/je_lua_common.h
+++ b/src/lua/common/je_lua_common.h
@@ -5,5 +5,6 @@
#include "je_lua_proxy.h"
#include "je_lua_shared.hpp"
#include "je_lua_error.h"
+#include "je_lua_reference.h"
#endif \ No newline at end of file
diff --git a/src/lua/common/je_lua_proxy.h b/src/lua/common/je_lua_proxy.h
index b476aa6..96b2093 100644
--- a/src/lua/common/je_lua_proxy.h
+++ b/src/lua/common/je_lua_proxy.h
@@ -13,40 +13,43 @@ namespace JinEngine
class Proxy
{
public:
- void bind(SharedBase* shared)
+ void bind(SharedBase* s)
{
- if (shared == nullptr)
+ if (s == nullptr)
return;
- reference = shared;
+ shared = s;
}
void release()
{
- if (reference != nullptr)
+ if (shared != nullptr)
{
- reference->release();
- reference = nullptr;
+ shared->release();
+ shared = nullptr;
}
}
void retain()
{
- if (reference != nullptr)
- reference->retain();
+ if (shared != nullptr)
+ shared->retain();
}
void setUserdata(void* data)
{
- if (reference != nullptr)
- reference->setUserdata(data);
+ if (shared != nullptr)
+ shared->setUserdata(data);
}
template<class T>
Shared<T>& getShared()
{
- return *(Shared<T>*) reference;
+ return *(Shared<T>*)shared;
}
+ ///
+ /// For convenience.
+ ///
template<class T>
T* getObject()
{
@@ -56,10 +59,10 @@ namespace JinEngine
const char* getObjectType()
{
- return reference->type;
+ return shared->type;
}
- SharedBase* reference;
+ SharedBase* shared;
};
diff --git a/src/lua/common/je_lua_reference.h b/src/lua/common/je_lua_reference.h
new file mode 100644
index 0000000..28c3bc3
--- /dev/null
+++ b/src/lua/common/je_lua_reference.h
@@ -0,0 +1,52 @@
+#ifndef __JIN_COMMON_REFERENCE_H
+#define __JIN_COMMON_REFERENCE_H
+
+#include "../luax.h"
+
+namespace JinEngine
+{
+ namespace Lua
+ {
+
+ ///
+ /// This class wraps the reference functionality built into Lua, which allows C++ code to refer to Lua
+ /// variables.
+ ///
+ class Reference
+ {
+ public:
+ Reference(lua_State* L, unsigned int i)
+ : mL(L)
+ {
+ luax_pushvalue(mL, i);
+ mIndex = luax_ref(mL, LUA_REGISTRYINDEX);
+ }
+
+ ~Reference()
+ {
+ unref();
+ }
+
+ void unref()
+ {
+ luax_unref(mL, LUA_REGISTRYINDEX, mIndex);
+ }
+
+ ///
+ /// Push value onto the stack.
+ ///
+ void push()
+ {
+ luax_rawgeti(mL, LUA_REGISTRYINDEX, mIndex);
+ }
+
+ private:
+ lua_State* const mL;
+ int mIndex;
+
+ };
+
+ } // namespace Lua
+} // namespace JinEngine
+
+#endif // __JIN_COMMON_REFERENCE_H \ No newline at end of file
diff --git a/src/lua/common/je_lua_shared.hpp b/src/lua/common/je_lua_shared.hpp
index 53f557a..7ac8923 100644
--- a/src/lua/common/je_lua_shared.hpp
+++ b/src/lua/common/je_lua_shared.hpp
@@ -1,5 +1,5 @@
-#ifndef __JIN_COMMON_REFERENCE_H
-#define __JIN_COMMON_REFERENCE_H
+#ifndef __JIN_COMMON_SHARED_H
+#define __JIN_COMMON_SHARED_H
namespace JinEngine
{
@@ -75,6 +75,7 @@ namespace JinEngine
// Disable copy constructor.
Shared(const Shared<T>& shared);
+ // Make shared only be able created with new.
~Shared()
{
T* obj = static_cast<T*>(object);
@@ -83,7 +84,7 @@ namespace JinEngine
};
- }
-}
+ } // namespace Lua
+} // namespace JinEngine
#endif \ No newline at end of file