[C++-sig] wrapping complex attributes

Mike Rovner mike at bindkey.com
Wed Nov 13 00:08:16 CET 2002

Here is another dark corner:

I have a complex C++ object:

class Heavy {
  void SetX(const char *name, const T1& val);
  T1 GetX(const char *name);
  void DelX(const char *name);
  Iter<T1> EnumX();

  void SetY(const char *name, const T2& val);
  T2 GetY(const char *name);
  void DelY(const char *name);
  Iter<T2> EnumY();

  void SetZ(const char *name, const T3& val);
  T3 GetZ(const char *name);
  void DelZ(const char *name);
  Iter<T3> EnumZ();

It seems natural to expose class Heavy with 3 attributes: x,y and z which
will behave like dictionaries.
How to do that?

I think in favor of following approach, but can't follow it to the finest

Create a helper object:

struct helper<R, Heavy::*pm> {
  static R create(Heavy& o) { return (o.*pm)(arg); }

Than create a helper class(es) ala dictionary:

helper_z =
  .def("__setattr__", helper<void,&Heavy::SetZ>::create)
  .def("__getattr__", helper<T3,&Heavy::GetZ>::create)
  .def("__delattr__", helper<void,&Heavy::DelZ>::create)
  .def("__iter__", helper<Iter<T3>,&Heavy::EnumZ>::create)

and expose them:

  .def_readwrite("x", helper_x)
  .def_readwrite("y", helper_y)
  .def_readwrite("z", helper_z)

Is it a right approach and if so, how to refine it?


More information about the Cplusplus-sig mailing list