staticmethod.cpp 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. // Copyright David Abrahams 2002.
  2. // Distributed under the Boost Software License, Version 1.0. (See
  3. // accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. #include <boost/python/class.hpp>
  6. #include <boost/python/module.hpp>
  7. #include <boost/python/def.hpp>
  8. #include <boost/python/call_method.hpp>
  9. #include <boost/ref.hpp>
  10. #include <boost/utility.hpp>
  11. #define BOOST_ENABLE_ASSERT_HANDLER
  12. #include <boost/assert.hpp>
  13. using namespace boost::python;
  14. struct X
  15. {
  16. explicit X(int x) : x(x), magic(7654321) { ++counter; }
  17. X(X const& rhs) : x(rhs.x), magic(7654321) { ++counter; }
  18. virtual ~X() { BOOST_ASSERT(magic == 7654321); magic = 6666666; x = 9999; --counter; }
  19. void set(int _x) { BOOST_ASSERT(magic == 7654321); this->x = _x; }
  20. int value() const { BOOST_ASSERT(magic == 7654321); return x; }
  21. static int count() { return counter; }
  22. private:
  23. void operator=(X const&);
  24. private:
  25. int x;
  26. long magic;
  27. static int counter;
  28. };
  29. int X::counter;
  30. int getXmagic(){return 7654321;}
  31. BOOST_PYTHON_MODULE(staticmethod_ext)
  32. {
  33. class_<X>("X", init<int>())
  34. .def("value", &X::value)
  35. .def("set", &X::set)
  36. .def("count", &X::count)
  37. .staticmethod("count")
  38. .def("magic", &getXmagic)
  39. .staticmethod("magic")
  40. ;
  41. }
  42. #include "module_tail.cpp"