Boost logo

Boost :

From: jkharris01 (john.harris_at_[hidden])
Date: 2002-01-16 14:15:15

Has anyone ever thought up a generic library for implementing
the 'Bridge' pattern, whereby you have a class that is exposed to
users that forwards member functions, usually (but not always) to
same-named virtual functions through a base 'impl' pointer obtained
from a (derived impl) factory?

It seems tedious to write the same function signature three times:
once for the facade (user) class, once for the base impl, and once
for a derived impl. But how do you abstract (or bind together)
member function names?

I've done at least the base part, that deals with the reference
counting of the impl and one or two typedefs, but it's not much:

//! Bridge pattern helper
  IB = type of ImplBase
  IB must define a type, 'factory_type', which is the _base_ factory
  that can create IB's
template <class IB>
class Bridge
        //! the factory that can create IB's
        typedef typename IB::factory_type factory_type;

        IB *tmp_ptr=0;
    virtual ~Bridge() {}
    IB& GetImpl() const {return *impl;}
    void SetImpl(IB* impl_) {impl.reset(impl_);}
    mutable boost::shared_ptr<IB> impl;
}; // end template class Bridge


//! front for HMX-compliant messaging service main class
  Employ the "Bridge" pattern to separate MS from implementation
class MS : public Bridge<MS_ImplBase>
    void Init(const std::string& pname_) {GetImpl().Init(pname_);}
    void Exit() {GetImpl().Exit();}
}; // end class MS

john harris
trading technologies

Boost list run by bdawes at, gregod at, cpdaniel at, john at