moved some headers out of detail/
[luabind.git] / luabind / detail / class_registry.hpp
blob867c38c56678b3a0924961616b324f3270d51a43
1 // Copyright (c) 2003 Daniel Wallin and Arvid Norberg
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.
24 #ifndef LUABIND_CLASS_REGISTRY_HPP_INCLUDED
25 #define LUABIND_CLASS_REGISTRY_HPP_INCLUDED
27 #include <luabind/config.hpp>
28 #include <luabind/open.hpp>
29 #include <string>
31 namespace luabind { namespace detail
33 class class_rep;
35 struct class_registry
37 class_registry(lua_State* L)
38 : m_cpp_instance_metatable(create_cpp_instance_metatable(L))
39 , m_cpp_class_metatable(create_cpp_class_metatable(L))
40 , m_lua_instance_metatable(create_lua_instance_metatable(L))
41 , m_lua_class_metatable(create_lua_class_metatable(L))
42 , m_lua_function_metatable(create_lua_function_metatable(L))
45 static inline class_registry* get_registry(lua_State* L)
48 #ifdef LUABIND_NOT_THREADSAFE
50 // if we don't have to be thread safe, we can keep a
51 // chache of the class_registry pointer without the
52 // need of a mutex
53 static lua_State* cache_key = 0;
54 static class_registry* registry_cache = 0;
55 if (cache_key == L) return registry_cache;
57 #endif
59 lua_pushstring(L, "__luabind_classes");
60 lua_gettable(L, LUA_REGISTRYINDEX);
61 class_registry* p = static_cast<class_registry*>(lua_touserdata(L, -1));
62 lua_pop(L, 1);
64 #ifdef LUABIND_NOT_THREADSAFE
66 cache_key = L;
67 registry_cache = p;
69 #endif
71 return p;
74 int cpp_instance() const { return m_cpp_instance_metatable; }
75 int cpp_class() const { return m_cpp_class_metatable; }
77 int lua_instance() const { return m_lua_instance_metatable; }
78 int lua_class() const { return m_lua_class_metatable; }
79 int lua_function() const { return m_lua_function_metatable; }
81 void add_class(LUABIND_TYPE_INFO info, class_rep* crep)
83 // class is already registered
84 assert((m_classes.find(info) == m_classes.end()) && "you are trying to register a class twice");
85 m_classes[info] = crep;
88 struct cmp
90 bool operator()(const std::type_info* a, const std::type_info* b) const
92 return a->before(*b) != 0;
95 template<class T>
96 bool operator()(const T& a, const T& b) const
98 return a < b;
102 class_rep* find_class(LUABIND_TYPE_INFO info)
104 std::map<LUABIND_TYPE_INFO, class_rep*, cmp>::iterator i = m_classes.find(info);
105 if (i == m_classes.end()) return 0; // the type is not registered
106 return i->second;
109 private:
111 std::map<LUABIND_TYPE_INFO, class_rep*, cmp> m_classes;
113 // this is a lua reference that points to the lua table
114 // that is to be used as meta table for all C++ class
115 // instances. It is a kind of v-table.
116 int m_cpp_instance_metatable;
118 // this is a lua reference to the metatable to be used
119 // for all classes defined in C++.
120 int m_cpp_class_metatable;
122 // this is a lua reference that points to the lua table
123 // that is to be used as meta table for all lua class
124 // instances. It is a kind of v-table.
125 int m_lua_instance_metatable;
127 // this is a lua reference to the metatable to be used
128 // for all classes defined in lua
129 int m_lua_class_metatable;
131 // this metatable only contains a destructor
132 // for luabind::Detail::free_functions::function_rep
133 int m_lua_function_metatable;
139 #endif // LUABIND_CLASS_REGISTRY_HPP_INCLUDED