Browse Source

Add __eq metamethod for C++ objects to get equality check working (#330)

Maximilian Stark 3 years ago
parent
commit
5064a9c068
2 changed files with 16 additions and 0 deletions
  1. 2 0
      Include/RmlUi/Lua/LuaType.h
  2. 14 0
      Include/RmlUi/Lua/LuaType.inl

+ 2 - 0
Include/RmlUi/Lua/LuaType.h

@@ -142,6 +142,8 @@ public:
     /** The __tostring metamethod.
     @return 1, because it pushes a string representation of the userdata on to the stack  */
     static inline int tostring_T(lua_State* L);
+    /** The __eq metamethod. Facilitates equality tests with C++-side pointers  */
+    static inline int eq_T(lua_State* L);
     /** The __index metamethod. Called whenever the user attempts to access a variable that is
     not in the immediate table. This handles the method calls and calls tofunctions in __getters    */
     static inline int index(lua_State* L);

+ 14 - 0
Include/RmlUi/Lua/LuaType.inl

@@ -62,6 +62,9 @@ void LuaType<T>::Register(lua_State* L)
     lua_pushcfunction(L, tostring_T);
     lua_setfield(L, metatable, "__tostring");
 
+    lua_pushcfunction(L, eq_T);
+    lua_setfield(L, metatable, "__eq");
+
     ExtraInit<T>(L,metatable); //optionally implemented by individual types
 
     lua_newtable(L); //for method table -> [3] = this table
@@ -201,6 +204,17 @@ int LuaType<T>::tostring_T(lua_State* L)
     return 1;
 }
 
+template<typename T>
+int LuaType<T>::eq_T(lua_State* L)
+{
+    T* o1 = check(L,1);
+    RMLUI_CHECK_OBJ(o1);
+    T* o2 = check(L,2);
+    RMLUI_CHECK_OBJ(o2);
+    lua_pushboolean(L, o1 == o2);
+    return 1;
+}
+
 
 template<typename T>
 int LuaType<T>::index(lua_State* L)