diff options
Diffstat (limited to 'cpp/src/qpid/framing/Handler.h')
| -rw-r--r-- | cpp/src/qpid/framing/Handler.h | 91 |
1 files changed, 74 insertions, 17 deletions
diff --git a/cpp/src/qpid/framing/Handler.h b/cpp/src/qpid/framing/Handler.h index 2f09911325..be49570f9b 100644 --- a/cpp/src/qpid/framing/Handler.h +++ b/cpp/src/qpid/framing/Handler.h @@ -27,33 +27,90 @@ namespace qpid { namespace framing { -/** Interface for handler for values of type T. - * Handlers can be linked into chains via the next pointer. - */ -template <class T> struct Handler { - typedef T ParamType; - typedef shared_ptr<Handler> Chain; +/** Generic handler that can be linked into chains. */ +template <class T> +struct Handler { + typedef T HandledType; + + Handler(Handler<T>* next_=0) : next(next_) {} + virtual ~Handler() {} + virtual void handle(T) = 0; + + /** Allow functor syntax for calling handle */ + void operator()(T t) { handle(t); } + - /** Handler chains for incoming and outgoing traffic. */ + /** Pointer to next handler in a linked list. */ + Handler<T>* next; + + /** A Chain is a handler that forwards to a modifiable + * linked list of handlers. + */ + struct Chain : public Handler<T> { + Chain(Handler<T>* first) : Handler(first) {} + void operator=(Handler<T>* h) { next = h; } + void handle(T t) { (*next)(t); } + // TODO aconway 2007-08-29: chain modifier ops here. + }; + + /** In/out pair of handler chains. */ struct Chains { - Chains() {} - Chains(Chain i, Chain o) : in(i), out(o) {} - Chains(Handler* i, Handler* o) : in(i), out(o) {} + Chains(Handler<T>* in_=0, Handler<T>* out_=0) : in(in_), out(out_) {} + void reset(Handler<T>* in_=0, Handler<T>* out_=0) { in = in_; out = out_; } Chain in; Chain out; }; - Handler() {} - Handler(Chain next_) : next(next_) {} - virtual ~Handler() {} + /** Adapt any void(T) functor as a Handler. + * Functor<F>(f) will copy f. + * Functor<F&>(f) will only take a reference to x. + */ + template <class F> class Functor : public Handler<T> { + public: + Functor(F f, Handler<T>* next=0) : Handler<T>(next), functor(f) {} + void handle(T t) { functor(t); } + private: + F functor; + }; - virtual void handle(T) = 0; + /** Adapt a member function of X as a Handler. + * MemFun<X, X::f> will copy x. + * MemFun<X&, X::f> will only take a reference to x. + */ + template <class X, void(*M)(T)> + class MemFun : public Handler<T> { + public: + MemFun(X x, Handler<T>* next=0) : Handler(next), object(x) {} + void handle(T t) { object.*M(t); } + private: + X object; + }; + + /** Support for implementing an in-out handler pair as a single class. + * Public interface is Handler<T>::Chains pair, but implementation + * overrides handleIn, handleOut functions in a single class. + */ + class InOutHandler { + public: + virtual ~InOutHandler() {} + + InOutHandler() : + in(*this, &InOutHandler::handleIn), + out(*this, &InOutHandler::handleOut) {} + + MemFun<InOutHandler, &InOutHandler::handleIn> in; + MemFun<InOutHandler, &InOutHandler::handleOut> out; + + protected: + virtual void handleIn(T) = 0; + virtual void handleOut(T) = 0; + private: + }; - /** Next handler. Public so chains can be modified by altering next. */ - Chain next; }; -}} +}} #endif /*!QPID_FRAMING_HANDLER_H*/ +// |
