data_members.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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/return_value_policy.hpp>
  8. #include <boost/python/return_by_value.hpp>
  9. #include "test_class.hpp"
  10. #if defined(_AIX) && defined(__EDG_VERSION__) && __EDG_VERSION__ < 245
  11. # include <iostream> // works around a KCC intermediate code generation bug
  12. #endif
  13. using namespace boost::python;
  14. typedef test_class<> X;
  15. struct Y : test_class<1>
  16. {
  17. Y(int v) : test_class<1>(v) {}
  18. Y& operator=(Y const& rhs) { x = rhs.x; return *this; }
  19. bool q;
  20. };
  21. double get_fair_value(X const& x) { return x.value(); }
  22. struct VarBase
  23. {
  24. VarBase(std::string name_) : name(name_) {}
  25. std::string const name;
  26. std::string get_name1() const { return name; }
  27. };
  28. struct Var : VarBase
  29. {
  30. Var(std::string name_) : VarBase(name_), value(), name2(name.c_str()), y(6) {}
  31. std::string const& get_name2() const { return name; }
  32. float value;
  33. char const* name2;
  34. Y y;
  35. static int static1;
  36. static Y static2;
  37. };
  38. int Var::static1 = 0;
  39. Y Var::static2(0);
  40. // Compilability regression tests
  41. namespace boost_python_test
  42. {
  43. struct trivial
  44. {
  45. trivial() : value(123) {}
  46. double value;
  47. };
  48. struct Color3
  49. {
  50. static const Color3 black;
  51. };
  52. const Color3 Color3::black
  53. #if !BOOST_WORKAROUND(BOOST_MSVC, <= 1300)
  54. = {}
  55. #endif
  56. ;
  57. void compilability_test()
  58. {
  59. class_<trivial>("trivial")
  60. .add_property("property", make_getter(&trivial::value, return_value_policy<return_by_value>()))
  61. .def_readonly("readonly", &trivial::value)
  62. ;
  63. class_< Color3 >("Color3", init< const Color3 & >())
  64. .def_readonly("BLACK", &Color3::black) // line 17
  65. ;
  66. }
  67. }
  68. BOOST_PYTHON_MODULE(data_members_ext)
  69. {
  70. using namespace boost_python_test;
  71. class_<X>("X", init<int>())
  72. .def("value", &X::value)
  73. .def("set", &X::set)
  74. .def_readonly("x", &X::x)
  75. .add_property("fair_value", get_fair_value)
  76. ;
  77. class_<Y>("Y", init<int>())
  78. .def("value", &Y::value)
  79. .def("set", &Y::set)
  80. .def_readwrite("x", &Y::x)
  81. .def_readwrite("q", &Y::q)
  82. ;
  83. class_<Var>("Var", init<std::string>())
  84. .def_readonly("name", &Var::name)
  85. .def_readonly("name2", &Var::name2)
  86. .def_readwrite("value", &Var::value)
  87. .def_readonly("y", &Var::y)
  88. // Test return_by_value for plain values and for
  89. // pointers... return_by_value was implemented as a
  90. // side-effect of implementing data member support, so it made
  91. // sense to add the test here.
  92. .def("get_name1", &Var::get_name1, return_value_policy<return_by_value>())
  93. .def("get_name2", &Var::get_name2, return_value_policy<return_by_value>())
  94. .add_property("name3", &Var::get_name1)
  95. // Test static data members
  96. .def_readonly("ro1a", &Var::static1)
  97. .def_readonly("ro1b", Var::static1)
  98. .def_readwrite("rw1a", &Var::static1)
  99. .def_readwrite("rw1b", Var::static1)
  100. .def_readonly("ro2a", &Var::static2)
  101. .def_readonly("ro2b", Var::static2)
  102. .def_readwrite("rw2a", &Var::static2)
  103. .def_readwrite("rw2b", Var::static2)
  104. ;
  105. }
  106. #include "module_tail.cpp"