1 // Copyright (c) 2003 Daniel Wallin and Arvid Norberg 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 24 #ifndef LUABIND_CLASS_REP_HPP_INCLUDED 25 #define LUABIND_CLASS_REP_HPP_INCLUDED 26 27 #include <boost/limits.hpp> 28 #include <boost/preprocessor/repetition/enum_params_with_a_default.hpp> 29 30 #include <string> 31 #include <utility> 32 #include <vector> 33 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> 45 46 namespace luabind { namespace detail 47 { 48 49 LUABIND_API std::string stack_content_by_name(lua_State* L, int start_index); 50 51 struct class_registration; 52 53 struct conversion_storage; 54 55 // This function is used as a tag to identify "properties". 56 LUABIND_API int property_tag(lua_State*); 57 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 66 67 class cast_graph; 68 class class_id_map; 69 70 class LUABIND_API class_rep 71 { 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: 79 80 enum class_type 81 { 82 cpp_class = 0, 83 lua_class = 1 84 }; 85 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 92 ); 93 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); 99 100 ~class_rep(); 101 102 std::pair<void*,void*> allocate(lua_State* L) const; 103 104 // this is called as metamethod __call on the class_rep. 105 static int constructor_dispatcher(lua_State* L); 106 107 struct base_info 108 { 109 int pointer_offset; // the offset added to the pointer to obtain a basepointer (due to multiple-inheritance) 110 class_rep* base; 111 }; 112 113 void add_base_class(const base_info& binfo); 114 bases() const115 const std::vector<base_info>& bases() const throw() { return m_bases; } 116 set_type(type_id const & t)117 void set_type(type_id const& t) { m_type = t; } type() const118 type_id const& type() const throw() { return m_type; } 119 name() const120 const char* name() const throw() { return m_name; } 121 122 // the lua reference to the metatable for this class' instances metatable_ref() const123 int metatable_ref() const throw() { return m_instance_metatable; } 124 get_table(lua_State * L) const125 void get_table(lua_State* L) const { m_table.push(L); } get_default_table(lua_State * L) const126 void get_default_table(lua_State* L) const { m_default_table.push(L); } 127 get_class_type() const128 class_type get_class_type() const { return m_class_type; } 129 130 void add_static_constant(const char* name, int val); 131 132 static int super_callback(lua_State* L); 133 134 static int lua_settable_dispatcher(lua_State* L); 135 136 // called from the metamethod for __index 137 // obj is the object pointer 138 static int static_class_gettable(lua_State* L); 139 140 bool has_operator_in_lua(lua_State*, int id); 141 casts() const142 cast_graph const& casts() const 143 { 144 return *m_casts; 145 } 146 classes() const147 class_id_map const& classes() const 148 { 149 return *m_classes; 150 } 151 152 private: 153 154 void cache_operators(lua_State*); 155 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; 162 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; 167 168 // the class' name (as given when registered to lua with class_) 169 const char* m_name; 170 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; 176 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; 183 184 // this table contains default implementations of the 185 // virtual functions in m_table. 186 handle m_default_table; 187 188 // the type of this class.. determines if it's written in c++ or lua 189 class_type m_class_type; 190 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; 195 196 std::map<const char*, int, ltstr> m_static_constants; 197 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; 202 203 cast_graph* m_casts; 204 class_id_map* m_classes; 205 }; 206 207 bool is_class_rep(lua_State* L, int index); 208 209 }} 210 211 //#include <luabind/detail/overload_rep_impl.hpp> 212 213 #endif // LUABIND_CLASS_REP_HPP_INCLUDED 214