[C++-sig] Boost v2 questions

David Abrahams dave at boost-consulting.com
Mon Aug 19 14:28:23 CEST 2002

From: "Stefan Franke" <franke at ableton.com>

> I have an application where I want to enable the user to write
> extension scripts which can access parts of the document. Think
> of a vector drawing program with graphical object the user can
> manipulate and assign properties to. Consider the following
> hypothetical script using two wrapped C++ classes Canvas and
> Shape:
> s = canvas().getShapeByName("myshape")
> # canvas() returns a CanvasWrapper instance
> # s is of class ShapeWrapper
> s.x = 100         # x: member of C++ class as a Python attribute
> s.mydata = [1, 2] # User assigned Pyton data
> canvas().deleteShape("myshape")
> print s.x             # Access C++ member
> Traceback (most recent call last):
>   File "<interactive input>", line 1, in ?
> ReferenceError: Cannot access property 'x' of object 's'.

I can't tell from the above whether you're illustrating a problem, desired
behavior, or something else. Also, since I can't see your C++ code I can't
tell how you got there.

> Here my questions:
> 1. Is it possible that an instace of the Boost-generated class
> ShapeWrapper has an instance dictionary that the user can assign
> values to?

Not only possible, but true.

> 2. The C++ function Canvas::deleteShape() deletes the shape. This
> is existing functionality that can hardly be changed. I'd like to
> have weak referencing behaviour for almost all of my wrapped objects.
> There already exists a weak pointer type for my C++ classes that I
> can easily extend.
> Is it possible to instruct Boost not to hold a plain pointer to the
> C++ instance inside its wrapper objects, but one of my smart
> pointers?

Sure. Have you looked at

You can simply specify your own smart pointer as the HeldType parameter,
and specialize the pointee<> template appropriately (sorry, no docs - see

> And further, is it possible to get a Python exception whenever a
> member function of a dangling object is called? I envision to put the
> checks into the dereferencing operators of my smart pointer type and
> then throwing a C++ exception to signal to Boost that the operation
> has failed.

Perfect. Just install a Python exception translator for your C++ exception
as neccessary:


> Are my desires too far-fetched? OTOH I'd say this behaviour is pretty
> standard whenever you want to enable user scripting for an existing
> application.
> Or am I missing something obvious and there's an easier way to
> achieve the same thing?

Easier than what?

> Note that I neither want to check object
> validity on the Python side (user scripting should be as easy as
> possible) nor write dedicated C++ classes like "ShapeAccessor" or
> "CanvasAccessor" that implement the validity checks (since there are
> just too many classes to wrap and I'd loose one of Boosts major
> advantage then).

Let me know if this has failed to help you.

           David Abrahams * Boost Consulting
dave at boost-consulting.com * http://www.boost-consulting.com

More information about the Cplusplus-sig mailing list