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
{
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);
45 int create_cpp_class_metatable(lua_State
* 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);
56 lua_pushstring(L
, "__gc");
59 , &garbage_collector_s
<
66 lua_pushstring(L
, "__call");
67 lua_pushcclosure(L
, &class_rep::constructor_dispatcher
, 0);
70 lua_pushstring(L
, "__index");
71 lua_pushcclosure(L
, &class_rep::static_class_gettable
, 0);
74 lua_pushstring(L
, "__newindex");
75 lua_pushcclosure(L
, &class_rep::lua_settable_dispatcher
, 0);
78 return luaL_ref(L
, LUA_REGISTRYINDEX
);
81 int create_cpp_instance_metatable(lua_State
* L
)
85 // just indicate that this really is a class and not just
87 lua_pushstring(L
, "__luabind_class");
88 lua_pushboolean(L
, 1);
91 // __index and __newindex will simply be references to the
92 // class_rep which in turn has it's own metamethods for __index
94 lua_pushstring(L
, "__index");
95 lua_pushcclosure(L
, &class_rep::gettable_dispatcher
, 0);
98 lua_pushstring(L
, "__newindex");
99 lua_pushcclosure(L
, &class_rep::settable_dispatcher
, 0);
102 lua_pushstring(L
, "__gc");
104 lua_pushcclosure(L
, detail::object_rep::garbage_collector
, 0);
107 lua_pushstring(L
, "__gettable");
108 lua_pushcclosure(L
, &class_rep::static_class_gettable
, 0);
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
)
125 lua_pushstring(L
, "__luabind_classrep");
126 lua_pushboolean(L
, 1);
129 lua_pushstring(L
, "__gc");
132 , &detail::garbage_collector_s
<
139 lua_pushstring(L
, "__newindex");
140 lua_pushcclosure(L
, &class_rep::lua_settable_dispatcher
, 0);
143 lua_pushstring(L
, "__call");
144 lua_pushcclosure(L
, &class_rep::constructor_dispatcher
, 0);
147 lua_pushstring(L
, "__index");
148 lua_pushcclosure(L
, &class_rep::static_class_gettable
, 0);
151 return luaL_ref(L
, LUA_REGISTRYINDEX
);
154 int create_lua_instance_metatable(lua_State
* L
)
158 // just indicate that this really is a class and not just
160 lua_pushstring(L
, "__luabind_class");
161 lua_pushboolean(L
, 1);
164 lua_pushstring(L
, "__index");
165 lua_pushcclosure(L
, &class_rep::gettable_dispatcher
, 0);
168 lua_pushstring(L
, "__newindex");
169 lua_pushcclosure(L
, &class_rep::settable_dispatcher
, 0);
172 lua_pushstring(L
, "__gc");
173 lua_pushcclosure(L
, detail::object_rep::garbage_collector
, 0);
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
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
203 static lua_State
* cache_key
= 0;
204 static class_registry
* registry_cache
= 0;
205 if (cache_key
== L
) return registry_cache
;
209 lua_pushstring(L
, "__luabind_classes");
210 lua_gettable(L
, LUA_REGISTRYINDEX
);
211 class_registry
* p
= static_cast<class_registry
*>(lua_touserdata(L
, -1));
214 #ifdef LUABIND_NOT_THREADSAFE
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
241 }} // namespace luabind::detail