00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00026 #ifndef IH_SENF_Packets_PacketRegistry_
00027 #define IH_SENF_Packets_PacketRegistry_ 1
00028
00029
00030 #include <limits>
00031 #include <boost/multi_index_container.hpp>
00032 #include <boost/multi_index/ordered_index.hpp>
00033 #include <boost/multi_index/composite_key.hpp>
00034 #include <boost/multi_index/member.hpp>
00035 #include <boost/multi_index/mem_fun.hpp>
00036 #include <boost/intrusive_ptr.hpp>
00037 #include <boost/utility.hpp>
00038 #include <senf/Utils/TypeIdValue.hh>
00039
00040
00041
00042 namespace senf {
00043 namespace detail {
00044
00045 struct TypeInfoCompare
00046 {
00047 bool operator()(std::type_info const & a, std::type_info const & b) const
00048 { return a.before(b); }
00049 };
00050
00055 class PacketRegistryImplBase
00056 : private boost::noncopyable
00057 {
00058 public:
00059 virtual ~PacketRegistryImplBase();
00060
00061 static void dump(std::ostream & os);
00062 static void clear();
00063
00064 protected:
00065 typedef std::map<std::string, PacketRegistryImplBase*> RegistryMap;
00066 static RegistryMap & registries();
00067
00068 private:
00069 virtual bool v_empty() const = 0;
00070 virtual void v_dump(std::ostream & os) const = 0;
00071 virtual void v_clear() = 0;
00072 };
00073
00078 template <class KeyType>
00079 class PacketRegistryImpl
00080 : public PacketRegistryImplBase
00081 {
00082 public:
00083 typedef KeyType key_t;
00084
00085 struct Entry : public intrusive_refcount
00086 {
00087 typedef boost::intrusive_ptr<Entry> ptr;
00088
00089 Entry(KeyType const & key_, int priority_);
00090 virtual ~Entry();
00091
00092 virtual Packet::factory_t factory() const = 0;
00093
00094 virtual std::string name() const = 0;
00095 virtual std::type_info const & type() const = 0;
00096
00097 KeyType key;
00098 int priority;
00099 };
00100
00101 private:
00102 struct ByKey {};
00103 struct ByType {};
00104
00105 struct RegistryIndices
00106 : public boost::multi_index::indexed_by<
00107 boost::multi_index::ordered_unique<
00108 boost::multi_index::tag<ByKey>,
00109 boost::multi_index::composite_key<
00110 Entry,
00111 boost::multi_index::member<Entry,KeyType,&Entry::key>,
00112 boost::multi_index::member<Entry,int,&Entry::priority> >,
00113 boost::multi_index::composite_key_compare<
00114 std::less<KeyType>,
00115 std::greater<int> > >,
00116 boost::multi_index::ordered_unique<
00117 boost::multi_index::tag<ByType>,
00118 boost::multi_index::mem_fun<Entry const,std::type_info const &,&Entry::type>,
00119 TypeInfoCompare> >
00120 {};
00121
00122 typedef boost::multi_index_container<typename Entry::ptr, RegistryIndices> Registry;
00123
00124 template <class PacketType>
00125 struct EntryImpl : public Entry
00126 {
00127 EntryImpl(KeyType const & key, int priority);
00128
00129 virtual Packet::factory_t factory() const;
00130 virtual std::string name() const;
00131 virtual std::type_info const & type() const;
00132 };
00133
00134 public:
00135
00136
00137
00138 typedef typename Registry::template index<ByKey>::type::const_iterator iterator;
00139
00140
00142
00143
00144 PacketRegistryImpl(std::string const & name);
00145
00146
00147
00148
00149 template <class PacketType>
00150 void registerPacket(key_t key, int priority=0);
00151
00152 template <class PacketType>
00153 void unregisterPacket();
00154 void unregisterPacket(key_t key, int priority=0);
00155
00156 key_t key(senf::TypeIdValue const & type);
00157 boost::optional<key_t> key(senf::TypeIdValue const & type, bool);
00158
00159 Entry const & lookup(key_t key);
00160 Entry const * lookup(key_t key, bool);
00161
00162 iterator begin() const;
00163 iterator end() const;
00164
00165 protected:
00166
00167 private:
00168 virtual bool v_empty() const;
00169 virtual void v_dump(std::ostream & os) const;
00170 virtual void v_clear();
00171
00172 Registry registry_;
00173 };
00174
00175 template <class KeyType, bool is_integral=std::numeric_limits<KeyType>::is_integer>
00176 struct DumpKey
00177 {
00178 static void dump(KeyType const & v, std::ostream & os);
00179 };
00180
00181 template <class KeyType>
00182 struct DumpKey<KeyType, true>
00183 {
00184 static void dump(KeyType const & v, std::ostream & os);
00185 };
00186
00187 }}
00188
00189
00190 #endif
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201