Skip to content
Extraits de code Groupes Projets
Vérifiée Valider 74b12bc7 rédigé par Kubat's avatar Kubat
Parcourir les fichiers

SCRIPT: Change the macro to have less to write

parent 4a12875a
Aucune branche associée trouvée
Aucune étiquette associée trouvée
1 requête de fusion!9Lua cpp expose
...@@ -58,12 +58,9 @@ CommentPragmas: '^ KEEP pragma:' ...@@ -58,12 +58,9 @@ CommentPragmas: '^ KEEP pragma:'
ForEachMacros: ForEachMacros:
- 'for_each' - 'for_each'
- 'FOR_EACH' - 'for_ever'
- 'FOR_EVER'
- 'FOR_EVER_IF'
- 'FOR_EVER_UNTIL'
- 'FOR_EACH_FLAT_LIST_ITEM'
- 'parallel_for' - 'parallel_for'
- 'script_class'
IncludeCategories: IncludeCategories:
- Regex: '.*' - Regex: '.*'
......
...@@ -11,15 +11,15 @@ namespace Vivy::Script ...@@ -11,15 +11,15 @@ namespace Vivy::Script
// clang-format off // clang-format off
#define LUA_DECL_METHOD(class, name) luaL_Reg{ #name, class ::name } #define LUA_DECL_METHOD(class, name) luaL_Reg{ #name, class ::name }
#define LUA_DECL_CREATE(class) luaL_Reg{ "new", class ::CREATE } #define LUA_DECL_CREATE(class) luaL_Reg{ "new", class ::CREATE }
#define LUA_DECL_SCRIPTABLE_CLASS(class) public CRTPLuaScriptObject<class>
#define LUA_SCRIPTABLE_CLASS(class) \ #define script_class(theClassName) \
static constexpr char className[] = #class; \ class theClassName final : public CRTPLuaScriptObject<theClassName>
friend CRTPLuaScriptObject<class>;
// clang-format on
// Simple type to hold table with methods for Lua's metatable and methodtable #define LUA_SCRIPTABLE_CLASS(theClassName) \
using LuaMethodTable = const std::vector<luaL_Reg>; VIVY_UNMOVABLE_OBJECT(theClassName) \
static constexpr char className[] = #theClassName; \
friend CRTPLuaScriptObject<theClassName>;
// clang-format on
// CRTP to expose objects to Lua // CRTP to expose objects to Lua
template <class Object> class CRTPLuaScriptObject { template <class Object> class CRTPLuaScriptObject {
...@@ -60,12 +60,12 @@ public: ...@@ -60,12 +60,12 @@ public:
{ {
// Fill the method table, newclass = {} // Fill the method table, newclass = {}
lua_newtable(L); lua_newtable(L);
int methodtable = lua_gettop(L); const int methodtable = lua_gettop(L);
// Fill the meta-table, metatable = {} // Fill the meta-table, metatable = {}
{ {
luaL_newmetatable(L, Object::className); luaL_newmetatable(L, Object::className);
int metatable = lua_gettop(L); const int metatable = lua_gettop(L);
lua_pushliteral(L, "__metatable"); lua_pushliteral(L, "__metatable");
lua_pushvalue(L, methodtable); lua_pushvalue(L, methodtable);
...@@ -75,7 +75,7 @@ public: ...@@ -75,7 +75,7 @@ public:
lua_pushvalue(L, methodtable); lua_pushvalue(L, methodtable);
lua_settable(L, metatable); lua_settable(L, metatable);
for (const luaL_Reg meta : Object::metaMethods) { for (const luaL_Reg &meta : Object::metaMethods) {
fprintf(stderr, "REGISTER %s\n", meta.name); fprintf(stderr, "REGISTER %s\n", meta.name);
lua_pushstring(L, meta.name); lua_pushstring(L, meta.name);
lua_pushcfunction(L, meta.func); lua_pushcfunction(L, meta.func);
...@@ -86,7 +86,7 @@ public: ...@@ -86,7 +86,7 @@ public:
} }
// Fill the method table with specified methods // Fill the method table with specified methods
for (const luaL_Reg method : Object::methods) { for (const luaL_Reg &method : Object::methods) {
fprintf(stderr, "REGISTER %s\n", method.name); fprintf(stderr, "REGISTER %s\n", method.name);
lua_pushstring(L, method.name); lua_pushstring(L, method.name);
lua_pushcfunction(L, method.func); lua_pushcfunction(L, method.func);
...@@ -100,17 +100,18 @@ public: ...@@ -100,17 +100,18 @@ public:
}; };
// Default sample exposer for lua scripts // Default sample exposer for lua scripts
class SampleObject final : LUA_DECL_SCRIPTABLE_CLASS(SampleObject) { script_class (SampleObject) {
VIVY_UNMOVABLE_OBJECT(SampleObject) // Specify that it's a Lua object
LUA_SCRIPTABLE_CLASS(SampleObject) LUA_SCRIPTABLE_CLASS(SampleObject)
public: // CTors and DTors
SampleObject() noexcept; SampleObject() noexcept;
~SampleObject() noexcept; ~SampleObject() noexcept;
// The methods must be static with Lua interface
static int foo(lua_State * L) noexcept; static int foo(lua_State * L) noexcept;
private: // Meta and method tables, Lua specific thing
static constexpr inline auto metaMethods = { luaRegDefaultGC, LUA_DECL_CREATE(SampleObject) }; static constexpr inline auto metaMethods = { luaRegDefaultGC, LUA_DECL_CREATE(SampleObject) };
static constexpr inline auto methods = { LUA_DECL_METHOD(SampleObject, foo) }; static constexpr inline auto methods = { LUA_DECL_METHOD(SampleObject, foo) };
}; };
......
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Veuillez vous inscrire ou vous pour commenter