OSDN Git Service

7a97bb3761a3badb6bb2f9d29ceece2fa2966fe5
[moflib/moflib.git] / moflib-1.0 / extlib / luabind-0.8 / src / create_class.cpp
1 // Copyright (c) 2003 Daniel Wallin and Arvid Norberg
2
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:
9
10 // The above copyright notice and this permission notice shall be included
11 // in all copies or substantial portions of the Software.
12
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.
22
23 #define LUABIND_BUILDING
24
25 #include <luabind/lua_include.hpp>
26
27 #include <luabind/luabind.hpp>
28
29 namespace luabind { namespace detail
30 {
31         namespace
32         {
33                 // expects two tables on the lua stack:
34                 // 1: destination
35                 // 2: source
36                 void copy_member_table(lua_State* L)
37                 {
38                         lua_pushnil(L);
39
40                         while (lua_next(L, -2))
41                         {
42                                 lua_pushstring(L, "__init");
43                                 if (lua_equal(L, -1, -3))
44                                 {
45                                         lua_pop(L, 2);
46                                         continue;
47                                 }
48                                 else lua_pop(L, 1); // __init string
49
50                                 lua_pushstring(L, "__finalize");
51                                 if (lua_equal(L, -1, -3))
52                                 {
53                                         lua_pop(L, 2);
54                                         continue;
55                                 }
56                                 else lua_pop(L, 1); // __finalize string
57
58                                 lua_pushvalue(L, -2); // copy key
59                                 lua_insert(L, -2);
60                                 lua_settable(L, -5);
61                         }
62                 }
63         }
64
65
66         int create_class::stage2(lua_State* L)
67         {
68                 class_rep* crep = static_cast<class_rep*>(lua_touserdata(L, lua_upvalueindex(1)));
69                 assert((crep != 0) && "internal error, please report");
70                 assert((is_class_rep(L, lua_upvalueindex(1))) && "internal error, please report");
71
72         #ifndef LUABIND_NO_ERROR_CHECKING
73
74                 if (!is_class_rep(L, 1))
75                 {
76                         lua_pushstring(L, "expected class to derive from or a newline");
77                         lua_error(L);
78                 }
79
80         #endif
81
82                 class_rep* base = static_cast<class_rep*>(lua_touserdata(L, 1));
83                 class_rep::base_info binfo;
84
85                 binfo.pointer_offset = 0;
86                 binfo.base = base;
87                 crep->add_base_class(binfo);
88
89                 // set holder size and alignment so that we can class_rep::allocate
90                 // can return the correctly sized buffers
91                 crep->derived_from(base);
92                 
93                 // copy base class members
94
95                 crep->get_table(L);
96                 base->get_table(L);
97                 copy_member_table(L);
98
99                 crep->get_default_table(L);
100                 base->get_default_table(L);
101                 copy_member_table(L);
102
103                 crep->set_type(base->type());
104
105                 return 0;
106         }
107
108         int create_class::stage1(lua_State* L)
109         {
110
111         #ifndef LUABIND_NO_ERROR_CHECKING
112
113                 if (lua_gettop(L) != 1 || lua_type(L, 1) != LUA_TSTRING || lua_isnumber(L, 1))
114                 {
115                         lua_pushstring(L, "invalid construct, expected class name");
116                         lua_error(L);
117                 }
118
119                 if (std::strlen(lua_tostring(L, 1)) != lua_strlen(L, 1))
120                 {
121                         lua_pushstring(L, "luabind does not support class names with extra nulls");
122                         lua_error(L);
123                 }
124
125         #endif
126
127                 const char* name = lua_tostring(L, 1);
128
129                 void* c = lua_newuserdata(L, sizeof(class_rep));
130                 new(c) class_rep(L, name);
131
132                 // make the class globally available
133                 lua_pushstring(L, name);
134                 lua_pushvalue(L, -2);
135                 lua_settable(L, LUA_GLOBALSINDEX);
136
137                 // also add it to the closure as return value
138                 lua_pushcclosure(L, &stage2, 1);
139
140                 return 1;
141         }
142
143 }}
144