00001 #ifndef ERIS_DISPATCH_H
00002 #define ERIS_DISPATCH_H
00003
00004 #include <map>
00005 #include <deque>
00006 #include <list>
00007
00008 #include <Atlas/Message/Object.h>
00009
00010 #include <Eris/Types.h>
00011
00012 namespace Eris {
00013
00014 class StdBranchDispatcher;
00015 class ClassDispatcher;
00016
00017
00018 typedef std::deque<Atlas::Message::Object> DispatchContextDeque;
00019
00021
00025 class Dispatcher
00026 {
00027 public:
00028 explicit Dispatcher(const std::string &nm);
00029 virtual ~Dispatcher();
00030
00031 virtual Dispatcher* addSubdispatch(Dispatcher *sub, const std::string data = std::string()) = 0;
00032 virtual void rmvSubdispatch(Dispatcher *sub) = 0;
00033
00035 virtual Dispatcher* getSubdispatch(const std::string &nm) = 0;
00036
00038 virtual bool dispatch(DispatchContextDeque &dq) = 0;
00039
00040 virtual bool empty() = 0;
00041
00042 const std::string& getName() const
00043 { return _name; }
00044
00045 virtual const std::string& getData() const
00046 { return _name; }
00047
00048 static std::string getAnonymousSuffix(Dispatcher *d);
00049
00050 static void enter();
00051 static void exit();
00052 protected:
00053 friend class StdBranchDispatcher;
00054 friend class ClassDispatcher;
00055
00056 virtual void purge() = 0;
00057
00058 const std::string _name;
00059 public:
00060 void addRef()
00061 {++_refcount;}
00062
00063 void decRef()
00064 {if (!(--_refcount)) delete this; }
00065 private:
00066 unsigned int _refcount;
00067
00068 static bool global_inDispatch;
00069 static std::list<Dispatcher*> global_needsPurging;
00070 };
00071
00074 class LeafDispatcher : public Dispatcher
00075 {
00076 public:
00077 explicit LeafDispatcher(const std::string &nm);
00078 virtual ~LeafDispatcher() {;}
00079
00080 virtual bool dispatch(DispatchContextDeque &dq);
00081
00082 virtual Dispatcher* addSubdispatch(Dispatcher*, const std::string)
00083 { throw InvalidOperation("called addSubdispatch on LeafDispatcher " + _name); }
00084
00085 virtual void rmvSubdispatch(Dispatcher*)
00086 { throw InvalidOperation("called rmvSubdispatch on LeafDispatcher " + _name); }
00087
00088 virtual Dispatcher* getSubdispatch(const std::string &nm);
00089
00090 virtual bool empty()
00091 { throw InvalidOperation("called empty() on LeafDispatcher " + _name); }
00092
00093 protected:
00094 virtual void purge()
00095 { throw InvalidOperation("called purge() on LeafDispatcher " + _name); }
00096 };
00097
00098 class StdBranchDispatcher: public Dispatcher
00099 {
00100 public:
00101 explicit StdBranchDispatcher(const std::string nm = "__branch");
00102 virtual ~StdBranchDispatcher();
00103
00104 virtual bool dispatch(DispatchContextDeque &dq)
00105 { return subdispatch(dq); }
00106
00107 virtual Dispatcher* addSubdispatch(Dispatcher *sub, const std::string data);
00108 virtual void rmvSubdispatch(Dispatcher *sub);
00109 virtual Dispatcher* getSubdispatch(const std::string &nm);
00110
00111 virtual bool empty()
00112 { return _subs.empty(); }
00113 protected:
00114 typedef std::map<std::string, Dispatcher*> DispatcherDict;
00115
00117 bool subdispatch(DispatchContextDeque &dq);
00118 void safeSubErase(const DispatcherDict::iterator &d);
00119
00120 virtual void purge();
00121
00122 DispatcherDict _subs;
00123 };
00124
00125 }
00126
00127 #endif