Differences between revisions 2 and 3
Revision 2 as of 2003-01-04 04:18:05
Size: 1444
Editor: MikeRovner
Comment:
Revision 3 as of 2003-01-04 04:20:36
Size: 1505
Editor: MikeRovner
Comment:
Deletions are marked like this. Additions are marked like this.
Line 37: Line 37:
Note:: ''interpreter()'' is to be added about now. [[Date]]

How to expose...

static class data members

object x_class 
    = class_<X>("X") 
         .def( ... ) 
         ... 
         ; 
 
x_class.attr("fu") = X::fu; 
x_class.attr("bar") = X::bar; 
... 

module level objects

at module creation time

First, create those objects like

object class_X = class_<X>("X");
object x = class_X();

Second, expose them:

scope().attr("x") = x; // injects x into current scope

By default current scope is module.

at run-time

Use a function:

template <class T>
void set(const std::string& name, const T& value) {
  interpreter()->mainmodule()[name] = value;
}

Note:: interpreter() is to be added about now. Date

mutable C++ object

Perhaps you'd like the resulting Python object to contain a raw pointer to the argument? In that case, the caveat is that if the lifetime of the C++ object ends before that of the Python object, that pointer will dangle and using the Python object may cause a crash.

There is a way to do that, but it's more convoluted than it should be:

  template <class T>
  T& identity(T& x)
  {
      return x;
  }

  template <class T>
  object get_object_reference(T& x)
  {
      // build a function object around identity
      object f
          = make_function(
                  &identity<T>, return_value_policy<reference_existing_object>());

      // and call
      return f(x);
  }

boost.python/HowTo (last edited 2015-04-21 17:00:36 by HansDembinski)

Unable to edit the page? See the FrontPage for instructions.