Luna Four

lua-users home
wiki

LunaV4 是现有 LunaWrapper 的一个扩展。它带来了重大的改变,但仍应向后兼容。

特性

代码

代码的缩进相当糟糕,但有注释。你可以在 LunaFourCode 找到它。如果你没有 luaL_testudata,你还需要包含它。它也可以在 LunaFourTestUserdata 找到。

用法 - Lua 修改

要使用属性,你需要创建一个 __setindex 并修改 __index 元运算符。即使属性已存在,也需要调用 __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}
};
将 T 替换为你的 C++ 类名。

用法 - 声明函数

你的函数必须是以下形式
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;
}

用法 - 元运算符

你可以为你的类声明元运算符。元运算符的声明与函数相同。如果我们想声明等号元运算符,我们会添加
{"__eq", &T::myComparisonFunction},
到我们的类中(请参阅设置)。左侧的对象存储在堆栈的第 2 位,右侧的对象存储在堆栈的第 3 位。(在函数中,第 1 位保留给 self 对象)。

用法 - 初始化我们的类

为了让用户能够初始化我们的类的实例,你需要调用
Luna < core::RColor >::Register(L,"Namespace");
在 dofile 或 dostring 之前。你可以将 Namespace 替换为你想要加载类的命名空间,或者留空将其放入全局空间。Namespace 必须是先前定义的表。在命名空间下创建类时,它们的初始化方式如下:
myobject = Namespace.MyClass();

RecentChanges · preferences
编辑 · 历史
最后编辑于 2009 年 1 月 10 日 晚上 10:40 GMT (diff)