class_info.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. #define LUABIND_BUILDING
  20. #include <luabind/lua_include.hpp>
  21. #include <luabind/luabind.hpp>
  22. #include <luabind/class_info.hpp>
  23. #include <luabind/detail/class_registry.hpp>
  24. namespace luabind
  25. {
  26. LUABIND_API class_info get_class_info(argument const& o)
  27. {
  28. lua_State* L = o.interpreter();
  29. o.push(L);
  30. detail::object_rep* obj = detail::get_instance(L, -1);
  31. if (!obj)
  32. {
  33. class_info result;
  34. result.name = lua_typename(L, lua_type(L, -1));
  35. lua_pop(L, 1);
  36. result.methods = newtable(L);
  37. result.attributes = newtable(L);
  38. return result;
  39. }
  40. lua_pop(L, 1);
  41. obj->crep()->get_table(L);
  42. object table(from_stack(L, -1));
  43. lua_pop(L, 1);
  44. class_info result;
  45. result.name = obj->crep()->name();
  46. result.methods = newtable(L);
  47. result.attributes = newtable(L);
  48. std::size_t index = 1;
  49. for (iterator i(table), e; i != e; ++i)
  50. {
  51. if (type(*i) != LUA_TFUNCTION)
  52. continue;
  53. // We have to create a temporary `object` here, otherwise the proxy
  54. // returned by operator->() will mess up the stack. This is a known
  55. // problem that probably doesn't show up in real code very often.
  56. object member(*i);
  57. member.push(L);
  58. detail::stack_pop pop(L, 1);
  59. if (lua_tocfunction(L, -1) == &detail::property_tag)
  60. {
  61. result.attributes[index++] = i.key();
  62. }
  63. else
  64. {
  65. result.methods[i.key()] = *i;
  66. }
  67. }
  68. return result;
  69. }
  70. LUABIND_API object get_class_names(lua_State* L)
  71. {
  72. detail::class_registry* reg = detail::class_registry::get_registry(L);
  73. std::map<type_id, detail::class_rep*> const& classes = reg->get_classes();
  74. object result = newtable(L);
  75. std::size_t index = 1;
  76. for (std::map<type_id, detail::class_rep*>::const_iterator iter = classes.begin();
  77. iter != classes.end(); ++iter)
  78. {
  79. result[index++] = iter->second->name();
  80. }
  81. return result;
  82. }
  83. LUABIND_API void bind_class_info(lua_State* L)
  84. {
  85. module(L)
  86. [
  87. class_<class_info>("class_info_data")
  88. .def_readonly("name", &class_info::name)
  89. .def_readonly("methods", &class_info::methods)
  90. .def_readonly("attributes", &class_info::attributes),
  91. def("class_info", &get_class_info),
  92. def("class_names", &get_class_names)
  93. ];
  94. }
  95. }