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