Reimplement class_info() for the new property system.
[luabind.git] / src / class_info.cpp
blobff2fe0d219860c1096b139124ff44011fcf40c38
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 #define LUABIND_BUILDING
25 #include <luabind/lua_include.hpp>
27 #include <luabind/luabind.hpp>
28 #include <luabind/class_info.hpp>
30 namespace luabind
32 class_info get_class_info(argument const& o)
34 lua_State* L = o.interpreter();
36 o.push(L);
37 detail::object_rep* obj = detail::is_class_object(L, -1);
38 lua_pop(L, 1);
40 obj->crep()->get_table(L);
41 object table(from_stack(L, -1));
42 lua_pop(L, 1);
44 class_info result;
45 result.name = obj->crep()->name();
46 result.methods = newtable(L);
47 result.attributes = newtable(L);
49 std::size_t index = 1;
51 for (iterator i(table), e; i != e; ++i)
53 if (type(*i) != LUA_TFUNCTION)
54 continue;
56 // We have to create a temporary `object` here, otherwise the proxy
57 // returned by operator->() will mess up the stack. This is a known
58 // problem that probably doesn't show up in real code very often.
59 object member(*i);
60 member.push(L);
61 detail::stack_pop pop(L, 1);
63 if (lua_tocfunction(L, -1) == &detail::property_tag)
65 result.attributes[index++] = i.key();
67 else
69 result.methods[i.key()] = *i;
73 return result;
76 void bind_class_info(lua_State* L)
78 module(L)
80 class_<class_info>("class_info_data")
81 .def_readonly("name", &class_info::name)
82 .def_readonly("methods", &class_info::methods)
83 .def_readonly("attributes", &class_info::attributes),
85 def("class_info", &get_class_info)