fixed tests for vc7
[luabind.git] / src / class_registry.cpp
blobafd5305b0c6ac832419d678588fd0e5d516526e7
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_pushnumber(L, op_index);
38 lua_pushcclosure(L, &class_rep::operator_dispatcher, 1);
39 lua_settable(L, -3);
42 int create_cpp_class_metatable(lua_State* L)
44 lua_newtable(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);
51 lua_rawset(L, -3);
53 lua_pushstring(L, "__gc");
54 lua_pushcclosure(
56 , &garbage_collector_s<
57 detail::class_rep
58 >::apply
59 , 0);
61 lua_rawset(L, -3);
63 lua_pushstring(L, "__call");
64 lua_pushcclosure(L, &class_rep::constructor_dispatcher, 0);
65 lua_rawset(L, -3);
67 lua_pushstring(L, "__index");
68 lua_pushcclosure(L, &class_rep::static_class_gettable, 0);
69 lua_rawset(L, -3);
71 return detail::ref(L);
74 int create_cpp_instance_metatable(lua_State* L)
76 lua_newtable(L);
78 // just indicate that this really is a class and not just
79 // any user data
80 lua_pushstring(L, "__luabind_class");
81 lua_pushboolean(L, 1);
82 lua_rawset(L, -3);
84 // __index and __newindex will simply be references to the
85 // class_rep which in turn has it's own metamethods for __index
86 // and __newindex
87 lua_pushstring(L, "__index");
88 lua_pushcclosure(L, &class_rep::gettable_dispatcher, 0);
89 lua_rawset(L, -3);
91 lua_pushstring(L, "__newindex");
92 lua_pushcclosure(L, &class_rep::settable_dispatcher, 0);
93 lua_rawset(L, -3);
95 lua_pushstring(L, "__gc");
97 lua_pushcclosure(L, detail::object_rep::garbage_collector, 0);
98 lua_rawset(L, -3);
100 lua_pushstring(L, "__gettable");
101 lua_pushcclosure(L, &class_rep::static_class_gettable, 0);
102 lua_rawset(L, -3);
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)
116 lua_newtable(L);
118 lua_pushstring(L, "__luabind_classrep");
119 lua_pushboolean(L, 1);
120 lua_rawset(L, -3);
122 lua_pushstring(L, "__gc");
123 lua_pushcclosure(
125 , &detail::garbage_collector_s<
126 detail::class_rep
127 >::apply
128 , 0);
130 lua_rawset(L, -3);
132 lua_pushstring(L, "__newindex");
133 lua_pushcclosure(L, &class_rep::lua_settable_dispatcher, 0);
134 lua_rawset(L, -3);
136 lua_pushstring(L, "__call");
137 lua_pushcclosure(L, &class_rep::construct_lua_class_callback, 0);
138 lua_rawset(L, -3);
140 lua_pushstring(L, "__index");
141 lua_pushcclosure(L, &class_rep::static_class_gettable, 0);
142 lua_rawset(L, -3);
144 return detail::ref(L);
147 int create_lua_instance_metatable(lua_State* L)
149 lua_newtable(L);
151 // just indicate that this really is a class and not just
152 // any user data
153 lua_pushstring(L, "__luabind_class");
154 lua_pushboolean(L, 1);
155 lua_rawset(L, -3);
157 lua_pushstring(L, "__index");
158 lua_pushcclosure(L, &class_rep::lua_class_gettable, 0);
159 lua_rawset(L, -3);
161 lua_pushstring(L, "__newindex");
162 lua_pushcclosure(L, &class_rep::lua_class_settable, 0);
163 lua_rawset(L, -3);
165 lua_pushstring(L, "__gc");
166 lua_pushcclosure(L, detail::object_rep::garbage_collector, 0);
167 lua_rawset(L, -3);
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)
178 lua_newtable(L);
180 lua_pushstring(L, "__gc");
181 lua_pushcclosure(
183 , detail::garbage_collector_s<
184 detail::free_functions::function_rep
185 >::apply
186 , 0);
188 lua_rawset(L, -3);
190 return detail::ref(L);
193 } // namespace unnamed
195 class class_rep;
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
213 // need of a mutex
214 static lua_State* cache_key = 0;
215 static class_registry* registry_cache = 0;
216 if (cache_key == L) return registry_cache;
218 #endif
220 lua_pushstring(L, "__luabind_classes");
221 lua_gettable(L, LUA_REGISTRYINDEX);
222 class_registry* p = static_cast<class_registry*>(lua_touserdata(L, -1));
223 lua_pop(L, 1);
225 #ifdef LUABIND_NOT_THREADSAFE
227 cache_key = L;
228 registry_cache = p;
230 #endif
232 return p;
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
249 return i->second;
252 }} // namespace luabind::detail