00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00026 #include "PacketRegistry.ih"
00027
00028
00029 #include <iostream>
00030 #include <iomanip>
00031 #include <senf/Utils/TypeInfo.hh>
00032 #include <senf/Utils/Format.hh>
00033 #include <senf/Utils/senfassert.hh>
00034
00035 #define prefix_
00036
00037
00038
00039
00040
00041 template <class KeyType>
00042 prefix_ senf::detail::PacketRegistryImpl<KeyType>::Entry::Entry(KeyType const & key_,
00043 int priority_)
00044 : key (key_), priority (priority_)
00045 {}
00046
00047 template <class KeyType>
00048 prefix_ senf::detail::PacketRegistryImpl<KeyType>::Entry::~Entry()
00049 {}
00050
00051
00052
00053
00054 template <class KeyType>
00055 template <class PacketType>
00056 prefix_ senf::detail::PacketRegistryImpl<KeyType>::EntryImpl<PacketType>::
00057 EntryImpl(KeyType const & key, int priority)
00058 : Entry (key, priority)
00059 {}
00060
00061 template <class KeyType>
00062 template <class PacketType>
00063 prefix_ senf::Packet::factory_t
00064 senf::detail::PacketRegistryImpl<KeyType>::EntryImpl<PacketType>::factory()
00065 const
00066 {
00067 return PacketType::factory();
00068 }
00069
00070 template <class KeyType>
00071 template <class PacketType>
00072 prefix_ std::string senf::detail::PacketRegistryImpl<KeyType>::EntryImpl<PacketType>::name()
00073 const
00074 {
00075 return prettyName(typeid(PacketType));
00076 }
00077
00078 template <class KeyType>
00079 template <class PacketType>
00080 prefix_ std::type_info const &
00081 senf::detail::PacketRegistryImpl<KeyType>::EntryImpl<PacketType>::type()
00082 const
00083 {
00084 return typeid(PacketType);
00085 }
00086
00087
00088
00089
00090 template <class KeyType>
00091 template <class PacketType>
00092 prefix_ void senf::detail::PacketRegistryImpl<KeyType>::registerPacket(key_t key, int priority)
00093 {
00094 SENF_ASSERT_EXPRESSION(
00095 registry_.insert(
00096 typename Entry::ptr(new EntryImpl<PacketType>(key,priority))).second,
00097 "Duplicate packet registration");
00098 }
00099
00100 template <class KeyType>
00101 template <class PacketType>
00102 prefix_ void senf::detail::PacketRegistryImpl<KeyType>::unregisterPacket()
00103 {
00104 registry_.template get<ByType>().erase(typeid(PacketType));
00105 }
00106
00107 template <class KeyType>
00108 prefix_ void senf::detail::PacketRegistryImpl<KeyType>::unregisterPacket(key_t key, int priority)
00109 {
00110
00111
00112 typename Registry::iterator i (registry_.find(boost::make_tuple(key,priority)));
00113 if (i != registry_.end())
00114 registry_.erase(i);
00115 }
00116
00117 template <class KeyType>
00118 prefix_ typename senf::detail::PacketRegistryImpl<KeyType>::key_t
00119 senf::detail::PacketRegistryImpl<KeyType>::key(senf::TypeIdValue const & type)
00120 {
00121 boost::optional<KeyType> k (key(type,true));
00122 if (! k)
00123 throw PacketTypeNotRegisteredException();
00124 return *k;
00125 }
00126
00127 template <class KeyType>
00128 prefix_ boost::optional<typename senf::detail::PacketRegistryImpl<KeyType>::key_t>
00129 senf::detail::PacketRegistryImpl<KeyType>::key(senf::TypeIdValue const & type, bool)
00130 {
00131 typedef typename Registry::template index<ByType>::type TypeIndex;
00132 TypeIndex const & typeIndex (registry_.template get<ByType>());
00133 typename TypeIndex::const_iterator i (typeIndex.find(type.id()));
00134 if (i == typeIndex.end())
00135 return boost::optional<key_t>();
00136 return (*i)->key;
00137 }
00138
00139 template <class KeyType>
00140 prefix_ typename senf::detail::PacketRegistryImpl<KeyType>::Entry const &
00141 senf::detail::PacketRegistryImpl<KeyType>::lookup(key_t key)
00142 {
00143 Entry const * e (lookup(key, true));
00144 if (!e)
00145 throw PacketTypeNotRegisteredException();
00146 return *e;
00147 }
00148
00149 template <class KeyType>
00150 prefix_ typename senf::detail::PacketRegistryImpl<KeyType>::Entry const *
00151 senf::detail::PacketRegistryImpl<KeyType>::lookup(key_t key, bool)
00152 {
00153 typedef typename Registry::template index<ByKey>::type KeyIndex;
00154 KeyIndex const & keyIndex (registry_.template get<ByKey>());
00155 typename KeyIndex::const_iterator i (keyIndex.lower_bound(key));
00156 if (i == keyIndex.end() || (*i)->key != key)
00157 return 0;
00158 return i->get();
00159 }
00160
00161 template <class KeyType>
00162 prefix_ bool senf::detail::PacketRegistryImpl<KeyType>::v_empty()
00163 const
00164 {
00165 return registry_.empty();
00166 }
00167
00168 template <class KeyType>
00169 prefix_ void senf::detail::PacketRegistryImpl<KeyType>::v_dump(std::ostream & os)
00170 const
00171 {
00172 typedef typename Registry::template index<ByKey>::type KeyIndex;
00173 KeyIndex const & keyIndex (registry_.template get<ByKey>());
00174 for (typename KeyIndex::iterator i (keyIndex.begin()), i_end (keyIndex.end());
00175 i != i_end; ++i) {
00176 std::string n ((*i)->name());
00177 senf::detail::DumpKey<KeyType>::dump((*i)->key, os);
00178 os << ' ' << std::setw(6) << (*i)->priority << ' ' << n.substr(21,n.size()-22) << '\n';
00179 }
00180 }
00181
00182 template <class KeyType>
00183 prefix_ void senf::detail::PacketRegistryImpl<KeyType>::v_clear()
00184 {
00185 registry_.clear();
00186 }
00187
00188
00189
00190
00191 template <class KeyType, bool is_integral>
00192 prefix_ void senf::detail::DumpKey<KeyType,is_integral>::dump(KeyType const & v,
00193 std::ostream & os)
00194 {
00195 os << " " << std::setw(16) << std::left << v << std::setw(0) << std::right;
00196 }
00197
00198
00199
00200 template <class KeyType>
00201 prefix_ void senf::detail::DumpKey<KeyType, true>::dump(KeyType const & v, std::ostream & os)
00202 {
00203 os << " " << senf::format::dumpint(v);
00204 }
00205
00206
00207 #undef prefix_
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218