Luna Four

lua-users home
wiki

LunaV4 是現有 LunaWrapper 的延伸。它帶來重大變更,但應仍向下相容。

特色

程式碼

程式碼的縮排有點糟,但有註解。你可以到 LunaFourCode 找到它。如果你沒有 luaL_testudata,你可能也需要納入它。也可以到 LunaFourTestUserdata 找到它。

用法 - Lua 修改

若要使用屬性,你需要建立 __setindex 並修改 __index meta 運算子。__setindex 需要呼叫,即使屬性已經存在,__index 需要呼叫,即使屬性存在。

用法 - 設定

若要使用 Luna,你必須在想要包裝的 C++ 類別中宣告下列靜態變數
// declaration
static const char *className;
static const Luna < T >::FunctionType Functions[];
static const Luna < T >::PropertyType Properties[];

bool isExisting; // This is used by Luna to see whether it's been created by createFromExisting.  Don't set it.
bool isPrecious; // This is used to tell Luna not to garbage collect the object, in case other objects might reference it.  Set it in your classes constructor.

// implementation
const char *T::className = "YourClassNameInLua";
const Luna < T >::FunctionType T::Functions[] = {
	{"myFunction", &T::updateAll},	
	{0}
};
const Luna < RPhysicsManager >::PropertyType RPhysicsManager::Properties[] = {
	{"myProperty", &T::getProperty, &layer::setProperty },
	{0}
};
使用你的 C++ 類別名稱取代 T。

用法 - 宣告函式

你的函式必須是表單
int myFunction(lua_State* L)

用法 - 宣告屬性

屬性比函式更複雜,需要兩個 C++ 函式:一個用於取得,一個用於設定。兩個 C++ 函數都與函數有相同的宣告型別。取得和設定函數的範例如下所示
int T::getProperty(lua_State* L)
{
	lua_pushnumber(L,X);
	return 1;
}

int T::setProperty(lua_State* L)
{
	myCPPClassVar = lua_tonumber(L,-1);
	return 0;
}

用法 - meta 運算子

你可以為你的類別宣告 meta 運算子。meta 運算子與函數有相同的宣告。如果我們要宣告相等 meta 運算子,我們會新增
{"__eq", &T::myComparisonFunction},
到我們的類別(請參照設定)。左方的物件儲存在堆疊上的第 2 個位置,右方的物件儲存在第 3 個位置。(第 1 個位置是用於函式的 self 物件。)

用法 - 初始化我們的類別

為了讓使用者能夠初始化我們類別的執行個體,你需要呼叫
Luna < core::RColor >::Register(L,"Namespace");
在你的 dofile 或 dostring 之前某個地方。你可用你想要載入類別的命名空間取代 Namespace,或留空讓它進入全域空間。Namespace 必須事先定義為表格。當在命名空間下建立類別時,它們可以初始化成像
myobject = Namespace.MyClass();

最近變更 · 喜好設定
編輯 · 歷史記錄
最後編輯時間 2009 年 1 月 11 日,星期日 上午 4:40 GMT (diff)