init.qbk 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. [section boost/python/init.hpp]
  2. [section Introduction]
  3. <boost/python/init.hpp> defines the interface for exposing C++ constructors to Python as extension class `__init__` functions.
  4. [section init-expressions]
  5. An init-expression is used to describe a family of `__init__` methods to be generated for an extension class, and the result has the following properties:
  6. [variablelist
  7. [[docstring][An [link ntbs] whose value will bound to the method's `__doc__` attribute]]
  8. [[keywords][A [link function_invocation_and_creation.boost_python_args_hpp.introduction.keyword_expressions keyword-expression] which will be used to name (a trailing subsequence of) the arguments to the generated `__init__` function(s).]]
  9. [[call_policies][An instance of a model of [link concepts.callpolicies CallPolicies].]]
  10. [[argument_types][An MPL sequence of C++ argument types which will be used to construct the wrapped C++ object. An init expression has one or more valid prefixes which are given by a sequence of prefixes of its argument types.]]
  11. ]
  12. [endsect]
  13. [endsect]
  14. [section Class template `init`]
  15. A MPL sequence which can be used to specify a family of one or more __init__ functions. Only the last Ti supplied may be an instantiation of optional<...>.
  16. ``
  17. namespace boost { namespace python
  18. {
  19. template <T1 = unspecified,...Tn = unspecified>
  20. struct init
  21. {
  22. init(char const* doc = 0);
  23. template <class Keywords> init(Keywords const& kw, char const* doc = 0);
  24. template <class Keywords> init(char const* doc, Keywords const& kw);
  25. template <class CallPolicies>
  26. unspecified operator[](CallPolicies const& policies) const
  27. };
  28. }}
  29. ``
  30. [section Class template `init` constructors]
  31. ``
  32. init(char const* doc = 0);
  33. template <class Keywords> init(Keywords const& kw, char const* doc = 0);
  34. template <class Keywords> init(char const* doc, Keywords const& kw);
  35. ``
  36. [variablelist
  37. [[Requires][If supplied, doc is an [link ntbs]. If supplied, kw is the result of a ]]
  38. [[Effects][The result is an init-expression whose docstring is doc and whose keywords are a reference to kw. If the first form is used, the resulting expression's keywords are empty. The expression's call policies are an instance of [link function_invocation_and_creation.models_of_callpolicies.boost_python_default_call_polici default_call_policies]. If Tn is [link high_level_components.boost_python_init_hpp.class_template_optional optional<U1, U2,... Um>], the expression's valid prefixes are given by: ``(T1, T2,...Tn-1), (T1, T2,...Tn-1 , U1), (T1, T2,...Tn-1 , U1, U2), ...(T1, T2,...Tn-1 , U1, U2,...Um)``.
  39. Otherwise, the expression has one valid prefix given by the template arguments the user specified. ]]
  40. ]
  41. [endsect]
  42. [section Class template `init` observer functions]
  43. ``
  44. template <class Policies>
  45. unspecified operator[](Policies const& policies) const
  46. ``
  47. [variablelist
  48. [[Requires][Policies is a model of [link concepts.callpolicies CallPolicies].]]
  49. [[Effects][Returns a new [link high_level_components.boost_python_init_hpp.introduction.init_expressions init-expression] with all the same properties as the init object except that its call policies are replaced by a reference to policies.]]
  50. ]
  51. [endsect]
  52. [endsect]
  53. [section Class template `optional` ]
  54. A MPL sequence which can be used to specify the optional arguments to an __init__ function.
  55. ``
  56. namespace boost { namespace python
  57. {
  58. template <T1 = unspecified,...Tn = unspecified>
  59. struct optional {};
  60. }}
  61. ``
  62. [endsect]
  63. [section Example]
  64. Given the C++ declarations:
  65. ``
  66. class Y;
  67. class X
  68. {
  69. public:
  70. X(int x, Y* y) : m_y(y) {}
  71. X(double);
  72. private:
  73. Y* m_y;
  74. };
  75. ``
  76. A corresponing Boost.Python extension class can be created with:
  77. ``
  78. using namespace boost::python;
  79. class_<X>("X", "This is X's docstring.",
  80. init<int,char const*>(args("x","y"), "X.__init__'s docstring")[
  81. with_custodian_and_ward<1,3>()]
  82. )
  83. .def(init<double>())
  84. ;
  85. ``
  86. [endsect]
  87. [endsect]