Creation functions

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Creation functions

anders langlands
Is it possible to specify some static function as a creator for a class instead of the class's constructor? As in:

class A : public RefCounted
{
public:

    static SmartPtr<A> create() { return new A; }

private:

    A(); //< Private constructor so only reference-counted objects may be created

};



Also is it possible to get around the limitation that if you're binding a class heirachy with holder objects ( e.g. shared_ptr), only the holder for the base class may be used?

Cheers,

Anders
Reply | Threaded
Open this post in threaded view
|

Re: Creation functions

Arvid Norberg

On Mar 19, 2006, at 01:17, anders langlands wrote:

> Is it possible to specify some static function as a creator for a  
> class instead of the class's constructor? As in:
>
> class A : public RefCounted
> {
> public:
>
>     static SmartPtr<A> create() { return new A; }
>
> private:
>
>     A(); //< Private constructor so only reference-counted objects  
> may be created
>
> };

Yes. You can bind the static member function either as a free  
function, or bind it as a free function inside the class' scope. For  
the latter, see:

http://www.rasterbar.com/products/luabind/docs.html#nested-scopes-and- 
static-functions

To access the static scope of a class, just ask for the member from  
the class in lua. For example:

MyType.my_static_function()

> Also is it possible to get around the limitation that if you're  
> binding a class heirachy with holder objects ( e.g. shared_ptr),  
> only the holder for the base class may be used?

Not really. The holder for the base type is the one that is stored  
inside luabind. You can always make your own dynamic_cast functions  
for down casts.


--
Arvid Norberg




-------------------------------------------------------
This SF.Net email is sponsored by xPML, a groundbreaking scripting language
that extends applications into web and mobile media. Attend the live webcast
and join the prime developer group breaking into this new coding territory!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=110944&bid=241720&dat=121642
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: Creation functions

anders langlands
Thanks Arvid, I'd read the docs on the static functions, what I wasn't very clear about was that I wanted to be able to pretend to Lua that the static function in question was actually the object's constructor, so calling

a = A()

in lua would actually call A::create() in C++. I guess this isn't possible tho.

Anders

On 3/28/06, Arvid Norberg <[hidden email]> wrote:

On Mar 19, 2006, at 01:17, anders langlands wrote:

> Is it possible to specify some static function as a creator for a
> class instead of the class's constructor? As in:
>
> class A : public RefCounted
> {
> public:
>
>     static SmartPtr<A> create() { return new A; }
>
> private:
>
>     A(); //< Private constructor so only reference-counted objects
> may be created
>
> };

Yes. You can bind the static member function either as a free
function, or bind it as a free function inside the class' scope. For
the latter, see:

http://www.rasterbar.com/products/luabind/docs.html#nested-scopes-and-
static-functions

To access the static scope of a class, just ask for the member from
the class in lua. For example:

MyType.my_static_function ()

> Also is it possible to get around the limitation that if you're
> binding a class heirachy with holder objects ( e.g. shared_ptr),
> only the holder for the base class may be used?

Not really. The holder for the base type is the one that is stored
inside luabind. You can always make your own dynamic_cast functions
for down casts.


--
Arvid Norberg




-------------------------------------------------------
This SF.Net email is sponsored by xPML, a groundbreaking scripting language
that extends applications into web and mobile media. Attend the live webcast
and join the prime developer group breaking into this new coding territory!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=110944&bid=241720&dat=121642
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user

Reply | Threaded
Open this post in threaded view
|

Re: Creation functions

Arvid Norberg-2

On Mar 28, 2006, at 11:56, anders langlands wrote:

> Thanks Arvid, I'd read the docs on the static functions, what I  
> wasn't very clear about was that I wanted to be able to pretend to  
> Lua that the static function in question was actually the object's  
> constructor, so calling
>
> a = A()
>
> in lua would actually call A::create() in C++. I guess this isn't  
> possible tho.

No. Not without wrapping A in a class that does that.


--
Arvid Norberg




-------------------------------------------------------
This SF.Net email is sponsored by xPML, a groundbreaking scripting language
that extends applications into web and mobile media. Attend the live webcast
and join the prime developer group breaking into this new coding territory!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=110944&bid=241720&dat=121642
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: Creation functions

anders langlands
Hehe, that's exactly what I've done to implement this :)

A

On 3/29/06, Arvid Norberg <[hidden email]> wrote:

On Mar 28, 2006, at 11:56, anders langlands wrote:

> Thanks Arvid, I'd read the docs on the static functions, what I
> wasn't very clear about was that I wanted to be able to pretend to
> Lua that the static function in question was actually the object's
> constructor, so calling
>
> a = A()
>
> in lua would actually call A::create() in C++. I guess this isn't
> possible tho.

No. Not without wrapping A in a class that does that.


--
Arvid Norberg




-------------------------------------------------------
This SF.Net email is sponsored by xPML, a groundbreaking scripting language
that extends applications into web and mobile media. Attend the live webcast
and join the prime developer group breaking into this new coding territory!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=110944&bid=241720&dat=121642
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user