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 #include <luabind/luabind.hpp>
26 #include <luabind/detail/class_registry.hpp>
27 #include <luabind/detail/class_rep.hpp>
28 #include <luabind/detail/operator_id.hpp>
30 namespace luabind
{ namespace detail
{
34 void add_operator_to_metatable(lua_State
* L
, int op_index
)
36 lua_pushstring(L
, get_operator_name(op_index
));
37 lua_pushstring(L
, get_operator_name(op_index
));
38 lua_pushboolean(L
, op_index
== op_unm
);
39 lua_pushcclosure(L
, &class_rep::operator_dispatcher
, 2);
43 int create_cpp_class_metatable(lua_State
* L
)
47 // mark the table with our (hopefully) unique tag
48 // that says that the user data that has this
49 // metatable is a class_rep
50 lua_pushstring(L
, "__luabind_classrep");
51 lua_pushboolean(L
, 1);
54 lua_pushstring(L
, "__gc");
57 , &garbage_collector_s
<
64 lua_pushstring(L
, "__call");
65 lua_pushcclosure(L
, &class_rep::constructor_dispatcher
, 0);
68 lua_pushstring(L
, "__index");
69 lua_pushcclosure(L
, &class_rep::static_class_gettable
, 0);
72 lua_pushstring(L
, "__newindex");
73 lua_pushcclosure(L
, &class_rep::lua_settable_dispatcher
, 0);
76 return detail::ref(L
);
79 int create_cpp_instance_metatable(lua_State
* L
)
83 // just indicate that this really is a class and not just
85 lua_pushstring(L
, "__luabind_class");
86 lua_pushboolean(L
, 1);
89 // __index and __newindex will simply be references to the
90 // class_rep which in turn has it's own metamethods for __index
92 lua_pushstring(L
, "__index");
93 lua_pushcclosure(L
, &class_rep::gettable_dispatcher
, 0);
96 lua_pushstring(L
, "__newindex");
97 lua_pushcclosure(L
, &class_rep::settable_dispatcher
, 0);
100 lua_pushstring(L
, "__gc");
102 lua_pushcclosure(L
, detail::object_rep::garbage_collector
, 0);
105 lua_pushstring(L
, "__gettable");
106 lua_pushcclosure(L
, &class_rep::static_class_gettable
, 0);
109 for (int i
= 0; i
< number_of_operators
; ++i
)
110 add_operator_to_metatable(L
, i
);
112 // store a reference to the instance-metatable in our class_rep
113 assert((lua_type(L
, -1) == LUA_TTABLE
)
114 && "internal error, please report");
116 return detail::ref(L
);
119 int create_lua_class_metatable(lua_State
* L
)
123 lua_pushstring(L
, "__luabind_classrep");
124 lua_pushboolean(L
, 1);
127 lua_pushstring(L
, "__gc");
130 , &detail::garbage_collector_s
<
137 lua_pushstring(L
, "__newindex");
138 lua_pushcclosure(L
, &class_rep::lua_settable_dispatcher
, 0);
141 lua_pushstring(L
, "__call");
142 lua_pushcclosure(L
, &class_rep::construct_lua_class_callback
, 0);
145 lua_pushstring(L
, "__index");
146 lua_pushcclosure(L
, &class_rep::static_class_gettable
, 0);
149 return detail::ref(L
);
152 int create_lua_instance_metatable(lua_State
* L
)
156 // just indicate that this really is a class and not just
158 lua_pushstring(L
, "__luabind_class");
159 lua_pushboolean(L
, 1);
162 lua_pushstring(L
, "__index");
163 lua_pushcclosure(L
, &class_rep::lua_class_gettable
, 0);
166 lua_pushstring(L
, "__newindex");
167 lua_pushcclosure(L
, &class_rep::lua_class_settable
, 0);
170 lua_pushstring(L
, "__gc");
171 lua_pushcclosure(L
, detail::object_rep::garbage_collector
, 0);
174 for (int i
= 0; i
< number_of_operators
; ++i
)
175 add_operator_to_metatable(L
, i
);
177 // store a reference to the instance-metatable in our class_rep
178 return detail::ref(L
);
181 int create_lua_function_metatable(lua_State
* L
)
185 lua_pushstring(L
, "__gc");
188 , detail::garbage_collector_s
<
189 detail::free_functions::function_rep
195 return detail::ref(L
);
198 } // namespace unnamed
202 class_registry::class_registry(lua_State
* L
)
203 : m_cpp_instance_metatable(create_cpp_instance_metatable(L
))
204 , m_cpp_class_metatable(create_cpp_class_metatable(L
))
205 , m_lua_instance_metatable(create_lua_instance_metatable(L
))
206 , m_lua_class_metatable(create_lua_class_metatable(L
))
207 , m_lua_function_metatable(create_lua_function_metatable(L
))
211 class_registry
* class_registry::get_registry(lua_State
* L
)
214 #ifdef LUABIND_NOT_THREADSAFE
216 // if we don't have to be thread safe, we can keep a
217 // chache of the class_registry pointer without the
219 static lua_State
* cache_key
= 0;
220 static class_registry
* registry_cache
= 0;
221 if (cache_key
== L
) return registry_cache
;
225 lua_pushstring(L
, "__luabind_classes");
226 lua_gettable(L
, LUA_REGISTRYINDEX
);
227 class_registry
* p
= static_cast<class_registry
*>(lua_touserdata(L
, -1));
230 #ifdef LUABIND_NOT_THREADSAFE
240 void class_registry::add_class(LUABIND_TYPE_INFO info
, class_rep
* crep
)
242 // class is already registered
243 assert((m_classes
.find(info
) == m_classes
.end())
244 && "you are trying to register a class twice");
245 m_classes
[info
] = crep
;
248 class_rep
* class_registry::find_class(LUABIND_TYPE_INFO info
) const
250 std::map
<LUABIND_TYPE_INFO
, class_rep
*, cmp
>::const_iterator
i(
251 m_classes
.find(info
));
253 if (i
== m_classes
.end()) return 0; // the type is not registered
257 }} // namespace luabind::detail