Initial revision
[luabind.git] / luabind / detail / class_registry.hpp
blob41b386ee97b751bf8fd0e64c646a4f1770e97887
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 <string>
30 namespace luabind { namespace detail
32 class class_rep;
34 std::string stack_content_by_name(lua_State* L, int start_index);
36 struct class_registry
38 class_registry()
39 : m_cpp_instance_metatable(LUA_NOREF)
40 , m_cpp_class_metatable(LUA_NOREF)
41 , m_lua_instance_metatable(LUA_NOREF)
42 , m_lua_class_metatable(LUA_NOREF)
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 // luabind::open() is required
72 //assert(p);
74 return p;
77 std::map<LUABIND_TYPE_INFO, class_rep*> classes;
79 // this is a lua reference that points to the lua table
80 // that is to be used as meta table for all C++ class
81 // instances. It is a kind of v-table.
82 int m_cpp_instance_metatable;
84 // this is a lua reference to the metatable to be used
85 // for all classes defined in C++.
86 int m_cpp_class_metatable;
88 // this is a lua reference that points to the lua table
89 // that is to be used as meta table for all lua class
90 // instances. It is a kind of v-table.
91 int m_lua_instance_metatable;
93 // this is a lua reference to the metatable to be used
94 // for all classes defined in lua
95 int m_lua_class_metatable;
101 #endif // LUABIND_CLASS_REGISTRY_HPP_INCLUDED