我有5000个数据项定义配置,我们希望将其设计为,

-- Name of the device
VARIABLE Name {
   type=Float,
   length=20,
   <<few more definition here>>
}

-- The device running elapsed time since its last boot
VARIABLE BootTime {
   type=Integer,
   <<few more definition here>>
}


我将使用不同的通信协议从设备中读取“名称”,“ BootTime”的值,其中使用了上面定义的属性。

我希望变量也具有pre_processor和post_processor函数的属性。


在Lua中如何定义这样的结构?如果这种结构不可能,那么卢阿的壁橱结构可能是什么?
我想为这个变量定义重载运算符,以便可以这样做,

我可以配置BootTime = 23456789或
进行类似BootTime + 100(毫秒)的运算
或进行比较,例如BootTime> 23456789,然后执行某些操作

最佳答案

如果可以省略关键字VARIABLE,则代码为Lua,而您只需要一点支持代码(某些__index元方法魔术)。

Integer="Integer"
setmetatable(_G,
        { __index = function(t,n)
                        return function (x) _G[n]=x.value end
                end })
BootTime {
        type=Integer,
        value=10000
}
print(BootTime+2345)


如果您想保留关键字VARIABLE,则您给出的语法将不再是普通Lua,但是如果您可以使用VARIABLE.BootTimeVARIABLE"BootTime"VARIABLE[BootTime],那么它就是普通Lua,并且可以使其适合使用元方法。

10-07 12:05