moved around some code
[luabind.git] / src / class_registry.cpp
blobf2c9863a9b8c79307c7556828054e4b26d3c389d
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 {
34 namespace {
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);
41 lua_settable(L, -3);
44 int create_cpp_class_metatable(lua_State* L)
46 lua_newtable(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);
53 lua_rawset(L, -3);
55 lua_pushstring(L, "__gc");
56 lua_pushcclosure(
58 , &garbage_collector_s<
59 detail::class_rep
60 >::apply
61 , 0);
63 lua_rawset(L, -3);
65 lua_pushstring(L, "__call");
66 lua_pushcclosure(L, &class_rep::constructor_dispatcher, 0);
67 lua_rawset(L, -3);
69 lua_pushstring(L, "__index");
70 lua_pushcclosure(L, &class_rep::static_class_gettable, 0);
71 lua_rawset(L, -3);
73 return detail::ref(L);
76 int create_cpp_instance_metatable(lua_State* L)
78 lua_newtable(L);
80 // just indicate that this really is a class and not just
81 // any user data
82 lua_pushstring(L, "__luabind_class");
83 lua_pushboolean(L, 1);
84 lua_rawset(L, -3);
86 // __index and __newindex will simply be references to the
87 // class_rep which in turn has it's own metamethods for __index
88 // and __newindex
89 lua_pushstring(L, "__index");
90 lua_pushcclosure(L, &class_rep::gettable_dispatcher, 0);
91 lua_rawset(L, -3);
93 lua_pushstring(L, "__newindex");
94 lua_pushcclosure(L, &class_rep::settable_dispatcher, 0);
95 lua_rawset(L, -3);
97 lua_pushstring(L, "__gc");
99 lua_pushcclosure(L, detail::object_rep::garbage_collector, 0);
100 lua_rawset(L, -3);
102 lua_pushstring(L, "__gettable");
103 lua_pushcclosure(L, &class_rep::static_class_gettable, 0);
104 lua_rawset(L, -3);
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)
118 lua_newtable(L);
120 lua_pushstring(L, "__luabind_classrep");
121 lua_pushboolean(L, 1);
122 lua_rawset(L, -3);
124 lua_pushstring(L, "__gc");
125 lua_pushcclosure(
127 , &detail::garbage_collector_s<
128 detail::class_rep
129 >::apply
130 , 0);
132 lua_rawset(L, -3);
134 lua_pushstring(L, "__newindex");
135 lua_pushcclosure(L, &class_rep::lua_settable_dispatcher, 0);
136 lua_rawset(L, -3);
138 lua_pushstring(L, "__call");
139 lua_pushcclosure(L, &class_rep::construct_lua_class_callback, 0);
140 lua_rawset(L, -3);
142 lua_pushstring(L, "__index");
143 lua_pushcclosure(L, &class_rep::static_class_gettable, 0);
144 lua_rawset(L, -3);
146 return detail::ref(L);
149 int create_lua_instance_metatable(lua_State* L)
151 lua_newtable(L);
153 // just indicate that this really is a class and not just
154 // any user data
155 lua_pushstring(L, "__luabind_class");
156 lua_pushboolean(L, 1);
157 lua_rawset(L, -3);
159 lua_pushstring(L, "__index");
160 lua_pushcclosure(L, &class_rep::lua_class_gettable, 0);
161 lua_rawset(L, -3);
163 lua_pushstring(L, "__newindex");
164 lua_pushcclosure(L, &class_rep::lua_class_settable, 0);
165 lua_rawset(L, -3);
167 lua_pushstring(L, "__gc");
168 lua_pushcclosure(L, detail::object_rep::garbage_collector, 0);
169 lua_rawset(L, -3);
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)
180 lua_newtable(L);
182 lua_pushstring(L, "__gc");
183 lua_pushcclosure(
185 , detail::garbage_collector_s<
186 detail::free_functions::function_rep
187 >::apply
188 , 0);
190 lua_rawset(L, -3);
192 return detail::ref(L);
195 } // namespace unnamed
197 class class_rep;
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
215 // need of a mutex
216 static lua_State* cache_key = 0;
217 static class_registry* registry_cache = 0;
218 if (cache_key == L) return registry_cache;
220 #endif
222 lua_pushstring(L, "__luabind_classes");
223 lua_gettable(L, LUA_REGISTRYINDEX);
224 class_registry* p = static_cast<class_registry*>(lua_touserdata(L, -1));
225 lua_pop(L, 1);
227 #ifdef LUABIND_NOT_THREADSAFE
229 cache_key = L;
230 registry_cache = p;
232 #endif
234 return p;
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
250 return i->second;
253 }} // namespace luabind::detail