Quantcast

Py++ and a std::vector of shared_ptrs

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Py++ and a std::vector of shared_ptrs

Joseph Lisee-2
Hello,

I am trying to wrap a std::vector<boost::shared_ptr<MyType> >, and Py++ will
have none of it.  I am using indexing suite version 2, and telling Py++ to right
the module as multiple files.  I exclude everything at the global namespace
level then I find the namespace_t that corresponds to the "sample" namespace and
 include all of it.  When I attempt to compile the resulting files I get the
following error:

Here is the compile error (with lots of "instantiated here", before this):
/opt/ram/local/include/boost-1_34_1/boost/python/suite/indexing/value_traits.hpp
:56:
error: no match for ‘operator<’ in ‘((const
boost::shared_ptr<samples::A>*)p1)->boost::shared_ptr<T>::operator* [with T =
samples::A]() < ((const
boost::shared_ptr<samples::A>*)p2)->boost::shared_ptr<T>::operator* [with T =
samples::A]()’
scons: ***
[build/wrappers/samples/generated/vector_less_boost_scope_shared_ptr_less_sample
s_scope_A_grate__comma_std_scope_allocator_less_boost_scope_shared_ptr_less_sam
ples_scope_A_grate___grate___grate_.pypp.os]
Error 1

That error appears only why I try to wrap the "funcVectorShared" function,
without that function the code compile and works fine.

Here is the code the generates it:

SharedPtrVector.h:

#ifndef RAM_TEST_SHAREDPTRVECTOR_H
#define RAM_TEST_SHAREDPTRVECTOR_H

// STD Includes
#include <vector>

// Library Includes
#include <boost/shared_ptr.hpp>

namespace samples
{

class A
{
};

boost::shared_ptr<A> func();

std::vector<A> funcVector();
   
std::vector<boost::shared_ptr<A> > funcVectorShared();

}

#endif // RAM_TEST_SHAREDPTRVECTOR_H


SharedPtrVector.cpp:

#include "include/SharedPtrVector.h"

namespace samples
{

boost::shared_ptr<A> func()
{
    return boost::shared_ptr<A>(new A());
}

std::vector<A> funcVector()
{
    std::vector<A> items;
    items.push_back(A());
    items.push_back(A());
    return items;
}
   
std::vector<boost::shared_ptr<A> > funcVectorShared()
{
    std::vector<boost::shared_ptr<A> > items;
    items.push_back(boost::shared_ptr<A>(new A()));
    items.push_back(boost::shared_ptr<A>(new A()));
    return items;
}

}

_______________________________________________
C++-sig mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/c++-sig
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Py++ and a std::vector of shared_ptrs

Roman Yakovenko
On Nov 21, 2007 8:19 PM, Joseph Lisee <[hidden email]> wrote:
Hello,

I am trying to wrap a std::vector<boost::shared_ptr<MyType> >, and Py++ will
have none of it.  I am using indexing suite version 2, and telling Py++ to right
the module as multiple files.  I exclude everything at the global namespace
level then I find the namespace_t that corresponds to the "sample" namespace and
 include all of it.  When I attempt to compile the resulting files I get the
following error:

Here is the compile error (with lots of "instantiated here", before this):
/opt/ram/local/include/boost-1_34_1/boost/python/suite/indexing/value_traits.hpp
:56:
error: no match for 'operator<' in '((const
boost::shared_ptr<samples::A>*)p1)->boost::shared_ptr<T>::operator* [with T =
samples::A]() < ((const
boost::shared_ptr<samples::A>*)p2)->boost::shared_ptr<T>::operator* [with T =
samples::A]()'
scons: ***
[build/wrappers/samples/generated/vector_less_boost_scope_shared_ptr_less_sample
s_scope_A_grate__comma_std_scope_allocator_less_boost_scope_shared_ptr_less_sam
ples_scope_A_grate___grate___grate_.pypp.os]
Error 1

Py++ is not as smart as it could be :-). The root of the problem is class A, it doesn't defines operator== and operator<. On the other side indexing suite v2 is smart enough to understand, that container contains pointers, so it will not compare pointers but pointee.

I was not aware to this functionality so didn't implemented it.

Solution:
0. to create patch to Py++ to support this functionality :-)))
or
1. add comparison operators to the class ( you can use free operators too )
or
2. disable methods, which require  compare operators:


        f = mb.free_fun( 'funcVectorShared' )
        v = declarations.remove_declarated( f.return_type )
        v.indexing_suite.disable_method( 'sort' )
        v.indexing_suite.disable_method( 'count' )
        v.indexing_suite.disable_method( 'index' )
        v.indexing_suite.disable_method( 'contains' )

HTH

--
Roman Yakovenko
C++ Python language binding
http://www.language-binding.net/
_______________________________________________
C++-sig mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/c++-sig
Loading...