Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 21st, 2012  |  syntax: None  |  size: 2.21 KB  |  hits: 14  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. boost::python and set::erase -> weird behaviour
  2. #include <set>
  3. #include <iostream>
  4.  
  5. #include <boost/shared_ptr.hpp>
  6. #include <boost/python.hpp>
  7.  
  8. using namespace std;
  9. using namespace boost;
  10. using namespace boost::python;
  11.  
  12. struct Bar
  13. {
  14.     Bar() {}
  15. };
  16.  
  17. struct Foo
  18. {
  19.     set< shared_ptr<Bar> > v_set;
  20.     shared_ptr<Bar> v_ptr;
  21.  
  22.     Foo() {}
  23.  
  24.     void add( shared_ptr<Bar> v_param ) {
  25.     cout << "storing " << v_param << "in v_set and v_ptr" << endl;
  26.     v_set.insert(v_param);
  27.     v_ptr = v_param;
  28.  
  29.     }
  30.  
  31.     void del( shared_ptr<Bar> v_param ) {
  32.     cout << "deleting " << v_param << endl;
  33.     if (v_param == v_ptr) {
  34.         cout << "v_param == v_ptr" << endl;
  35.     } else {
  36.         cout << "v_param != v_ptr" << endl;
  37.     }
  38.  
  39.     cout << "erasing from v_set using v_param" << endl;
  40.     if (v_set.erase(v_param) == 0) {
  41.         cout << "didn't erase anything" << endl;
  42.     } else {
  43.         cout << "erased !" << endl;
  44.     }
  45.  
  46.     cout << "erasing from v_set using v_ptr" << endl;
  47.     if (v_set.erase(v_ptr) == 0) {
  48.         cout << "didn't erase anything" << endl;
  49.     } else {
  50.         cout << "erased !" << endl;
  51.     }
  52.     }
  53. };
  54.  
  55. BOOST_PYTHON_MODULE (test)
  56. {
  57.     class_< Foo, shared_ptr<Foo> >("Foo")
  58.         .def("add",&Foo::add)
  59.         .def("remove",&Foo::del);
  60.  
  61.     class_< Bar, shared_ptr<Bar> >("Bar");    
  62. }
  63.        
  64. %> 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
  65.  
  66. %> 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
  67.        
  68. from test import *
  69.  
  70. f = Foo()
  71. b = Bar()
  72.  
  73. f.add(b)
  74.  
  75. f.remove(b)
  76.        
  77. storing 0x8c8bc58in v_set and v_ptr
  78. deleting 0x8c8bc58
  79. v_param == v_ptr
  80. erasing from v_set using v_param
  81. didn't erase anything
  82. erasing from v_set using v_ptr
  83. erased !
  84.        
  85. assert(!(v_param < v_ptr));
  86. assert(!(v_ptr < v_param));
  87.        
  88. template<class T> inline T * get_pointer(std::shared_ptr<T> const & p)
  89. {
  90.     return p.get();
  91. }
  92.        
  93. template <typename T>
  94. struct SmartComparator
  95. {
  96.     bool operator()(shared_ptr<T> const& lhs, shared_ptr<T> const& rhs) {
  97.         return lhs.get() < rhs.get();
  98.     }
  99. };
  100.        
  101. set< shared_ptr<Bar>, SmartComparator<Bar> > v_set;