Replace ref/unref with luaL_ref/luaL_unref.
[luabind.git] / src / class_registry.cpp
blobba1ba6cdeb1c0aaa315410f435dd726e0a04416d
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 #define LUABIND_BUILDING
25 #include <luabind/lua_include.hpp>
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_pushstring(L, get_operator_name(op_index));
40 lua_pushboolean(L, op_index == op_unm || op_index == op_len);
41 lua_pushcclosure(L, &class_rep::operator_dispatcher, 2);
42 lua_settable(L, -3);
45 int create_cpp_class_metatable(lua_State* L)
47 lua_newtable(L);
49 // mark the table with our (hopefully) unique tag
50 // that says that the user data that has this
51 // metatable is a class_rep
52 lua_pushstring(L, "__luabind_classrep");
53 lua_pushboolean(L, 1);
54 lua_rawset(L, -3);
56 lua_pushstring(L, "__gc");
57 lua_pushcclosure(
59 , &garbage_collector_s<
60 detail::class_rep
61 >::apply
62 , 0);
64 lua_rawset(L, -3);
66 lua_pushstring(L, "__call");
67 lua_pushcclosure(L, &class_rep::constructor_dispatcher, 0);
68 lua_rawset(L, -3);
70 lua_pushstring(L, "__index");
71 lua_pushcclosure(L, &class_rep::static_class_gettable, 0);
72 lua_rawset(L, -3);
74 lua_pushstring(L, "__newindex");
75 lua_pushcclosure(L, &class_rep::lua_settable_dispatcher, 0);
76 lua_rawset(L, -3);
78 return luaL_ref(L, LUA_REGISTRYINDEX);
81 int create_cpp_instance_metatable(lua_State* L)
83 lua_newtable(L);
85 // just indicate that this really is a class and not just
86 // any user data
87 lua_pushstring(L, "__luabind_class");
88 lua_pushboolean(L, 1);
89 lua_rawset(L, -3);
91 // __index and __newindex will simply be references to the
92 // class_rep which in turn has it's own metamethods for __index
93 // and __newindex
94 lua_pushstring(L, "__index");
95 lua_pushcclosure(L, &class_rep::gettable_dispatcher, 0);
96 lua_rawset(L, -3);
98 lua_pushstring(L, "__newindex");
99 lua_pushcclosure(L, &class_rep::settable_dispatcher, 0);
100 lua_rawset(L, -3);
102 lua_pushstring(L, "__gc");
104 lua_pushcclosure(L, detail::object_rep::garbage_collector, 0);
105 lua_rawset(L, -3);
107 lua_pushstring(L, "__gettable");
108 lua_pushcclosure(L, &class_rep::static_class_gettable, 0);
109 lua_rawset(L, -3);
111 for (int i = 0; i < number_of_operators; ++i)
112 add_operator_to_metatable(L, i);
114 // store a reference to the instance-metatable in our class_rep
115 assert((lua_type(L, -1) == LUA_TTABLE)
116 && "internal error, please report");
118 return luaL_ref(L, LUA_REGISTRYINDEX);
121 int create_lua_class_metatable(lua_State* L)
123 lua_newtable(L);
125 lua_pushstring(L, "__luabind_classrep");
126 lua_pushboolean(L, 1);
127 lua_rawset(L, -3);
129 lua_pushstring(L, "__gc");
130 lua_pushcclosure(
132 , &detail::garbage_collector_s<
133 detail::class_rep
134 >::apply
135 , 0);
137 lua_rawset(L, -3);
139 lua_pushstring(L, "__newindex");
140 lua_pushcclosure(L, &class_rep::lua_settable_dispatcher, 0);
141 lua_rawset(L, -3);
143 lua_pushstring(L, "__call");
144 lua_pushcclosure(L, &class_rep::constructor_dispatcher, 0);
145 lua_rawset(L, -3);
147 lua_pushstring(L, "__index");
148 lua_pushcclosure(L, &class_rep::static_class_gettable, 0);
149 lua_rawset(L, -3);
151 return luaL_ref(L, LUA_REGISTRYINDEX);
154 int create_lua_instance_metatable(lua_State* L)
156 lua_newtable(L);
158 // just indicate that this really is a class and not just
159 // any user data
160 lua_pushstring(L, "__luabind_class");
161 lua_pushboolean(L, 1);
162 lua_rawset(L, -3);
164 lua_pushstring(L, "__index");
165 lua_pushcclosure(L, &class_rep::gettable_dispatcher, 0);
166 lua_rawset(L, -3);
168 lua_pushstring(L, "__newindex");
169 lua_pushcclosure(L, &class_rep::settable_dispatcher, 0);
170 lua_rawset(L, -3);
172 lua_pushstring(L, "__gc");
173 lua_pushcclosure(L, detail::object_rep::garbage_collector, 0);
174 lua_rawset(L, -3);
176 for (int i = 0; i < number_of_operators; ++i)
177 add_operator_to_metatable(L, i);
179 // store a reference to the instance-metatable in our class_rep
180 return luaL_ref(L, LUA_REGISTRYINDEX);
183 } // namespace unnamed
185 class class_rep;
187 class_registry::class_registry(lua_State* L)
188 : m_cpp_instance_metatable(create_cpp_instance_metatable(L))
189 , m_cpp_class_metatable(create_cpp_class_metatable(L))
190 , m_lua_instance_metatable(create_lua_instance_metatable(L))
191 , m_lua_class_metatable(create_lua_class_metatable(L))
195 class_registry* class_registry::get_registry(lua_State* L)
198 #ifdef LUABIND_NOT_THREADSAFE
200 // if we don't have to be thread safe, we can keep a
201 // chache of the class_registry pointer without the
202 // need of a mutex
203 static lua_State* cache_key = 0;
204 static class_registry* registry_cache = 0;
205 if (cache_key == L) return registry_cache;
207 #endif
209 lua_pushstring(L, "__luabind_classes");
210 lua_gettable(L, LUA_REGISTRYINDEX);
211 class_registry* p = static_cast<class_registry*>(lua_touserdata(L, -1));
212 lua_pop(L, 1);
214 #ifdef LUABIND_NOT_THREADSAFE
216 cache_key = L;
217 registry_cache = p;
219 #endif
221 return p;
224 void class_registry::add_class(LUABIND_TYPE_INFO info, class_rep* crep)
226 // class is already registered
227 assert((m_classes.find(info) == m_classes.end())
228 && "you are trying to register a class twice");
229 m_classes[info] = crep;
232 class_rep* class_registry::find_class(LUABIND_TYPE_INFO info) const
234 std::map<LUABIND_TYPE_INFO, class_rep*, cmp>::const_iterator i(
235 m_classes.find(info));
237 if (i == m_classes.end()) return 0; // the type is not registered
238 return i->second;
241 }} // namespace luabind::detail