[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Interesting C++ way to allocate user data (placement new)
- From: Leo Razoumov <slonik.az@...>
- Date: Wed, 16 May 2012 19:00:53 -0400
On Wed, May 16, 2012 at 3:14 AM, Patrick Rapin <toupie300@gmail.com> wrote:
>
> One could then get a step further and specify the metatable registry
> name typically associated with the userdata.
>
> void * operator new( size_t size , lua_State * L, const char* metaname )
> {
> void* obj = lua_newuserdata( L , size );
> luaL_getmetatable(L, metaname);
> lua_setmetatable(L, -2);
> return obj;
> }
>
> So:
> Foo * foo = new ( L, "FooMetatable" ) Foo( constructor arguments );
>
To spare some typing one can define a corresponding macro:
// untested, targeting GCC
#define NEW(L, Type, args...) (new(L, #Type "Metatable")Type(args))
Foo *foo = NEW(L, Foo, constructor arguments);
--Leo--