??LuaBridge?Lua??C/C++????
???????????? ???????[ 2014/4/2 9:37:22 ] ????????C++ Lua ???
?????????淽????
<span style="font-size:14px;"> getGlobalNamespace (L)
.beginNamespace ("test")
.beginClass<A>("A")
.addConstructor <void (*) (void)> ()
.addStaticData ("staticData"?? &A::staticData)
.addStaticProperty ("staticProperty"?? &A::getStaticData)
.addStaticFunction ("getStaticProperty"?? &A::getStaticProperty) //read-only
.addStaticCFunction ("staticCFunc"?? &A::staticCFunc)
.addData ("data"?? &A::dataMember)
.addProperty ("prop"?? &A::getProperty?? &A::setProperty)
.addFunction ("func1"?? &A::func1)
.addFunction ("virtualFunc"?? &A::virtualFunc)
.addCFunction ("cfunc"?? &A::cfunc)
.endClass ()
.deriveClass<B?? A>("B")
.addConstructor <void (*) (void)> ()
.addData ("data"?? &B::dataMember2)
.addFunction ("func1"?? &B::func1)
.addFunction ("func2"?? &B::func2)
.endClass ()
.endNamespace ();</span>
??????????????Lua????а???·??????
<span style="font-size:14px;"> local AClassObj = test.A () --create class A instance
print("before:"??test.A.staticData) -- access class A static member
test.A.staticData = 8 -- modify class A static member
print("after:"??test.A.staticData)
print("before:"?? test.A.getStaticProperty())
--test.A.staticProperty = 1.2 --error:can not modify
print("staticCFunc")
test.A.staticCFunc()
AClassObj.data = "sting"
print("dataMember:"??AClassObj.data)
AClassObj.prop = 'a'
print("property:"??AClassObj.prop)
AClassObj:func1()
AClassObj:virtualFunc()
AClassObj:cfunc()
BClassObj = test.B()
BClassObj:func1()
BClassObj:func2()
BClassObj:virtualFunc() </span>
???????????????
<span style="font-size:14px;"> A constructor
before: 3
after: 8
before: 0.5
staticCFunc
dataMember: sting
property: a
func1 In Class A
virtualFunc In Class A
cfunc In Class A
A constructor
B constructor
func1 In Class B
func2 In Class B
virtualFunc In Class B</span>
???????????????????????????????麯?????????????????????????LuaBridge?У??????const???????????????м????????????????????????const object????????const object???????Lua?????????Lua??????????????????????const???????????const???????????????????????????????????????????????????????????????????????If a class has a base class that is **not** registeredwith Lua?? there is no need to declare it as a subclass.
????Constructors
?????????Lua?У?????????????????addConstructor??????????????????LuaBridge??????????????????????????????????????????????????????????????????????addConstructor????????LuaBridge??Lua???????????????????????磺
<span style="font-size:14px;"> struct A {
A ();
};
struct B {
explicit B (char const* s?? int nChars);
};
getGlobalNamespace (L)
.beginNamespace ("test")
.beginClass <A> ("A")
.addConstructor <void (*) (void)> ()
.endClass ()
.beginClass <B> ("B")
.addConstructor <void (*) (char const*?? int)> ()
.endClass ();
.endNamespace ()</span>
??????Lua?У??????Щ?????????A??B?????:
????<span style="font-size:14px;"> a = test.A () -- Create a new A.
????b = test.B ("hello"?? 5) -- Create a new B.
????b = test.B () -- Error: expected string in argument 1</span>
????lua_State*
????????????????????????????lua_State*???????????????????LuaBridge??????????????????????lua_State*???????????ɡ????磺
????<span style="font-size:14px;"> void useStateAndArgs (int i?? std::string s?? lua_State* L);
????getGlobalNamespace (L).addFunction ("useStateAndArgs"?? &useStateAndArgs); </span>
??????Lua?У???????·??????
????<span style="font-size:14px;"> useStateAndArgs(42??"hello")</span>
?????????У???貧????????????????ɡ???? lua_State*?????????????????????????????δ??????
????Class Object Types
???????????????T?????????·???????Lua?????
????<span style="font-size:14px;"> `T*` or `T&`: Passed by reference?? with _C++ lifetime_.
????`T const*` or `T const&`: Passed by const reference?? with _C++ lifetime_.
????`T` or `T const`: Passed by value (a copy)?? with _Lua lifetime_.</span>
????C++ Lifetime
????????C++ lifetime??????????????????C++????????Lua GC?????????Щ??????Lua???lua_State*??????????????????????????????????????δ?????????????磬??????·?????Lua????
????C++ lifetime?????
<span style="font-size:14px;"> A a;
push (L?? &a); // pointer to 'a'?? C++ lifetime
lua_setglobal (L?? "a");
push (L?? (A const*)&a); // pointer to 'a const'?? C++ lifetime
lua_setglobal (L?? "ac");
push <A const*> (L?? &a); // equivalent to push (L?? (A const*)&a)
lua_setglobal (L?? "ac2");
push (L?? new A); // compiles?? but will leak memory
lua_setglobal (L?? "ap");
</span>
????Lua Lifetime
??????C++?????????Lua??????????????????Lua lifetime???????????????????Lua????userdata??????棬?????Lua????????????????????????GC???????userdata???????????????????
??????????????????á???C++?????lua lifetime???????????????????????GC????????????????δ?????????磬??????·?????Lua???????Lua lifetime????£?
????<span style="font-size:14px;"> B b;
????push (L?? b); // Copy of b passed?? Lua lifetime.
????lua_setglobal (L?? "b");</span>
????????Lua?е????????????????????????????????????Lua lifetime????????????????????GC?????????????????????????????????????????????C++??????????C++???????????????????
??????????б?GC?????
????Pointers?? References?? and Pass by Value
??????C++?????????????Lua?д????C++?????????LuaBridge???????????????????磬??Lua???????????C++??????
????<span style="font-size:14px;"> void func0 (A a);
????void func1 (A* a);
????void func2 (A const* a);
????void func3 (A& a);
????void func4 (A const& a);</span>
????????Lua?У?????????·????????????????
????<span style="font-size:14px;"> func0 (a) -- Passes a copy of a?? using A's copy constructor.
????func1 (a) -- Passes a pointer to a.
????func2 (a) -- Passes a pointer to a const a.
????func3 (a) -- Passes a reference to a.
????func4 (a) -- Passes a reference to a const a. </span>
???????????к??????????????a????????????????????????????C++???к????????????á????磺
????<span style="font-size:14px;"> void func5 (B b);
????void func6 (B* b); </span>
??????lua?е????
????<span style="font-size:14px;"> func5 (b) - Passes a copy of b?? using B's copy constructor.
????func6 (b) - Passes a pointer to b.
????func6 (a) - Error: Pointer to B expected.
????func1 (b) - Okay?? b is a subclass of a. </span>
??????C++??Lua??????????NULL???LuaBridge?????????nil???檔???????Lua??C++?????nil???????C++?????????NULL???
??????
![](/images/ad-banner/ad-banner.png)
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11