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 ())
366 lua_pushboolean (L, false);
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 ())
403 lua_pushboolean (L, false);
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 ())
445 lua_pushboolean (L, false);
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 template <class Params, class T, class C>
477 static int ctorPtrPlacementProxy (lua_State* L)
479 ArgList <Params, 2> args (L);
480 T newobject (Constructor <C, Params>::call (args));
481 Stack<T>::push (L, newobject);
486 static int ctorNilPtrPlacementProxy (lua_State* L)
488 const T* newobject = new T ();
489 Stack<T>::push (L, *newobject);
493 //--------------------------------------------------------------------------
497 void pop (int n) const
499 if (m_stackSize >= n && lua_gettop (L) >= n)
506 throw std::logic_error ("invalid stack");
511 //--------------------------------------------------------------------------
512 explicit ClassBase (lua_State* L_)
518 //--------------------------------------------------------------------------
522 ClassBase (ClassBase const& other)
526 , _name (other._name)
527 , _parent (other._parent)
530 m_stackSize = other.m_stackSize;
531 other.m_stackSize = 0;
540 //============================================================================
544 //============================================================================
546 Provides a class registration in a lua_State.
548 After contstruction the Lua stack holds these objects:
552 -4 (enclosing namespace)
555 class Class : virtual public ClassBase
558 //==========================================================================
560 Register a new class or add to an existing class registration.
562 Class (char const* name, Namespace const* parent) : ClassBase (parent->L)
566 _name = parent->_name + name + ":";
568 PRINTDOC ("[C] Class", parent->_name << name, std::string(), type_name <T>())
569 m_stackSize = parent->m_stackSize + 3;
570 parent->m_stackSize = 0;
572 assert (lua_istable (L, -1));
573 rawgetfield (L, -1, name);
575 if (lua_isnil (L, -1))
579 createConstTable (name);
580 lua_pushcfunction (L, &CFunc::gcMetaMethod <T>);
581 rawsetfield (L, -2, "__gc");
582 lua_pushcclosure (L, &CFunc::ClassEqualCheck <T>::f, 0);
583 rawsetfield (L, -2, "__eq");
585 createClassTable (name);
586 lua_pushcfunction (L, &CFunc::gcMetaMethod <T>);
587 rawsetfield (L, -2, "__gc");
588 lua_pushcclosure (L, &CFunc::ClassEqualCheck <T>::f, 0);
589 rawsetfield (L, -2, "__eq");
591 createStaticTable (name);
593 // Map T back to its tables.
594 lua_pushvalue (L, -1);
595 lua_rawsetp (L, LUA_REGISTRYINDEX, ClassInfo <T>::getStaticKey ());
596 lua_pushvalue (L, -2);
597 lua_rawsetp (L, LUA_REGISTRYINDEX, ClassInfo <T>::getClassKey ());
598 lua_pushvalue (L, -3);
599 lua_rawsetp (L, LUA_REGISTRYINDEX, ClassInfo <T>::getConstKey ());
604 lua_rawgetp (L, LUA_REGISTRYINDEX, ClassInfo <T>::getStaticKey ());
605 rawgetfield (L, -1, "__class");
606 rawgetfield (L, -1, "__const");
608 // Reverse the top 3 stack elements
614 //==========================================================================
618 Class (char const* name, Namespace const* parent, void const* const staticKey)
619 : ClassBase (parent->L)
623 _name = parent->_name + name + ":";
625 m_stackSize = parent->m_stackSize + 3;
626 parent->m_stackSize = 0;
628 assert (lua_istable (L, -1));
630 createConstTable (name);
631 lua_pushcfunction (L, &CFunc::gcMetaMethod <T>);
632 rawsetfield (L, -2, "__gc");
633 lua_pushcclosure (L, &CFunc::ClassEqualCheck <T>::f, 0);
634 rawsetfield (L, -2, "__eq");
636 createClassTable (name);
637 lua_pushcfunction (L, &CFunc::gcMetaMethod <T>);
638 rawsetfield (L, -2, "__gc");
639 lua_pushcclosure (L, &CFunc::ClassEqualCheck <T>::f, 0);
640 rawsetfield (L, -2, "__eq");
642 createStaticTable (name);
644 lua_rawgetp (L, LUA_REGISTRYINDEX, staticKey);
645 assert (lua_istable (L, -1));
646 rawgetfield (L, -1, "__class");
647 assert (lua_istable (L, -1));
648 rawgetfield (L, -1, "__const");
649 assert (lua_istable (L, -1));
651 rawsetfield (L, -6, "__parent");
652 rawsetfield (L, -4, "__parent");
653 rawsetfield (L, -2, "__parent");
655 lua_pushvalue (L, -1);
656 lua_rawsetp (L, LUA_REGISTRYINDEX, ClassInfo <T>::getStaticKey ());
657 lua_pushvalue (L, -2);
658 lua_rawsetp (L, LUA_REGISTRYINDEX, ClassInfo <T>::getClassKey ());
659 lua_pushvalue (L, -3);
660 lua_rawsetp (L, LUA_REGISTRYINDEX, ClassInfo <T>::getConstKey ());
663 //--------------------------------------------------------------------------
665 Continue registration in the enclosing namespace.
667 Namespace endClass ()
669 return Namespace (this);
672 //--------------------------------------------------------------------------
674 Add or replace a static data member.
677 Class <T>& addStaticData (char const* name, U* pu, bool isWritable = true)
679 DATADOC ("Static Data Member", name, pu)
680 assert (lua_istable (L, -1));
682 rawgetfield (L, -1, "__propget");
683 assert (lua_istable (L, -1));
684 lua_pushlightuserdata (L, pu);
685 lua_pushcclosure (L, &CFunc::getVariable <U>, 1);
686 rawsetfield (L, -2, name);
689 rawgetfield (L, -1, "__propset");
690 assert (lua_istable (L, -1));
693 lua_pushlightuserdata (L, pu);
694 lua_pushcclosure (L, &CFunc::setVariable <U>, 1);
698 lua_pushstring (L, name);
699 lua_pushcclosure (L, &CFunc::readOnlyError, 1);
701 rawsetfield (L, -2, name);
707 //--------------------------------------------------------------------------
710 Add or replace a static property member.
712 If the set function is null, the property is read-only.
715 Class <T>& addStaticProperty (char const* name, U (*get)(), void (*set)(U) = 0)
717 typedef U (*get_t)();
718 typedef void (*set_t)(U);
720 assert (lua_istable (L, -1));
722 rawgetfield (L, -1, "__propget");
723 assert (lua_istable (L, -1));
724 new (lua_newuserdata (L, sizeof (get))) get_t (get);
725 lua_pushcclosure (L, &CFunc::Call <U (*) (void)>::f, 1);
726 rawsetfield (L, -2, name);
729 rawgetfield (L, -1, "__propset");
730 assert (lua_istable (L, -1));
733 new (lua_newuserdata (L, sizeof (set))) set_t (set);
734 lua_pushcclosure (L, &CFunc::Call <void (*) (U)>::f, 1);
738 lua_pushstring (L, name);
739 lua_pushcclosure (L, &CFunc::readOnlyError, 1);
741 rawsetfield (L, -2, name);
748 //--------------------------------------------------------------------------
750 Add or replace a static member function.
753 Class <T>& addStaticFunction (char const* name, FP const fp)
755 FUNDOC ("Static Member Function", name, FP)
756 new (lua_newuserdata (L, sizeof (fp))) FP (fp);
757 lua_pushcclosure (L, &CFunc::Call <FP>::f, 1);
758 rawsetfield (L, -2, name);
763 //--------------------------------------------------------------------------
765 Add or replace a lua_CFunction.
767 Class <T>& addStaticCFunction (char const* name, int (*const fp)(lua_State*))
769 DATADOC ("Static C Function", name, fp)
770 lua_pushcfunction (L, fp);
771 rawsetfield (L, -2, name);
775 //--------------------------------------------------------------------------
777 Add or replace a data member.
780 Class <T>& addData (char const* name, const U T::* mp, bool isWritable = true)
782 DATADOC ("Data Member", name, mp)
783 typedef const U T::*mp_t;
785 // Add to __propget in class and const tables.
787 rawgetfield (L, -2, "__propget");
788 rawgetfield (L, -4, "__propget");
789 new (lua_newuserdata (L, sizeof (mp_t))) mp_t (mp);
790 lua_pushcclosure (L, &CFunc::getProperty <T,U>, 1);
791 lua_pushvalue (L, -1);
792 rawsetfield (L, -4, name);
793 rawsetfield (L, -2, name);
799 // Add to __propset in class table.
800 rawgetfield (L, -2, "__propset");
801 assert (lua_istable (L, -1));
802 new (lua_newuserdata (L, sizeof (mp_t))) mp_t (mp);
803 lua_pushcclosure (L, &CFunc::setProperty <T,U>, 1);
804 rawsetfield (L, -2, name);
811 //--------------------------------------------------------------------------
813 Add or replace a property member.
815 template <class TG, class TS>
816 Class <T>& addProperty (char const* name, TG (T::* get) () const, bool (T::* set) (TS))
818 DATADOC ("Property", name, get)
819 // Add to __propget in class and const tables.
821 rawgetfield (L, -2, "__propget");
822 rawgetfield (L, -4, "__propget");
823 typedef TG (T::*get_t) () const;
824 new (lua_newuserdata (L, sizeof (get_t))) get_t (get);
825 lua_pushcclosure (L, &CFunc::CallConstMember <get_t>::f, 1);
826 lua_pushvalue (L, -1);
827 rawsetfield (L, -4, name);
828 rawsetfield (L, -2, name);
833 // Add to __propset in class table.
834 rawgetfield (L, -2, "__propset");
835 assert (lua_istable (L, -1));
836 typedef bool (T::* set_t) (TS);
837 new (lua_newuserdata (L, sizeof (set_t))) set_t (set);
838 lua_pushcclosure (L, &CFunc::CallMember <set_t>::f, 1);
839 rawsetfield (L, -2, name);
849 Class <T>& addProperty (char const* name, TG (T::* get) () const)
851 // Add to __propget in class and const tables.
852 rawgetfield (L, -2, "__propget");
853 rawgetfield (L, -4, "__propget");
854 typedef TG (T::*get_t) () const;
855 new (lua_newuserdata (L, sizeof (get_t))) get_t (get);
856 lua_pushcclosure (L, &CFunc::CallConstMember <get_t>::f, 1);
857 lua_pushvalue (L, -1);
858 rawsetfield (L, -4, name);
859 rawsetfield (L, -2, name);
866 //--------------------------------------------------------------------------
868 Add or replace a property member, by proxy.
870 When a class is closed for modification and does not provide (or cannot
871 provide) the function signatures necessary to implement get or set for
872 a property, this will allow non-member functions act as proxies.
874 Both the get and the set functions require a T const* and T* in the first
875 argument respectively.
877 template <class TG, class TS>
878 Class <T>& addProperty (char const* name, TG (*get) (T const*), bool (*set) (T*, TS))
880 // Add to __propget in class and const tables.
882 rawgetfield (L, -2, "__propget");
883 rawgetfield (L, -4, "__propget");
884 typedef TG (*get_t) (T const*);
885 new (lua_newuserdata (L, sizeof (get_t))) get_t (get);
886 lua_pushcclosure (L, &CFunc::Call <get_t>::f, 1);
887 lua_pushvalue (L, -1);
888 rawsetfield (L, -4, name);
889 rawsetfield (L, -2, name);
895 // Add to __propset in class table.
896 rawgetfield (L, -2, "__propset");
897 assert (lua_istable (L, -1));
898 typedef void (*set_t) (T*, TS);
899 new (lua_newuserdata (L, sizeof (set_t))) set_t (set);
900 lua_pushcclosure (L, &CFunc::Call <set_t>::f, 1);
901 rawsetfield (L, -2, name);
910 template <class TG, class TS>
911 Class <T>& addProperty (char const* name, TG (*get) (T const*))
913 // Add to __propget in class and const tables.
914 rawgetfield (L, -2, "__propget");
915 rawgetfield (L, -4, "__propget");
916 typedef TG (*get_t) (T const*);
917 new (lua_newuserdata (L, sizeof (get_t))) get_t (get);
918 lua_pushcclosure (L, &CFunc::Call <get_t>::f, 1);
919 lua_pushvalue (L, -1);
920 rawsetfield (L, -4, name);
921 rawsetfield (L, -2, name);
927 //--------------------------------------------------------------------------
929 Add or replace a member function.
931 template <class MemFn>
932 Class <T>& addFunction (char const* name, MemFn mf)
934 FUNDOC("Member Function", name, MemFn)
935 CFunc::CallMemberFunctionHelper <MemFn, FuncTraits <MemFn>::isConstMemberFunction>::add (L, name, mf);
939 template <class MemFn>
940 Class <T>& addPtrFunction (char const* name, MemFn mf)
942 FUNDOC("Member Pointer Function", name, MemFn)
943 CFunc::CallMemberPtrFunctionHelper <MemFn>::add (L, name, mf);
947 template <class MemFn>
948 Class <T>& addWPtrFunction (char const* name, MemFn mf)
950 FUNDOC("Member Weak Pointer Function", name, MemFn)
951 CFunc::CallMemberWPtrFunctionHelper <MemFn>::add (L, name, mf);
955 template <class MemFn>
956 Class <T>& addRefFunction (char const* name, MemFn mf)
958 FUNDOC("Member Function RefReturn", name, MemFn)
959 CFunc::CallMemberRefFunctionHelper <MemFn, FuncTraits <MemFn>::isConstMemberFunction>::add (L, name, mf);
964 //--------------------------------------------------------------------------
966 Add or replace a member lua_CFunction.
968 Class <T>& addCFunction (char const* name, int (T::*mfp)(lua_State*))
970 DATADOC ("C Function", name, mfp)
971 typedef int (T::*MFP)(lua_State*);
972 assert (lua_istable (L, -1));
973 new (lua_newuserdata (L, sizeof (mfp))) MFP (mfp);
974 lua_pushcclosure (L, &CFunc::CallMemberCFunction <T>::f, 1);
975 rawsetfield (L, -3, name); // class table
980 // custom callback - extend existing classes
981 // with non-class member functions (e.g STL iterator)
982 Class <T>& addExtCFunction (char const* name, int (*const fp)(lua_State*))
984 DATADOC ("Ext C Function", name, fp)
985 assert (lua_istable (L, -1));
986 lua_pushcclosure (L, fp, 0);
987 lua_pushvalue (L, -1);
988 rawsetfield (L, -5, name); // const table
989 rawsetfield (L, -3, name); // class table
993 //--------------------------------------------------------------------------
995 Add or replace a const member lua_CFunction.
997 Class <T>& addCFunction (char const* name, int (T::*mfp)(lua_State*) const)
999 DATADOC ("Const C Member Function", name, mfp)
1000 typedef int (T::*MFP)(lua_State*) const;
1001 assert (lua_istable (L, -1));
1002 new (lua_newuserdata (L, sizeof (mfp))) MFP (mfp);
1003 lua_pushcclosure (L, &CFunc::CallConstMemberCFunction <T>::f, 1);
1004 lua_pushvalue (L, -1);
1005 rawsetfield (L, -5, name); // const table
1006 rawsetfield (L, -3, name); // class table
1012 Add or replace a static const data
1014 template <typename U>
1015 Class <T>& addConst (char const* name, const U val)
1017 DATADOC ("Constant/Enum Member", name, val)
1018 assert (lua_istable (L, -1));
1020 rawgetfield (L, -1, "__propget"); // static
1021 new (lua_newuserdata (L, sizeof (val))) U (val);
1022 lua_pushcclosure (L, &CFunc::getConst <U>, 1);
1023 rawsetfield (L, -2, name);
1026 rawgetfield (L, -1, "__propset"); // static
1027 lua_pushstring (L, name);
1028 lua_pushcclosure (L, &CFunc::readOnlyError, 1);
1029 rawsetfield (L, -2, name);
1034 //--------------------------------------------------------------------------
1036 Add or replace a primary Constructor.
1038 The primary Constructor is invoked when calling the class type table
1041 The template parameter should be a function pointer type that matches
1042 the desired Constructor (since you can't take the address of a Constructor
1043 and pass it as an argument).
1045 template <class MemFn, class C>
1046 Class <T>& addConstructor ()
1048 FUNDOC("Constructor", "", MemFn)
1049 lua_pushcclosure (L,
1050 &ctorContainerProxy <typename FuncTraits <MemFn>::Params, C>, 0);
1051 rawsetfield(L, -2, "__call");
1056 template <class MemFn>
1057 Class <T>& addConstructor ()
1059 FUNDOC("Constructor", "", MemFn)
1060 lua_pushcclosure (L,
1061 &ctorPlacementProxy <typename FuncTraits <MemFn>::Params, T>, 0);
1062 rawsetfield(L, -2, "__call");
1067 template <class MemFn, class PT>
1068 Class <T>& addPtrConstructor ()
1070 FUNDOC("Constructor", "", MemFn)
1071 lua_pushcclosure (L,
1072 &ctorPtrPlacementProxy <typename FuncTraits <MemFn>::Params, T, PT>, 0);
1073 rawsetfield(L, -2, "__call");
1078 Class <T>& addVoidConstructor ()
1080 return addConstructor <void (*) ()> ();
1084 Class <T>& addVoidPtrConstructor ()
1086 return addPtrConstructor <void (*) (), PT> ();
1089 Class <T>& addEqualCheck ()
1091 assert (lua_istable (L, -1));
1092 lua_pushcclosure (L, &CFunc::ClassEqualCheck <T>::f, 0);
1093 rawsetfield (L, -3, "sameinstance");
1098 Class <T>& addCast (char const* name)
1100 PRINTDOC("Cast", _name << name,
1102 type_name< U >() << " (" << type_name< T >() << "::*)()")
1104 assert (lua_istable (L, -1));
1105 lua_pushcclosure (L, &CFunc::CastClass <T, U>::f, 0);
1106 rawsetfield (L, -3, name); // class table
1108 lua_pushcclosure (L, &CFunc::CastConstClass <T, U>::f, 0);
1109 rawsetfield (L, -4, name); // const table
1115 /** C Array to/from table */
1116 template <typename T>
1117 class Array : virtual public ClassBase
1120 Array (char const* name, Namespace const* parent) : ClassBase (parent->L)
1122 #ifdef LUABINDINGDOC
1124 _name = parent->_name + name + ":";
1126 PRINTDOC ("[C] Array", parent->_name << name,
1127 std::string(), type_name <T>() + "*")
1128 PRINTDOC ("Ext C Function", _name << "array",
1129 std::string(""), "int (*)(lua_State*)")
1130 PRINTDOC ("Ext C Function", _name << "get_table",
1131 std::string(""), "int (*)(lua_State*)")
1132 PRINTDOC ("Ext C Function", _name << "set_table",
1133 std::string(""), "int (*)(lua_State*)")
1134 PRINTDOC("Member Function", _name << "offset",
1135 std::string(type_name <T>() + "*"), std::string(type_name <T>() + "* (*)(unsigned int)"))
1137 m_stackSize = parent->m_stackSize + 3;
1138 parent->m_stackSize = 0;
1140 #if 0 // don't allow to duplicates handlers for same array-type
1141 assert (lua_istable (L, -1));
1142 lua_rawgetp (L, LUA_REGISTRYINDEX, ClassInfo <T>::getClassKey ());
1143 if (lua_istable (L, -1)) {
1151 assert (lua_istable (L, -1));
1152 rawgetfield (L, -1, name);
1154 if (lua_isnil (L, -1))
1158 // register array access in global namespace
1159 luaL_newmetatable (L, typeid(T).name());
1160 lua_pushcclosure (L, CFunc::array_index<T>, 0);
1161 lua_setfield(L, -2, "__index");
1162 lua_pushcclosure (L, CFunc::array_newindex<T>, 0);
1163 lua_setfield(L, -2, "__newindex");
1164 if (Security::hideMetatables ())
1166 lua_pushboolean (L, false);
1167 rawsetfield (L, -2, "__metatable");
1172 createConstTable (name);
1173 lua_pushcfunction (L, &CFunc::gcMetaMethod <T>);
1174 rawsetfield (L, -2, "__gc");
1175 lua_pushcclosure (L, &CFunc::ClassEqualCheck <T>::f, 0);
1176 rawsetfield (L, -2, "__eq");
1178 createClassTable (name);
1179 lua_pushcfunction (L, &CFunc::gcMetaMethod <T>);
1180 rawsetfield (L, -2, "__gc");
1181 lua_pushcclosure (L, &CFunc::ClassEqualCheck <T>::f, 0);
1182 rawsetfield (L, -2, "__eq");
1184 createStaticTable (name);
1186 // Map T back to its tables.
1187 lua_pushvalue (L, -1);
1188 lua_rawsetp (L, LUA_REGISTRYINDEX, ClassInfo <T>::getStaticKey ());
1189 lua_pushvalue (L, -2);
1190 lua_rawsetp (L, LUA_REGISTRYINDEX, ClassInfo <T>::getClassKey ());
1191 lua_pushvalue (L, -3);
1192 lua_rawsetp (L, LUA_REGISTRYINDEX, ClassInfo <T>::getConstKey ());
1194 assert (lua_istable (L, -1));
1195 lua_pushcclosure (L, &CFunc::getArray <T>, 0);
1196 rawsetfield (L, -3, "array"); // class table
1198 lua_pushcclosure (L, &CFunc::getTable <T>, 0);
1199 rawsetfield (L, -3, "get_table"); // class table
1201 lua_pushcclosure (L, &CFunc::setTable <T>, 0);
1202 rawsetfield (L, -3, "set_table"); // class table
1204 lua_pushcclosure (L, &CFunc::ClassEqualCheck <T>::f, 0);
1205 rawsetfield (L, -3, "sameinstance");
1207 lua_pushcclosure (L, &CFunc::offsetArray <T>, 0);
1208 rawsetfield (L, -3, "offset"); // class table
1218 Namespace endArray ()
1220 return Namespace (this);
1224 /** Boost Weak & Shared Pointer Class Wrapper */
1226 class WSPtrClass : virtual public ClassBase
1229 WSPtrClass (char const* name, Namespace const* parent)
1230 : ClassBase (parent->L)
1231 , shared (name, parent)
1232 , weak (name, parent)
1234 #ifdef LUABINDINGDOC
1236 _name = parent->_name + name + ":";
1238 PRINTDOC ("[C] Weak/Shared Pointer Class",
1239 parent->_name + name,
1240 std::string(), type_name <T>())
1241 m_stackSize = shared.m_stackSize;
1242 parent->m_stackSize = weak.m_stackSize = shared.m_stackSize = 0;
1246 WSPtrClass (char const* name, Namespace const* parent, void const* const sharedkey, void const* const weakkey)
1247 : ClassBase (parent->L)
1248 , shared (name, parent, sharedkey)
1249 , weak (name, parent, weakkey)
1251 #ifdef LUABINDINGDOC
1253 _name = parent->_name + name + ":";
1255 m_stackSize = shared.m_stackSize;
1256 parent->m_stackSize = weak.m_stackSize = shared.m_stackSize = 0;
1260 template <class MemFn>
1261 WSPtrClass <T>& addFunction (char const* name, MemFn mf)
1263 FUNDOC ("Weak/Shared Pointer Function", name, MemFn)
1264 set_shared_class ();
1265 CFunc::CallMemberPtrFunctionHelper <MemFn>::add (L, name, mf);
1268 CFunc::CallMemberWPtrFunctionHelper <MemFn>::add (L, name, mf);
1272 template <class MemFn>
1273 WSPtrClass <T>& addRefFunction (char const* name, MemFn mf)
1275 FUNDOC ("Weak/Shared Pointer Function RefReturn", name, MemFn)
1276 set_shared_class ();
1277 CFunc::CallMemberRefPtrFunctionHelper <MemFn>::add (L, name, mf);
1280 CFunc::CallMemberRefWPtrFunctionHelper <MemFn>::add (L, name, mf);
1284 template <class MemFn>
1285 WSPtrClass <T>& addConstructor ()
1287 FUNDOC ("Weak/Shared Pointer Constructor", "", MemFn)
1288 set_shared_class ();
1289 lua_pushcclosure (L,
1290 &shared. template ctorPtrPlacementProxy <typename FuncTraits <MemFn>::Params, boost::shared_ptr<T>, T >, 0);
1291 rawsetfield(L, -2, "__call");
1294 // NOTE: this constructs an empty weak-ptr,
1295 // ideally we'd construct a weak-ptr from a referenced shared-ptr
1296 lua_pushcclosure (L,
1297 &weak. template ctorPlacementProxy <typename FuncTraits <MemFn>::Params, boost::weak_ptr<T> >, 0);
1298 rawsetfield(L, -2, "__call");
1302 WSPtrClass <T>& addVoidConstructor ()
1304 return addConstructor <void (*) ()> ();
1307 WSPtrClass <T>& addNilPtrConstructor ()
1309 FUNDOC ("Weak/Shared Pointer NIL Constructor", "", void (*) ())
1310 set_shared_class ();
1311 lua_pushcclosure (L,
1312 &shared. template ctorNilPtrPlacementProxy <boost::shared_ptr<T> >, 0);
1313 rawsetfield(L, -2, "__call");
1316 // NOTE: this constructs an empty weak-ptr,
1317 // ideally we'd construct a weak-ptr from a referenced shared-ptr
1318 lua_pushcclosure (L,
1319 &weak. template ctorNilPtrPlacementProxy <boost::weak_ptr<T> >, 0);
1320 rawsetfield(L, -2, "__call");
1325 WSPtrClass <T>& addExtCFunction (char const* name, int (*const fp)(lua_State*))
1327 DATADOC ("Weak/Shared Ext C Function", name, fp)
1328 set_shared_class ();
1329 assert (lua_istable (L, -1));
1330 lua_pushcclosure (L, fp, 0);
1331 lua_pushvalue (L, -1);
1332 rawsetfield (L, -5, name); // const table
1333 rawsetfield (L, -3, name); // class table
1336 assert (lua_istable (L, -1));
1337 lua_pushcclosure (L, fp, 0);
1338 lua_pushvalue (L, -1);
1339 rawsetfield (L, -5, name); // const table
1340 rawsetfield (L, -3, name); // class table
1346 WSPtrClass <T>& addCast (char const* name)
1348 PRINTDOC("Weak/Shared Pointer Cast", _name << name,
1350 type_name< U >() << " (" << type_name< T >() << "::*)()")
1353 set_shared_class ();
1354 assert (lua_istable (L, -1));
1355 lua_pushcclosure (L, &CFunc::CastMemberPtr <T, U>::f, 0);
1356 rawsetfield (L, -3, name); // class table
1360 WSPtrClass <T>& addNullCheck ()
1362 PRINTDOC("Weak/Shared Null Check", _name << "isnil", std::string("bool"), std::string("void (*)()"))
1363 set_shared_class ();
1364 assert (lua_istable (L, -1));
1365 lua_pushcclosure (L, &CFunc::PtrNullCheck <T>::f, 0);
1366 rawsetfield (L, -3, "isnil"); // class table
1369 assert (lua_istable (L, -1));
1370 lua_pushcclosure (L, &CFunc::WPtrNullCheck <T>::f, 0);
1371 rawsetfield (L, -3, "isnil"); // class table
1376 WSPtrClass <T>& addEqualCheck ()
1378 set_shared_class ();
1379 assert (lua_istable (L, -1));
1380 lua_pushcclosure (L, &CFunc::PtrEqualCheck <T>::f, 0);
1381 rawsetfield (L, -3, "sameinstance"); // class table
1384 assert (lua_istable (L, -1));
1385 lua_pushcclosure (L, &CFunc::WPtrEqualCheck <T>::f, 0);
1386 rawsetfield (L, -3, "sameinstance"); // class table
1392 WSPtrClass <T>& addData (char const* name, const U T::* mp, bool isWritable = true)
1394 DATADOC ("Data Member", name, mp)
1395 typedef const U T::*mp_t;
1398 assert (lua_istable (L, -1));
1399 // Add to __propget in class and const tables.
1401 rawgetfield (L, -2, "__propget");
1402 rawgetfield (L, -4, "__propget");
1403 new (lua_newuserdata (L, sizeof (mp_t))) mp_t (mp);
1404 lua_pushcclosure (L, &CFunc::getWPtrProperty <T,U>, 1);
1405 lua_pushvalue (L, -1);
1406 rawsetfield (L, -4, name);
1407 rawsetfield (L, -2, name);
1413 // Add to __propset in class table.
1414 rawgetfield (L, -2, "__propset");
1415 assert (lua_istable (L, -1));
1416 new (lua_newuserdata (L, sizeof (mp_t))) mp_t (mp);
1417 lua_pushcclosure (L, &CFunc::setWPtrProperty <T,U>, 1);
1418 rawsetfield (L, -2, name);
1422 set_shared_class ();
1423 assert (lua_istable (L, -1));
1424 // Add to __propget in class and const tables.
1426 rawgetfield (L, -2, "__propget");
1427 rawgetfield (L, -4, "__propget");
1428 new (lua_newuserdata (L, sizeof (mp_t))) mp_t (mp);
1429 lua_pushcclosure (L, &CFunc::getPtrProperty <T,U>, 1);
1430 lua_pushvalue (L, -1);
1431 rawsetfield (L, -4, name);
1432 rawsetfield (L, -2, name);
1438 // Add to __propset in class table.
1439 rawgetfield (L, -2, "__propset");
1440 assert (lua_istable (L, -1));
1441 new (lua_newuserdata (L, sizeof (mp_t))) mp_t (mp);
1442 lua_pushcclosure (L, &CFunc::setPtrProperty <T,U>, 1);
1443 rawsetfield (L, -2, name);
1451 Namespace endClass ()
1453 return Namespace (this);
1457 void set_weak_class () {
1459 lua_rawgetp (L, LUA_REGISTRYINDEX, ClassInfo <boost::weak_ptr<T> >::getStaticKey ());
1460 rawgetfield (L, -1, "__class");
1461 rawgetfield (L, -1, "__const");
1465 void set_shared_class () {
1467 lua_rawgetp (L, LUA_REGISTRYINDEX, ClassInfo <boost::shared_ptr<T> >::getStaticKey ());
1468 rawgetfield (L, -1, "__class");
1469 rawgetfield (L, -1, "__const");
1473 Class<boost::shared_ptr<T> > shared;
1474 Class<boost::weak_ptr<T> > weak;
1479 //----------------------------------------------------------------------------
1481 Open the global namespace for registrations.
1483 explicit Namespace (lua_State* L_)
1486 #ifdef LUABINDINGDOC
1491 lua_getglobal (L, "_G");
1495 #ifdef LUABINDINGDOC
1497 Namespace const * _parent;
1500 //----------------------------------------------------------------------------
1502 Open a namespace for registrations.
1504 The namespace is created if it doesn't already exist.
1505 The parent namespace is at the top of the Lua stack.
1507 Namespace (char const* name, Namespace const* parent)
1510 #ifdef LUABINDINGDOC
1511 , _name (parent->_name + name + ":")
1515 m_stackSize = parent->m_stackSize + 1;
1516 parent->m_stackSize = 0;
1518 assert (lua_istable (L, -1));
1519 rawgetfield (L, -1, name);
1520 if (lua_isnil (L, -1))
1525 lua_pushvalue (L, -1);
1526 lua_setmetatable (L, -2);
1527 lua_pushcfunction (L, &CFunc::indexMetaMethod);
1528 rawsetfield (L, -2, "__index");
1529 lua_pushcfunction (L, &CFunc::newindexMetaMethod);
1530 rawsetfield (L, -2, "__newindex");
1532 rawsetfield (L, -2, "__propget");
1534 rawsetfield (L, -2, "__propset");
1535 lua_pushvalue (L, -1);
1536 rawsetfield (L, -3, name);
1538 lua_pushcfunction (L, &tostringMetaMethod);
1539 rawsetfield (L, -2, "__tostring");
1541 if (Security::hideMetatables ())
1543 lua_pushboolean (L, false);
1544 rawsetfield (L, -2, "__metatable");
1550 //----------------------------------------------------------------------------
1552 Creates a continued registration from a child namespace.
1554 explicit Namespace (Namespace const* child)
1557 #ifdef LUABINDINGDOC
1558 , _name (child->_parent ? child->_parent->_name : "")
1559 , _parent (child->_parent ? child->_parent->_parent : NULL)
1562 m_stackSize = child->m_stackSize - 1;
1563 child->m_stackSize = 1;
1566 // It is not necessary or valid to call
1567 // endNamespace() for the global namespace!
1569 assert (m_stackSize != 0);
1572 //----------------------------------------------------------------------------
1574 Creates a continued registration from a child class.
1576 explicit Namespace (ClassBase const* child)
1579 #ifdef LUABINDINGDOC
1580 , _name (child->_parent ? child->_parent->_name : "")
1581 , _parent (child->_parent ? child->_parent->_parent : NULL)
1584 m_stackSize = child->m_stackSize - 3;
1585 child->m_stackSize = 3;
1590 //----------------------------------------------------------------------------
1594 Ownership of the stack is transferred to the new object. This happens
1595 when the compiler emits temporaries to hold these objects while chaining
1596 registrations across namespaces.
1598 Namespace (Namespace const& other) : L (other.L)
1600 m_stackSize = other.m_stackSize;
1601 other.m_stackSize = 0;
1602 #ifdef LUABINDINGDOC
1603 _name = other._name;
1604 _parent = other._parent;
1608 //----------------------------------------------------------------------------
1610 Closes this namespace registration.
1617 //----------------------------------------------------------------------------
1619 Open the global namespace.
1621 static Namespace getGlobalNamespace (lua_State* L)
1623 return Namespace (L);
1626 //----------------------------------------------------------------------------
1628 Open a new or existing namespace for registrations.
1630 Namespace beginNamespace (char const* name)
1632 return Namespace (name, this);
1635 //----------------------------------------------------------------------------
1637 Continue namespace registration in the parent.
1639 Do not use this on the global namespace.
1641 Namespace endNamespace ()
1643 return Namespace (this);
1646 //----------------------------------------------------------------------------
1648 Add or replace a variable.
1651 Namespace& addVariable (char const* name, T* pt, bool isWritable = true)
1653 assert (lua_istable (L, -1));
1655 rawgetfield (L, -1, "__propget");
1656 assert (lua_istable (L, -1));
1657 lua_pushlightuserdata (L, pt);
1658 lua_pushcclosure (L, &CFunc::getVariable <T>, 1);
1659 rawsetfield (L, -2, name);
1662 rawgetfield (L, -1, "__propset");
1663 assert (lua_istable (L, -1));
1666 lua_pushlightuserdata (L, pt);
1667 lua_pushcclosure (L, &CFunc::setVariable <T>, 1);
1671 lua_pushstring (L, name);
1672 lua_pushcclosure (L, &CFunc::readOnlyError, 1);
1674 rawsetfield (L, -2, name);
1680 template <typename U>
1681 Namespace& addConst (char const* name, const U val)
1683 DATADOC ("Constant/Enum", name, val)
1684 assert (lua_istable (L, -1));
1685 rawgetfield (L, -1, "__propget");
1686 new (lua_newuserdata (L, sizeof (val))) U (val);
1687 lua_pushcclosure (L, &CFunc::getConst <U>, 1);
1688 rawsetfield (L, -2, name);
1691 rawgetfield (L, -1, "__propset");
1692 assert (lua_istable (L, -1));
1693 lua_pushstring (L, name);
1694 lua_pushcclosure (L, &CFunc::readOnlyError, 1);
1695 rawsetfield (L, -2, name);
1700 //----------------------------------------------------------------------------
1702 Add or replace a property.
1704 If the set function is omitted or null, the property is read-only.
1707 template <class TG, class TS>
1708 Namespace& addProperty (char const* name, TG (*get) (), void (*set)(TS) = 0)
1710 assert (lua_istable (L, -1));
1712 rawgetfield (L, -1, "__propget");
1713 assert (lua_istable (L, -1));
1714 typedef TG (*get_t) ();
1715 new (lua_newuserdata (L, sizeof (get_t))) get_t (get);
1716 lua_pushcclosure (L, &CFunc::Call <TG (*) (void)>::f, 1);
1717 rawsetfield (L, -2, name);
1720 rawgetfield (L, -1, "__propset");
1721 assert (lua_istable (L, -1));
1724 typedef void (*set_t) (TS);
1725 new (lua_newuserdata (L, sizeof (set_t))) set_t (set);
1726 lua_pushcclosure (L, &CFunc::Call <void (*) (TS)>::f, 1);
1730 lua_pushstring (L, name);
1731 lua_pushcclosure (L, &CFunc::readOnlyError, 1);
1733 rawsetfield (L, -2, name);
1740 //----------------------------------------------------------------------------
1742 Add or replace a free function.
1745 Namespace& addFunction (char const* name, FP const fp)
1747 FUNDOC ("Free Function", name, FP)
1748 assert (lua_istable (L, -1));
1750 new (lua_newuserdata (L, sizeof (fp))) FP (fp);
1751 lua_pushcclosure (L, &CFunc::Call <FP>::f, 1);
1752 rawsetfield (L, -2, name);
1758 Namespace& addRefFunction (char const* name, FP const fp)
1760 FUNDOC ("Free Function RefReturn", name, FP)
1761 assert (lua_istable (L, -1));
1763 new (lua_newuserdata (L, sizeof (fp))) FP (fp);
1764 lua_pushcclosure (L, &CFunc::CallRef <FP>::f, 1);
1765 rawsetfield (L, -2, name);
1770 //----------------------------------------------------------------------------
1772 Add or replace a array type
1775 template <typename T>
1776 Namespace registerArray (char const* name)
1778 return Array <T> (name, this).endArray();
1782 //----------------------------------------------------------------------------
1784 Add or replace a lua_CFunction.
1786 Namespace& addCFunction (char const* name, int (*const fp)(lua_State*))
1788 DATADOC ("Free C Function", name, fp)
1789 lua_pushcfunction (L, fp);
1790 rawsetfield (L, -2, name);
1795 //----------------------------------------------------------------------------
1797 Open a new or existing class for registrations.
1800 Class <T> beginClass (char const* name)
1802 return Class <T> (name, this);
1805 /** weak & shared pointer class */
1807 WSPtrClass <T> beginWSPtrClass (char const* name)
1809 return WSPtrClass <T> (name, this)
1814 //----------------------------------------------------------------------------
1816 template <class K, class V>
1817 Class<std::map<K, V> > beginStdMap (char const* name)
1819 typedef std::map<K, V> LT;
1820 typedef std::pair<const K, V> T;
1822 typedef typename std::map<K, V>::size_type T_SIZE;
1824 return beginClass<LT> (name)
1825 .addVoidConstructor ()
1826 .addFunction ("empty", <::empty)
1827 .addFunction ("size", <::size)
1828 .addFunction ("clear", (void (LT::*)())<::clear)
1829 .addFunction ("count", (T_SIZE (LT::*)(const K&) const)<::count)
1830 .addExtCFunction ("add", &CFunc::tableToMap<K, V>)
1831 .addExtCFunction ("iter", &CFunc::mapIter<K, V>)
1832 .addExtCFunction ("table", &CFunc::mapToTable<K, V>)
1833 .addExtCFunction ("at", &CFunc::mapAt<K, V>);
1837 Class<std::set<T> > beginStdSet (char const* name)
1839 typedef std::set<T> LT;
1840 return beginClass<LT> (name)
1841 .addVoidConstructor ()
1842 .addFunction ("clear", (void (LT::*)())<::clear)
1843 .addFunction ("empty", <::empty)
1844 .addFunction ("size", <::size)
1845 .addExtCFunction ("iter", &CFunc::setIter<T, LT>)
1846 .addExtCFunction ("table", &CFunc::setToTable<T, LT>);
1849 template <unsigned int T>
1850 Class<std::bitset<T> > beginStdBitSet (char const* name)
1852 typedef std::bitset<T> BS;
1853 return beginClass<BS> (name)
1854 .addVoidConstructor ()
1855 .addFunction ("reset", (BS& (BS::*)())&BS::reset)
1856 .addFunction ("set", (BS& (BS::*)(size_t, bool))&BS::set)
1857 .addFunction ("count", &BS::count)
1858 .addFunction ("any", &BS::any)
1859 .addFunction ("none", &BS::none)
1860 .addFunction ("test", &BS::test)
1861 .addFunction ("size", &BS::size)
1862 .addExtCFunction ("add", &CFunc::tableToBitSet<T>)
1863 .addExtCFunction ("table", &CFunc::bitSetToTable<T>);
1867 Class<std::list<T> > beginConstStdList (char const* name)
1869 typedef std::list<T> LT;
1870 return beginClass<LT> (name)
1871 .addVoidConstructor ()
1872 .addFunction ("empty", <::empty)
1873 .addFunction ("size", <::size)
1874 .addFunction ("reverse", <::reverse)
1875 .addFunction ("front", static_cast<const T& (LT::*)() const>(<::front))
1876 .addFunction ("back", static_cast<const T& (LT::*)() const>(<::back))
1877 .addExtCFunction ("iter", &CFunc::listIter<T, LT>)
1878 .addExtCFunction ("table", &CFunc::listToTable<T, LT>);
1882 Class<std::list<T> > beginStdList (char const* name)
1884 typedef std::list<T> LT;
1885 return beginConstStdList<T> (name)
1886 .addFunction ("unique", (void (LT::*)())<::unique)
1887 .addFunction ("push_back", (void (LT::*)(const T&))<::push_back)
1888 .addExtCFunction ("add", &CFunc::tableToList<T, LT>);
1892 Class<std::list<T*> > beginConstStdCPtrList (char const* name)
1895 typedef std::list<TP> LT;
1896 return beginClass<LT> (name)
1897 .addVoidConstructor ()
1898 .addFunction ("empty", <::empty)
1899 .addFunction ("size", <::size)
1900 .addFunction ("reverse", <::reverse)
1901 .addFunction ("front", static_cast<const TP& (LT::*)() const>(<::front))
1902 .addFunction ("back", static_cast<const TP& (LT::*)() const>(<::back))
1903 .addExtCFunction ("iter", &CFunc::listIter<T*, LT>)
1904 .addExtCFunction ("table", &CFunc::listToTable<T*, LT>);
1908 Class<std::list<T*> > beginStdCPtrList (char const* name)
1911 typedef std::list<TP> LT;
1912 return beginConstStdCPtrList<T> (name)
1913 .addFunction ("unique", (void (LT::*)())<::unique)
1914 .addFunction ("push_back", (void (LT::*)(const TP&))<::push_back);
1919 Class<std::vector<T> > beginConstStdVector (char const* name)
1921 typedef std::vector<T> LT;
1922 typedef typename std::vector<T>::reference T_REF;
1923 typedef typename std::vector<T>::size_type T_SIZE;
1925 return beginClass<LT> (name)
1926 .addVoidConstructor ()
1927 .addFunction ("empty", <::empty)
1928 .addFunction ("size", <::size)
1929 .addFunction ("at", (T_REF (LT::*)(T_SIZE))<::at)
1930 .addExtCFunction ("iter", &CFunc::listIter<T, LT>)
1931 .addExtCFunction ("table", &CFunc::listToTable<T, LT>);
1935 Class<std::vector<T> > beginStdVector (char const* name)
1937 typedef std::vector<T> LT;
1938 return beginConstStdVector<T> (name)
1939 .addVoidConstructor ()
1940 .addFunction ("push_back", (void (LT::*)(const T&))<::push_back)
1941 .addExtCFunction ("add", &CFunc::tableToList<T, LT>);
1946 //----------------------------------------------------------------------------
1949 Class<boost::shared_ptr<std::list<T> > > beginPtrStdList (char const* name)
1951 typedef std::list<T> LT;
1952 return beginClass<boost::shared_ptr<LT> > (name)
1953 //.addVoidPtrConstructor<LT> ()
1954 .addPtrFunction ("empty", <::empty)
1955 .addPtrFunction ("size", <::size)
1956 .addPtrFunction ("reverse", <::reverse)
1957 .addPtrFunction ("unique", (void (LT::*)())<::unique)
1958 .addPtrFunction ("push_back", (void (LT::*)(const T&))<::push_back)
1959 .addExtCFunction ("add", &CFunc::ptrTableToList<T, LT>)
1960 .addExtCFunction ("iter", &CFunc::ptrListIter<T, LT>)
1961 .addExtCFunction ("table", &CFunc::ptrListToTable<T, LT>);
1965 Class<boost::shared_ptr<std::vector<T> > > beginPtrStdVector (char const* name)
1967 typedef std::vector<T> LT;
1968 typedef typename std::vector<T>::reference T_REF;
1969 typedef typename std::vector<T>::size_type T_SIZE;
1971 return beginClass<boost::shared_ptr<LT> > (name)
1972 //.addVoidPtrConstructor<LT> ()
1973 .addPtrFunction ("empty", <::empty)
1974 .addPtrFunction ("empty", <::empty)
1975 .addPtrFunction ("size", <::size)
1976 .addPtrFunction ("push_back", (void (LT::*)(const T&))<::push_back)
1977 .addPtrFunction ("at", (T_REF (LT::*)(T_SIZE))<::at)
1978 .addExtCFunction ("add", &CFunc::ptrTableToList<T, LT>)
1979 .addExtCFunction ("iter", &CFunc::ptrListIter<T, LT>)
1980 .addExtCFunction ("table", &CFunc::ptrListToTable<T, LT>);
1983 //----------------------------------------------------------------------------
1985 Derive a new class for registrations.
1987 To continue registrations for the class later, use beginClass().
1988 Do not call deriveClass() again.
1990 template <class T, class U>
1991 Class <T> deriveClass (char const* name)
1993 CLASSDOC ("[C] Derived Class", _name << name, type_name <T>(), type_name <U>())
1994 return Class <T> (name, this, ClassInfo <U>::getStaticKey ());
1997 template <class T, class U>
1998 WSPtrClass <T> deriveWSPtrClass (char const* name)
2001 CLASSDOC ("[C] Derived Class", _name << name, type_name <boost::shared_ptr<T> >(), type_name <boost::shared_ptr<U> >())
2002 CLASSDOC ("[C] Derived Class", _name << name, type_name <boost::weak_ptr<T> >(), type_name <boost::weak_ptr<U> >())
2003 CLASSDOC ("[C] Derived Pointer Class", _name << name, type_name <T>(), type_name <U>())
2004 return WSPtrClass <T> (name, this,
2005 ClassInfo <boost::shared_ptr<U> >::getStaticKey (),
2006 ClassInfo <boost::weak_ptr<U> >::getStaticKey ())
2013 //------------------------------------------------------------------------------
2015 Retrieve the global namespace.
2017 It is recommended to put your namespace inside the global namespace, and
2018 then add your classes and functions to it, rather than adding many classes
2019 and functions directly to the global namespace.
2021 inline Namespace getGlobalNamespace (lua_State* L)
2023 return Namespace::getGlobalNamespace (L);
2034 /* vim: set et sw=2: */