Initial commit, includes Lua with broken Luabind as a backup for branching purposes
[terrastrategy.git] / include / luabind / detail / class_cache.hpp
blob17d3a4460010cfbec8834e12ba9e5e0ad1b77644
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 #ifndef CLASS_CACHE_040218_HPP
24 #define CLASS_CACHE_040218_HPP
26 #include <luabind/prefix.hpp>
27 #include <boost/type_traits/add_reference.hpp>
28 #include <boost/type_traits/add_const.hpp>
30 namespace luabind { namespace detail {
32 #ifdef LUABIND_NOT_THREADSAFE
34 class class_rep;
36 template<class T>
37 struct class_cache_impl
39 static lua_State* state;
40 static class_rep* class_;
43 template<class T>
44 lua_State* class_cache_impl<T>::state = 0;
46 template<class T>
47 class_rep* class_cache_impl<T>::class_ = 0;
49 template<class T>
50 struct class_cache
51 : class_cache_impl<
52 typename boost::add_reference<
53 typename boost::add_const<
55 >::type
56 >::type
61 template<class T>
62 class_rep* get_class_rep(lua_State* L, void(*)(T*) = 0)
64 if (class_cache<T>::state != L)
66 class_cache<T>::state = L;
68 class_registry* registry = class_registry::get_registry(L);
69 class_cache<T>::class_ = registry->find_class(LUABIND_TYPEID(T));
72 return class_cache<T>::class_;
75 #else
77 template<class T>
78 class_rep* get_class_rep(lua_State* L, void(*)(T*) = 0)
80 class_registry* registry = class_registry::get_registry(L);
81 return registry->find_class(LUABIND_TYPEID(T));
84 #endif
86 }} // namespace luabind::detail
88 #endif // CLASS_CACHE_040218_HPP