class_rep.hpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. // Copyright (c) 2003 Daniel Wallin and Arvid Norberg
  2. // Permission is hereby granted, free of charge, to any person obtaining a
  3. // copy of this software and associated documentation files (the "Software"),
  4. // to deal in the Software without restriction, including without limitation
  5. // the rights to use, copy, modify, merge, publish, distribute, sublicense,
  6. // and/or sell copies of the Software, and to permit persons to whom the
  7. // Software is furnished to do so, subject to the following conditions:
  8. // The above copyright notice and this permission notice shall be included
  9. // in all copies or substantial portions of the Software.
  10. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
  11. // ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  12. // TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  13. // PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
  14. // SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
  15. // ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  16. // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
  18. // OR OTHER DEALINGS IN THE SOFTWARE.
  19. #ifndef LUABIND_CLASS_REP_HPP_INCLUDED
  20. #define LUABIND_CLASS_REP_HPP_INCLUDED
  21. #include <boost/limits.hpp>
  22. #include <string>
  23. #include <utility>
  24. #include <vector>
  25. #include <luabind/config.hpp>
  26. #include <luabind/lua_include.hpp>
  27. #include <luabind/detail/garbage_collector.hpp>
  28. #include <luabind/detail/operator_id.hpp>
  29. #include <luabind/detail/class_registry.hpp>
  30. #include <luabind/error.hpp>
  31. #include <luabind/handle.hpp>
  32. #include <luabind/detail/primitives.hpp>
  33. #include <luabind/typeid.hpp>
  34. #include <luabind/detail/ref.hpp>
  35. namespace luabind { namespace detail
  36. {
  37. LUABIND_API std::string stack_content_by_name(lua_State* L, int start_index);
  38. struct class_registration;
  39. struct conversion_storage;
  40. // This function is used as a tag to identify "properties".
  41. LUABIND_API int property_tag(lua_State*);
  42. // this is class-specific information, poor man's vtable
  43. // this is allocated statically (removed by the compiler)
  44. // a pointer to this structure is stored in the lua tables'
  45. // metatable with the name __classrep
  46. // it is used when matching parameters to function calls
  47. // to determine possible implicit casts
  48. // it is also used when finding the best match for overloaded
  49. // methods
  50. class cast_graph;
  51. class class_id_map;
  52. class LUABIND_API class_rep
  53. {
  54. friend struct class_registration;
  55. friend int super_callback(lua_State*);
  56. //TODO: avoid the lua-prefix
  57. friend int lua_class_gettable(lua_State*);
  58. friend int lua_class_settable(lua_State*);
  59. friend int static_class_gettable(lua_State*);
  60. public:
  61. enum class_type
  62. {
  63. cpp_class = 0,
  64. lua_class = 1
  65. };
  66. // EXPECTS THE TOP VALUE ON THE LUA STACK TO
  67. // BE THE USER DATA WHERE THIS CLASS IS BEING
  68. // INSTANTIATED!
  69. class_rep(type_id const& type
  70. , const char* name
  71. , lua_State* L
  72. );
  73. // used when creating a lua class
  74. // EXPECTS THE TOP VALUE ON THE LUA STACK TO
  75. // BE THE USER DATA WHERE THIS CLASS IS BEING
  76. // INSTANTIATED!
  77. class_rep(lua_State* L, const char* name);
  78. ~class_rep();
  79. std::pair<void*,void*> allocate(lua_State* L) const;
  80. // this is called as metamethod __call on the class_rep.
  81. static int constructor_dispatcher(lua_State* L);
  82. struct base_info
  83. {
  84. int pointer_offset; // the offset added to the pointer to obtain a basepointer (due to multiple-inheritance)
  85. class_rep* base;
  86. };
  87. void add_base_class(const base_info& binfo);
  88. const std::vector<base_info>& bases() const throw() { return m_bases; }
  89. void set_type(type_id const& t) { m_type = t; }
  90. type_id const& type() const throw() { return m_type; }
  91. const char* name() const throw() { return name_; }
  92. // the lua reference to the metatable for this class' instances
  93. int metatable_ref() const throw() { return m_instance_metatable; }
  94. void get_table(lua_State* L) const { m_table.push(L); }
  95. void get_default_table(lua_State* L) const { m_default_table.push(L); }
  96. class_type get_class_type() const { return m_class_type; }
  97. void add_static_constant(const char* name, int val);
  98. static int super_callback(lua_State* L);
  99. static int lua_settable_dispatcher(lua_State* L);
  100. // called from the metamethod for __index
  101. // obj is the object pointer
  102. static int static_class_gettable(lua_State* L);
  103. bool has_operator_in_lua(lua_State*, int id);
  104. cast_graph const& casts() const
  105. {
  106. return *m_casts;
  107. }
  108. class_id_map const& classes() const
  109. {
  110. return *m_classes;
  111. }
  112. private:
  113. void cache_operators(lua_State*);
  114. // this is a pointer to the type_info structure for
  115. // this type
  116. // warning: this may be a problem when using dll:s, since
  117. // typeid() may actually return different pointers for the same
  118. // type.
  119. type_id m_type;
  120. // a list of info for every class this class derives from
  121. // the information stored here is sufficient to do
  122. // type casts to the base classes
  123. std::vector<base_info> m_bases;
  124. // the class' name (as given when registered to lua with class_)
  125. const char* name_;
  126. // a reference to this structure itself. Since this struct
  127. // is kept inside lua (to let lua collect it when lua_close()
  128. // is called) we need to lock it to prevent collection.
  129. // the actual reference is not currently used.
  130. detail::lua_reference m_self_ref;
  131. // this should always be used when accessing
  132. // members in instances of a class.
  133. // this table contains c closures for all
  134. // member functions in this class, they
  135. // may point to both static and virtual functions
  136. handle m_table;
  137. // this table contains default implementations of the
  138. // virtual functions in m_table.
  139. handle m_default_table;
  140. // the type of this class.. determines if it's written in c++ or lua
  141. class_type m_class_type;
  142. // this is a lua reference that points to the lua table
  143. // that is to be used as meta table for all instances
  144. // of this class.
  145. int m_instance_metatable;
  146. std::map<const char*, int, ltstr> m_static_constants;
  147. // the first time an operator is invoked
  148. // we check the associated lua table
  149. // and cache the result
  150. int m_operator_cache;
  151. cast_graph* m_casts;
  152. class_id_map* m_classes;
  153. };
  154. bool is_class_rep(lua_State* L, int index);
  155. }}
  156. //#include <luabind/detail/overload_rep_impl.hpp>
  157. #endif // LUABIND_CLASS_REP_HPP_INCLUDED