OSDN Git Service

build moflib1.0 on cmake-base system
[moflib/moflib.git] / moflib-1.0 / extlib / luabind-0.8 / luabind / detail / class_cache.hpp
1 // Copyright (c) 2004 Daniel Wallin
2
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:
9
10 // The above copyright notice and this permission notice shall be included
11 // in all copies or substantial portions of the Software.
12
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.
22
23 #ifndef CLASS_CACHE_040218_HPP
24 #define CLASS_CACHE_040218_HPP
25
26 #include <luabind/prefix.hpp>
27 #include <boost/type_traits/add_reference.hpp>
28 #include <boost/type_traits/add_const.hpp>
29
30 namespace luabind { namespace detail {
31
32 #ifdef LUABIND_NOT_THREADSAFE
33     
34     class class_rep;
35     
36     template<class T>
37     struct class_cache_impl
38     {
39         static lua_State* state;
40         static class_rep* class_;
41     };
42
43     template<class T>
44     lua_State* class_cache_impl<T>::state = 0;
45
46     template<class T>
47     class_rep* class_cache_impl<T>::class_ = 0;
48
49     template<class T>
50     struct class_cache
51         : class_cache_impl<
52               typename boost::add_reference<
53                   typename boost::add_const<
54                       T
55                   >::type
56               >::type
57           >
58     {
59     };
60     
61     template<class T>
62     class_rep* get_class_rep(lua_State* L, void(*)(T*) = 0)
63     {
64         if (class_cache<T>::state != L)
65         {
66             class_cache<T>::state = L;
67
68             class_registry* registry = class_registry::get_registry(L);
69                         class_cache<T>::class_ = registry->find_class(LUABIND_TYPEID(T));
70         }
71
72         return class_cache<T>::class_;
73     }
74
75 #else
76
77     template<class T>
78     class_rep* get_class_rep(lua_State* L, void(*)(T*) = 0)
79     {
80         class_registry* registry = class_registry::get_registry(L);
81         return registry->find_class(LUABIND_TYPEID(T));
82     }
83
84 #endif
85
86 }} // namespace luabind::detail
87
88 #endif // CLASS_CACHE_040218_HPP
89