My Project
class_rep.hpp
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 
29 #include <string>
30 #include <utility>
31 #include <vector>
32 
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>
43 
44 namespace luabind { namespace detail
45 {
46 
47  LUABIND_API std::string stack_content_by_name(lua_State* L, int start_index);
48 
49  struct class_registration;
50 
51  struct conversion_storage;
52 
53  // This function is used as a tag to identify "properties".
54  LUABIND_API int property_tag(lua_State*);
55 
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
64 
65  class cast_graph;
66  class class_id_map;
67 
68  class LUABIND_API class_rep
69  {
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:
77 
78  enum class_type
79  {
80  cpp_class = 0,
81  lua_class = 1
82  };
83 
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
90  );
91 
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);
97 
98  ~class_rep();
99 
100  std::pair<void*,void*> allocate(lua_State* L) const;
101 
102  // this is called as metamethod __call on the class_rep.
103  static int constructor_dispatcher(lua_State* L);
104 
105  struct base_info
106  {
107  int pointer_offset; // the offset added to the pointer to obtain a basepointer (due to multiple-inheritance)
108  class_rep* base;
109  };
110 
111  void add_base_class(const base_info& binfo);
112 
113  const std::vector<base_info>& bases() const throw() { return m_bases; }
114 
115  void set_type(type_id const& t) { m_type = t; }
116  type_id const& type() const throw() { return m_type; }
117 
118  const char* name() const throw() { return m_name; }
119 
120  // the lua reference to the metatable for this class' instances
121  int metatable_ref() const throw() { return m_instance_metatable; }
122 
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); }
125 
126  class_type get_class_type() const { return m_class_type; }
127 
128  void add_static_constant(const char* name, int val);
129 
130  static int super_callback(lua_State* L);
131 
132  static int lua_settable_dispatcher(lua_State* L);
133 
134  // called from the metamethod for __index
135  // obj is the object pointer
136  static int static_class_gettable(lua_State* L);
137 
138  bool has_operator_in_lua(lua_State*, int id);
139 
140  cast_graph const& casts() const
141  {
142  return *m_casts;
143  }
144 
145  class_id_map const& classes() const
146  {
147  return *m_classes;
148  }
149 
150  private:
151 
152  void cache_operators(lua_State*);
153 
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;
160 
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;
165 
166  // the class' name (as given when registered to lua with class_)
167  const char* m_name;
168 
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;
174 
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;
181 
182  // this table contains default implementations of the
183  // virtual functions in m_table.
184  handle m_default_table;
185 
186  // the type of this class.. determines if it's written in c++ or lua
187  class_type m_class_type;
188 
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;
193 
194  std::map<const char*, int, ltstr> m_static_constants;
195 
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;
200 
201  cast_graph* m_casts;
202  class_id_map* m_classes;
203  };
204 
205  bool is_class_rep(lua_State* L, int index);
206 
207 }}
208 
209 //#include <luabind/detail/overload_rep_impl.hpp>
210 
211 #endif // LUABIND_CLASS_REP_HPP_INCLUDED
Definition: class.cpp:61
Definition: handle.hpp:33
Definition: class.hpp:170
Definition: typeid.hpp:22
Definition: inheritance.hpp:49
Definition: ref.hpp:39
Definition: class_rep.hpp:105
Definition: minilua.c:461
Definition: PEtypes.h:507
Definition: inheritance.hpp:25
Definition: class_rep.hpp:68