- boost::python and set::erase -> weird behaviour
- #include <set>
- #include <iostream>
- #include <boost/shared_ptr.hpp>
- #include <boost/python.hpp>
- using namespace std;
- using namespace boost;
- using namespace boost::python;
- struct Bar
- {
- Bar() {}
- };
- struct Foo
- {
- set< shared_ptr<Bar> > v_set;
- shared_ptr<Bar> v_ptr;
- Foo() {}
- void add( shared_ptr<Bar> v_param ) {
- cout << "storing " << v_param << "in v_set and v_ptr" << endl;
- v_set.insert(v_param);
- v_ptr = v_param;
- }
- void del( shared_ptr<Bar> v_param ) {
- cout << "deleting " << v_param << endl;
- if (v_param == v_ptr) {
- cout << "v_param == v_ptr" << endl;
- } else {
- cout << "v_param != v_ptr" << endl;
- }
- cout << "erasing from v_set using v_param" << endl;
- if (v_set.erase(v_param) == 0) {
- cout << "didn't erase anything" << endl;
- } else {
- cout << "erased !" << endl;
- }
- cout << "erasing from v_set using v_ptr" << endl;
- if (v_set.erase(v_ptr) == 0) {
- cout << "didn't erase anything" << endl;
- } else {
- cout << "erased !" << endl;
- }
- }
- };
- BOOST_PYTHON_MODULE (test)
- {
- class_< Foo, shared_ptr<Foo> >("Foo")
- .def("add",&Foo::add)
- .def("remove",&Foo::del);
- class_< Bar, shared_ptr<Bar> >("Bar");
- }
- %> gcc -pthread -fno-strict-aliasing -march=i686 -mtune=generic -O2 -pipe -DNDEBUG -march=i686 -mtune=generic -O2 -pipe -fPIC -I/usr/include/python2.7 -c test.cpp -o test.o
- %> g++ -pthread -shared -Wl,--hash-style=gnu -Wl,--as-needed build/temp.linux-i686-2.7/test.o -L/usr/lib -lboost_python -lpython2.7 -o test.so
- from test import *
- f = Foo()
- b = Bar()
- f.add(b)
- f.remove(b)
- storing 0x8c8bc58in v_set and v_ptr
- deleting 0x8c8bc58
- v_param == v_ptr
- erasing from v_set using v_param
- didn't erase anything
- erasing from v_set using v_ptr
- erased !
- assert(!(v_param < v_ptr));
- assert(!(v_ptr < v_param));
- template<class T> inline T * get_pointer(std::shared_ptr<T> const & p)
- {
- return p.get();
- }
- template <typename T>
- struct SmartComparator
- {
- bool operator()(shared_ptr<T> const& lhs, shared_ptr<T> const& rhs) {
- return lhs.get() < rhs.get();
- }
- };
- set< shared_ptr<Bar>, SmartComparator<Bar> > v_set;