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_pushnumber(L
, op_index
);
38 lua_pushcclosure(L
, &class_rep::operator_dispatcher
, 1);
42 int create_cpp_class_metatable(lua_State
* L
)
46 // mark the table with our (hopefully) unique tag
47 // that says that the user data that has this
48 // metatable is a class_rep
49 lua_pushstring(L
, "__luabind_classrep");
50 lua_pushboolean(L
, 1);
53 lua_pushstring(L
, "__gc");
56 , &garbage_collector_s
<
63 lua_pushstring(L
, "__call");
64 lua_pushcclosure(L
, &class_rep::constructor_dispatcher
, 0);
67 lua_pushstring(L
, "__index");
68 lua_pushcclosure(L
, &class_rep::static_class_gettable
, 0);
71 return detail::ref(L
);
74 int create_cpp_instance_metatable(lua_State
* L
)
78 // just indicate that this really is a class and not just
80 lua_pushstring(L
, "__luabind_class");
81 lua_pushboolean(L
, 1);
84 // __index and __newindex will simply be references to the
85 // class_rep which in turn has it's own metamethods for __index
87 lua_pushstring(L
, "__index");
88 lua_pushcclosure(L
, &class_rep::gettable_dispatcher
, 0);
91 lua_pushstring(L
, "__newindex");
92 lua_pushcclosure(L
, &class_rep::settable_dispatcher
, 0);
95 lua_pushstring(L
, "__gc");
97 lua_pushcclosure(L
, detail::object_rep::garbage_collector
, 0);
100 lua_pushstring(L
, "__gettable");
101 lua_pushcclosure(L
, &class_rep::static_class_gettable
, 0);
104 for (int i
= 0; i
< number_of_operators
; ++i
)
105 add_operator_to_metatable(L
, i
);
107 // store a reference to the instance-metatable in our class_rep
108 assert((lua_type(L
, -1) == LUA_TTABLE
)
109 && "internal error, please report");
111 return detail::ref(L
);
114 int create_lua_class_metatable(lua_State
* L
)
118 lua_pushstring(L
, "__luabind_classrep");
119 lua_pushboolean(L
, 1);
122 lua_pushstring(L
, "__gc");
125 , &detail::garbage_collector_s
<
132 lua_pushstring(L
, "__newindex");
133 lua_pushcclosure(L
, &class_rep::lua_settable_dispatcher
, 0);
136 lua_pushstring(L
, "__call");
137 lua_pushcclosure(L
, &class_rep::construct_lua_class_callback
, 0);
140 lua_pushstring(L
, "__index");
141 lua_pushcclosure(L
, &class_rep::static_class_gettable
, 0);
144 return detail::ref(L
);
147 int create_lua_instance_metatable(lua_State
* L
)
151 // just indicate that this really is a class and not just
153 lua_pushstring(L
, "__luabind_class");
154 lua_pushboolean(L
, 1);
157 lua_pushstring(L
, "__index");
158 lua_pushcclosure(L
, &class_rep::lua_class_gettable
, 0);
161 lua_pushstring(L
, "__newindex");
162 lua_pushcclosure(L
, &class_rep::lua_class_settable
, 0);
165 lua_pushstring(L
, "__gc");
166 lua_pushcclosure(L
, detail::object_rep::garbage_collector
, 0);
169 for (int i
= 0; i
< number_of_operators
; ++i
)
170 add_operator_to_metatable(L
, i
);
172 // store a reference to the instance-metatable in our class_rep
173 return detail::ref(L
);
176 int create_lua_function_metatable(lua_State
* L
)
180 lua_pushstring(L
, "__gc");
183 , detail::garbage_collector_s
<
184 detail::free_functions::function_rep
190 return detail::ref(L
);
193 } // namespace unnamed
197 class_registry::class_registry(lua_State
* L
)
198 : m_cpp_instance_metatable(create_cpp_instance_metatable(L
))
199 , m_cpp_class_metatable(create_cpp_class_metatable(L
))
200 , m_lua_instance_metatable(create_lua_instance_metatable(L
))
201 , m_lua_class_metatable(create_lua_class_metatable(L
))
202 , m_lua_function_metatable(create_lua_function_metatable(L
))
206 class_registry
* class_registry::get_registry(lua_State
* L
)
209 #ifdef LUABIND_NOT_THREADSAFE
211 // if we don't have to be thread safe, we can keep a
212 // chache of the class_registry pointer without the
214 static lua_State
* cache_key
= 0;
215 static class_registry
* registry_cache
= 0;
216 if (cache_key
== L
) return registry_cache
;
220 lua_pushstring(L
, "__luabind_classes");
221 lua_gettable(L
, LUA_REGISTRYINDEX
);
222 class_registry
* p
= static_cast<class_registry
*>(lua_touserdata(L
, -1));
225 #ifdef LUABIND_NOT_THREADSAFE
235 void class_registry::add_class(LUABIND_TYPE_INFO info
, class_rep
* crep
)
237 // class is already registered
238 assert((m_classes
.find(info
) == m_classes
.end())
239 && "you are trying to register a class twice");
240 m_classes
[info
] = crep
;
243 class_rep
* class_registry::find_class(LUABIND_TYPE_INFO info
) const
245 std::map
<LUABIND_TYPE_INFO
, class_rep
*, cmp
>::const_iterator
i(
246 m_classes
.find(info
));
248 if (i
== m_classes
.end()) return 0; // the type is not registered
252 }} // namespace luabind::detail