Factories as script constructors.

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

Factories as script constructors.

Thomas Nelson-10

I have a C++ class that I want to export to Lua.  This class requires a factory method to construct and I would like to hide this little detail from the script side of things.

If my class looks like this:

class Foo
{
    public:
        static boost::shared_ptr<Foo> factoryFoo( int N );
               
        void doSomething( );
    protected:
        Foo() ;
};

module(L)
[
    class_<Foo, boost::shared_ptr<Foo> >("LuaFoo")
        .factory(&Foo::factoryFoo)
        .def("doSomething", &Foo::doSomething)
];

From the script I would be able to do the following:

myF = LuaFoo( 5 ) -- Actually calls the static method Foo::factoryFoo&nbsp;
myF:doSomething()

Has anyone else done something like this? &nbsp; I’ve gotten quite a ways by cloning and mutating the constructor.hpp implementation and adapting it to call a static member but I’m now getting lost in template expansion hell.&nbsp;

I seem to be encountering two problems:
1) Figuring out the Signature deduction templates and making my static function “look” like a constructor.
2) Determining the boost::shared_ptr<foo> when I’m deep inside the factory wrapper.

If anybody has some experience in these areas it would be great.

TomN

------------------------------------------------------------------------------
Virtualization & Cloud Management Using Capacity Planning
Cloud computing makes use of virtualization - but cloud computing
also focuses on allowing computing to be delivered as a service.
http://www.accelacomm.com/jaw/sfnl/114/51521223/
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: Factories as script constructors.

Nigel Atkinson-2
This looks interesting, and in my opinion would make a useful addition  
to the library.  Unfortunately I'm probably not better than you at  
template meta programming, but I'm happy to take a look and see if  
anything "jumps out".

I have a related yet different technique for singleton classes.  In  
this case I want to make it explicit in the Lua code that it is a  
singleton.  Fortunately vanilla Luabind can be used.

class_< SomeManager>( "SomeManager" )
.scope
[
        .def( "getSingleton", &SomeManager::getSingleton )
]
.def( "func", &SomeManager::memberfunc )


mgr = SomeManager.getSingleton()
mgr:func()


Nigel



On 24/02/2012, at 9:57 AM, Thomas Nelson wrote:

>
> I have a C++ class that I want to export to Lua. &nbsp;This class  
> requires a factory method to construct and I would like to hide this  
> little detail from the script side of things.
>
> If my class looks like this:
>
> class Foo
> {
>    public:
>        static boost::shared_ptr<Foo> factoryFoo( int N );
>
>        void doSomething( );
>    protected:
>        Foo() ;
> };
>
> module(L)
> [
>    class_<Foo, boost::shared_ptr<Foo> >("LuaFoo")
>        .factory(&Foo::factoryFoo)
>        .def("doSomething", &Foo::doSomething)
> ];
>
> From the script I would be able to do the following:
>
> myF = LuaFoo( 5 ) -- Actually calls the static method  
> Foo::factoryFoo&nbsp;
> myF:doSomething()
>
> Has anyone else done something like this? &nbsp; I’ve gotten quite a  
> ways by cloning and mutating the constructor.hpp implementation and  
> adapting it to call a static member but I’m now getting lost in  
> template expansion hell.&nbsp;
>
> I seem to be encountering two problems:
> 1) Figuring out the Signature deduction templates and making my  
> static function “look” like a constructor.
> 2) Determining the boost::shared_ptr<foo> when I’m deep inside the  
> factory wrapper.
>
> If anybody has some experience in these areas it would be great.
>
> TomN
>
> ------------------------------------------------------------------------------
> Virtualization & Cloud Management Using Capacity Planning
> Cloud computing makes use of virtualization - but cloud computing
> also focuses on allowing computing to be delivered as a service.
> http://www.accelacomm.com/jaw/sfnl/114/51521223/
> _______________________________________________
> luabind-user mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/luabind-user


------------------------------------------------------------------------------
Virtualization & Cloud Management Using Capacity Planning
Cloud computing makes use of virtualization - but cloud computing
also focuses on allowing computing to be delivered as a service.
http://www.accelacomm.com/jaw/sfnl/114/51521223/
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: Factories as script constructors.

Thomas Nelson-10
Here is what I have so far. I've included class.hpp and
detail/factory_ctor.hpp. I'm still on luabind 9.1 by the way. (Also my
plan is to get it working for 0 or 1 parameter before adding
PP_ITERATION back in. Take a look and see if anything jumps out at you.

The code is based on detail/constructor.hpp since that seemed to be the
closest to the functionality that I was trying to duplicate.
factory_ctor should create a structure with the function call operator
defined.  The trick, I think, is how to get luabind to correctly
recognize the signature and build the call correctly.

I'm going to be looking at this more over the weekend.

Thanks again
Tom

On 02/23/2012 04:49 PM, Nigel Atkinson wrote:

> This looks interesting, and in my opinion would make a useful addition
> to the library.  Unfortunately I'm probably not better than you at
> template meta programming, but I'm happy to take a look and see if
> anything "jumps out".
>
> I have a related yet different technique for singleton classes.  In
> this case I want to make it explicit in the Lua code that it is a
> singleton.  Fortunately vanilla Luabind can be used.
>
> class_<  SomeManager>( "SomeManager" )
> .scope
> [
> .def( "getSingleton",&SomeManager::getSingleton )
> ]
> .def( "func",&SomeManager::memberfunc )
>
>
> mgr = SomeManager.getSingleton()
> mgr:func()
>
>
> Nigel
>
>
>
> On 24/02/2012, at 9:57 AM, Thomas Nelson wrote:
>
>> I have a C++ class that I want to export to Lua.&nbsp;This class
>> requires a factory method to construct and I would like to hide this
>> little detail from the script side of things.
>>
>> If my class looks like this:
>>
>> class Foo
>> {
>>     public:
>>         static boost::shared_ptr<Foo>  factoryFoo( int N );
>>
>>         void doSomething( );
>>     protected:
>>         Foo() ;
>> };
>>
>> module(L)
>> [
>>     class_<Foo, boost::shared_ptr<Foo>  >("LuaFoo")
>>         .factory(&Foo::factoryFoo)
>>         .def("doSomething",&Foo::doSomething)
>> ];
>>
>>  From the script I would be able to do the following:
>>
>> myF = LuaFoo( 5 ) -- Actually calls the static method
>> Foo::factoryFoo&nbsp;
>> myF:doSomething()
>>
>> Has anyone else done something like this?&nbsp; I’ve gotten quite a
>> ways by cloning and mutating the constructor.hpp implementation and
>> adapting it to call a static member but I’m now getting lost in
>> template expansion hell.&nbsp;
>>
>> I seem to be encountering two problems:
>> 1) Figuring out the Signature deduction templates and making my
>> static function “look” like a constructor.
>> 2) Determining the boost::shared_ptr<foo>  when I’m deep inside the
>> factory wrapper.
>>
>> If anybody has some experience in these areas it would be great.
>>
>> Tom

------------------------------------------------------------------------------
Virtualization & Cloud Management Using Capacity Planning
Cloud computing makes use of virtualization - but cloud computing
also focuses on allowing computing to be delivered as a service.
http://www.accelacomm.com/jaw/sfnl/114/51521223/
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user

luabindfactory.zip (9K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Factories as script constructors.

Ryan Pavlik-4
Seems like an useful addition and it sounds like you're on the right track (at least, that's how I'd do it :D) Please feel free to fork on github to keep track of your progress and share. Would be great to add a test at the same time for that functionality.

Ryan

On Fri, Feb 24, 2012 at 9:38 AM, Tom Nelson <[hidden email]> wrote:
Here is what I have so far. I've included class.hpp and detail/factory_ctor.hpp. I'm still on luabind 9.1 by the way. (Also my plan is to get it working for 0 or 1 parameter before adding PP_ITERATION back in. Take a look and see if anything jumps out at you.

The code is based on detail/constructor.hpp since that seemed to be the closest to the functionality that I was trying to duplicate. factory_ctor should create a structure with the function call operator defined.  The trick, I think, is how to get luabind to correctly recognize the signature and build the call correctly.

I'm going to be looking at this more over the weekend.

Thanks again
Tom


On 02/23/2012 04:49 PM, Nigel Atkinson wrote:
This looks interesting, and in my opinion would make a useful addition
to the library.  Unfortunately I'm probably not better than you at
template meta programming, but I'm happy to take a look and see if
anything "jumps out".

I have a related yet different technique for singleton classes.  In
this case I want to make it explicit in the Lua code that it is a
singleton.  Fortunately vanilla Luabind can be used.

class_<  SomeManager>( "SomeManager" )
.scope
[
       .def( "getSingleton",&SomeManager::getSingleton )
]
.def( "func",&SomeManager::memberfunc )



mgr = SomeManager.getSingleton()
mgr:func()


Nigel



On 24/02/2012, at 9:57 AM, Thomas Nelson wrote:

I have a C++ class that I want to export to Lua.&nbsp;This class

requires a factory method to construct and I would like to hide this
little detail from the script side of things.

If my class looks like this:

class Foo
{
   public:
       static boost::shared_ptr<Foo>  factoryFoo( int N );
               
       void doSomething( );
   protected:
       Foo() ;
};

module(L)
[
   class_<Foo, boost::shared_ptr<Foo>  >("LuaFoo")
       .factory(&Foo::factoryFoo)
       .def("doSomething",&Foo::doSomething)

];

 From the script I would be able to do the following:

myF = LuaFoo( 5 ) -- Actually calls the static method
Foo::factoryFoo&nbsp;
myF:doSomething()

Has anyone else done something like this?&nbsp; I’ve gotten quite a

ways by cloning and mutating the constructor.hpp implementation and
adapting it to call a static member but I’m now getting lost in
template expansion hell.&nbsp;

I seem to be encountering two problems:
1)      Figuring out the Signature deduction templates and making my
static function “look” like a constructor.
2)      Determining the boost::shared_ptr<foo>  when I’m deep inside the
factory wrapper.

If anybody has some experience in these areas it would be great.

Tom

------------------------------------------------------------------------------
Virtualization & Cloud Management Using Capacity Planning
Cloud computing makes use of virtualization - but cloud computing
also focuses on allowing computing to be delivered as a service.
http://www.accelacomm.com/jaw/sfnl/114/51521223/
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user




--
Ryan Pavlik
HCI Graduate Student
Virtual Reality Applications Center
Iowa State University

[hidden email]
http://academic.cleardefinition.com

------------------------------------------------------------------------------
Virtualization & Cloud Management Using Capacity Planning
Cloud computing makes use of virtualization - but cloud computing
also focuses on allowing computing to be delivered as a service.
http://www.accelacomm.com/jaw/sfnl/114/51521223/
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: Factories as script constructors.

Kristoffer Danielsson
Hi,
 
In my scripts, I've had singleton classes since version 0.7. Maybe not what you are looking for, but here's how I did it:
 
1) For the bound class, simply remove the constructor bindings: luabind::constructor<>
2) Instantiate the bound class in C++ and put it in globals: luabind::globals(L)["MyClass"] = &myClass;
3) Then in Lua: MyClass:foo()
 
Kristoffer
 

Date: Fri, 24 Feb 2012 11:52:33 -0600
From: [hidden email]
To: [hidden email]
Subject: Re: [luabind] Factories as script constructors.

Seems like an useful addition and it sounds like you're on the right track (at least, that's how I'd do it :D) Please feel free to fork on github to keep track of your progress and share. Would be great to add a test at the same time for that functionality.

Ryan

On Fri, Feb 24, 2012 at 9:38 AM, Tom Nelson <[hidden email]> wrote:
Here is what I have so far. I've included class.hpp and detail/factory_ctor.hpp. I'm still on luabind 9.1 by the way. (Also my plan is to get it working for 0 or 1 parameter before adding PP_ITERATION back in. Take a look and see if anything jumps out at you.

The code is based on detail/constructor.hpp since that seemed to be the closest to the functionality that I was trying to duplicate. factory_ctor should create a structure with the function call operator defined.  The trick, I think, is how to get luabind to correctly recognize the signature and build the call correctly.

I'm going to be looking at this more over the weekend.

Thanks again
Tom


On 02/23/2012 04:49 PM, Nigel Atkinson wrote:
This looks interesting, and in my opinion would make a useful addition
to the library.  Unfortunately I'm probably not better than you at
template meta programming, but I'm happy to take a look and see if
anything "jumps out".

I have a related yet different technique for singleton classes.  In
this case I want to make it explicit in the Lua code that it is a
singleton.  Fortunately vanilla Luabind can be used.

class_<  SomeManager>( "SomeManager" )
.scope
[
       .def( "getSingleton",&SomeManager::getSingleton )
]
.def( "func",&SomeManager::memberfunc )



mgr = SomeManager.getSingleton()
mgr:func()


Nigel



On 24/02/2012, at 9:57 AM, Thomas Nelson wrote:

I have a C++ class that I want to export to Lua.&nbsp;This class

requires a factory method to construct and I would like to hide this
little detail from the script side of things.

If my class looks like this:

class Foo
{
   public:
       static boost::shared_ptr<Foo>  factoryFoo( int N );
               
       void doSomething( );
   protected:
       Foo() ;
};

module(L)
[
   class_<Foo, boost::shared_ptr<Foo>  >("LuaFoo")
       .factory(&Foo::factoryFoo)
       .def("doSomething",&Foo::doSomething)

];

 From the script I would be able to do the following:

myF = LuaFoo( 5 ) -- Actually calls the static method
Foo::factoryFoo&nbsp;
myF:doSomething()

Has anyone else done something like this?&nbsp; I’ve gotten quite a

ways by cloning and mutating the constructor.hpp implementation and
adapting it to call a static member but I’m now getting lost in
template expansion hell.&nbsp;

I seem to be encountering two problems:
1)      Figuring out the Signature deduction templates and making my
static function “look” like a constructor.
2)      Determining the boost::shared_ptr<foo>  when I’m deep inside the
factory wrapper.

If anybody has some experience in these areas it would be great.

Tom

------------------------------------------------------------------------------
Virtualization & Cloud Management Using Capacity Planning
Cloud computing makes use of virtualization - but cloud computing
also focuses on allowing computing to be delivered as a service.
http://www.accelacomm.com/jaw/sfnl/114/51521223/
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user




--
Ryan Pavlik
HCI Graduate Student
Virtual Reality Applications Center
Iowa State University

[hidden email]
http://academic.cleardefinition.com

------------------------------------------------------------------------------ Virtualization & Cloud Management Using Capacity Planning Cloud computing makes use of virtualization - but cloud computing also focuses on allowing computing to be delivered as a service. http://www.accelacomm.com/jaw/sfnl/114/51521223/
_______________________________________________ luabind-user mailing list [hidden email] https://lists.sourceforge.net/lists/listinfo/luabind-user

------------------------------------------------------------------------------
Virtualization & Cloud Management Using Capacity Planning
Cloud computing makes use of virtualization - but cloud computing
also focuses on allowing computing to be delivered as a service.
http://www.accelacomm.com/jaw/sfnl/114/51521223/
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: Factories as script constructors.

Thomas Nelson-10
In reply to this post by Ryan Pavlik-4
If I ever have children they too shall bear the scars inflicted by BOOST_PP_ITERATION and the mpl.

Here's the my first pass .factory constructor I described below.

I'm a bit of a git newb and my version of luabind is a bit stale so I'm going to hold off branching until I've had a chance to sync up (and get myself onto 5.2). In the mean time I've included the files, just diff and patch my changes in class.hpp into your own. As of now this has only been tested on MSVC I'll get around to gcc this weekend or the next (with some luck.)

Tom

On 02/24/2012 09:52 AM, Ryan Pavlik wrote:
Seems like an useful addition and it sounds like you're on the right track (at least, that's how I'd do it :D) Please feel free to fork on github to keep track of your progress and share. Would be great to add a test at the same time for that functionality.

Ryan

On Fri, Feb 24, 2012 at 9:38 AM, Tom Nelson <[hidden email]> wrote:
Here is what I have so far. I've included class.hpp and detail/factory_ctor.hpp. I'm still on luabind 9.1 by the way. (Also my plan is to get it working for 0 or 1 parameter before adding PP_ITERATION back in. Take a look and see if anything jumps out at you.

The code is based on detail/constructor.hpp since that seemed to be the closest to the functionality that I was trying to duplicate. factory_ctor should create a structure with the function call operator defined.  The trick, I think, is how to get luabind to correctly recognize the signature and build the call correctly.

I'm going to be looking at this more over the weekend.

Thanks again
Tom


On 02/23/2012 04:49 PM, Nigel Atkinson wrote:
This looks interesting, and in my opinion would make a useful addition
to the library.  Unfortunately I'm probably not better than you at
template meta programming, but I'm happy to take a look and see if
anything "jumps out".

I have a related yet different technique for singleton classes.  In
this case I want to make it explicit in the Lua code that it is a
singleton.  Fortunately vanilla Luabind can be used.

class_<  SomeManager>( "SomeManager" )
.scope
[
       .def( "getSingleton",&SomeManager::getSingleton )
]
.def( "func",&SomeManager::memberfunc )



mgr = SomeManager.getSingleton()
mgr:func()


Nigel



On 24/02/2012, at 9:57 AM, Thomas Nelson wrote:

I have a C++ class that I want to export to Lua.&nbsp;This class

requires a factory method to construct and I would like to hide this
little detail from the script side of things.

If my class looks like this:

class Foo
{
   public:
       static boost::shared_ptr<Foo>  factoryFoo( int N );
               
       void doSomething( );
   protected:
       Foo() ;
};

module(L)
[
   class_<Foo, boost::shared_ptr<Foo>  >("LuaFoo")
       .factory(&Foo::factoryFoo)
       .def("doSomething",&Foo::doSomething)

];

 From the script I would be able to do the following:

myF = LuaFoo( 5 ) -- Actually calls the static method
Foo::factoryFoo&nbsp;
myF:doSomething()

Has anyone else done something like this?&nbsp; I’ve gotten quite a

ways by cloning and mutating the constructor.hpp implementation and
adapting it to call a static member but I’m now getting lost in
template expansion hell.&nbsp;

I seem to be encountering two problems:
1)      Figuring out the Signature deduction templates and making my
static function “look” like a constructor.
2)      Determining the boost::shared_ptr<foo>  when I’m deep inside the
factory wrapper.

If anybody has some experience in these areas it would be great.

Tom



------------------------------------------------------------------------------
Virtualization & Cloud Management Using Capacity Planning
Cloud computing makes use of virtualization - but cloud computing
also focuses on allowing computing to be delivered as a service.
http://www.accelacomm.com/jaw/sfnl/114/51521223/
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user

factoryctor.zip (10K) Download Attachment