Test object identity with shared_ptr_converter.
[luabind.git] / luabind / detail / class_rep.hpp
blobaa5e866d394c823a604cbc27ce544a879ffe8f94
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_REP_HPP_INCLUDED
25 #define LUABIND_CLASS_REP_HPP_INCLUDED
27 #include <boost/limits.hpp>
28 #include <boost/preprocessor/repetition/enum_params_with_a_default.hpp>
30 #include <string>
31 #include <utility>
32 #include <vector>
34 #include <luabind/config.hpp>
35 #include <luabind/lua_include.hpp>
36 #include <luabind/detail/object_rep.hpp>
37 #include <luabind/detail/garbage_collector.hpp>
38 #include <luabind/detail/operator_id.hpp>
39 #include <luabind/detail/class_registry.hpp>
40 #include <luabind/error.hpp>
41 #include <luabind/handle.hpp>
42 #include <luabind/detail/primitives.hpp>
43 #include <luabind/typeid.hpp>
44 #include <luabind/detail/ref.hpp>
46 namespace luabind { namespace detail
49 LUABIND_API std::string stack_content_by_name(lua_State* L, int start_index);
51 struct class_registration;
53 struct conversion_storage;
55 // This function is used as a tag to identify "properties".
56 LUABIND_API int property_tag(lua_State*);
58 // this is class-specific information, poor man's vtable
59 // this is allocated statically (removed by the compiler)
60 // a pointer to this structure is stored in the lua tables'
61 // metatable with the name __classrep
62 // it is used when matching parameters to function calls
63 // to determine possible implicit casts
64 // it is also used when finding the best match for overloaded
65 // methods
67 class cast_graph;
68 class class_id_map;
70 class LUABIND_API class_rep
72 friend struct class_registration;
73 friend int super_callback(lua_State*);
74 //TODO: avoid the lua-prefix
75 friend int lua_class_gettable(lua_State*);
76 friend int lua_class_settable(lua_State*);
77 friend int static_class_gettable(lua_State*);
78 public:
80 enum class_type
82 cpp_class = 0,
83 lua_class = 1
86 // EXPECTS THE TOP VALUE ON THE LUA STACK TO
87 // BE THE USER DATA WHERE THIS CLASS IS BEING
88 // INSTANTIATED!
89 class_rep(type_id const& type
90 , const char* name
91 , lua_State* L
94 // used when creating a lua class
95 // EXPECTS THE TOP VALUE ON THE LUA STACK TO
96 // BE THE USER DATA WHERE THIS CLASS IS BEING
97 // INSTANTIATED!
98 class_rep(lua_State* L, const char* name);
100 ~class_rep();
102 std::pair<void*,void*> allocate(lua_State* L) const;
104 // this is called as metamethod __call on the class_rep.
105 static int constructor_dispatcher(lua_State* L);
107 struct base_info
109 int pointer_offset; // the offset added to the pointer to obtain a basepointer (due to multiple-inheritance)
110 class_rep* base;
113 void add_base_class(const base_info& binfo);
115 const std::vector<base_info>& bases() const throw() { return m_bases; }
117 void set_type(type_id const& t) { m_type = t; }
118 type_id const& type() const throw() { return m_type; }
120 const char* name() const throw() { return m_name; }
122 // the lua reference to the metatable for this class' instances
123 int metatable_ref() const throw() { return m_instance_metatable; }
125 void get_table(lua_State* L) const { m_table.push(L); }
126 void get_default_table(lua_State* L) const { m_default_table.push(L); }
128 class_type get_class_type() const { return m_class_type; }
130 void add_static_constant(const char* name, int val);
132 static int super_callback(lua_State* L);
134 static int lua_settable_dispatcher(lua_State* L);
136 // called from the metamethod for __index
137 // obj is the object pointer
138 static int static_class_gettable(lua_State* L);
140 bool has_operator_in_lua(lua_State*, int id);
142 cast_graph const& casts() const
144 return *m_casts;
147 class_id_map const& classes() const
149 return *m_classes;
152 private:
154 void cache_operators(lua_State*);
156 // this is a pointer to the type_info structure for
157 // this type
158 // warning: this may be a problem when using dll:s, since
159 // typeid() may actually return different pointers for the same
160 // type.
161 type_id m_type;
163 // a list of info for every class this class derives from
164 // the information stored here is sufficient to do
165 // type casts to the base classes
166 std::vector<base_info> m_bases;
168 // the class' name (as given when registered to lua with class_)
169 const char* m_name;
171 // a reference to this structure itself. Since this struct
172 // is kept inside lua (to let lua collect it when lua_close()
173 // is called) we need to lock it to prevent collection.
174 // the actual reference is not currently used.
175 detail::lua_reference m_self_ref;
177 // this should always be used when accessing
178 // members in instances of a class.
179 // this table contains c closures for all
180 // member functions in this class, they
181 // may point to both static and virtual functions
182 handle m_table;
184 // this table contains default implementations of the
185 // virtual functions in m_table.
186 handle m_default_table;
188 // the type of this class.. determines if it's written in c++ or lua
189 class_type m_class_type;
191 // this is a lua reference that points to the lua table
192 // that is to be used as meta table for all instances
193 // of this class.
194 int m_instance_metatable;
196 std::map<const char*, int, ltstr> m_static_constants;
198 // the first time an operator is invoked
199 // we check the associated lua table
200 // and cache the result
201 int m_operator_cache;
203 cast_graph* m_casts;
204 class_id_map* m_classes;
207 bool is_class_rep(lua_State* L, int index);
211 //#include <luabind/detail/overload_rep_impl.hpp>
213 #endif // LUABIND_CLASS_REP_HPP_INCLUDED