fixed leaking lua-references as reported by Tom Spilman.
[luabind.git] / src / class_info.cpp
blob2d1f52f9955ce5ad3e5c38b5db01ed38d7b6c44e
1 // Copyright (c) 2003 Daniel Wallin and Arvid Norberg
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:
10 // The above copyright notice and this permission notice shall be included
11 // in all copies or substantial portions of the Software.
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.
23 #include <luabind/lua_include.hpp>
25 #define LUABIND_BUILDING
27 #include <luabind/luabind.hpp>
28 #include <luabind/class_info.hpp>
30 namespace luabind
32 class_info get_class_info(const object& o)
34 lua_State* L = o.lua_state();
36 class_info result(L);
38 o.pushvalue();
39 detail::object_rep* obj = static_cast<detail::object_rep*>(lua_touserdata(L, -1));
40 lua_pop(L, 1);
42 result.name = obj->crep()->name();
43 detail::getref(L, obj->crep()->table_ref());
44 result.methods.set();
46 result.attributes = newtable(L);
48 typedef detail::class_rep::property_map map_type;
50 std::size_t index = 1;
52 for (map_type::const_iterator i = obj->crep()->properties().begin();
53 i != obj->crep()->properties().end(); ++i)
55 result.attributes[index] = i->first;
58 return result;
61 void bind_class_info(lua_State* L)
63 module(L)
65 class_<class_info>("class_info_data")
66 .def_readonly("name", &class_info::name)
67 .def_readonly("methods", &class_info::methods)
68 .def_readonly("attributes", &class_info::attributes),
70 def("class_info", &get_class_info)