data_members.qbk 4.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. [section boost/python/data_members.hpp]
  2. [section Introduction]
  3. `make_getter()` and `make_setter()` are the functions used internally by [link high_level_components.boost_python_class_hpp.class_template_class_t_bases_hel.class_template_class_modifier_fu `class_<>::def_readonly`] and [link high_level_components.boost_python_class_hpp.class_template_class_t_bases_hel.class_template_class_modifier_fu `class_<>::def_readwrite`] to produce Python callable objects which wrap C++ data members.
  4. [endsect]
  5. [section Functions]
  6. ``
  7. template <class C, class D>
  8. object make_getter(D C::*pm);
  9. template <class C, class D, class Policies>
  10. object make_getter(D C::*pm, Policies const& policies);
  11. ``
  12. [variablelist
  13. [[Requires][Policies is a model of [link concepts.callpolicies `CallPolicies`].]]
  14. [[Effects][Creates a Python callable object which accepts a single argument that can be converted from_python to C*, and returns the corresponding member D member of the C object, converted to_python. If policies is supplied, it will be applied to the function as described here. Otherwise, the library attempts to determine whether D is a user-defined class type, and if so uses return_internal_reference<>
  15. for Policies. Note that this test may inappropriately choose return_internal_reference<> in some cases when D is a smart pointer type. This is a known defect.]]
  16. [[Returns][An instance of object which holds the new Python callable object.]]
  17. ]
  18. ``
  19. template <class D>
  20. object make_getter(D const& d);
  21. template <class D, class Policies>
  22. object make_getter(D const& d, Policies const& policies);
  23. template <class D>
  24. object make_getter(D const* p);
  25. template <class D, class Policies>
  26. object make_getter(D const* p, Policies const& policies);
  27. ``
  28. [variablelist
  29. [[Requires][Policies is a model of CallPolicies.]]
  30. [[Effects][Creates a Python callable object which accepts no arguments and returns d or *p, converted to_python on demand. If policies is supplied, it will be applied to the function as described here. Otherwise, the library attempts to determine whether D is a user-defined class type, and if so uses reference_existing_object for Policies.]]
  31. [[Returns][An instance of object which holds the new Python callable object.]]
  32. ]
  33. ``
  34. template <class C, class D>
  35. object make_setter(D C::*pm);
  36. template <class C, class D, class Policies>
  37. object make_setter(D C::*pm, Policies const& policies);
  38. ``
  39. [variablelist
  40. [[Requires][Policies is a model of CallPolicies.]]
  41. [[Effects][Creates a Python callable object which, when called from Python, expects two arguments which can be converted from_python to C* and D const&, respectively, and sets the corresponding D member of the C object. If policies is supplied, it will be applied to the function as described here.]]
  42. [[Returns][An instance of object which holds the new Python callable object.]]
  43. ]
  44. ``
  45. template <class D>
  46. object make_setter(D& d);
  47. template <class D, class Policies>
  48. object make_setter(D& d, Policies const& policies);
  49. template <class D>
  50. object make_setter(D* p);
  51. template <class D, class Policies>
  52. object make_setter(D* p, Policies const& policies);
  53. ``
  54. [variablelist
  55. [[Requires][Policies is a model of CallPolicies.]]
  56. [[Effects][Creates a Python callable object which accepts one argument, which is converted from Python to D const& and written into d or *p, respectively. If policies is supplied, it will be applied to the function as described here.]]
  57. [[Returns][An instance of object which holds the new Python callable object.]]
  58. ]
  59. [endsect]
  60. [section Example]
  61. The code below uses make_getter and make_setter to expose a data member as functions:
  62. ``
  63. #include <boost/python/data_members.hpp>
  64. #include <boost/python/module.hpp>
  65. #include <boost/python/class.hpp>
  66. struct X
  67. {
  68. X(int x) : y(x) {}
  69. int y;
  70. };
  71. using namespace boost::python;
  72. BOOST_PYTHON_MODULE_INIT(data_members_example)
  73. {
  74. class_<X>("X", init<int>())
  75. .def("get", make_getter(&X::y))
  76. .def("set", make_setter(&X::y))
  77. ;
  78. }
  79. ``
  80. It can be used this way in Python:
  81. ``
  82. >>> from data_members_example import *
  83. >>> x = X(1)
  84. >>> x.get()
  85. 1
  86. >>> x.set(2)
  87. >>> x.get()
  88. 2
  89. ``
  90. [endsect]
  91. [endsect]