|
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
{
public:
//! the factory that can create IB's
typedef typename IB::factory_type factory_type;
Bridge()
{
IB *tmp_ptr=0;
factory_type::GetFactory()->Dispatch(/*ptrref*/tmp_ptr);
impl.reset(tmp_ptr);
}
virtual ~Bridge() {}
protected:
IB& GetImpl() const {return *impl;}
void SetImpl(IB* impl_) {impl.reset(impl_);}
private:
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
(MS_ImplBase)
*/
class MS : public Bridge<MS_ImplBase>
{
public:
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 acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk