1 //------------------------------------------------------------------------------
3 https://github.com/vinniefalco/LuaBridge
5 Copyright 2016, Robin Gareus <robin@gareus.org>
6 Copyright 2012, Vinnie Falco <vinnie.falco@gmail.com>
7 Copyright 2007, Nathan Reed
9 License: The MIT License (http://www.opensource.org/licenses/mit-license.php)
11 Permission is hereby granted, free of charge, to any person obtaining a copy
12 of this software and associated documentation files (the "Software"), to deal
13 in the Software without restriction, including without limitation the rights
14 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 copies of the Software, and to permit persons to whom the Software is
16 furnished to do so, subject to the following conditions:
18 The above copyright notice and this permission notice shall be included in all
19 copies or substantial portions of the Software.
21 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 //==============================================================================
35 #include <type_traits>
42 std::string type_name()
44 typedef typename std::remove_reference<T>::type TR;
45 std::unique_ptr<char, void(*)(void*)> own
47 abi::__cxa_demangle(typeid(TR).name(), nullptr,
51 std::string r = own != nullptr ? own.get() : typeid(TR).name();
52 if (std::is_const<TR>::value)
54 if (std::is_volatile<TR>::value)
56 if (std::is_lvalue_reference<T>::value)
58 else if (std::is_rvalue_reference<T>::value)
65 #ifdef LUADOCOUT // lua
67 #define KEYEND "\"] = "
70 #define KEYEND "\" : "
73 #define CLASSDOC(TYPE, LUANAME, DECL, PARENTDECL) \
74 if (LuaBindingDoc::printBindings ()) { \
75 std::cout << "{ " << KEYSTA << "type" << KEYEND << " \"" << TYPE << "\",\n"; \
76 std::cout << " " << KEYSTA << "lua" << KEYEND << " \"" << LUANAME << "\",\n"; \
77 std::cout << " " << KEYSTA << "decl" << KEYEND << " \"" << DECL << "\",\n"; \
78 std::cout << " " << KEYSTA << "parent" << KEYEND << "\"" << PARENTDECL << "\"\n"; \
79 std::cout << "},\n"; \
82 #define PRINTDOC(TYPE, LUANAME, RETVAL, DECL) \
83 if (LuaBindingDoc::printBindings ()) { \
84 std::cout << "{ " << KEYSTA << "type" << KEYEND << " \"" << TYPE << "\",\n"; \
85 std::cout << " " << KEYSTA << "lua" << KEYEND << " \"" << LUANAME << "\",\n"; \
86 if (!(RETVAL).empty()) { \
87 std::cout << " " << KEYSTA << "ret" << KEYEND << " \"" << (RETVAL) << "\",\n"; \
89 std::cout << " " << KEYSTA << "decl" << KEYEND << " \"" << DECL << "\"\n"; \
90 std::cout << "},\n"; \
93 #define FUNDOC(TYPE, NAME, FUNCTOR) \
94 PRINTDOC(TYPE, _name << NAME, \
95 type_name< typename FuncTraits <FUNCTOR>::ReturnType >(), \
96 type_name< typename FuncTraits <FUNCTOR>::DeclType >())
98 #define DATADOC(TYPE, NAME, FUNCTOR) \
99 PRINTDOC(TYPE, _name << NAME, \
101 type_name< decltype(FUNCTOR) >())\
106 #define CLASSDOC(TYPE, LUANAME, DECL, PARENTDECL)
107 #define PRINTDOC(TYPE, LUANAME, RETVAL, DECL)
108 #define FUNDOC(TYPE, NAME, FUNCTOR)
109 #define DATADOC(TYPE, NAME, FUNCTOR)
113 /** Provides C++ to Lua registration capabilities.
115 This class is not instantiated directly, call `getGlobalNamespace` to start
116 the registration process.
121 Namespace& operator= (Namespace const& other);
124 int mutable m_stackSize;
127 //============================================================================
131 VF: This function looks handy, why aren't we using it?
134 static int luaError (lua_State* L, std::string message)
136 assert (lua_isstring (L, lua_upvalueindex (1)));
139 // Get information on the caller's caller to format the message,
140 // so the error appears to originate from the Lua source.
142 int result = lua_getstack (L, 2, &ar);
145 lua_getinfo (L, "Sl", &ar);
147 if (ar.currentline != -1)
149 // poor mans int to string to avoid <strstrream>.
150 lua_pushnumber (L, ar.currentline);
151 s = s + ":" + lua_tostring (L, -1) + ": ";
158 return luaL_error (L, s.c_str ());
162 //----------------------------------------------------------------------------
166 void pop (int n) const
168 if (m_stackSize >= n && lua_gettop (L) >= n)
175 throw std::logic_error ("invalid stack");
181 Factored base to reduce template instantiations.
186 ClassBase& operator= (ClassBase const& other);
189 friend class Namespace;
192 int mutable m_stackSize;
196 const Namespace* _parent;
200 //--------------------------------------------------------------------------
202 __index metamethod for a class.
204 This implements member functions, data members, and property members.
205 Functions are stored in the metatable and const metatable. Data members
206 and property members are in the __propget table.
208 If the key is not found, the search proceeds up the hierarchy of base
211 static int indexMetaMethod (lua_State* L)
215 assert (lua_isuserdata (L, 1)); // warn on security bypass
216 lua_getmetatable (L, 1); // get metatable for object
219 lua_pushvalue (L, 2); // push key arg2
220 lua_rawget (L, -2); // lookup key in metatable
221 if (lua_iscfunction (L, -1)) // ensure its a cfunction
223 lua_remove (L, -2); // remove metatable
227 else if (lua_isnil (L, -1))
234 throw std::logic_error ("not a cfunction");
237 rawgetfield (L, -1, "__propget"); // get __propget table
238 if (lua_istable (L, -1)) // ensure it is a table
240 lua_pushvalue (L, 2); // push key arg2
241 lua_rawget (L, -2); // lookup key in __propget
242 lua_remove (L, -2); // remove __propget
243 if (lua_iscfunction (L, -1)) // ensure its a cfunction
245 lua_remove (L, -2); // remove metatable
246 lua_pushvalue (L, 1); // push class arg1
251 else if (lua_isnil (L, -1))
259 // We only put cfunctions into __propget.
260 throw std::logic_error ("not a cfunction");
267 // __propget is missing, or not a table.
268 throw std::logic_error ("missing __propget table");
271 // Repeat the lookup in the __parent metafield,
272 // or return nil if the field doesn't exist.
273 rawgetfield (L, -1, "__parent");
274 if (lua_istable (L, -1))
276 // Remove metatable and repeat the search in __parent.
279 else if (lua_isnil (L, -1))
288 throw std::logic_error ("__parent is not a table");
295 //--------------------------------------------------------------------------
297 __newindex metamethod for classes.
299 This supports writable variables and properties on class objects. The
300 corresponding object is passed in the first parameter to the set function.
302 static int newindexMetaMethod (lua_State* L)
306 lua_getmetatable (L, 1);
311 rawgetfield (L, -1, "__propset");
312 if (!lua_isnil (L, -1))
314 lua_pushvalue (L, 2);
316 if (!lua_isnil (L, -1))
318 // found it, call the setFunction.
319 assert (lua_isfunction (L, -1));
320 lua_pushvalue (L, 1);
321 lua_pushvalue (L, 3);
330 // Repeat the lookup in the __parent metafield.
331 rawgetfield (L, -1, "__parent");
332 if (lua_isnil (L, -1))
334 // Either the property or __parent must exist.
335 result = luaL_error (L,
336 "no member named '%s'", lua_tostring (L, 2));
344 //--------------------------------------------------------------------------
346 Create the const table.
348 void createConstTable (char const* name)
351 lua_pushvalue (L, -1);
352 lua_setmetatable (L, -2);
353 lua_pushboolean (L, 1);
354 lua_rawsetp (L, -2, getIdentityKey ());
355 lua_pushstring (L, (std::string ("const ") + name).c_str ());
356 rawsetfield (L, -2, "__type");
357 lua_pushcfunction (L, &indexMetaMethod);
358 rawsetfield (L, -2, "__index");
359 lua_pushcfunction (L, &newindexMetaMethod);
360 rawsetfield (L, -2, "__newindex");
362 rawsetfield (L, -2, "__propget");
364 if (Security::hideMetatables ())
367 rawsetfield (L, -2, "__metatable");
371 //--------------------------------------------------------------------------
373 Create the class table.
375 The Lua stack should have the const table on top.
377 void createClassTable (char const* name)
380 lua_pushvalue (L, -1);
381 lua_setmetatable (L, -2);
382 lua_pushboolean (L, 1);
383 lua_rawsetp (L, -2, getIdentityKey ());
384 lua_pushstring (L, name);
385 rawsetfield (L, -2, "__type");
386 lua_pushcfunction (L, &indexMetaMethod);
387 rawsetfield (L, -2, "__index");
388 lua_pushcfunction (L, &newindexMetaMethod);
389 rawsetfield (L, -2, "__newindex");
391 rawsetfield (L, -2, "__propget");
393 rawsetfield (L, -2, "__propset");
395 lua_pushvalue (L, -2);
396 rawsetfield (L, -2, "__const"); // point to const table
398 lua_pushvalue (L, -1);
399 rawsetfield (L, -3, "__class"); // point const table to class table
401 if (Security::hideMetatables ())
404 rawsetfield (L, -2, "__metatable");
408 //--------------------------------------------------------------------------
410 Create the static table.
412 The Lua stack should have:
415 -3 enclosing namespace
417 void createStaticTable (char const* name)
421 lua_pushvalue (L, -1);
422 lua_setmetatable (L, -3);
424 rawsetfield (L, -5, name);
427 lua_pushlightuserdata (L, this);
428 lua_pushcclosure (L, &tostringMetaMethod, 1);
429 rawsetfield (L, -2, "__tostring");
431 lua_pushcfunction (L, &CFunc::indexMetaMethod);
432 rawsetfield (L, -2, "__index");
433 lua_pushcfunction (L, &CFunc::newindexMetaMethod);
434 rawsetfield (L, -2, "__newindex");
436 rawsetfield (L, -2, "__propget");
438 rawsetfield (L, -2, "__propset");
440 lua_pushvalue (L, -2);
441 rawsetfield (L, -2, "__class"); // point to class table
443 if (Security::hideMetatables ())
446 rawsetfield (L, -2, "__metatable");
450 //==========================================================================
452 lua_CFunction to construct a class object wrapped in a container.
454 template <class Params, class C>
455 static int ctorContainerProxy (lua_State* L)
457 typedef typename ContainerTraits <C>::Type T;
458 ArgList <Params, 2> args (L);
459 T* const p = Constructor <T, Params>::call (args);
460 UserdataSharedHelper <C, false>::push (L, p);
464 //--------------------------------------------------------------------------
466 lua_CFunction to construct a class object in-place in the userdata.
468 template <class Params, class T>
469 static int ctorPlacementProxy (lua_State* L)
471 ArgList <Params, 2> args (L);
472 Constructor <T, Params>::call (UserdataValue <T>::place (L), args);
476 //--------------------------------------------------------------------------
480 void pop (int n) const
482 if (m_stackSize >= n && lua_gettop (L) >= n)
489 throw std::logic_error ("invalid stack");
494 //--------------------------------------------------------------------------
495 explicit ClassBase (lua_State* L_)
501 //--------------------------------------------------------------------------
505 ClassBase (ClassBase const& other)
509 , _name (other._name)
510 , _parent (other._parent)
513 m_stackSize = other.m_stackSize;
514 other.m_stackSize = 0;
523 //============================================================================
527 //============================================================================
529 Provides a class registration in a lua_State.
531 After contstruction the Lua stack holds these objects:
535 -4 (enclosing namespace)
538 class Class : virtual public ClassBase
541 //==========================================================================
543 Register a new class or add to an existing class registration.
545 Class (char const* name, Namespace const* parent) : ClassBase (parent->L)
549 _name = parent->_name + name + ":";
551 PRINTDOC ("[C] Class", parent->_name << name, std::string(), type_name <T>())
552 m_stackSize = parent->m_stackSize + 3;
553 parent->m_stackSize = 0;
555 assert (lua_istable (L, -1));
556 rawgetfield (L, -1, name);
558 if (lua_isnil (L, -1))
562 createConstTable (name);
563 lua_pushcfunction (L, &CFunc::gcMetaMethod <T>);
564 rawsetfield (L, -2, "__gc");
566 createClassTable (name);
567 lua_pushcfunction (L, &CFunc::gcMetaMethod <T>);
568 rawsetfield (L, -2, "__gc");
570 createStaticTable (name);
572 // Map T back to its tables.
573 lua_pushvalue (L, -1);
574 lua_rawsetp (L, LUA_REGISTRYINDEX, ClassInfo <T>::getStaticKey ());
575 lua_pushvalue (L, -2);
576 lua_rawsetp (L, LUA_REGISTRYINDEX, ClassInfo <T>::getClassKey ());
577 lua_pushvalue (L, -3);
578 lua_rawsetp (L, LUA_REGISTRYINDEX, ClassInfo <T>::getConstKey ());
583 lua_rawgetp (L, LUA_REGISTRYINDEX, ClassInfo <T>::getStaticKey ());
584 rawgetfield (L, -1, "__class");
585 rawgetfield (L, -1, "__const");
587 // Reverse the top 3 stack elements
593 //==========================================================================
597 Class (char const* name, Namespace const* parent, void const* const staticKey)
598 : ClassBase (parent->L)
602 _name = parent->_name + name + ":";
604 m_stackSize = parent->m_stackSize + 3;
605 parent->m_stackSize = 0;
607 assert (lua_istable (L, -1));
609 createConstTable (name);
610 lua_pushcfunction (L, &CFunc::gcMetaMethod <T>);
611 rawsetfield (L, -2, "__gc");
613 createClassTable (name);
614 lua_pushcfunction (L, &CFunc::gcMetaMethod <T>);
615 rawsetfield (L, -2, "__gc");
617 createStaticTable (name);
619 lua_rawgetp (L, LUA_REGISTRYINDEX, staticKey);
620 assert (lua_istable (L, -1));
621 rawgetfield (L, -1, "__class");
622 assert (lua_istable (L, -1));
623 rawgetfield (L, -1, "__const");
624 assert (lua_istable (L, -1));
626 rawsetfield (L, -6, "__parent");
627 rawsetfield (L, -4, "__parent");
628 rawsetfield (L, -2, "__parent");
630 lua_pushvalue (L, -1);
631 lua_rawsetp (L, LUA_REGISTRYINDEX, ClassInfo <T>::getStaticKey ());
632 lua_pushvalue (L, -2);
633 lua_rawsetp (L, LUA_REGISTRYINDEX, ClassInfo <T>::getClassKey ());
634 lua_pushvalue (L, -3);
635 lua_rawsetp (L, LUA_REGISTRYINDEX, ClassInfo <T>::getConstKey ());
638 //--------------------------------------------------------------------------
640 Continue registration in the enclosing namespace.
642 Namespace endClass ()
644 return Namespace (this);
647 //--------------------------------------------------------------------------
649 Add or replace a static data member.
652 Class <T>& addStaticData (char const* name, U* pu, bool isWritable = true)
654 DATADOC ("Static Data Member", name, pu)
655 assert (lua_istable (L, -1));
657 rawgetfield (L, -1, "__propget");
658 assert (lua_istable (L, -1));
659 lua_pushlightuserdata (L, pu);
660 lua_pushcclosure (L, &CFunc::getVariable <U>, 1);
661 rawsetfield (L, -2, name);
664 rawgetfield (L, -1, "__propset");
665 assert (lua_istable (L, -1));
668 lua_pushlightuserdata (L, pu);
669 lua_pushcclosure (L, &CFunc::setVariable <U>, 1);
673 lua_pushstring (L, name);
674 lua_pushcclosure (L, &CFunc::readOnlyError, 1);
676 rawsetfield (L, -2, name);
682 //--------------------------------------------------------------------------
685 Add or replace a static property member.
687 If the set function is null, the property is read-only.
690 Class <T>& addStaticProperty (char const* name, U (*get)(), void (*set)(U) = 0)
692 typedef U (*get_t)();
693 typedef void (*set_t)(U);
695 assert (lua_istable (L, -1));
697 rawgetfield (L, -1, "__propget");
698 assert (lua_istable (L, -1));
699 new (lua_newuserdata (L, sizeof (get))) get_t (get);
700 lua_pushcclosure (L, &CFunc::Call <U (*) (void)>::f, 1);
701 rawsetfield (L, -2, name);
704 rawgetfield (L, -1, "__propset");
705 assert (lua_istable (L, -1));
708 new (lua_newuserdata (L, sizeof (set))) set_t (set);
709 lua_pushcclosure (L, &CFunc::Call <void (*) (U)>::f, 1);
713 lua_pushstring (L, name);
714 lua_pushcclosure (L, &CFunc::readOnlyError, 1);
716 rawsetfield (L, -2, name);
723 //--------------------------------------------------------------------------
725 Add or replace a static member function.
728 Class <T>& addStaticFunction (char const* name, FP const fp)
730 FUNDOC ("Static Member Function", name, FP)
731 new (lua_newuserdata (L, sizeof (fp))) FP (fp);
732 lua_pushcclosure (L, &CFunc::Call <FP>::f, 1);
733 rawsetfield (L, -2, name);
738 //--------------------------------------------------------------------------
740 Add or replace a lua_CFunction.
742 Class <T>& addStaticCFunction (char const* name, int (*const fp)(lua_State*))
744 DATADOC ("Static C Function", name, fp)
745 lua_pushcfunction (L, fp);
746 rawsetfield (L, -2, name);
750 //--------------------------------------------------------------------------
752 Add or replace a data member.
755 Class <T>& addData (char const* name, const U T::* mp, bool isWritable = true)
757 DATADOC ("Data Member", name, mp)
758 typedef const U T::*mp_t;
760 // Add to __propget in class and const tables.
762 rawgetfield (L, -2, "__propget");
763 rawgetfield (L, -4, "__propget");
764 new (lua_newuserdata (L, sizeof (mp_t))) mp_t (mp);
765 lua_pushcclosure (L, &CFunc::getProperty <T,U>, 1);
766 lua_pushvalue (L, -1);
767 rawsetfield (L, -4, name);
768 rawsetfield (L, -2, name);
774 // Add to __propset in class table.
775 rawgetfield (L, -2, "__propset");
776 assert (lua_istable (L, -1));
777 new (lua_newuserdata (L, sizeof (mp_t))) mp_t (mp);
778 lua_pushcclosure (L, &CFunc::setProperty <T,U>, 1);
779 rawsetfield (L, -2, name);
787 //--------------------------------------------------------------------------
789 Add or replace a property member.
791 template <class TG, class TS>
792 Class <T>& addProperty (char const* name, TG (T::* get) () const, void (T::* set) (TS))
794 // Add to __propget in class and const tables.
796 rawgetfield (L, -2, "__propget");
797 rawgetfield (L, -4, "__propget");
798 typedef TG (T::*get_t) () const;
799 new (lua_newuserdata (L, sizeof (get_t))) get_t (get);
800 lua_pushcclosure (L, &CFunc::CallConstMember <get_t>::f, 1);
801 lua_pushvalue (L, -1);
802 rawsetfield (L, -4, name);
803 rawsetfield (L, -2, name);
808 // Add to __propset in class table.
809 rawgetfield (L, -2, "__propset");
810 assert (lua_istable (L, -1));
811 typedef void (T::* set_t) (TS);
812 new (lua_newuserdata (L, sizeof (set_t))) set_t (set);
813 lua_pushcclosure (L, &CFunc::CallMember <set_t>::f, 1);
814 rawsetfield (L, -2, name);
823 Class <T>& addProperty (char const* name, TG (T::* get) () const)
825 // Add to __propget in class and const tables.
826 rawgetfield (L, -2, "__propget");
827 rawgetfield (L, -4, "__propget");
828 typedef TG (T::*get_t) () const;
829 new (lua_newuserdata (L, sizeof (get_t))) get_t (get);
830 lua_pushcclosure (L, &CFunc::CallConstMember <get_t>::f, 1);
831 lua_pushvalue (L, -1);
832 rawsetfield (L, -4, name);
833 rawsetfield (L, -2, name);
839 //--------------------------------------------------------------------------
841 Add or replace a property member, by proxy.
843 When a class is closed for modification and does not provide (or cannot
844 provide) the function signatures necessary to implement get or set for
845 a property, this will allow non-member functions act as proxies.
847 Both the get and the set functions require a T const* and T* in the first
848 argument respectively.
850 template <class TG, class TS>
851 Class <T>& addProperty (char const* name, TG (*get) (T const*), void (*set) (T*, TS))
853 // Add to __propget in class and const tables.
855 rawgetfield (L, -2, "__propget");
856 rawgetfield (L, -4, "__propget");
857 typedef TG (*get_t) (T const*);
858 new (lua_newuserdata (L, sizeof (get_t))) get_t (get);
859 lua_pushcclosure (L, &CFunc::Call <get_t>::f, 1);
860 lua_pushvalue (L, -1);
861 rawsetfield (L, -4, name);
862 rawsetfield (L, -2, name);
868 // Add to __propset in class table.
869 rawgetfield (L, -2, "__propset");
870 assert (lua_istable (L, -1));
871 typedef void (*set_t) (T*, TS);
872 new (lua_newuserdata (L, sizeof (set_t))) set_t (set);
873 lua_pushcclosure (L, &CFunc::Call <set_t>::f, 1);
874 rawsetfield (L, -2, name);
882 template <class TG, class TS>
883 Class <T>& addProperty (char const* name, TG (*get) (T const*))
885 // Add to __propget in class and const tables.
886 rawgetfield (L, -2, "__propget");
887 rawgetfield (L, -4, "__propget");
888 typedef TG (*get_t) (T const*);
889 new (lua_newuserdata (L, sizeof (get_t))) get_t (get);
890 lua_pushcclosure (L, &CFunc::Call <get_t>::f, 1);
891 lua_pushvalue (L, -1);
892 rawsetfield (L, -4, name);
893 rawsetfield (L, -2, name);
899 //--------------------------------------------------------------------------
901 Add or replace a member function.
903 template <class MemFn>
904 Class <T>& addFunction (char const* name, MemFn mf)
906 FUNDOC("Member Function", name, MemFn)
907 CFunc::CallMemberFunctionHelper <MemFn, FuncTraits <MemFn>::isConstMemberFunction>::add (L, name, mf);
911 template <class MemFn>
912 Class <T>& addPtrFunction (char const* name, MemFn mf)
914 FUNDOC("Member Pointer Function", name, MemFn)
915 CFunc::CallMemberPtrFunctionHelper <MemFn>::add (L, name, mf);
919 template <class MemFn>
920 Class <T>& addWPtrFunction (char const* name, MemFn mf)
922 FUNDOC("Member Weak Pointer Function", name, MemFn)
923 CFunc::CallMemberWPtrFunctionHelper <MemFn>::add (L, name, mf);
927 template <class MemFn>
928 Class <T>& addRefFunction (char const* name, MemFn mf)
930 FUNDOC("Member Function RefReturn", name, MemFn)
931 CFunc::CallMemberRefFunctionHelper <MemFn, FuncTraits <MemFn>::isConstMemberFunction>::add (L, name, mf);
936 //--------------------------------------------------------------------------
938 Add or replace a member lua_CFunction.
940 Class <T>& addCFunction (char const* name, int (T::*mfp)(lua_State*))
942 DATADOC ("C Function", name, mfp)
943 typedef int (T::*MFP)(lua_State*);
944 assert (lua_istable (L, -1));
945 new (lua_newuserdata (L, sizeof (mfp))) MFP (mfp);
946 lua_pushcclosure (L, &CFunc::CallMemberCFunction <T>::f, 1);
947 rawsetfield (L, -3, name); // class table
952 // custom callback - extend existing classes
953 // with non-class member functions (e.g STL iterator)
954 Class <T>& addExtCFunction (char const* name, int (*const fp)(lua_State*))
956 DATADOC ("Ext C Function", name, fp)
957 assert (lua_istable (L, -1));
958 lua_pushcclosure (L, fp, 0);
959 rawsetfield (L, -3, name); // class table
963 //--------------------------------------------------------------------------
965 Add or replace a const member lua_CFunction.
967 Class <T>& addCFunction (char const* name, int (T::*mfp)(lua_State*) const)
969 DATADOC ("Const C Member Function", name, mfp)
970 typedef int (T::*MFP)(lua_State*) const;
971 assert (lua_istable (L, -1));
972 new (lua_newuserdata (L, sizeof (mfp))) MFP (mfp);
973 lua_pushcclosure (L, &CFunc::CallConstMemberCFunction <T>::f, 1);
974 lua_pushvalue (L, -1);
975 rawsetfield (L, -5, name); // const table
976 rawsetfield (L, -3, name); // class table
982 Add or replace a static const data
984 template <typename U>
985 Class <T>& addConst (char const* name, const U val)
987 DATADOC ("Constant/Enum Member", name, val)
988 assert (lua_istable (L, -1));
990 rawgetfield (L, -1, "__propget"); // static
991 new (lua_newuserdata (L, sizeof (val))) U (val);
992 lua_pushcclosure (L, &CFunc::getConst <U>, 1);
993 rawsetfield (L, -2, name);
996 rawgetfield (L, -1, "__propset"); // static
997 lua_pushstring (L, name);
998 lua_pushcclosure (L, &CFunc::readOnlyError, 1);
999 rawsetfield (L, -2, name);
1004 //--------------------------------------------------------------------------
1006 Add or replace a primary Constructor.
1008 The primary Constructor is invoked when calling the class type table
1011 The template parameter should be a function pointer type that matches
1012 the desired Constructor (since you can't take the address of a Constructor
1013 and pass it as an argument).
1015 template <class MemFn, class C>
1016 Class <T>& addConstructor ()
1018 FUNDOC("Constructor", "", MemFn)
1019 lua_pushcclosure (L,
1020 &ctorContainerProxy <typename FuncTraits <MemFn>::Params, C>, 0);
1021 rawsetfield(L, -2, "__call");
1026 template <class MemFn>
1027 Class <T>& addConstructor ()
1029 FUNDOC("Constructor", "", MemFn)
1030 lua_pushcclosure (L,
1031 &ctorPlacementProxy <typename FuncTraits <MemFn>::Params, T>, 0);
1032 rawsetfield(L, -2, "__call");
1037 Class <T>& addVoidConstructor ()
1039 return addConstructor <void (*) ()> ();
1042 Class <T>& addEqualCheck ()
1044 PRINTDOC("Member Function", _name << "sameinstance", std::string("bool"), std::string("void (*)(" + type_name <T>() + ")"))
1045 assert (lua_istable (L, -1));
1046 lua_pushcclosure (L, &CFunc::ClassEqualCheck <T>::f, 0);
1047 rawsetfield (L, -3, "sameinstance");
1052 Class <T>& addCast (char const* name)
1054 PRINTDOC("Cast", _name << name,
1056 type_name< U >() << " (" << type_name< T >() << "::*)()")
1058 assert (lua_istable (L, -1));
1059 lua_pushcclosure (L, &CFunc::CastClass <T, U>::f, 0);
1060 rawsetfield (L, -3, name); // class table
1062 lua_pushcclosure (L, &CFunc::CastConstClass <T, U>::f, 0);
1063 rawsetfield (L, -4, name); // const table
1069 /** C Array to/from table */
1070 template <typename T>
1071 class Array : virtual public ClassBase
1074 Array (char const* name, Namespace const* parent) : ClassBase (parent->L)
1076 #ifdef LUABINDINGDOC
1078 _name = parent->_name + name + ":";
1080 PRINTDOC ("[C] Array", parent->_name << name,
1081 std::string(), type_name <T>() + "*")
1082 PRINTDOC ("Ext C Function", _name << "array",
1083 std::string(""), "int (*)(lua_State*)")
1084 PRINTDOC ("Ext C Function", _name << "get_table",
1085 std::string(""), "int (*)(lua_State*)")
1086 PRINTDOC ("Ext C Function", _name << "set_table",
1087 std::string(""), "int (*)(lua_State*)")
1088 PRINTDOC("Member Function", _name << "sameinstance",
1089 std::string("bool"), std::string("bool (*)(" + type_name <T>() + "*)"))
1090 PRINTDOC("Member Function", _name << "offset",
1091 std::string(type_name <T>() + "*"), std::string(type_name <T>() + "* (*)(unsigned int)"))
1093 m_stackSize = parent->m_stackSize + 3;
1094 parent->m_stackSize = 0;
1096 #if 0 // don't allow to duplicates handlers for same array-type
1097 assert (lua_istable (L, -1));
1098 lua_rawgetp (L, LUA_REGISTRYINDEX, ClassInfo <T>::getClassKey ());
1099 if (lua_istable (L, -1)) {
1107 assert (lua_istable (L, -1));
1108 rawgetfield (L, -1, name);
1110 if (lua_isnil (L, -1))
1114 // register array access in global namespace
1115 luaL_newmetatable (L, typeid(T).name());
1116 lua_pushcclosure (L, CFunc::array_index<T>, 0);
1117 lua_setfield(L, -2, "__index");
1118 lua_pushcclosure (L, CFunc::array_newindex<T>, 0);
1119 lua_setfield(L, -2, "__newindex");
1122 createConstTable (name);
1123 lua_pushcfunction (L, &CFunc::gcMetaMethod <T>);
1124 rawsetfield (L, -2, "__gc");
1126 createClassTable (name);
1127 lua_pushcfunction (L, &CFunc::gcMetaMethod <T>);
1128 rawsetfield (L, -2, "__gc");
1130 createStaticTable (name);
1132 // Map T back to its tables.
1133 lua_pushvalue (L, -1);
1134 lua_rawsetp (L, LUA_REGISTRYINDEX, ClassInfo <T>::getStaticKey ());
1135 lua_pushvalue (L, -2);
1136 lua_rawsetp (L, LUA_REGISTRYINDEX, ClassInfo <T>::getClassKey ());
1137 lua_pushvalue (L, -3);
1138 lua_rawsetp (L, LUA_REGISTRYINDEX, ClassInfo <T>::getConstKey ());
1140 assert (lua_istable (L, -1));
1141 lua_pushcclosure (L, &CFunc::getArray <T>, 0);
1142 rawsetfield (L, -3, "array"); // class table
1144 lua_pushcclosure (L, &CFunc::getTable <T>, 0);
1145 rawsetfield (L, -3, "get_table"); // class table
1147 lua_pushcclosure (L, &CFunc::setTable <T>, 0);
1148 rawsetfield (L, -3, "set_table"); // class table
1150 lua_pushcclosure (L, &CFunc::ClassEqualCheck <T>::f, 0);
1151 rawsetfield (L, -3, "sameinstance");
1153 lua_pushcclosure (L, &CFunc::offsetArray <T>, 0);
1154 rawsetfield (L, -3, "offset"); // class table
1164 Namespace endArray ()
1166 return Namespace (this);
1170 /** Boost Weak & Shared Pointer Class Wrapper */
1172 class WSPtrClass : virtual public ClassBase
1175 WSPtrClass (char const* name, Namespace const* parent)
1176 : ClassBase (parent->L)
1177 , weak (name, parent)
1178 , shared (name, parent)
1180 #ifdef LUABINDINGDOC
1182 _name = parent->_name + name + ":";
1184 PRINTDOC ("[C] Weak/Shared Pointer Class",
1185 parent->_name + name,
1186 std::string(), type_name <T>())
1187 m_stackSize = weak.m_stackSize;
1188 parent->m_stackSize = weak.m_stackSize = shared.m_stackSize = 0;
1192 WSPtrClass (char const* name, Namespace const* parent, void const* const sharedkey, void const* const weakkey)
1193 : ClassBase (parent->L)
1194 , weak (name, parent, weakkey)
1195 , shared (name, parent, sharedkey)
1197 #ifdef LUABINDINGDOC
1199 _name = parent->_name + name + ":";
1201 m_stackSize = weak.m_stackSize;
1202 parent->m_stackSize = weak.m_stackSize = shared.m_stackSize = 0;
1206 template <class MemFn>
1207 WSPtrClass <T>& addFunction (char const* name, MemFn mf)
1209 FUNDOC ("Weak/Shared Pointer Function", name, MemFn)
1211 CFunc::CallMemberWPtrFunctionHelper <MemFn>::add (L, name, mf);
1213 set_shared_class ();
1214 CFunc::CallMemberPtrFunctionHelper <MemFn>::add (L, name, mf);
1218 template <class MemFn>
1219 WSPtrClass <T>& addRefFunction (char const* name, MemFn mf)
1221 FUNDOC ("Weak/Shared Pointer Function RefReturn", name, MemFn)
1223 CFunc::CallMemberRefWPtrFunctionHelper <MemFn>::add (L, name, mf);
1225 set_shared_class ();
1226 CFunc::CallMemberRefPtrFunctionHelper <MemFn>::add (L, name, mf);
1230 template <class MemFn>
1231 WSPtrClass <T>& addConstructor ()
1233 FUNDOC ("Weak/Shared Pointer Constructor", "", MemFn)
1235 lua_pushcclosure (L,
1236 &weak. template ctorPlacementProxy <typename FuncTraits <MemFn>::Params, boost::weak_ptr<T> >, 0);
1237 rawsetfield(L, -2, "__call");
1239 set_shared_class ();
1240 lua_pushcclosure (L,
1241 &shared. template ctorPlacementProxy <typename FuncTraits <MemFn>::Params, boost::shared_ptr<T> >, 0);
1242 rawsetfield(L, -2, "__call");
1246 WSPtrClass <T>& addVoidConstructor ()
1248 return addConstructor <void (*) ()> ();
1251 WSPtrClass <T>& addExtCFunction (char const* name, int (*const fp)(lua_State*))
1253 DATADOC ("Weak/Shared Ext C Function", name, fp)
1255 assert (lua_istable (L, -1));
1256 lua_pushcclosure (L, fp, 0);
1257 rawsetfield (L, -3, name); // class table
1259 set_shared_class ();
1260 assert (lua_istable (L, -1));
1261 lua_pushcclosure (L, fp, 0);
1262 rawsetfield (L, -3, name); // class table
1268 WSPtrClass <T>& addCast (char const* name)
1270 PRINTDOC("Weak/Shared Pointer Cast", _name << name,
1272 type_name< U >() << " (" << type_name< T >() << "::*)()")
1275 set_shared_class ();
1276 assert (lua_istable (L, -1));
1277 lua_pushcclosure (L, &CFunc::CastMemberPtr <T, U>::f, 0);
1278 rawsetfield (L, -3, name); // class table
1282 WSPtrClass <T>& addNullCheck ()
1284 PRINTDOC("Weak/Shared Null Check", _name << "isnil", std::string("bool"), std::string("void (*)()"))
1286 assert (lua_istable (L, -1));
1287 lua_pushcclosure (L, &CFunc::WPtrNullCheck <T>::f, 0);
1288 rawsetfield (L, -3, "isnil"); // class table
1290 set_shared_class ();
1291 assert (lua_istable (L, -1));
1292 lua_pushcclosure (L, &CFunc::PtrNullCheck <T>::f, 0);
1293 rawsetfield (L, -3, "isnil"); // class table
1299 Namespace endClass ()
1301 return Namespace (this);
1305 void set_weak_class () {
1307 lua_rawgetp (L, LUA_REGISTRYINDEX, ClassInfo <boost::weak_ptr<T> >::getStaticKey ());
1308 rawgetfield (L, -1, "__class");
1309 rawgetfield (L, -1, "__const");
1313 void set_shared_class () {
1315 lua_rawgetp (L, LUA_REGISTRYINDEX, ClassInfo <boost::shared_ptr<T> >::getStaticKey ());
1316 rawgetfield (L, -1, "__class");
1317 rawgetfield (L, -1, "__const");
1321 Class<boost::weak_ptr<T> > weak;
1322 Class<boost::shared_ptr<T> > shared;
1327 //----------------------------------------------------------------------------
1329 Open the global namespace for registrations.
1331 explicit Namespace (lua_State* L_)
1334 #ifdef LUABINDINGDOC
1339 lua_getglobal (L, "_G");
1343 #ifdef LUABINDINGDOC
1345 Namespace const * _parent;
1348 //----------------------------------------------------------------------------
1350 Open a namespace for registrations.
1352 The namespace is created if it doesn't already exist.
1353 The parent namespace is at the top of the Lua stack.
1355 Namespace (char const* name, Namespace const* parent)
1358 #ifdef LUABINDINGDOC
1359 , _name (parent->_name + name + ":")
1363 m_stackSize = parent->m_stackSize + 1;
1364 parent->m_stackSize = 0;
1366 assert (lua_istable (L, -1));
1367 rawgetfield (L, -1, name);
1368 if (lua_isnil (L, -1))
1373 lua_pushvalue (L, -1);
1374 lua_setmetatable (L, -2);
1375 lua_pushcfunction (L, &CFunc::indexMetaMethod);
1376 rawsetfield (L, -2, "__index");
1377 lua_pushcfunction (L, &CFunc::newindexMetaMethod);
1378 rawsetfield (L, -2, "__newindex");
1380 rawsetfield (L, -2, "__propget");
1382 rawsetfield (L, -2, "__propset");
1383 lua_pushvalue (L, -1);
1384 rawsetfield (L, -3, name);
1386 lua_pushcfunction (L, &tostringMetaMethod);
1387 rawsetfield (L, -2, "__tostring");
1392 //----------------------------------------------------------------------------
1394 Creates a continued registration from a child namespace.
1396 explicit Namespace (Namespace const* child)
1399 #ifdef LUABINDINGDOC
1400 , _name (child->_parent ? child->_parent->_name : "")
1401 , _parent (child->_parent ? child->_parent->_parent : NULL)
1404 m_stackSize = child->m_stackSize - 1;
1405 child->m_stackSize = 1;
1408 // It is not necessary or valid to call
1409 // endNamespace() for the global namespace!
1411 assert (m_stackSize != 0);
1414 //----------------------------------------------------------------------------
1416 Creates a continued registration from a child class.
1418 explicit Namespace (ClassBase const* child)
1421 #ifdef LUABINDINGDOC
1422 , _name (child->_parent ? child->_parent->_name : "")
1423 , _parent (child->_parent ? child->_parent->_parent : NULL)
1426 m_stackSize = child->m_stackSize - 3;
1427 child->m_stackSize = 3;
1432 //----------------------------------------------------------------------------
1436 Ownership of the stack is transferred to the new object. This happens
1437 when the compiler emits temporaries to hold these objects while chaining
1438 registrations across namespaces.
1440 Namespace (Namespace const& other) : L (other.L)
1442 m_stackSize = other.m_stackSize;
1443 other.m_stackSize = 0;
1444 #ifdef LUABINDINGDOC
1445 _name = other._name;
1446 _parent = other._parent;
1450 //----------------------------------------------------------------------------
1452 Closes this namespace registration.
1459 //----------------------------------------------------------------------------
1461 Open the global namespace.
1463 static Namespace getGlobalNamespace (lua_State* L)
1465 return Namespace (L);
1468 //----------------------------------------------------------------------------
1470 Open a new or existing namespace for registrations.
1472 Namespace beginNamespace (char const* name)
1474 return Namespace (name, this);
1477 //----------------------------------------------------------------------------
1479 Continue namespace registration in the parent.
1481 Do not use this on the global namespace.
1483 Namespace endNamespace ()
1485 return Namespace (this);
1488 //----------------------------------------------------------------------------
1490 Add or replace a variable.
1493 Namespace& addVariable (char const* name, T* pt, bool isWritable = true)
1495 assert (lua_istable (L, -1));
1497 rawgetfield (L, -1, "__propget");
1498 assert (lua_istable (L, -1));
1499 lua_pushlightuserdata (L, pt);
1500 lua_pushcclosure (L, &CFunc::getVariable <T>, 1);
1501 rawsetfield (L, -2, name);
1504 rawgetfield (L, -1, "__propset");
1505 assert (lua_istable (L, -1));
1508 lua_pushlightuserdata (L, pt);
1509 lua_pushcclosure (L, &CFunc::setVariable <T>, 1);
1513 lua_pushstring (L, name);
1514 lua_pushcclosure (L, &CFunc::readOnlyError, 1);
1516 rawsetfield (L, -2, name);
1522 template <typename U>
1523 Namespace& addConst (char const* name, const U val)
1525 DATADOC ("Constant/Enum", name, val)
1526 assert (lua_istable (L, -1));
1527 rawgetfield (L, -1, "__propget");
1528 new (lua_newuserdata (L, sizeof (val))) U (val);
1529 lua_pushcclosure (L, &CFunc::getConst <U>, 1);
1530 rawsetfield (L, -2, name);
1533 rawgetfield (L, -1, "__propset");
1534 assert (lua_istable (L, -1));
1535 lua_pushstring (L, name);
1536 lua_pushcclosure (L, &CFunc::readOnlyError, 1);
1537 rawsetfield (L, -2, name);
1542 //----------------------------------------------------------------------------
1544 Add or replace a property.
1546 If the set function is omitted or null, the property is read-only.
1548 template <class TG, class TS>
1549 Namespace& addProperty (char const* name, TG (*get) (), void (*set)(TS) = 0)
1551 assert (lua_istable (L, -1));
1553 rawgetfield (L, -1, "__propget");
1554 assert (lua_istable (L, -1));
1555 typedef TG (*get_t) ();
1556 new (lua_newuserdata (L, sizeof (get_t))) get_t (get);
1557 lua_pushcclosure (L, &CFunc::Call <TG (*) (void)>::f, 1);
1558 rawsetfield (L, -2, name);
1561 rawgetfield (L, -1, "__propset");
1562 assert (lua_istable (L, -1));
1565 typedef void (*set_t) (TS);
1566 new (lua_newuserdata (L, sizeof (set_t))) set_t (set);
1567 lua_pushcclosure (L, &CFunc::Call <void (*) (TS)>::f, 1);
1571 lua_pushstring (L, name);
1572 lua_pushcclosure (L, &CFunc::readOnlyError, 1);
1574 rawsetfield (L, -2, name);
1580 //----------------------------------------------------------------------------
1582 Add or replace a free function.
1585 Namespace& addFunction (char const* name, FP const fp)
1587 FUNDOC ("Free Function", name, FP)
1588 assert (lua_istable (L, -1));
1590 new (lua_newuserdata (L, sizeof (fp))) FP (fp);
1591 lua_pushcclosure (L, &CFunc::Call <FP>::f, 1);
1592 rawsetfield (L, -2, name);
1598 Namespace& addRefFunction (char const* name, FP const fp)
1600 FUNDOC ("Free Function RefReturn", name, FP)
1601 assert (lua_istable (L, -1));
1603 new (lua_newuserdata (L, sizeof (fp))) FP (fp);
1604 lua_pushcclosure (L, &CFunc::CallRef <FP>::f, 1);
1605 rawsetfield (L, -2, name);
1610 //----------------------------------------------------------------------------
1612 Add or replace a array type
1615 template <typename T>
1616 Namespace registerArray (char const* name)
1618 return Array <T> (name, this).endArray();
1622 //----------------------------------------------------------------------------
1624 Add or replace a lua_CFunction.
1626 Namespace& addCFunction (char const* name, int (*const fp)(lua_State*))
1628 DATADOC ("Free C Function", name, fp)
1629 lua_pushcfunction (L, fp);
1630 rawsetfield (L, -2, name);
1635 //----------------------------------------------------------------------------
1637 Open a new or existing class for registrations.
1640 Class <T> beginClass (char const* name)
1642 return Class <T> (name, this);
1645 /** weak & shared pointer class */
1647 WSPtrClass <T> beginWSPtrClass (char const* name)
1649 return WSPtrClass <T> (name, this)
1653 //----------------------------------------------------------------------------
1655 template <class K, class V>
1656 Class<std::map<K, V> > beginStdMap (char const* name)
1658 typedef std::map<K, V> LT;
1659 typedef std::pair<const K, V> T;
1661 return beginClass<LT> (name)
1662 .addVoidConstructor ()
1663 .addFunction ("empty", <::empty)
1664 .addFunction ("size", <::size)
1665 .addFunction ("clear", (void (LT::*)())<::clear)
1666 .addFunction ("count", (void (LT::*)())<::count)
1667 .addExtCFunction ("add", &CFunc::tableToMap<K, V>)
1668 .addExtCFunction ("iter", &CFunc::mapIter<K, V>)
1669 .addExtCFunction ("table", &CFunc::mapToTable<K, V>);
1673 Class<std::set<T> > beginStdSet (char const* name)
1675 typedef std::set<T> LT;
1676 return beginClass<LT> (name)
1677 .addVoidConstructor ()
1678 .addFunction ("clear", (void (LT::*)())<::clear)
1679 .addFunction ("empty", <::empty)
1680 .addFunction ("size", <::size)
1681 .addExtCFunction ("add", &CFunc::tableToSet<T>)
1682 .addExtCFunction ("iter", &CFunc::setIter<T>)
1683 .addExtCFunction ("table", &CFunc::setToTable<T>);
1686 template <unsigned int T>
1687 Class<std::bitset<T> > beginStdBitSet (char const* name)
1689 typedef std::bitset<T> BS;
1690 return beginClass<BS> (name)
1691 .addVoidConstructor ()
1692 .addFunction ("reset", (BS& (BS::*)())&BS::reset)
1693 .addFunction ("set", (BS& (BS::*)(size_t, bool))&BS::set)
1694 .addFunction ("count", &BS::count)
1695 .addFunction ("any", &BS::any)
1696 .addFunction ("none", &BS::none)
1697 .addFunction ("test", &BS::test)
1698 .addFunction ("size", &BS::size)
1699 .addExtCFunction ("add", &CFunc::tableToBitSet<T>)
1700 .addExtCFunction ("table", &CFunc::bitSetToTable<T>);
1704 Class<std::list<T> > beginConstStdList (char const* name)
1706 typedef std::list<T> LT;
1707 return beginClass<LT> (name)
1708 .addVoidConstructor ()
1709 .addFunction ("empty", <::empty)
1710 .addFunction ("size", <::size)
1711 .addFunction ("reverse", <::reverse)
1712 .addExtCFunction ("iter", &CFunc::listIter<T, LT>)
1713 .addExtCFunction ("table", &CFunc::listToTable<T, LT>);
1717 Class<std::list<T> > beginStdList (char const* name)
1719 typedef std::list<T> LT;
1720 return beginConstStdList<T> (name)
1721 .addFunction ("unique", (void (LT::*)())<::unique)
1722 .addFunction ("push_back", (void (LT::*)(const T&))<::push_back)
1723 .addExtCFunction ("add", &CFunc::tableToList<T, LT>);
1727 Class<std::vector<T> > beginStdVector (char const* name)
1729 typedef std::vector<T> LT;
1730 typedef typename std::vector<T>::reference T_REF;
1731 typedef typename std::vector<T>::size_type T_SIZE;
1733 return beginClass<LT> (name)
1734 .addVoidConstructor ()
1735 .addFunction ("empty", <::empty)
1736 .addFunction ("size", <::size)
1737 .addFunction ("push_back", (void (LT::*)(const T&))<::push_back)
1738 .addFunction ("at", (T_REF (LT::*)(T_SIZE))<::at)
1739 .addExtCFunction ("add", &CFunc::tableToList<T, LT>)
1740 .addExtCFunction ("iter", &CFunc::listIter<T, LT>)
1741 .addExtCFunction ("table", &CFunc::listToTable<T, LT>);
1744 //----------------------------------------------------------------------------
1747 Class<boost::shared_ptr<std::list<T> > > beginPtrStdList (char const* name)
1749 typedef std::list<T> LT;
1751 return beginClass<boost::shared_ptr<LT> > (name)
1752 .addVoidConstructor ()
1753 .addPtrFunction ("empty", <::empty)
1754 .addPtrFunction ("size", <::size)
1755 .addPtrFunction ("reverse", <::reverse)
1756 .addPtrFunction ("unique", (void (LT::*)())<::unique)
1757 .addPtrFunction ("push_back", (void (LT::*)(const T&))<::push_back)
1758 .addExtCFunction ("add", &CFunc::ptrTableToList<T, LT>)
1759 .addExtCFunction ("iter", &CFunc::ptrListIter<T, LT>)
1760 .addExtCFunction ("table", &CFunc::ptrListToTable<T, LT>);
1764 Class<boost::shared_ptr<std::vector<T> > > beginPtrStdVector (char const* name)
1766 typedef std::vector<T> LT;
1767 typedef typename std::vector<T>::reference T_REF;
1768 typedef typename std::vector<T>::size_type T_SIZE;
1770 return beginClass<boost::shared_ptr<LT> > (name)
1771 .addVoidConstructor ()
1772 .addPtrFunction ("empty", <::empty)
1773 .addPtrFunction ("empty", <::empty)
1774 .addPtrFunction ("size", <::size)
1775 .addPtrFunction ("push_back", (void (LT::*)(const T&))<::push_back)
1776 .addPtrFunction ("at", (T_REF (LT::*)(T_SIZE))<::at)
1777 .addExtCFunction ("add", &CFunc::ptrTableToList<T, LT>)
1778 .addExtCFunction ("iter", &CFunc::ptrListIter<T, LT>)
1779 .addExtCFunction ("table", &CFunc::ptrListToTable<T, LT>);
1782 //----------------------------------------------------------------------------
1784 Derive a new class for registrations.
1786 To continue registrations for the class later, use beginClass().
1787 Do not call deriveClass() again.
1789 template <class T, class U>
1790 Class <T> deriveClass (char const* name)
1792 CLASSDOC ("[C] Derived Class", _name << name, type_name <T>(), type_name <U>())
1793 return Class <T> (name, this, ClassInfo <U>::getStaticKey ());
1796 template <class T, class U>
1797 WSPtrClass <T> deriveWSPtrClass (char const* name)
1800 CLASSDOC ("[C] Derived Class", _name << name, type_name <boost::shared_ptr<T> >(), type_name <boost::shared_ptr<U> >())
1801 CLASSDOC ("[C] Derived Class", _name << name, type_name <boost::weak_ptr<T> >(), type_name <boost::weak_ptr<U> >())
1802 CLASSDOC ("[C] Derived Pointer Class", _name << name, type_name <T>(), type_name <U>())
1803 return WSPtrClass <T> (name, this,
1804 ClassInfo <boost::shared_ptr<U> >::getStaticKey (),
1805 ClassInfo <boost::weak_ptr<U> >::getStaticKey ())
1811 //------------------------------------------------------------------------------
1813 Retrieve the global namespace.
1815 It is recommended to put your namespace inside the global namespace, and
1816 then add your classes and functions to it, rather than adding many classes
1817 and functions directly to the global namespace.
1819 inline Namespace getGlobalNamespace (lua_State* L)
1821 return Namespace::getGlobalNamespace (L);
1832 /* vim: set et sw=2: */