libRocket
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
LuaType.h
Go to the documentation of this file.
1 /*
2  * This source file is part of libRocket, the HTML/CSS Interface Middleware
3  *
4  * For the latest information, see http://www.librocket.com
5  *
6  * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a copy
9  * of this software and associated documentation files (the "Software"), to deal
10  * in the Software without restriction, including without limitation the rights
11  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12  * copies of the Software, and to permit persons to whom the Software is
13  * furnished to do so, subject to the following conditions:
14  *
15  * The above copyright notice and this permission notice shall be included in
16  * all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24  * THE SOFTWARE.
25  *
26  */
27 
28 #ifndef ROCKETCORELUALUATYPE_H
29 #define ROCKETCORELUALUATYPE_H
30 
31 #include <Rocket/Core/Lua/Header.h>
32 #include <Rocket/Core/Lua/lua.hpp>
33 
34 
35 //As an example, if you used this macro like
36 //LUAMETHOD(Unit,GetId)
37 //it would result in code that looks like
38 //{ "GetId", UnitGetId },
39 //Which would force you to create a global function named UnitGetId in C with the correct function signature, usually int(*)(lua_State*,type*);
40 #define LUAMETHOD(type,name) { #name, type##name },
41 
42 //See above, but the method must match the function signature int(*)(lua_State*) and as example:
43 //LUAGETTER(Unit,Id) would mean you need a function named UnitGetAttrId
44 //The first stack position will be the userdata
45 #define LUAGETTER(type,varname) { #varname, type##GetAttr##varname },
46 
47 //Same method signature as above, but as example:
48 //LUASETTER(Unit,Id) would mean you need a function named UnitSetAttrId
49 //The first stack position will be the userdata, and the second will be value on the other side of the equal sign
50 #define LUASETTER(type,varname) { #varname, type##SetAttr##varname },
51 
52 #define CHECK_BOOL(L,narg) (lua_toboolean((L),(narg)) > 0 ? true : false )
53 #define LUACHECKOBJ(obj) if((obj) == NULL) { lua_pushnil(L); return 1; }
54 
60 #define LUACORETYPEDEFINE(type,is_ref_counted) \
61  template<> const char* GetTClassName<type>() { return #type; } \
62  template<> RegType<type>* GetMethodTable<type>() { return type##Methods; } \
63  template<> luaL_Reg* GetAttrTable<type>() { return type##Getters; } \
64  template<> luaL_Reg* SetAttrTable<type>() { return type##Setters; } \
65  template<> bool IsReferenceCounted<type>() { return (is_ref_counted); } \
66 
67 //We can't use LUACORETYPEDEFINE due to namespace issues
68 #define LUACONTROLSTYPEDEFINE(type,is_ref_counted) \
69  template<> const char* GetTClassName<type>() { return #type; } \
70  template<> RegType<type>* GetMethodTable<type>() { return Rocket::Controls::Lua::type##Methods; } \
71  template<> luaL_Reg* GetAttrTable<type>() { return Rocket::Controls::Lua::type##Getters; } \
72  template<> luaL_Reg* SetAttrTable<type>() { return Rocket::Controls::Lua::type##Setters; } \
73  template<> bool IsReferenceCounted<type>() { return (is_ref_counted); } \
74 
75 
79 #define LUACORETYPEDECLARE(type) \
80  template<> ROCKETLUA_API const char* GetTClassName<type>(); \
81  template<> ROCKETLUA_API RegType<type>* GetMethodTable<type>(); \
82  template<> ROCKETLUA_API luaL_Reg* GetAttrTable<type>(); \
83  template<> ROCKETLUA_API luaL_Reg* SetAttrTable<type>(); \
84  template<> ROCKETLUA_API bool IsReferenceCounted<type>(); \
85 
86 
90 #define LUACONTROLSTYPEDECLARE(type) \
91  template<> ROCKETLUA_API const char* GetTClassName<type>(); \
92  template<> ROCKETLUA_API RegType<type>* GetMethodTable<type>(); \
93  template<> ROCKETLUA_API luaL_Reg* GetAttrTable<type>(); \
94  template<> ROCKETLUA_API luaL_Reg* SetAttrTable<type>(); \
95  template<> ROCKETLUA_API bool IsReferenceCounted<type>(); \
96 
97 namespace Rocket {
98 namespace Core {
99 namespace Lua {
100 //replacement for luaL_Reg that uses a different function pointer signature, but similar syntax
101 template<typename T>
103 {
104  const char* name;
105  int (*ftnptr)(lua_State*,T*);
106 };
107 
109 template<typename T> ROCKETLUA_API RegType<T>* GetMethodTable();
111 template<typename T> ROCKETLUA_API luaL_Reg* GetAttrTable();
113 template<typename T> ROCKETLUA_API luaL_Reg* SetAttrTable();
115 template<typename T> ROCKETLUA_API const char* GetTClassName();
117 template<typename T> ROCKETLUA_API bool IsReferenceCounted();
123 template<typename T> ROCKETLUA_API void ExtraInit(lua_State* L, int metatable_index);
124 
131 template<typename T>
133 {
134 public:
135  typedef int (*ftnptr)(lua_State* L, T* ptr);
137  typedef struct { const char* name; ftnptr func; } RegType;
138 
142  static inline void Register(lua_State *L);
148  static inline int push(lua_State *L, T* obj, bool gc=false);
152  static inline T* check(lua_State* L, int narg);
153 
156  static inline int thunk(lua_State* L);
158  static inline void tostring(char* buff, void* obj);
159  //these are metamethods
164  static inline int gc_T(lua_State* L);
167  static inline int tostring_T(lua_State* L);
170  static inline int index(lua_State* L);
173  static inline int newindex(lua_State* L);
174 
178  static inline void _regfunctions(lua_State* L, int meta, int method);
179 private:
180  LuaType(); //hide constructor
181 
182 };
183 
184 
185 }
186 }
187 }
188 
189 #include "LuaType.inl"
190 #endif
ROCKETLUA_API void ExtraInit(lua_State *L, int metatable_index)
#define ROCKETLUA_API
Definition: Header.h:47
Definition: LuaType.h:102
Definition: Clipboard.h:33
ROCKETLUA_API luaL_Reg * GetAttrTable()
ROCKETLUA_API RegType< T > * GetMethodTable()
ROCKETLUA_API const char * GetTClassName()
Definition: LuaType.h:137
const char * name
Definition: LuaType.h:104
Definition: LuaType.h:132
const char * name
Definition: LuaType.h:137
ROCKETLUA_API bool IsReferenceCounted()
ROCKETLUA_API luaL_Reg * SetAttrTable()