smartpointers downcast

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

smartpointers downcast

Yaros
Hello! I have a problem with downcast smartpointers:

namespace luabind
{
        template<class T>
                T* get_pointer(boost::shared_ptr<T>& p)
        {       return p.get(); }

        template<class A>
                boost::shared_ptr<const A>*
                get_const_holder(boost::shared_ptr<A>*)
        {       return 0;       }
}

class A
{
};

class B: public A
{
};


void func(boost::shared_ptr<A> pA)
{
        printf("func() called\n");
}

void main()
{
        using namespace luabind;
       
        lua_State* pL=lua_open();
        open(pL);
        module(pL)
        [
                class_<A,boost::shared_ptr<A> >("A")
                        .def(constructor<>()),
                class_<B,A,boost::shared_ptr<B> >("B")
                        .def(constructor<>()),

                def("func",func)
        ];
       
        const char* szCmd = "b=B()\n func(b)\n";
        if(luaL_loadbuffer(pL, szCmd, strlen(szCmd), NULL) || lua_pcall(pL, 0, 0, 0))
        {
                std::string str=lua_tostring(pL, -1);
                lua_pop(pL, 1);
                printf("Critical error: %s\n",str.c_str());
        }
}

---------------------------------------
Error message:

no match for function call 'func' with the parameters (B)
candidates are:
func(smart_ptr<A>)
 

I'm using Luabind-0.7 with Lua-5.0.2. Can you help me?

--
Best regards,
 Yaros


Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&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: smartpointers downcast

Evan Wies-2-2
Yaros,

If you look in the table of smart pointer conversions in the docs (http://www.rasterbar.com/products/luabind/docs.html#smart-pointers), you'll see that there is NO conversion for:
holder_type<A> holder_type<B>
holder_type<A> holder_type<B const>
(here B is the base class, whereas in your example A is the base... the following code is for B as the base)

It's trivial in C++, but a pain in luabind.   Once upon a time there was a shared_ptr_policy that helped with this (at the expense of runtime efficiency?), but I don't have it anymore. 
Note that SWIG can have similar problems.

The workaround is to bind a downcasting method and invoke it on the args to func:
holder_type<B> toB( holder_type<A> spA ) { return holder_type<B>( spA ); }
const char* szCmd = "a=A()\n func( toB(a))\n";

Cheers,
Evan



Yaros wrote:
Hello! I have a problem with downcast smartpointers:

namespace luabind
{
        template<class T>
                T* get_pointer(boost::shared_ptr<T>& p) 
        {       return p.get(); }

        template<class A>
                boost::shared_ptr<const A>* 
                get_const_holder(boost::shared_ptr<A>*)
        {       return 0;       }
}

class A
{
};

class B: public A
{
};


void func(boost::shared_ptr<A> pA)
{
        printf("func() called\n");
}

void main()
{
        using namespace luabind;
        
        lua_State* pL=lua_open();
        open(pL);
        module(pL)
        [
                class_<A,boost::shared_ptr<A> >("A")
                        .def(constructor<>()),
                class_<B,A,boost::shared_ptr<B> >("B")
                        .def(constructor<>()),

                def("func",func)
        ];
        
        const char* szCmd = "b=B()\n func(b)\n";
        if(luaL_loadbuffer(pL, szCmd, strlen(szCmd), NULL) || lua_pcall(pL, 0, 0, 0))
        {
                std::string str=lua_tostring(pL, -1);
                lua_pop(pL, 1);
                printf("Critical error: %s\n",str.c_str());
        }
}

---------------------------------------
Error message:

no match for function call 'func' with the parameters (B)
candidates are:
func(smart_ptr<A>)
  

I'm using Luabind-0.7 with Lua-5.0.2. Can you help me?

  


-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&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: smartpointers downcast

Arvid Norberg-2
In reply to this post by Yaros

On Jul 4, 2006, at 17:00, Yaros wrote:

> Hello! I have a problem with downcast smartpointers:
>
> [...]
>
> void main()
> {
>         using namespace luabind;
>
>         lua_State* pL=lua_open();
>         open(pL);
>         module(pL)
>         [
>                 class_<A,boost::shared_ptr<A> >("A")
>                         .def(constructor<>()),
>                 class_<B,A,boost::shared_ptr<B> >("B")
                             ^^^^^^^^^^^^^^^^^^^^

What if you change this to boost::shared_ptr<A>?

I think that's the way it's meant to be used.

>                         .def(constructor<>()),
>
>                 def("func",func)
>         ];
>
>         const char* szCmd = "b=B()\n func(b)\n";
>         if(luaL_loadbuffer(pL, szCmd, strlen(szCmd), NULL) ||  
> lua_pcall(pL, 0, 0, 0))
>         {
>                 std::string str=lua_tostring(pL, -1);
>                 lua_pop(pL, 1);
>                 printf("Critical error: %s\n",str.c_str());
>         }
> }

--
Arvid Norberg




-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&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: smartpointers downcast

Josh Green-3
Further to this:

Is it possible to do just:
> class_<A,boost::shared_ptr<A> >("A")
>                         .def(constructor<>()),
>                 class_<B,A>("B")

By doing this, can I ASSUME, that B is stored in a boost::shared_ptr<A>?
Or do I need to specifically specify it?
Thanks

--Josh

-----Original Message-----
From: [hidden email]
[mailto:[hidden email]] On Behalf Of Arvid
Norberg
Sent: Thursday, 13 July 2006 5:33 AM
To: Yaros; [hidden email]
Subject: Re: [luabind] smartpointers downcast



On Jul 4, 2006, at 17:00, Yaros wrote:

> Hello! I have a problem with downcast smartpointers:
>
> [...]
>
> void main()
> {
>         using namespace luabind;
>
>         lua_State* pL=lua_open();
>         open(pL);
>         module(pL)
>         [
>                 class_<A,boost::shared_ptr<A> >("A")
>                         .def(constructor<>()),
>                 class_<B,A,boost::shared_ptr<B> >("B")
                             ^^^^^^^^^^^^^^^^^^^^

What if you change this to boost::shared_ptr<A>?

I think that's the way it's meant to be used.

>                         .def(constructor<>()),
>
>                 def("func",func)
>         ];
>
>         const char* szCmd = "b=B()\n func(b)\n";
>         if(luaL_loadbuffer(pL, szCmd, strlen(szCmd), NULL) ||
> lua_pcall(pL, 0, 0, 0))
>         {
>                 std::string str=lua_tostring(pL, -1);
>                 lua_pop(pL, 1);
>                 printf("Critical error: %s\n",str.c_str());
>         }
> }

--
Arvid Norberg




------------------------------------------------------------------------
-
Using Tomcat but need to do more? Need to support web services,
security? Get stuff done quickly with pre-integrated technology to make
your job easier Download IBM WebSphere Application Server v.1.0.1 based
on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys -- and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user