Removed Makefiles.
[luabind.git] / src / class_registry.cpp
blob5fa1a5d9e405c8f1ad8003970e5e90456062d41f
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 {
32 namespace {
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);
40 lua_settable(L, -3);
43 int create_cpp_class_metatable(lua_State* L)
45 lua_newtable(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);
52 lua_rawset(L, -3);
54 lua_pushstring(L, "__gc");
55 lua_pushcclosure(
57 , &garbage_collector_s<
58 detail::class_rep
59 >::apply
60 , 0);
62 lua_rawset(L, -3);
64 lua_pushstring(L, "__call");
65 lua_pushcclosure(L, &class_rep::constructor_dispatcher, 0);
66 lua_rawset(L, -3);
68 lua_pushstring(L, "__index");
69 lua_pushcclosure(L, &class_rep::static_class_gettable, 0);
70 lua_rawset(L, -3);
72 lua_pushstring(L, "__newindex");
73 lua_pushcclosure(L, &class_rep::lua_settable_dispatcher, 0);
74 lua_rawset(L, -3);
76 return detail::ref(L);
79 int create_cpp_instance_metatable(lua_State* L)
81 lua_newtable(L);
83 // just indicate that this really is a class and not just
84 // any user data
85 lua_pushstring(L, "__luabind_class");
86 lua_pushboolean(L, 1);
87 lua_rawset(L, -3);
89 // __index and __newindex will simply be references to the
90 // class_rep which in turn has it's own metamethods for __index
91 // and __newindex
92 lua_pushstring(L, "__index");
93 lua_pushcclosure(L, &class_rep::gettable_dispatcher, 0);
94 lua_rawset(L, -3);
96 lua_pushstring(L, "__newindex");
97 lua_pushcclosure(L, &class_rep::settable_dispatcher, 0);
98 lua_rawset(L, -3);
100 lua_pushstring(L, "__gc");
102 lua_pushcclosure(L, detail::object_rep::garbage_collector, 0);
103 lua_rawset(L, -3);
105 lua_pushstring(L, "__gettable");
106 lua_pushcclosure(L, &class_rep::static_class_gettable, 0);
107 lua_rawset(L, -3);
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)
121 lua_newtable(L);
123 lua_pushstring(L, "__luabind_classrep");
124 lua_pushboolean(L, 1);
125 lua_rawset(L, -3);
127 lua_pushstring(L, "__gc");
128 lua_pushcclosure(
130 , &detail::garbage_collector_s<
131 detail::class_rep
132 >::apply
133 , 0);
135 lua_rawset(L, -3);
137 lua_pushstring(L, "__newindex");
138 lua_pushcclosure(L, &class_rep::lua_settable_dispatcher, 0);
139 lua_rawset(L, -3);
141 lua_pushstring(L, "__call");
142 lua_pushcclosure(L, &class_rep::construct_lua_class_callback, 0);
143 lua_rawset(L, -3);
145 lua_pushstring(L, "__index");
146 lua_pushcclosure(L, &class_rep::static_class_gettable, 0);
147 lua_rawset(L, -3);
149 return detail::ref(L);
152 int create_lua_instance_metatable(lua_State* L)
154 lua_newtable(L);
156 // just indicate that this really is a class and not just
157 // any user data
158 lua_pushstring(L, "__luabind_class");
159 lua_pushboolean(L, 1);
160 lua_rawset(L, -3);
162 lua_pushstring(L, "__index");
163 lua_pushcclosure(L, &class_rep::lua_class_gettable, 0);
164 lua_rawset(L, -3);
166 lua_pushstring(L, "__newindex");
167 lua_pushcclosure(L, &class_rep::lua_class_settable, 0);
168 lua_rawset(L, -3);
170 lua_pushstring(L, "__gc");
171 lua_pushcclosure(L, detail::object_rep::garbage_collector, 0);
172 lua_rawset(L, -3);
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)
183 lua_newtable(L);
185 lua_pushstring(L, "__gc");
186 lua_pushcclosure(
188 , detail::garbage_collector_s<
189 detail::free_functions::function_rep
190 >::apply
191 , 0);
193 lua_rawset(L, -3);
195 return detail::ref(L);
198 } // namespace unnamed
200 class class_rep;
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
218 // need of a mutex
219 static lua_State* cache_key = 0;
220 static class_registry* registry_cache = 0;
221 if (cache_key == L) return registry_cache;
223 #endif
225 lua_pushstring(L, "__luabind_classes");
226 lua_gettable(L, LUA_REGISTRYINDEX);
227 class_registry* p = static_cast<class_registry*>(lua_touserdata(L, -1));
228 lua_pop(L, 1);
230 #ifdef LUABIND_NOT_THREADSAFE
232 cache_key = L;
233 registry_cache = p;
235 #endif
237 return p;
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
254 return i->second;
257 }} // namespace luabind::detail