def_visitor.qbk 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. [section boost/python/def_visitor.hpp]
  2. [section Introduction]
  3. <boost/python/def_visitor.hpp> provides a generic visitation interface through which the [link high_level_components.boost_python_class_hpp.class_template_class_t_bases_hel class_] def member functionality can be extended non-intrusively to avoid cluttering the [link high_level_components.boost_python_class_hpp.class_template_class_t_bases_hel class_] interface. It declares the `def_visitor<T>` class template, which is parameterized on the derived type `DerivedVisitor`, which provides the actual `def` functionality through its `visit` member functions.
  4. [endsect]
  5. [section Class `def_visitor`]
  6. The class `def_visitor` is a base class paramaterized by its derived class. The `def_visitor` class is a protocol class. Its derived class, DerivedVisitor, is expected to have a member function `visit`. The `def_visitor` class is never instantiated directly. Instead, an instance of its subclass, DerivedVisitor, is passed on as an argument to the [link high_level_components.boost_python_class_hpp.class_template_class_t_bases_hel class_] `def` member function.
  7. ``
  8. namespace boost { namespace python {
  9. template <class DerivedVisitor>
  10. class def_visitor {};
  11. }
  12. ``
  13. [variablelist
  14. [[Requires][The client supplied class DerivedVisitor template parameter is expected to:
  15. * be privately derived from def_visitor
  16. * grant friend access to class def_visitor_access
  17. * define either or both visit member functions listed in the table below:
  18. [table
  19. [[Expression][Return Type][Requirements][Effects]]
  20. [[`visitor.visit(cls)`][`void`]
  21. [`cls` is an instance of a [link high_level_components.boost_python_class_hpp.class_template_class_t_bases_hel class_] being wrapped to Python. `visitor` is a `def_visitor` derived class.]
  22. [A call to `cls.def(visitor)` forwards to this member function.]]
  23. [[`visitor.visit(cls, name, options)`][`void`]
  24. [`cls` is a [link high_level_components.boost_python_class_hpp.class_template_class_t_bases_hel class_] instance, name is a C string. `visitor` is a `def_visitor` derived class. options is a context specific optional argument.]
  25. [A call to `cls.def(name, visitor)` or `cls.def(name, visitor, options)` forwards to this member function. ]]]
  26. ]]
  27. ]
  28. [endsect]
  29. [section Example]
  30. ``
  31. class X {/*...*/};
  32. class my_def_visitor : boost::python::def_visitor<my_def_visitor>
  33. {
  34. friend class def_visitor_access;
  35. template <class classT>
  36. void visit(classT& c) const
  37. {
  38. c.def("foo", &my_def_visitor::foo);
  39. c.def("bar", &my_def_visitor::bar);
  40. }
  41. static void foo(X& self);
  42. static void bar(X& self);
  43. };
  44. BOOST_PYTHON_MODULE(my_ext)
  45. {
  46. class_<X>("X")
  47. .def(my_def_visitor());
  48. }
  49. ``
  50. [endsect]
  51. [endsect]