docs: Fix spelling.
[luabind.git] / src / class_info.cpp
blob531f6e0cc073c63d1ea7be961ebd7d38a8f50975
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>
29 #include <luabind/detail/class_registry.hpp>
31 namespace luabind
33 LUABIND_API class_info get_class_info(argument const& o)
35 lua_State* L = o.interpreter();
37 o.push(L);
38 detail::object_rep* obj = detail::get_instance(L, -1);
40 if (!obj)
42 class_info result;
43 result.name = lua_typename(L, lua_type(L, -1));
44 lua_pop(L, 1);
45 result.methods = newtable(L);
46 result.attributes = newtable(L);
47 return result;
50 lua_pop(L, 1);
52 obj->crep()->get_table(L);
53 object table(from_stack(L, -1));
54 lua_pop(L, 1);
56 class_info result;
57 result.name = obj->crep()->name();
58 result.methods = newtable(L);
59 result.attributes = newtable(L);
61 std::size_t index = 1;
63 for (iterator i(table), e; i != e; ++i)
65 if (type(*i) != LUA_TFUNCTION)
66 continue;
68 // We have to create a temporary `object` here, otherwise the proxy
69 // returned by operator->() will mess up the stack. This is a known
70 // problem that probably doesn't show up in real code very often.
71 object member(*i);
72 member.push(L);
73 detail::stack_pop pop(L, 1);
75 if (lua_tocfunction(L, -1) == &detail::property_tag)
77 result.attributes[index++] = i.key();
79 else
81 result.methods[i.key()] = *i;
85 return result;
88 LUABIND_API object get_class_names(lua_State* L)
90 detail::class_registry* reg = detail::class_registry::get_registry(L);
92 std::map<type_id, detail::class_rep*> const& classes = reg->get_classes();
94 object result = newtable(L);
95 std::size_t index = 1;
97 for (std::map<type_id, detail::class_rep*>::const_iterator iter = classes.begin();
98 iter != classes.end(); ++iter)
100 result[index++] = iter->second->name();
103 return result;
106 LUABIND_API void bind_class_info(lua_State* L)
108 module(L)
110 class_<class_info>("class_info_data")
111 .def_readonly("name", &class_info::name)
112 .def_readonly("methods", &class_info::methods)
113 .def_readonly("attributes", &class_info::attributes),
115 def("class_info", &get_class_info),
116 def("class_names", &get_class_names)