Boost C++ Libraries

...one of the most highly regarded and expertly designed C++ library projects in the world. Herb Sutter and Andrei Alexandrescu, C++ Coding Standards

PrevUpHomeNext

boost/python/def_visitor.hpp

Introduction
Class def_visitor
Example

<boost/python/def_visitor.hpp> provides a generic visitation interface through which the class_ def member functionality can be extended non-intrusively to avoid cluttering the 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.

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 class_ def member function.

namespace boost { namespace python {

    template <class DerivedVisitor>
    class def_visitor {};
}

Requires

The client supplied class DerivedVisitor template parameter is expected to: * be privately derived from def_visitor * grant friend access to class def_visitor_access * define either or both visit member functions listed in the table below:

Expression

Return Type

Requirements

Effects

visitor.visit(cls)

void

cls is an instance of a class_ being wrapped to Python. visitor is a def_visitor derived class.

A call to cls.def(visitor) forwards to this member function.

visitor.visit(cls, name, options)

void

cls is a class_ instance, name is a C string. visitor is a def_visitor derived class. options is a context specific optional argument.

A call to cls.def(name, visitor) or cls.def(name, visitor, options) forwards to this member function.

class X {/*...*/};

class my_def_visitor : boost::python::def_visitor<my_def_visitor>
{
  friend class def_visitor_access;

  template <class classT>
  void visit(classT& c) const
  {
    c.def("foo", &my_def_visitor::foo);
    c.def("bar", &my_def_visitor::bar);
  }

  static void foo(X& self);
  static void bar(X& self);
};

BOOST_PYTHON_MODULE(my_ext)
{
  class_<X>("X")
    .def(my_def_visitor());
}

PrevUpHomeNext