const errors trying to use an instrusive smart pointer as a holder

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

const errors trying to use an instrusive smart pointer as a holder

anders langlands
I'm trying to bind a simple test class that uses my own intrusive smart pointer as a holder type:



class A : public RefCounted
{
public:
    A() { cout << "A's constructor" << endl; }
   
    virtual void print( ) { cout << "print A" << endl; }
};

namespace luabind {

template<class T>
T* get_pointer(SmartPtr<T>& p)
{
    return getRawPtr( p );
}

template<class A>
SmartPtr<const A>*
get_const_holder(SmartPtr<A>*)
{
    return 0;
}

}

RefCounted has a refcount variable and addReference() and removeReference() functions that update the refcount in the SmartPtr *tor functions. It's these that are causing problems:

./include/RefCounted.h: In destructor 'SmartPtr<T>::~SmartPtr() [with T = const A]':
/usr/include/luabind/class.hpp:330: instantiated from 'static void luabind::detail::convert_holder<HeldType, ConstHolderType>::apply(void*, void*) [with HeldType = SmartPtr<A>, ConstHolderType = SmartPtr<const A>]'
/usr/include/luabind/class.hpp:343: instantiated from 'static void (* luabind::detail::const_converter<HeldType>::apply(ConstHolderType*))(void*, void*) [with ConstHolderType = SmartPtr<const A>, HeldType = SmartPtr<A>]'
/usr/include/luabind/class.hpp:1159: instantiated from 'void luabind::class_<T, X1, X2, X3>::init() [with T = A, X1 = SmartPtr<A>, X2 = luabind::detail::unspecified, X3 = luabind::detail::unspecified]'
/usr/include/luabind/class.hpp:860: instantiated from 'luabind::class_<T, X1, X2, X3>::class_(const char*) [with T = A, X1 = SmartPtr<A>, X2 = luabind::detail::unspecified, X3 = luabind::detail::unspecified]'
src/plugins/myplugin.cpp:92: instantiated from here
./include/RefCounted.h:82: error: passing 'const A' as 'this' argument of 'void RefCounted::removeReference()' discards qualifiers

I'm not 100% clear on what's going on here. Anyone have any idea how I can get around this? Seems luabind's using SmartPtr<const A>* when I'd rather it just used SmartPtr<A>* and that's causing the compiler to generate the errors.

A

Reply | Threaded
Open this post in threaded view
|

Re: const errors trying to use an instrusive smart pointer as a holder

anders langlands
OK, so I got round the compile time error by changing

template<class A>
SmartPtr<const A>*
get_const_holder(SmartPtr<A>*)
{
    return 0;
}

to

template<class A>
SmartPtr<A>*
get_const_holder(SmartPtr<A>*)
{
    return 0;
}

which is probably very wrong, and indeed, when I come to register the class at runtime, I get:

class_registry.cpp:244: void luabind::detail::class_registry::add_class(const std::type_info*, luabind::detail::class_rep*): Assertion `(m_classes.find(info) == m_classes.end()) && "you are trying to register a class twice"' failed.
Aborted

The code I use to register the class is this:

module( r )
    [
         class_< A, SmartPtr<A> >( "A" )
    ];


On 2/20/06, anders langlands <[hidden email]> wrote:
I'm trying to bind a simple test class that uses my own intrusive smart pointer as a holder type:



class A : public RefCounted
{
public:
    A() { cout << "A's constructor" << endl; }
   
    virtual void print( ) { cout << "print A" << endl; }
};

namespace luabind {

template<class T>
T* get_pointer(SmartPtr<T>& p)
{
    return getRawPtr( p );
}

template<class A>
SmartPtr<const A>*
get_const_holder(SmartPtr<A>*)
{
    return 0;
}

}

RefCounted has a refcount variable and addReference() and removeReference() functions that update the refcount in the SmartPtr *tor functions. It's these that are causing problems:

./include/RefCounted.h: In destructor 'SmartPtr<T>::~SmartPtr() [with T = const A]':
/usr/include/luabind/class.hpp:330: instantiated from 'static void luabind::detail::convert_holder<HeldType, ConstHolderType>::apply(void*, void*) [with HeldType = SmartPtr<A>, ConstHolderType = SmartPtr<const A>]'
/usr/include/luabind/class.hpp:343: instantiated from 'static void (* luabind::detail::const_converter<HeldType>::apply(ConstHolderType*))(void*, void*) [with ConstHolderType = SmartPtr<const A>, HeldType = SmartPtr<A>]'
/usr/include/luabind/class.hpp:1159: instantiated from 'void luabind::class_<T, X1, X2, X3>::init() [with T = A, X1 = SmartPtr<A>, X2 = luabind::detail::unspecified, X3 = luabind::detail::unspecified]'
/usr/include/luabind/class.hpp:860: instantiated from 'luabind::class_<T, X1, X2, X3>::class_(const char*) [with T = A, X1 = SmartPtr<A>, X2 = luabind::detail::unspecified, X3 = luabind::detail::unspecified]'
src/plugins/myplugin.cpp:92: instantiated from here
./include/RefCounted.h:82: error: passing 'const A' as 'this' argument of 'void RefCounted::removeReference()' discards qualifiers

I'm not 100% clear on what's going on here. Anyone have any idea how I can get around this? Seems luabind's using SmartPtr<const A>* when I'd rather it just used SmartPtr<A>* and that's causing the compiler to generate the errors.

A


Reply | Threaded
Open this post in threaded view
|

Re: const errors trying to use an instrusive smart pointer as a holder

Arvid Norberg
In reply to this post by anders langlands

On Feb 20, 2006, at 23:50, anders langlands wrote:

> [...]
>
> RefCounted has a refcount variable and addReference() and  
> removeReference() functions that update the refcount in the  
> SmartPtr *tor functions. It's these that are causing problems:
>
> ./include/RefCounted.h: In destructor 'SmartPtr<T>::~SmartPtr()  
> [with T = const A]':
> /usr/include/luabind/class.hpp:330: instantiated from 'static void  
> luabind::detail::convert_holder<HeldType, ConstHolderType>::apply
> (void*, void*) [with HeldType = SmartPtr<A>, ConstHolderType =  
> SmartPtr<const A>]'
> /usr/include/luabind/class.hpp:343: instantiated from 'static void  
> (* luabind::detail::const_converter<HeldType>::apply
> (ConstHolderType*))(void*, void*) [with ConstHolderType =  
> SmartPtr<const A>, HeldType = SmartPtr<A>]'
> /usr/include/luabind/class.hpp:1159: instantiated from 'void  
> luabind::class_<T, X1, X2, X3>::init() [with T = A, X1 =  
> SmartPtr<A>, X2 = luabind::detail::unspecified, X3 =  
> luabind::detail::unspecified]'
> /usr/include/luabind/class.hpp:860: instantiated from  
> 'luabind::class_<T, X1, X2, X3>::class_(const char*) [with T = A,  
> X1 = SmartPtr<A>, X2 = luabind::detail::unspecified, X3 =  
> luabind::detail::unspecified]'
> src/plugins/myplugin.cpp:92: instantiated from here
> ./include/RefCounted.h:82: error: passing 'const A' as 'this'  
> argument of 'void RefCounted::removeReference()' discards qualifiers
>
> I'm not 100% clear on what's going on here. Anyone have any idea  
> how I can get around this? Seems luabind's using SmartPtr<const A>*  
> when I'd rather it just used SmartPtr<A>* and that's causing the  
> compiler to generate the errors.

It looks like your RefCounted reference counter isn't mutable.  
RefCounted::removeReference() has to be const.

--
Arvid Norberg




-------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc. Do you grep through log files
for problems?  Stop!  Download the new AJAX search engine that makes
searching your log files as easy as surfing the  web.  DOWNLOAD SPLUNK!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=103432&bid=230486&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: const errors trying to use an instrusive smart pointer as a holder

anders langlands
Thanks, making nref mutable works great!

But now the next problem :)

I have a class heirachy, A <- B <- C, defined as follows:

class A : public RefCounted
{
public:
   
    A() { cout << "A's constructor" << endl; }
   
    virtual void print( ) {
        cout << "Print A" << endl;
    }
       
protected:

};

class B : public A
{
public:
   
    B() { cout << "B's constructor" << endl; }
   
    virtual void print( ) {
        cout << "Print B" << endl;
    }
       
protected:

};

class C : public B
{
public:
   
    C() { cout << "C's constructor" << endl; }
   
    virtual void print( ) {
        cout << "Print C" << endl;
    }
       
protected:

};


And bound like so:

module(L)
    [
        class_< A, SmartPtr<A> >( "A" )
            .def( constructor<>() ),
        class_< B, A, SmartPtr<A> >( "B" )
            .def( constructor<>() ),
        class_< C, B, SmartPtr<A> >( "C" )
            .def( constructor<>() )
    ];

I also define a couple of free functions that call each class's print function:

void printa( SmartPtr<A> ap )
{
    ap->print();
}

void printb( SmartPtr<B> bp )
{
    bp->print();
}

module( L )
    [
  
        def( "printa", printa ),
        def( "printb", printb )
       
    ];


Now I can call construct all the classes fine, and calling printa() with each of the classes produces the correct output. However, if I try calling printb() with class B or C, I get:

---------------------------------------------------------------------------------------------------------
b = B()
>>> printb( b )
no match for function call 'printb' with the parameters (B)
candidates are:
printb(custom [8SmartPtrI1BE])
---------------------------------------------------------------------------------------------------------


What's going on internally to luabind when it's calling these functions, is it passing around the raw pointers, then constructing a smart pointer before calling the C++ function?

If I define a derived class as having a holder templated against the base class type, what type does luabind store the pointer as internally? Is it even possible for it to resolve these kind of relationships properly? I'ts kind of essential for my work!

A





On 2/20/06, Arvid Norberg <[hidden email]> wrote:

On Feb 20, 2006, at 23:50, anders langlands wrote:

> [...]
>
> RefCounted has a refcount variable and addReference() and
> removeReference() functions that update the refcount in the
> SmartPtr *tor functions. It's these that are causing problems:
>
> ./include/RefCounted.h: In destructor 'SmartPtr<T>::~SmartPtr()
> [with T = const A]':
> /usr/include/luabind/class.hpp:330: instantiated from 'static void
> luabind::detail::convert_holder<HeldType, ConstHolderType>::apply
> (void*, void*) [with HeldType = SmartPtr<A>, ConstHolderType =
> SmartPtr<const A>]'
> /usr/include/luabind/class.hpp:343: instantiated from 'static void
> (* luabind::detail::const_converter<HeldType>::apply
> (ConstHolderType*))(void*, void*) [with ConstHolderType =
> SmartPtr<const A>, HeldType = SmartPtr<A>]'
> /usr/include/luabind/class.hpp:1159: instantiated from 'void
> luabind::class_<T, X1, X2, X3>::init() [with T = A, X1 =
> SmartPtr<A>, X2 = luabind::detail::unspecified, X3 =
> luabind::detail::unspecified]'
> /usr/include/luabind/class.hpp:860: instantiated from
> 'luabind::class_<T, X1, X2, X3>::class_(const char*) [with T = A,
> X1 = SmartPtr<A>, X2 = luabind::detail::unspecified, X3 =
> luabind::detail::unspecified]'
> src/plugins/myplugin.cpp:92: instantiated from here
> ./include/RefCounted.h:82: error: passing 'const A' as 'this'
> argument of 'void RefCounted::removeReference()' discards qualifiers
>
> I'm not 100% clear on what's going on here. Anyone have any idea
> how I can get around this? Seems luabind's using SmartPtr<const A>*
> when I'd rather it just used SmartPtr<A>* and that's causing the
> compiler to generate the errors.

It looks like your RefCounted reference counter isn't mutable.
RefCounted::removeReference() has to be const.

--
Arvid Norberg




-------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc. Do you grep through log files
for problems?  Stop!  Download the new AJAX search engine that makes
searching your log files as easy as surfing the  web.  DOWNLOAD SPLUNK!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=103432&bid=230486&dat=121642
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user