1 // Copyright (c) 2004 Daniel Wallin
3 // Permission is hereby granted, free of charge, to any person obtaining a
4 // copy of this software and associated documentation files (the "Software"),
5 // to deal in the Software without restriction, including without limitation
6 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
7 // and/or sell copies of the Software, and to permit persons to whom the
8 // Software is furnished to do so, subject to the following conditions:
10 // The above copyright notice and this permission notice shall be included
11 // in all copies or substantial portions of the Software.
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
14 // ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
15 // TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
16 // PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
17 // SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
18 // ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
19 // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
21 // OR OTHER DEALINGS IN THE SOFTWARE.
23 #include <luabind/lua_include.hpp>
25 #define LUABIND_BUILDING
27 #include <luabind/luabind.hpp>
28 #include <luabind/detail/class_registry.hpp>
29 #include <luabind/detail/class_rep.hpp>
30 #include <luabind/detail/operator_id.hpp>
32 namespace luabind
{ namespace detail
{
36 void add_operator_to_metatable(lua_State
* L
, int op_index
)
38 lua_pushstring(L
, get_operator_name(op_index
));
39 lua_pushnumber(L
, op_index
);
40 lua_pushcclosure(L
, &class_rep::operator_dispatcher
, 1);
44 int create_cpp_class_metatable(lua_State
* L
)
48 // mark the table with our (hopefully) unique tag
49 // that says that the user data that has this
50 // metatable is a class_rep
51 lua_pushstring(L
, "__luabind_classrep");
52 lua_pushboolean(L
, 1);
55 lua_pushstring(L
, "__gc");
58 , &garbage_collector_s
<
65 lua_pushstring(L
, "__call");
66 lua_pushcclosure(L
, &class_rep::constructor_dispatcher
, 0);
69 lua_pushstring(L
, "__index");
70 lua_pushcclosure(L
, &class_rep::static_class_gettable
, 0);
73 return detail::ref(L
);
76 int create_cpp_instance_metatable(lua_State
* L
)
80 // just indicate that this really is a class and not just
82 lua_pushstring(L
, "__luabind_class");
83 lua_pushboolean(L
, 1);
86 // __index and __newindex will simply be references to the
87 // class_rep which in turn has it's own metamethods for __index
89 lua_pushstring(L
, "__index");
90 lua_pushcclosure(L
, &class_rep::gettable_dispatcher
, 0);
93 lua_pushstring(L
, "__newindex");
94 lua_pushcclosure(L
, &class_rep::settable_dispatcher
, 0);
97 lua_pushstring(L
, "__gc");
99 lua_pushcclosure(L
, detail::object_rep::garbage_collector
, 0);
102 lua_pushstring(L
, "__gettable");
103 lua_pushcclosure(L
, &class_rep::static_class_gettable
, 0);
106 for (int i
= 0; i
< number_of_operators
; ++i
)
107 add_operator_to_metatable(L
, i
);
109 // store a reference to the instance-metatable in our class_rep
110 assert((lua_type(L
, -1) == LUA_TTABLE
)
111 && "internal error, please report");
113 return detail::ref(L
);
116 int create_lua_class_metatable(lua_State
* L
)
120 lua_pushstring(L
, "__luabind_classrep");
121 lua_pushboolean(L
, 1);
124 lua_pushstring(L
, "__gc");
127 , &detail::garbage_collector_s
<
134 lua_pushstring(L
, "__newindex");
135 lua_pushcclosure(L
, &class_rep::lua_settable_dispatcher
, 0);
138 lua_pushstring(L
, "__call");
139 lua_pushcclosure(L
, &class_rep::construct_lua_class_callback
, 0);
142 lua_pushstring(L
, "__index");
143 lua_pushcclosure(L
, &class_rep::static_class_gettable
, 0);
146 return detail::ref(L
);
149 int create_lua_instance_metatable(lua_State
* L
)
153 // just indicate that this really is a class and not just
155 lua_pushstring(L
, "__luabind_class");
156 lua_pushboolean(L
, 1);
159 lua_pushstring(L
, "__index");
160 lua_pushcclosure(L
, &class_rep::lua_class_gettable
, 0);
163 lua_pushstring(L
, "__newindex");
164 lua_pushcclosure(L
, &class_rep::lua_class_settable
, 0);
167 lua_pushstring(L
, "__gc");
168 lua_pushcclosure(L
, detail::object_rep::garbage_collector
, 0);
171 for (int i
= 0; i
< number_of_operators
; ++i
)
172 add_operator_to_metatable(L
, i
);
174 // store a reference to the instance-metatable in our class_rep
175 return detail::ref(L
);
178 int create_lua_function_metatable(lua_State
* L
)
182 lua_pushstring(L
, "__gc");
185 , detail::garbage_collector_s
<
186 detail::free_functions::function_rep
192 return detail::ref(L
);
195 } // namespace unnamed
199 class_registry::class_registry(lua_State
* L
)
200 : m_cpp_instance_metatable(create_cpp_instance_metatable(L
))
201 , m_cpp_class_metatable(create_cpp_class_metatable(L
))
202 , m_lua_instance_metatable(create_lua_instance_metatable(L
))
203 , m_lua_class_metatable(create_lua_class_metatable(L
))
204 , m_lua_function_metatable(create_lua_function_metatable(L
))
208 class_registry
* class_registry::get_registry(lua_State
* L
)
211 #ifdef LUABIND_NOT_THREADSAFE
213 // if we don't have to be thread safe, we can keep a
214 // chache of the class_registry pointer without the
216 static lua_State
* cache_key
= 0;
217 static class_registry
* registry_cache
= 0;
218 if (cache_key
== L
) return registry_cache
;
222 lua_pushstring(L
, "__luabind_classes");
223 lua_gettable(L
, LUA_REGISTRYINDEX
);
224 class_registry
* p
= static_cast<class_registry
*>(lua_touserdata(L
, -1));
227 #ifdef LUABIND_NOT_THREADSAFE
237 void class_registry::add_class(LUABIND_TYPE_INFO info
, class_rep
* crep
)
239 // class is already registered
240 assert((m_classes
.find(info
) == m_classes
.end()) && "you are trying to register a class twice");
241 m_classes
[info
] = crep
;
244 class_rep
* class_registry::find_class(LUABIND_TYPE_INFO info
) const
246 std::map
<LUABIND_TYPE_INFO
, class_rep
*, cmp
>::const_iterator
i(
247 m_classes
.find(info
));
249 if (i
== m_classes
.end()) return 0; // the type is not registered
253 }} // namespace luabind::detail