/Src/Dependencies/Boost/libs/python/test/shared_ptr.cpp

http://hadesmem.googlecode.com/ · C++ · 217 lines · 158 code · 47 blank · 12 comment · 0 complexity · 91b01cc079a6d7b2bb7c253c20e8e1a4 MD5 · raw file

  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/module.hpp>
  6. #include <boost/python/class.hpp>
  7. #include <boost/python/call_method.hpp>
  8. #include <boost/python/extract.hpp>
  9. #include <boost/python/def.hpp>
  10. #include <boost/shared_ptr.hpp>
  11. #include "test_class.hpp"
  12. #include <memory>
  13. using namespace boost::python;
  14. using boost::shared_ptr;
  15. typedef test_class<> X;
  16. typedef test_class<1> Y;
  17. template <class T>
  18. struct functions
  19. {
  20. static int look(shared_ptr<T> const& x)
  21. {
  22. return (x.get()) ? x->value() : -1;
  23. }
  24. static void store(shared_ptr<T> x)
  25. {
  26. storage = x;
  27. }
  28. static void release_store()
  29. {
  30. store(shared_ptr<T>());
  31. }
  32. static void modify(shared_ptr<T>& x)
  33. {
  34. x.reset();
  35. }
  36. static shared_ptr<T> get() { return storage; }
  37. static shared_ptr<T> &get1() { return storage; }
  38. static int look_store()
  39. {
  40. return look(get());
  41. }
  42. template <class C>
  43. static void expose(C const& c)
  44. {
  45. def("look", &look);
  46. def("store", &store);
  47. def("modify", &modify);
  48. def("identity", &identity);
  49. def("null", &null);
  50. const_cast<C&>(c)
  51. .def("look", &look)
  52. .staticmethod("look")
  53. .def("store", &store)
  54. .staticmethod("store")
  55. .def("modify", &modify)
  56. .staticmethod("modify")
  57. .def("look_store", &look_store)
  58. .staticmethod("look_store")
  59. .def("identity", &identity)
  60. .staticmethod("identity")
  61. .def("null", &null)
  62. .staticmethod("null")
  63. .def("get1", &get1, return_internal_reference<>())
  64. .staticmethod("get1")
  65. .def("get", &get)
  66. .staticmethod("get")
  67. .def("count", &T::count)
  68. .staticmethod("count")
  69. .def("release", &release_store)
  70. .staticmethod("release")
  71. ;
  72. }
  73. static shared_ptr<T> identity(shared_ptr<T> x) { return x; }
  74. static shared_ptr<T> null(T const&) { return shared_ptr<T>(); }
  75. static shared_ptr<T> storage;
  76. };
  77. template <class T> shared_ptr<T> functions<T>::storage;
  78. struct Z : test_class<2>
  79. {
  80. Z(int x) : test_class<2>(x) {}
  81. virtual int v() { return this->value(); }
  82. };
  83. struct ZWrap : Z
  84. {
  85. ZWrap(PyObject* self, int x)
  86. : Z(x), m_self(self) {}
  87. virtual int v() { return call_method<int>(m_self, "v"); }
  88. int default_v() { return Z::v(); }
  89. PyObject* m_self;
  90. };
  91. struct YY : Y
  92. {
  93. YY(int n) : Y(n) {}
  94. };
  95. struct YYY : Y
  96. {
  97. YYY(int n) : Y(n) {}
  98. };
  99. shared_ptr<Y> factory(int n)
  100. {
  101. return shared_ptr<Y>(n < 42 ? new Y(n) : new YY(n));
  102. }
  103. // regressions from Nicodemus
  104. struct A
  105. {
  106. virtual ~A() {}; // silence compiler warnings
  107. virtual int f() = 0;
  108. static int call_f(shared_ptr<A>& a) { return a->f(); }
  109. };
  110. struct B: A
  111. {
  112. int f() { return 1; }
  113. };
  114. boost::shared_ptr<A> New(bool make)
  115. {
  116. return boost::shared_ptr<A>( make ? new B() : 0 );
  117. }
  118. struct A_Wrapper: A
  119. {
  120. A_Wrapper(PyObject* self_):
  121. A(), self(self_) {}
  122. int f() {
  123. return call_method< int >(self, "f");
  124. }
  125. PyObject* self;
  126. };
  127. // ------
  128. // from Neal Becker
  129. struct Test {
  130. boost::shared_ptr<X> x;
  131. };
  132. // ------
  133. BOOST_PYTHON_MODULE(shared_ptr_ext)
  134. {
  135. class_<A, boost::shared_ptr<A_Wrapper>, boost::noncopyable>("A")
  136. .def("call_f", &A::call_f)
  137. .staticmethod("call_f")
  138. ;
  139. // This is the ugliness required to register a to-python converter
  140. // for shared_ptr<A>.
  141. objects::class_value_wrapper<
  142. shared_ptr<A>
  143. , objects::make_ptr_instance<A, objects::pointer_holder<shared_ptr<A>,A> >
  144. >();
  145. def("New", &New);
  146. def("factory", factory);
  147. functions<X>::expose(
  148. class_<X, boost::noncopyable>("X", init<int>())
  149. .def("value", &X::value)
  150. );
  151. functions<Y>::expose(
  152. class_<Y, boost::shared_ptr<Y> >("Y", init<int>())
  153. .def("value", &Y::value)
  154. );
  155. class_<YY, bases<Y>, boost::noncopyable>("YY", init<int>())
  156. ;
  157. class_<YYY, shared_ptr<YYY>, bases<Y> >("YYY", init<int>())
  158. ;
  159. functions<Z>::expose(
  160. class_<Z, ZWrap>("Z", init<int>())
  161. .def("value", &Z::value)
  162. .def("v", &Z::v, &ZWrap::default_v)
  163. );
  164. // from Neal Becker
  165. class_<Test> ("Test")
  166. .def_readonly ("x", &Test::x, "x")
  167. ;
  168. // ------
  169. }
  170. #include "module_tail.cpp"