/usr/share/cruisecontrol-bin-2.6.1/projects/qpid-trunk/cpp/src/qpid/framing/Handler.h

00001 #ifndef QPID_FRAMING_HANDLER_H
00002 #define QPID_FRAMING_HANDLER_H
00003 
00004 /*
00005  *
00006  * Licensed to the Apache Software Foundation (ASF) under one
00007  * or more contributor license agreements.  See the NOTICE file
00008  * distributed with this work for additional information
00009  * regarding copyright ownership.  The ASF licenses this file
00010  * to you under the Apache License, Version 2.0 (the
00011  * "License"); you may not use this file except in compliance
00012  * with the License.  You may obtain a copy of the License at
00013  * 
00014  *   http://www.apache.org/licenses/LICENSE-2.0
00015  * 
00016  * Unless required by applicable law or agreed to in writing,
00017  * software distributed under the License is distributed on an
00018  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
00019  * KIND, either express or implied.  See the License for the
00020  * specific language governing permissions and limitations
00021  * under the License.
00022  *
00023  */
00024 #include "qpid/shared_ptr.h"
00025 #include <boost/type_traits/remove_reference.hpp>
00026 #include <assert.h>
00027 
00028 namespace qpid {
00029 namespace framing {
00030 
00032 template <class T>
00033 struct Handler {
00034     typedef T HandledType;
00035     typedef void handleFptr(T);
00036     typedef void result_type;   // Compatible with std/boost functors.
00037 
00038     Handler(Handler<T>* next_=0) : next(next_) {}
00039     virtual ~Handler() {}
00040     virtual void handle(T) = 0;
00041     
00043     void operator()(T t)  { handle(t); }
00044 
00045 
00047     Handler<T>* next;
00048 
00052     struct Chain : public Handler<T> {
00053         Chain(Handler<T>* first=0) : Handler(first) {}
00054         void operator=(Handler<T>* h) { next = h; }
00055         void handle(T t) { next->handle(t); }
00056         // TODO aconway 2007-08-29: chain modifier ops here.
00057     };
00058 
00060     struct Chains {
00061         Chains(Handler<T>* in_=0, Handler<T>* out_=0) : in(in_), out(out_) {}
00062         void reset(Handler<T>* in_=0, Handler<T>* out_=0) { in = in_; out = out_; }
00063         Chain in;
00064         Chain out;
00065     };
00066 
00071     template <class F> class Functor : public Handler<T> {
00072       public:
00073         Functor(F f, Handler<T>* next=0) : Handler<T>(next), functor(f) {}
00074         void handle(T t) { functor(t); }
00075       private:
00076         F functor;
00077     };
00078 
00082     template <class X, void (X::*F)(T)>
00083     class MemFunRef : public Handler<T> {
00084       public:
00085         MemFunRef(X& x, Handler<T>* next=0) : Handler(next), target(x) {}
00086         void handle(T t) { (target.*F)(t); }
00087 
00089         MemFunRef* operator->() { return this; }
00090 
00091       private:
00092         X& target;
00093     };
00094 
00099     class InOutHandlerInterface {
00100       public:
00101         virtual ~InOutHandlerInterface() {}
00102         virtual void handleIn(T) = 0;
00103         virtual void handleOut(T) = 0;
00104     };
00105         
00110     struct InOutHandler : protected InOutHandlerInterface {
00111         InOutHandler(Handler<T>* nextIn=0, Handler<T>* nextOut=0) : in(*this, nextIn), out(*this, nextOut) {}
00112         MemFunRef<InOutHandlerInterface, &InOutHandlerInterface::handleIn> in;
00113         MemFunRef<InOutHandlerInterface, &InOutHandlerInterface::handleOut> out;
00114     };
00115 
00116 };
00117 
00118 
00119 
00120 }}
00121 #endif  
00122 //

Generated on Thu Apr 10 11:08:18 2008 for Qpid by  doxygen 1.4.7