00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00026 #include "PacketImpl.ih"
00027
00028
00029 #include <iterator>
00030 #include <map>
00031 #include <string>
00032 #include <boost/format.hpp>
00033 #include <senf/Utils/String.hh>
00034 #include "Packets.hh"
00035
00036
00037 #define prefix_
00038
00039
00040
00041
00042
00043 prefix_ senf::detail::PacketImpl::~PacketImpl()
00044 {
00045
00046 ++refcount_;
00047 eraseInterpreters(interpreters_.begin(), interpreters_.end());
00048 }
00049
00050 prefix_ void senf::detail::PacketImpl::release()
00051 {
00052 SENF_ASSERT(refcount_ >= 1, "Internal failure: Releasing dead PacketImpl ??");
00053
00054
00055
00056
00057 if (refcount_ == 1)
00058 delete this;
00059 else
00060 -- refcount_;
00061 }
00062
00063
00064
00065 prefix_ void senf::detail::PacketImpl::appendInterpreter(PacketInterpreterBase * p)
00066 {
00067 interpreters_.push_back(*p);
00068 p->assignImpl(this);
00069 }
00070
00071 prefix_ void senf::detail::PacketImpl::prependInterpreter(PacketInterpreterBase * p)
00072 {
00073 interpreters_.push_front(*p);
00074 p->assignImpl(this);
00075 }
00076
00077 prefix_ void senf::detail::PacketImpl::prependInterpreter(PacketInterpreterBase * p,
00078 PacketInterpreterBase * before)
00079 {
00080 interpreter_list::iterator i (interpreter_list::current(*before));
00081 interpreters_.insert(i, *p);
00082 p->assignImpl(this);
00083 }
00084
00085
00086
00087 prefix_ void senf::detail::PacketImpl::clear(PacketData * self)
00088 {
00089 PacketInterpreterBase * n (next(static_cast<PacketInterpreterBase*>(self)));
00090 if (n)
00091 truncateInterpreters(n);
00092 iterator first (boost::next(begin(),self->begin_));
00093 data_.erase(first, boost::next(begin(),self->end_));
00094 updateIterators(self,self->begin_,-self->size());
00095 }
00096
00097
00098
00099 prefix_ void senf::detail::PacketImpl::eraseInterpreters(interpreter_list::iterator b,
00100 interpreter_list::iterator e)
00101 {
00102 while (b!=e) {
00103 interpreter_list::iterator i (b++);
00104 PacketInterpreterBase * p (&(*i));
00105 interpreters_.erase(i);
00106 p->releaseImpl();
00107 }
00108 }
00109
00110 prefix_ void senf::detail::PacketImpl::updateIterators(PacketData * self, difference_type pos,
00111 difference_type n)
00112 {
00113
00114
00115
00116
00117 interpreter_list::iterator i (interpreters_.begin());
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130 for (; &(*i) != static_cast<PacketInterpreterBase*>(self); ++i) i->end_ += n;
00131
00132
00133 i->end_ += n;
00134
00135
00136 interpreter_list::iterator const i_end (interpreters_.end());
00137 if (++i != i_end)
00138 if (pos <= difference_type(i->begin_))
00139
00140 for (; i != i_end; ++i) {
00141 i->begin_ += n;
00142 i->end_ += n;
00143 }
00144
00145 }
00146
00147
00148
00149 prefix_ void senf::detail::PacketImpl::dumpAnnotations(std::ostream & os)
00150 {
00151 for (AnnotationRegistry::iterator i (AnnotationRegistry::instance().begin());
00152 i != AnnotationRegistry::instance().end(); ++i) {
00153 void * antn (annotation(*i));
00154 if (antn)
00155 AnnotationRegistry::instance().dump(*i, os, antn);
00156 }
00157 }
00158
00159 prefix_ void senf::detail::PacketImpl::clearAnnotations()
00160 {
00161 ::memset(simpleAnnotations_, 0, sizeof(simpleAnnotations_));
00162 complexAnnotations_.clear();
00163 }
00164
00165 prefix_ void senf::detail::PacketImpl::assignAnnotations(PacketImpl const & other)
00166 {
00167 std::copy(&other.simpleAnnotations_[0], &other.simpleAnnotations_[0] +
00168 sizeof(simpleAnnotations_)/sizeof(simpleAnnotations_[0]), simpleAnnotations_);
00169 complexAnnotations_.assign(
00170 other.complexAnnotations_.begin(), other.complexAnnotations_.end());
00171 }
00172
00173 prefix_ void * senf::detail::PacketImpl::complexAnnotation(AnnotationRegistry::key_type key)
00174 {
00175 SENF_ASSERT( key < 0, "complexAnnotation called with invalid key");
00176 return (ComplexAnnotations::size_type(-key-1) >= complexAnnotations_.size()
00177 || complexAnnotations_.is_null(-key-1))
00178 ? 0 : complexAnnotations_[-key-1].get();
00179 }
00180
00181
00182
00183
00184 prefix_ void senf::detail::AnnotationRegistry::dump(key_type key, std::ostream & os,
00185 void * annotation)
00186 const
00187 {
00188 Registry::const_iterator i (registry_.find(key));
00189 if (i != registry_.end()) {
00190 os << fieldName(i->second->v_name());
00191 i->second->v_dump(os, annotation);
00192 os << "\n";
00193 }
00194 }
00195
00196 prefix_ void senf::detail::AnnotationRegistry::dumpRegistrations(std::ostream & os)
00197 {
00198 boost::format fmt ("%-56.56s %-4.4s %-7.7s %5d\n");
00199 os << "SENF_PACKET_ANNOTATION_SLOTS = " << SENF_PACKET_ANNOTATION_SLOTS << "\n"
00200 << "SENF_PACKET_ANNOTATION_SLOTSIZE = " << SENF_PACKET_ANNOTATION_SLOTSIZE << "\n";
00201 os << fmt % "TYPE" % "FAST" % "COMPLEX" % "SIZE";
00202
00203 for (Index::const_iterator i (index_.begin()), i_end (index_.end()); i != i_end; ++i) {
00204 key_type key (i->second);
00205 std::string nm (i->first);
00206 if (nm.size() > 56) nm.erase(nm.begin(), nm.begin()+nm.size()-32);
00207 os << fmt
00208 % nm
00209 % (key >= 0 ? "yes" : "no")
00210 % (isComplex(key) ? "yes" : "no")
00211 % size(key);
00212 }
00213 }
00214
00215 prefix_ void senf::dumpPacketAnnotationRegistry(std::ostream & os)
00216 {
00217 senf::detail::AnnotationRegistry::instance().dumpRegistrations(os);
00218 }
00219
00220
00221 #undef prefix_
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233