Luna Five

Luna Five

lua-users home
wiki

LunaFive  is an enhanced Version of  LunaFour . In contrast to  LunaFour  it works with Lua 5.2 (Also 5.2.1). It is also faster, when it comes to pushing and creating Objects from both Lua and C++. I also restructured Luna to use less memory since an Objects Methods and Properties are only stored once in a whole State. Furthermore, you can take an objects method an store it in a variable, just like any other function. It still refers to the object you read the method from.
       local var = object()
       local varFunc = var.getColor;

       varFunc();     -- Calls var.getColor()

       varFunc( 35 ); -- Calls var.getColor( 35 )

Help & Documentation

I did not have the time yet to document the Wrapper but the use is fairly identical to LunaFour

See Also

The Code

#include "lua.hpp"
#include <string.h> // For strlen

template < class T > class Luna {
  public:

    struct PropertyType {
	const char     *name;
	int             (T::*getter) (lua_State *);
	int             (T::*setter) (lua_State *);
    };

    struct FunctionType {
	const char     *name;
	int             (T::*func) (lua_State *);
    };

/*
  @ check
  Arguments:
    * L - Lua State
    * narg - Position to check

  Description:
    Retrieves a wrapped class from the arguments passed to the func, specified by narg (position).
    This func will raise an exception if the argument is not of the correct type.
*/
    static T* check(lua_State * L, int narg)
	{
		T** obj = static_cast <T **>(luaL_checkudata(L, narg, T::className));
		if ( !obj )
			return nullptr; // lightcheck returns nullptr if not found.
		return *obj;		// pointer to T object
	}

/*
  @ lightcheck
  Arguments:
    * L - Lua State
    * narg - Position to check

  Description:
    Retrieves a wrapped class from the arguments passed to the func, specified by narg (position).
    This func will return nullptr if the argument is not of the correct type.  Useful for supporting
    multiple types of arguments passed to the func
*/ 
	static T* lightcheck(lua_State * L, int narg) {
		T** obj = static_cast <T **>(luaL_testudata(L, narg, T::className));
		if ( !obj )
			return nullptr; // lightcheck returns nullptr if not found.
		return *obj;		// pointer to T object
    }

/*
  @ Register
  Arguments:
    * L - Lua State
    * namespac - Namespace to load into

  Description:
    Registers your class with Lua.  Leave namespac "" if you want to load it into the global space.
*/
    // REGISTER CLASS AS A GLOBAL TABLE 
    static void Register(lua_State * L, const char *namespac = NULL ) {

		if ( namespac && strlen(namespac) )
		{
			lua_getglobal(L, namespac);
			if( lua_isnil(L,-1) ) // Create namespace if not present
			{
				lua_newtable(L);
				lua_pushvalue(L,-1); // Duplicate table pointer since setglobal pops the value
				lua_setglobal(L,namespac);
			}
			lua_pushcfunction(L, &Luna < T >::constructor);
			lua_setfield(L, -2, T::className);
			lua_pop(L, 1);
		} else {
			lua_pushcfunction(L, &Luna < T >::constructor);
			lua_setglobal(L, T::className);
		}
		
		luaL_newmetatable(L, T::className);
		int             metatable = lua_gettop(L);
		
		lua_pushstring(L, "__gc");
		lua_pushcfunction(L, &Luna < T >::gc_obj);
		lua_settable(L, metatable);
		
		lua_pushstring(L, "__tostring");
		lua_pushcfunction(L, &Luna < T >::to_string);
		lua_settable(L, metatable);

		lua_pushstring(L, "__index");
		lua_pushcfunction(L, &Luna < T >::property_getter);
		lua_settable(L, metatable);

		lua_pushstring(L, "__newindex");
		lua_pushcfunction(L, &Luna < T >::property_setter);
		lua_settable(L, metatable);
		
		for (int i = 0; T::properties[i].name; i++) { 				// Register some properties in it
			lua_pushstring(L, T::properties[i].name);				// Having some string associated with them
			lua_pushnumber(L, i); 									// And a number indexing which property it is
			lua_settable(L, metatable);
		}
		
		for (int i = 0; T::methods[i].name; i++) {
			lua_pushstring(L, T::methods[i].name); 					// Register some functions in it
			lua_pushnumber(L, i | ( 1 << 8 ) );						// Add a number indexing which func it is
			lua_settable(L, metatable);								//
		}
    }

/*
  @ constructor (internal)
  Arguments:
    * L - Lua State
*/
    static int constructor(lua_State * L)
	{
		T*  ap = new T(L);
		T** a = static_cast<T**>(lua_newuserdata(L, sizeof(T *))); // Push value = userdata
		*a = ap;
		
		luaL_getmetatable(L, T::className); 		// Fetch global metatable T::classname
		lua_setmetatable(L, -2);
		return 1;
    }

/*
  @ createNew
  Arguments:
    * L - Lua State
	T*	- Instance to push

  Description:
    Loads an instance of the class into the Lua stack, and provides you a pointer so you can modify it.
*/
    static void push(lua_State * L, T* instance )
	{
		T **a = (T **) lua_newuserdata(L, sizeof(T *)); // Create userdata
		*a = instance;
		
		luaL_getmetatable(L, T::className);
		
		lua_setmetatable(L, -2);
    }

/*
  @ property_getter (internal)
  Arguments:
    * L - Lua State
*/
    static int property_getter(lua_State * L)
	{
		lua_getmetatable(L, 1); // Look up the index of a name
		lua_pushvalue(L, 2);	// Push the name
		lua_rawget(L, -2);		// Get the index
		
		if (lua_isnumber(L, -1)) { // Check if we got a valid index
			
			int _index = lua_tonumber(L, -1);
			
			T** obj = static_cast<T**>(lua_touserdata(L, 1));
			
			lua_pushvalue(L, 3);
			
			if( _index & ( 1 << 8 ) ) // A func
			{
				lua_pushnumber(L, _index ^ ( 1 << 8 ) ); // Push the right func index
				lua_pushlightuserdata(L, obj);
				lua_pushcclosure(L, &Luna < T >::function_dispatch, 2);
				return 1; // Return a func
			}
			
			lua_pop(L,2);    // Pop metatable and _index
			lua_remove(L,1); // Remove userdata
			lua_remove(L,1); // Remove [key]
			
			return ((*obj)->*(T::properties[_index].getter)) (L);
		}
		
		return 1;
    }

/*
  @ property_setter (internal)
  Arguments:
    * L - Lua State
*/
    static int property_setter(lua_State * L)
	{
		
		lua_getmetatable(L, 1); // Look up the index from name
		lua_pushvalue(L, 2);	//
		lua_rawget(L, -2);		//
		
		if ( lua_isnumber(L, -1) ) // Check if we got a valid index
		{
			
			int _index = lua_tonumber(L, -1);
			
			T** obj = static_cast<T**>(lua_touserdata(L, 1));
			
			if( !obj || !*obj )
			{
				luaL_error( L , "Internal error, no object given!" );
				return 0;
			}
			
			if( _index >> 8 ) // Try to set a func
			{
				char c[128];
				sprintf( c , "Trying to set the method [%s] of class [%s]" , (*obj)->T::methods[_index ^ ( 1 << 8 ) ].name , T::className );
				luaL_error( L , c );
				return 0;
			}
			
			lua_pop(L,2);    // Pop metatable and _index
			lua_remove(L,1); // Remove userdata
			lua_remove(L,1); // Remove [key]
			
			return ((*obj)->*(T::properties[_index].setter)) (L);
		}
		
		return 0;
    }

/*
  @ function_dispatch (internal)
  Arguments:
    * L - Lua State
*/
    static int function_dispatch(lua_State * L)
	{
		int i = (int) lua_tonumber(L, lua_upvalueindex(1));
		T** obj = static_cast < T ** >(lua_touserdata(L, lua_upvalueindex(2)));
		
		return ((*obj)->*(T::methods[i].func)) (L);
    }

/*
  @ gc_obj (internal)
  Arguments:
    * L - Lua State
*/
    static int gc_obj(lua_State * L)
	{
		T** obj = static_cast < T ** >(lua_touserdata(L, -1));
		
		if( obj && *obj )
			delete(*obj);
		
		return 0;
    }
	
	static int to_string(lua_State* L)
	{
		T** obj = static_cast<T**>(lua_touserdata(L, -1));
		
		if( obj )
			lua_pushfstring(L, "%s (%p)", T::className, (void*)*obj);
		else
			lua_pushstring(L,"Empty object");
		
		return 1;
	}
};
基于数据驱动的 Koopman 算子的递归神经网络模型线性化,用于纳米定位系统的预测控制研究(Matlab代码实现)内容概要:本文围绕“基于数据驱动的Koopman算子的递归神经网络模型线性化”展开,旨在研究纳米定位系统的预测控制方法。通过结合数据驱动技术与Koopman算子理论,将非线性系统动态近似为高维线性系统,进而利用递归神经网络(RNN)建模并实现系统行为的精确预测。文中详细阐述了模型构建流程、线性化策略及在预测控制中的集成应用,并提供了完整的Matlab代码实现,便于科研人员复现实验、优化算法并拓展至其他精密控制系统。该方法有效提升了纳米级定位系统的控制精度与动态响应性能。; 适合人群:具备自动控制、机器学习或信号处理背景,熟悉Matlab编程,从事精密仪器控制、智能制造或先进控制算法研究的研究生、科研人员及工程技术人员。; 使用场景及目标:①实现非线性动态系统的数据驱动线性化建模;②提升纳米定位平台的轨迹跟踪与预测控制性能;③为高精度控制系统提供可复现的Koopman-RNN融合解决方案; 阅读建议:建议结合Matlab代码逐段理解算法实现细节,重点关注Koopman观测矩阵构造、RNN训练流程与模型预测控制器(MPC)的集成方式,鼓励在实际硬件平台上验证并调整参数以适应具体应用场景。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值