Re: [C++-sig] Problem with map_indexing_suite

2010-07-12 Thread Pentix

Hi, Damien!

I've got exactly the same problem... Have you got any achievements?



Damien Dupuis wrote:
> 
> error: no match for call to ‘(const
> boost::python::detail::specify_a_return_value_policy_to_wrap_functions_returning)
> (X*)‘
> 

-- 
View this message in context: 
http://old.nabble.com/Problem-with-map_indexing_suite-tp28925347p29135982.html
Sent from the Python - c++-sig mailing list archive at Nabble.com.

___
Cplusplus-sig mailing list
[email protected]
http://mail.python.org/mailman/listinfo/cplusplus-sig

Re: [C++-sig] Problem with map_indexing_suite

2010-07-12 Thread John Reid

Pentix wrote:

Hi, Damien!

I've got exactly the same problem... Have you got any achievements?


Have a look at:
http://www.boost.org/doc/libs/1_43_0/libs/python/doc/tutorial/doc/html/python/functions.html#python.call_policies





Damien Dupuis wrote:

error: no match for call to ‘(const
boost::python::detail::specify_a_return_value_policy_to_wrap_functions_returning)
(X*)‘





___
Cplusplus-sig mailing list
[email protected]
http://mail.python.org/mailman/listinfo/cplusplus-sig

Re: [C++-sig] Problem with map_indexing_suite

2010-07-12 Thread John Reid



Damien Dupuis wrote:

I'm trying to wrap a whole C++ that contains a lot of access to std::vector and 
std::map.

I managed to wrap vectors but i've got problems with maps.

The following simple example fails to compile with the error:
error: no match for call to ‘(const 
boost::python::detail::specify_a_return_value_policy_to_wrap_functions_returning)
 (X*)‘


#include 
#include 
#include 
using namespace boost::python;

class X {
public:
X(std::string s): _s(s) {}
std::string const repr() { return _s; }

private:
std::string _s;
};

class Y {
public:
Y(): _vec(), _map() {};
void addToVec(X* x){ _vec.push_back(x); }
void addToMap(int i, X* x) { _map[i] = x;   }
const std::vector& getXVec()   { return _vec;   }
const std::map& getXMap() { return _map; }

private:
std::vector   _vec;
std::map _map;
};

BOOST_PYTHON_MODULE(pyTEST) {
class_("X", init())
.def("__repr__", &X::repr)
;

class_ >("XVec")
.def(vector_indexing_suite, true>())
;

class_ >("XMap")
.def(map_indexing_suite, true>())
;

class_("Y", init<>())
.def("addToVec", &Y::addToVec)
.def("addToMap", &Y::addToMap)
.def("getXVec" , &Y::getXVec , return_internal_reference<>())
.def("getXMap" , &Y::getXMap , return_internal_reference<>())
;
}



I tried to use boost::shared_ptr instead of X* in this small example and it 
works, but I would like not to have to correct all my existing C++ code to use 
boost::shared_ptr.
And since it works well with std::vector, I think it should work with maps.

Some google search make me think I should add a return_value_policy to the 
__getitem__ method of std::map class, but when I try I get the error
error: address of overloaded function with no contextual type information
I'm clearly doing something wrong here.


That sounds like the right idea. Are you taking the address of an 
overloaded function? In which case the compiler might not know which 
overload to use.



___
Cplusplus-sig mailing list
[email protected]
http://mail.python.org/mailman/listinfo/cplusplus-sig


Re: [C++-sig] Problem with map_indexing_suite

2010-07-12 Thread Damien Dupuis
Since I didn't find any solution when using map_indexing_suite, I wrote a 
simple wrapping that works in my case :

==
#include 
#include 

namespace myProject {
template
struct map_item {
typedef std::map Map;

static Val get(Map & self, const Key idx) {
  if (self.find(idx) == self.end()) {
  PyErr_SetString(PyExc_KeyError,"Map key not found");
  throw_error_already_set();
  }
  return self[idx];
}

static void set(Map& self, const Key idx, const Val val) { self[idx]=val; }
static void del(Map& self, const Key n)  { self.erase(n); }
static bool in (Map const& self, const Key n){ return 
self.find(n) != self.end(); }

static list keys(Map const& self) {
list t;
for(typename Map::const_iterator it = self.begin() ; it!=self.end() ; 
++it)
t.append(it->first);
return t;
}

static list values(Map const& self) {
list t;
for(typename Map::const_iterator it=self.begin(); it!=self.end(); ++it)
t.append(it->second);
return t;
}

static list items(Map const& self) {
list t;
for(typename Map::const_iterator it=self.begin(); it!=self.end(); ++it)
t.append( make_tuple(it->first, it->second) );
return t;
}
};

#define STL_MAP_WRAPPING_PTR(KEY_TYPE, VALUE_TYPE, PYTHON_TYPE_NAME)
   \
class_ 
>((std::string(PYTHON_TYPE_NAME)+std::string("DATA")).c_str()) \
.def_readonly ("key"  , &std::pair::first ) 
\
.def_readwrite("value", &std::pair::second) 
\
;  \
class_ >(PYTHON_TYPE_NAME)  \
.def("__len__" , &std::map::size)\
.def("__iter__", boost::python::iterator, return_internal_reference<> >()) \
.def("__getitem__" , &map_item().get, 
return_internal_reference<>()) \
.def("__setitem__" , &map_item().set   ) \
.def("__delitem__" , &map_item().del   ) \
.def("__contains__", &map_item().in) \
.def("clear"   , &std::map::clear  ) \
.def("has_key" , &map_item().in) \
.def("keys", &map_item().keys  ) \
.def("values"  , &map_item().values) \
.def("items"   , &map_item().items ) \
;

#define STL_MAP_WRAPPING(KEY_TYPE, VALUE_TYPE, PYTHON_TYPE_NAME)   \
class_ 
>((std::string(PYTHON_TYPE_NAME)+std::string("DATA")).c_str()) \
.def_readonly ("key"  , &std::pair::first ) 
\
.def_readwrite("value", &std::pair::second) 
\
;  \
class_ >(PYTHON_TYPE_NAME)  \
.def("__len__" , &std::map::size)\
.def("__iter__", boost::python::iterator, return_internal_reference<> >()) \
.def("__getitem__" , &map_item().get   ) \
.def("__setitem__" , &map_item().set   ) \
.def("__delitem__" , &map_item().del   ) \
.def("__contains__", &map_item().in) \
.def("clear"   , &std::map::clear  ) \
.def("has_key" , &map_item().in) \
.def("keys", &map_item().keys  ) \
.def("values"  , &map_item().values) \
.def("items"   , &map_item().items ) \
;

} // namespace
=



Simply use STL_MAP_WRAPPING_PTR(KeyClass, ClassA*, "ClassAMap") to bind a 
map
or STL_MAP_WRAPPING(KeyClass, ClassB, "ClassBMap") to bind a map

This code works in my case, fell free to correct or complete any error / 
missing.

---
Damien Dupuis

Le 12 juil. 2010 à 09:13, Pentix a écrit :

> 
> Hi, Damien!
> 
> I've got exactly the same problem... Have you got any achievements?
> 
> 
> 
> Damien Dupuis wrote:
>> 
>> error: no match for call to ‘(const
>> boost::python::detail::specify_a_return_value_policy_to_wrap_functions_returning)
>> (X*)‘
>> 
> 
> -- 
> View this message in context: 
> http://old.nabble.com/Problem-with-map_indexing_suite-tp28925347p29135982.html
> Sent from the Python - c++-sig mailing list archive at Nabble.com.
> 
> ___
> Cplusplus-sig mailing list
> [email protected]
> http://mail.python.org/mailman/listinfo/cplusplus-sig
> 








___
Cplusplus-sig mailing list
[email protected]
http://mail.python.org/mailman/listinfo/cplusplus-sig