class.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. // Copyright (c) 2004 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. #define LUABIND_BUILDING
  20. #include <boost/foreach.hpp>
  21. #include <luabind/lua_include.hpp>
  22. #include <luabind/config.hpp>
  23. #include <luabind/class.hpp>
  24. #include <luabind/nil.hpp>
  25. #include <cstring>
  26. #include <iostream>
  27. namespace luabind
  28. {
  29. LUABIND_API detail::nil_type nil;
  30. }
  31. namespace luabind { namespace detail {
  32. namespace
  33. {
  34. struct cast_entry
  35. {
  36. cast_entry(class_id src, class_id target, cast_function cast)
  37. : src(src)
  38. , target(target)
  39. , cast(cast)
  40. {}
  41. class_id src;
  42. class_id target;
  43. cast_function cast;
  44. };
  45. } // namespace unnamed
  46. struct class_registration : registration
  47. {
  48. class_registration(char const* name);
  49. void register_(lua_State* L) const;
  50. const char* name_;
  51. mutable std::map<const char*, int, detail::ltstr> m_static_constants;
  52. typedef std::pair<type_id, cast_function> base_desc;
  53. mutable std::vector<base_desc> m_bases;
  54. type_id m_type;
  55. class_id m_id;
  56. class_id m_wrapper_id;
  57. type_id m_wrapper_type;
  58. std::vector<cast_entry> m_casts;
  59. scope m_scope;
  60. scope m_members;
  61. scope m_default_members;
  62. };
  63. class_registration::class_registration(char const* name)
  64. {
  65. name_ = name;
  66. }
  67. void class_registration::register_(lua_State* L) const
  68. {
  69. LUABIND_CHECK_STACK(L);
  70. assert(lua_type(L, -1) == LUA_TTABLE);
  71. lua_pushstring(L, name_);
  72. detail::class_rep* crep;
  73. detail::class_registry* r = detail::class_registry::get_registry(L);
  74. // create a class_rep structure for this class.
  75. // allocate it within lua to let lua collect it on
  76. // lua_close(). This is better than allocating it
  77. // as a static, since it will then be destructed
  78. // when the program exits instead.
  79. // warning: we assume that lua will not
  80. // move the userdata memory.
  81. lua_newuserdata(L, sizeof(detail::class_rep));
  82. crep = reinterpret_cast<detail::class_rep*>(lua_touserdata(L, -1));
  83. new(crep) detail::class_rep(
  84. m_type
  85. , name_
  86. , L
  87. );
  88. // register this new type in the class registry
  89. r->add_class(m_type, crep);
  90. lua_pushstring(L, "__luabind_class_map");
  91. lua_rawget(L, LUA_REGISTRYINDEX);
  92. class_map& classes = *static_cast<class_map*>(
  93. lua_touserdata(L, -1));
  94. lua_pop(L, 1);
  95. classes.put(m_id, crep);
  96. bool const has_wrapper = m_wrapper_id != registered_class<null_type>::id;
  97. if (has_wrapper)
  98. classes.put(m_wrapper_id, crep);
  99. crep->m_static_constants.swap(m_static_constants);
  100. detail::class_registry* registry = detail::class_registry::get_registry(L);
  101. crep->get_default_table(L);
  102. m_scope.register_(L);
  103. m_default_members.register_(L);
  104. lua_pop(L, 1);
  105. crep->get_table(L);
  106. m_members.register_(L);
  107. lua_pop(L, 1);
  108. lua_pushstring(L, "__luabind_cast_graph");
  109. lua_gettable(L, LUA_REGISTRYINDEX);
  110. cast_graph* const casts = static_cast<cast_graph*>(
  111. lua_touserdata(L, -1));
  112. lua_pop(L, 1);
  113. lua_pushstring(L, "__luabind_class_id_map");
  114. lua_gettable(L, LUA_REGISTRYINDEX);
  115. class_id_map* const class_ids = static_cast<class_id_map*>(
  116. lua_touserdata(L, -1));
  117. lua_pop(L, 1);
  118. class_ids->put(m_id, m_type);
  119. if (has_wrapper)
  120. class_ids->put(m_wrapper_id, m_wrapper_type);
  121. BOOST_FOREACH(cast_entry const& e, m_casts)
  122. {
  123. casts->insert(e.src, e.target, e.cast);
  124. }
  125. for (std::vector<base_desc>::iterator i = m_bases.begin();
  126. i != m_bases.end(); ++i)
  127. {
  128. LUABIND_CHECK_STACK(L);
  129. // the baseclass' class_rep structure
  130. detail::class_rep* bcrep = registry->find_class(i->first);
  131. detail::class_rep::base_info base;
  132. base.pointer_offset = 0;
  133. base.base = bcrep;
  134. crep->add_base_class(base);
  135. // copy base class table
  136. crep->get_table(L);
  137. bcrep->get_table(L);
  138. lua_pushnil(L);
  139. while (lua_next(L, -2))
  140. {
  141. lua_pushvalue(L, -2); // copy key
  142. lua_gettable(L, -5);
  143. if (!lua_isnil(L, -1))
  144. {
  145. lua_pop(L, 2);
  146. continue;
  147. }
  148. lua_pop(L, 1);
  149. lua_pushvalue(L, -2); // copy key
  150. lua_insert(L, -2);
  151. lua_settable(L, -5);
  152. }
  153. lua_pop(L, 2);
  154. // copy base class detaults table
  155. crep->get_default_table(L);
  156. bcrep->get_default_table(L);
  157. lua_pushnil(L);
  158. while (lua_next(L, -2))
  159. {
  160. lua_pushvalue(L, -2); // copy key
  161. lua_gettable(L, -5);
  162. if (!lua_isnil(L, -1))
  163. {
  164. lua_pop(L, 2);
  165. continue;
  166. }
  167. lua_pop(L, 1);
  168. lua_pushvalue(L, -2); // copy key
  169. lua_insert(L, -2);
  170. lua_settable(L, -5);
  171. }
  172. lua_pop(L, 2);
  173. }
  174. lua_settable(L, -3);
  175. }
  176. // -- interface ---------------------------------------------------------
  177. class_base::class_base(char const* name)
  178. : scope(std::auto_ptr<registration>(
  179. m_registration = new class_registration(name))
  180. )
  181. {
  182. }
  183. void class_base::init(
  184. type_id const& type_id_, class_id id
  185. , type_id const& wrapper_type, class_id wrapper_id)
  186. {
  187. m_registration->m_type = type_id_;
  188. m_registration->m_id = id;
  189. m_registration->m_wrapper_type = wrapper_type;
  190. m_registration->m_wrapper_id = wrapper_id;
  191. }
  192. void class_base::add_base(type_id const& base, cast_function cast)
  193. {
  194. m_registration->m_bases.push_back(std::make_pair(base, cast));
  195. }
  196. void class_base::add_member(registration* member)
  197. {
  198. std::auto_ptr<registration> ptr(member);
  199. m_registration->m_members.operator,(scope(ptr));
  200. }
  201. void class_base::add_default_member(registration* member)
  202. {
  203. std::auto_ptr<registration> ptr(member);
  204. m_registration->m_default_members.operator,(scope(ptr));
  205. }
  206. const char* class_base::name() const
  207. {
  208. return m_registration->name_;
  209. }
  210. void class_base::add_static_constant(const char* name, int val)
  211. {
  212. m_registration->m_static_constants[name] = val;
  213. }
  214. void class_base::add_inner_scope(scope& s)
  215. {
  216. m_registration->m_scope.operator,(s);
  217. }
  218. void class_base::add_cast(
  219. class_id src, class_id target, cast_function cast)
  220. {
  221. m_registration->m_casts.push_back(cast_entry(src, target, cast));
  222. }
  223. void add_custom_name(type_id const& i, std::string& s)
  224. {
  225. s += " [";
  226. s += i.name();
  227. s += "]";
  228. }
  229. std::string get_class_name(lua_State* L, type_id const& i)
  230. {
  231. std::string ret;
  232. assert(L);
  233. class_registry* r = class_registry::get_registry(L);
  234. class_rep* crep = r->find_class(i);
  235. if (crep == 0)
  236. {
  237. ret = "custom";
  238. add_custom_name(i, ret);
  239. }
  240. else
  241. {
  242. /* TODO reimplement this?
  243. if (i == crep->holder_type())
  244. {
  245. ret += "smart_ptr<";
  246. ret += crep->name();
  247. ret += ">";
  248. }
  249. else if (i == crep->const_holder_type())
  250. {
  251. ret += "smart_ptr<const ";
  252. ret += crep->name();
  253. ret += ">";
  254. }
  255. else*/
  256. {
  257. ret += crep->name();
  258. }
  259. }
  260. return ret;
  261. }
  262. }} // namespace luabind::detail