[oolua] Re: shared_ptr question

  • From: "Liam Devine" <liamdevine@xxxxxxxxx>
  • To: oolua@xxxxxxxxxxxxx
  • Date: Mon, 27 Jan 2014 17:00:47 -0000

On 27 Jan 2014, at 15:23, SteelRat wrote:

> Hi, Liam, i have a third party library so i can't change source.

It would be nice if I could see the source as your example makes no sense to 
me. For example the constructor of A is private and not available in the 
derived class B.


> There are all objects used with shared_ptr, NO raw pointers at all.
> It looks like this:
> 
>     class A
>     {
>         public:
>             typedef std::shared_ptr<A> APtr;
>             static APtr Create();
> 
>        private:
>              A();
>             ~A();
>     };
> 
> I have a problem to access "get" method of APtr.

Could you explain why you need to expose or use the get method in Lua?

> 
> typedef A::APtr AAPtr;
> 
> OOLUA_PROXY( AAPtr )
>         OOLUA_TAGS(Shared, No_public_constructors)
> //        OOLUA_MFUNC(get) // proxy class of A is not declared yet
> OOLUA_PROXY_END
> 

This is a mistake which is probably due to the lack of documentation at the 
moment. You do not create a proxy for shared pointers only the underlying types.
Also see [1] and [2] about the Shared tag.

> 
> OOLUA_PROXY( A )
>     OOLUA_TAGS(No_public_constructors)
>     OOLUA_SFUNC(Create)
> OOLUA_PROXY_END
> 
> 
> And if i'm switching order of proxy declaration (A before APtr), there are 
> error with "Create" method. There no docs on OOLua_shared_ptr yet, so may be 
> i'm just doing something wrong?
> Now i'm using external function to get object, but i don't like it:
> 
>     int getSharedA(lua_State *L)
>     {    
>         A::Ptr* obj;
>         if (OOLUA::pull(L, obj))
>         {    
>             OOLUA::push(L, obj->get());
>             return 1;
>         }
>         return 0;
>     }
> 
> 
> 
> 
> And second trouble, here is another class derived from A:
> 
>     class B: public A
>     {
>         public:
>             typedef std::shared_ptr<B> BPtr;
>             
>             void DoSomething(A::APtr obj);
>             static BPtr Create();
>                   
>     };
> 
> typedef B::BPtr BBPtr;
> 
> OOLUA_PROXY( BBPtr)
>         OOLUA_TAGS(Shared, No_public_constructors)
> OOLUA_PROXY_END
> 
> OOLUA_PROXY( B, A )
>     OOLUA_TAGS(No_public_constructors)
>     OOLUA_SFUNC(Create)
>     OOLUA_MFUNC(DoSomething)
> OOLUA_PROXY_END
> 
> Lua code:
>     varX = B.Create();
>     varB = B.Create();
>     varB:DoSomething(varX)
> 
> Calling of DoSomthing causes error:
>     "tried to pull type APtr_const which is not the type or a base of the 
> type on the stack"
> In C++ it ( bPtr->DoSomething(xPtr) )  works, but BBPtr is not derived from 
> AAPtr... What can i do with this?
> 

That is how C++'s shared_ptr is defined. shared_ptr<B> is not related to 
shared_ptr<A>, they are two separate types.


> Sorry for schematic example, but full working code is too big.
>     Thank you.
> 

Regardless, I need to see code that will at least compile and show what you are 
trying to do. If you provide that I will be more than happy to explain where 
you have gone wrong and provide a working example of your situation, if I can.


[1]  
https://bitbucket.org/liamdevine/oolua/src/68cf0c0ec913cbe019d54fb33ab53d8f18cb06d6/include/proxy_tags.h?at=shared_ptr#cl-115
[2] 
https://bitbucket.org/liamdevine/oolua/src/68cf0c0ec913cbe019d54fb33ab53d8f18cb06d6/include/oolua_config.h?at=shared_ptr#cl-277

--
Liam

Other related posts: