From e183227707d150b1f42e750df0e90cd7dac8744e Mon Sep 17 00:00:00 2001 From: Alan Conway Date: Wed, 29 Aug 2007 23:27:40 +0000 Subject: * src/qpid/broker/Session.h, .cpp: Session holds all state of a session including handlers created for that session. Session is not directly associated with a channel. * src/qpid/broker/SessionAdapter.h, .cpp: SessionAdapter is bound to a channel managed by the Connection. It can be attached to and detatched from a Session. * src/qpid/broker/Connection.cpp, .h: Use SessionAdapter. * src/qpid/framing/Handler.h: Removed use of shared_ptr. Handlers belong either to a Session or a Connection and are destroyed with it. * src/qpid/framing/InputHandler.h, OutputHandler.h: Both now inherit from FrameHandler and can be used as FrameHandlers. Intermediate step to removing them entirely. * src/qpid/broker/ConnectionAdapter.h: * src/qpid/client/ConnectionHandler.h: * src/qpid/framing/ChannelAdapter.cpp, .h: Minor changes required by Handler changes. git-svn-id: https://svn.apache.org/repos/asf/incubator/qpid/trunk/qpid@570982 13f79535-47bb-0310-9956-ffa450edef68 --- cpp/src/qpid/framing/Handler.h | 91 ++++++++++++++++++++++++++++++++++-------- 1 file changed, 74 insertions(+), 17 deletions(-) (limited to 'cpp/src/qpid/framing/Handler.h') 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 struct Handler { - typedef T ParamType; - typedef shared_ptr Chain; +/** Generic handler that can be linked into chains. */ +template +struct Handler { + typedef T HandledType; + + Handler(Handler* 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* next; + + /** A Chain is a handler that forwards to a modifiable + * linked list of handlers. + */ + struct Chain : public Handler { + Chain(Handler* first) : Handler(first) {} + void operator=(Handler* 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* in_=0, Handler* out_=0) : in(in_), out(out_) {} + void reset(Handler* in_=0, Handler* 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) will copy f. + * Functor(f) will only take a reference to x. + */ + template class Functor : public Handler { + public: + Functor(F f, Handler* next=0) : Handler(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 will copy x. + * MemFun will only take a reference to x. + */ + template + class MemFun : public Handler { + public: + MemFun(X x, Handler* 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::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 in; + MemFun 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*/ +// -- cgit v1.2.1