Boost logo

Boost :

From: christopher diggins (cdiggins_at_[hidden])
Date: 2005-02-02 01:08:15


I notice that member functions for many classes can be divided up into a
minimal set, and derivable functions. For instance:

class MyClass {
  void Fu() { cout << "fu"; }
  void Bar() { cout << "bar"; }
  void FuBar() { Fu(); Bar(); }
}

Clearly a minimal interface for representing FuBar would be:

BOOST_IDL_BEGIN(IMyInterface)
  BOOST_IDL_FN0(Fu, void)
  BOOST_IDL_FN1(Bar, void)
BOOST_IDL_END(IMyInterface)

Using IMyInterface in our code instead of IMyInterface is more generic, and
reduces coupling, but we lose the syntactic pleasantries of FuBar().

In order to achieve this effect I am currently using an extension class (or
veneer if you prefer):

template<typename Base_T>
class MyClass_ext : public Base_T {
  MyClass_ext() : T() { };
  template<typename T>
  MyClass_ext(const T& x) : T(x) { };
  void FuBar() { T::Fu(); T::Bar(); }
  // ... assignment operators etc.
}

I can then write a new extended interface as follows:

typedef MyClass_ext<IMyInterface> IMyInterface2;

And of course I would also rewrite the original class, to avoid code
redundancy:

class MyClass_base {
  void Fu() { cout << "fu"; }
  void Bar() { cout << "bar"; }
}

typedef MyClass_ext<MyClass_base> MyClass;

I don't how solid this technique is for real code. My terminology and naming
convention also leaves me disatisfied. Any comments or suggestions?

Christopher Diggins
Object Oriented Template Library (OOTL)
http://www.ootl.org


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk