[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Interesting C++ way to allocate user data (placement new)
- From: Rena <hyperhacker@...>
- Date: Wed, 16 May 2012 02:26:10 -0600
On Wed, May 16, 2012 at 1:14 AM, Patrick Rapin <toupie300@gmail.com> wrote:
>> Just wanted to share this idea:
>>
>> void * operator new( size_t size , lua_State * L )
>> {
>> return lua_newuserdata( L , size );
>> }
>> Foo * foo = new ( L ) Foo( constructor arguments );
>
> Nice idea. It is a cleaner replacement to the more common idiom:
> Foo * obj = new(lua_newuserdata(L, sizeof(Foo )) Foo (constructor arguments );
>
> 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 );
>
Combined with C++ templates, I think this could be used to
transparently wrap any type. But is placement new and operator new a
recent standard, or an extension, or has it been around all this time?
--
Sent from my Game Boy.