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