Search:

SENF Extensible Network Framework

  • Home
  • Download
  • Wiki
  • BerliOS
  • ChangeLog
  • Browse SVN
  • Bug Tracker
  • Overview
  • Examples
  • HowTos
  • Glossary
  • PPI
  • Packets
  • Scheduler
  • Socket
  • Utils
  • Console
  • Daemon
  • Logger
  • Termlib
  • Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

PacketImpl.cci

Go to the documentation of this file.
00001 // $Id: PacketImpl.cci 1781 2011-04-11 12:10:19Z tho $
00002 //
00003 // Copyright (C) 2007
00004 // Fraunhofer (FOKUS)
00005 // Competence Center NETwork research (NET), St. Augustin, GERMANY
00006 //     Stefan Bund <g0dil@berlios.de>
00007 //
00008 // This program is free software; you can redistribute it and/or modify
00009 // it under the terms of the GNU General Public License as published by
00010 // the Free Software Foundation; either version 2 of the License, or
00011 // (at your option) any later version.
00012 //
00013 // This program is distributed in the hope that it will be useful,
00014 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016 // GNU General Public License for more details.
00017 //
00018 // You should have received a copy of the GNU General Public License
00019 // along with this program; if not, write to the
00020 // Free Software Foundation, Inc.,
00021 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00022 
00026 // Custom includes
00027 
00028 #define prefix_ inline
00029 //-/////////////////////////////////////////////////////////////////////////////////////////////////
00030 
00031 //-/////////////////////////////////////////////////////////////////////////////////////////////////
00032 // senf::detail::AnnotationRegistry
00033 
00034 prefix_ std::string senf::detail::AnnotationRegistry::name(key_type key)
00035     const
00036 {
00037     Registry::const_iterator i (registry_.find(key));
00038     return i == registry_.end() ? "" : i->second->v_name();
00039 }
00040 
00041 prefix_ bool senf::detail::AnnotationRegistry::isComplex(key_type key)
00042     const
00043 {
00044     Registry::const_iterator i (registry_.find(key));
00045     return i != registry_.end() && i->second->v_isComplex();
00046 }
00047 
00048 prefix_ unsigned senf::detail::AnnotationRegistry::size(key_type key)
00049     const
00050 {
00051     Registry::const_iterator i (registry_.find(key));
00052     return i == registry_.end() ? 0 : i->second->v_size();
00053 }
00054 
00055 prefix_ senf::detail::AnnotationRegistry::iterator senf::detail::AnnotationRegistry::begin()
00056     const
00057 {
00058     return boost::make_transform_iterator(index_.begin(),
00059                                           __gnu_cxx::select2nd<Index::value_type>());
00060 }
00061 
00062 prefix_ senf::detail::AnnotationRegistry::iterator senf::detail::AnnotationRegistry::end()
00063     const
00064 {
00065     return boost::make_transform_iterator(index_.end(),
00066                                           __gnu_cxx::select2nd<Index::value_type>());
00067 }
00068 
00069 prefix_ senf::detail::AnnotationRegistry::AnnotationRegistry()
00070     : simpleAnnotationCount_ (0), complexAnnotationCount_ (0)
00071 {}
00072 
00073 //-/////////////////////////////////////////////////////////////////////////////////////////////////
00074 
00075 // Memory management:
00076 //
00077 // * The PacketImpl destructor will *explicitly* clean-up the interpreters_ list by removing
00078 //   each element from the list and deleting it if it's (intrusive) refcount is 0
00079 // * The PacketInterpreters use safe hooks -> they know whether they are part of a list or not
00080 // * PacketHandle has an intrusive_ptr to PacketInterpreterBase. The intrusive_ptr_add_ref
00081 //   will refcount both the PacketImpl as well as the PacketInterpreterBase
00082 // * intrusive_ptr_remove will only delete the object if it's not in a container
00083 // * removing an object from the list will decrement the PacketImpl refcount accordingly
00084 // * inserting an object into the list will increment the PacketImpl refcount accordingly
00085 // * each PacketInterpreterBase instance holds a *raw* pointer to the PacketImpl
00086 //
00087 // The following operations change refcounts:
00088 //
00089 // * intrusive_ptr_add_ref(PacketInterpreterBase *);
00090 // * intrusive_ptr_remove(PacketInterpreterBase *);
00091 // * PacketImpl::appendInterpreter();
00092 // * PacketImpl::prependInterpreter();
00093 // * PacketImpl::truncateInterpreters();
00094 //
00095 // The last three also modify the impl_ member accordingly by calling
00096 // PacketInterpreterBase::assign/release
00097 
00098 //-/////////////////////////////////////////////////////////////////////////////////////////////////
00099 // senf::detail::PacketImpl
00100 
00101 prefix_ senf::detail::PacketImpl::PacketImpl()
00102     : refcount_(0)
00103 {
00104     ::memset(simpleAnnotations_, 0, sizeof(simpleAnnotations_));
00105 }
00106 
00107 prefix_ senf::detail::PacketImpl::PacketImpl(size_type size, byte initValue)
00108     : refcount_(0), data_(size,initValue)
00109 {
00110     ::memset(simpleAnnotations_, 0, sizeof(simpleAnnotations_));
00111 }
00112 
00113 // reference/memory management
00114 
00115 prefix_ void senf::detail::PacketImpl::add_ref()
00116 {
00117     ++ refcount_;
00118 }
00119 
00120 prefix_ senf::detail::PacketImpl::refcount_t senf::detail::PacketImpl::refcount()
00121     const
00122 {
00123     return refcount_;
00124 }
00125 
00126 // Interpreter chain
00127 
00128 prefix_ senf::PacketInterpreterBase * senf::detail::PacketImpl::first()
00129 {
00130     return interpreters_.empty() ? 0 : & interpreters_.front();
00131 }
00132 
00133 prefix_ senf::PacketInterpreterBase * senf::detail::PacketImpl::last()
00134 {
00135     return interpreters_.empty() ? 0 : & interpreters_.back();
00136 }
00137 
00138 prefix_ senf::PacketInterpreterBase * senf::detail::PacketImpl::next(PacketInterpreterBase * p)
00139 {
00140     interpreter_list::iterator i (interpreter_list::current(*p));
00141     return (++i == interpreters_.end()) ? 0 : &*i;
00142 }
00143 
00144 prefix_ senf::PacketInterpreterBase * senf::detail::PacketImpl::prev(PacketInterpreterBase * p)
00145 {
00146     interpreter_list::iterator i (interpreter_list::current(*p));
00147     return (i == interpreters_.begin()) ? 0 : &*(--i);
00148 }
00149 
00150 prefix_ void senf::detail::PacketImpl::truncateInterpreters(PacketInterpreterBase * p)
00151 {
00152     Guard guard (this);
00153     eraseInterpreters(interpreter_list::current(*p),interpreters_.end());
00154 }
00155 
00156 prefix_ void senf::detail::PacketImpl::truncateInterpretersBackwards(PacketInterpreterBase * p)
00157 {
00158     Guard guard (this);
00159     eraseInterpreters(interpreters_.begin(),boost::next(interpreter_list::current(*p)));
00160 }
00161 
00162 // Data container
00163 
00164 prefix_ senf::detail::PacketImpl::iterator senf::detail::PacketImpl::begin()
00165 {
00166     return data_.begin();
00167 }
00168 
00169 prefix_ senf::detail::PacketImpl::iterator senf::detail::PacketImpl::end()
00170 {
00171     return data_.end();
00172 }
00173 
00174 prefix_ senf::detail::PacketImpl::size_type senf::detail::PacketImpl::size()
00175 {
00176     return data_.size();
00177 }
00178 
00179 prefix_ void senf::detail::PacketImpl::insert(PacketData * self, iterator pos, byte v)
00180 {
00181     difference_type ix (std::distance(begin(),pos));
00182     data_.insert(pos,v);
00183     updateIterators(self,ix,1);
00184 }
00185 
00186 prefix_ void senf::detail::PacketImpl::insert(PacketData * self, iterator pos, size_type n,
00187                                               byte v)
00188 {
00189     difference_type ix (std::distance(begin(),pos));
00190     data_.insert(pos,n,v);
00191     updateIterators(self,ix,n);
00192 }
00193 
00194 prefix_ void senf::detail::PacketImpl::erase(PacketData * self, iterator pos)
00195 {
00196     difference_type ix (std::distance(begin(),pos));
00197     data_.erase(pos);
00198     updateIterators(self,ix,-1);
00199 }
00200 
00201 prefix_ void senf::detail::PacketImpl::erase(PacketData * self, iterator first, iterator last)
00202 {
00203     difference_type ix (std::distance(begin(),first));
00204     difference_type delta (std::distance(first,last));
00205     data_.erase(first,last);
00206     updateIterators(self,ix,-delta);
00207 }
00208 
00209 prefix_ void senf::detail::PacketImpl::reserve(size_type n)
00210 {
00211     data_.reserve(n);
00212 }
00213 
00214 prefix_ senf::detail::PacketImpl::size_type senf::detail::PacketImpl::capacity()
00215     const
00216 {
00217     return data_.capacity();
00218 }
00219 
00220 // Annotations
00221 
00222 prefix_ void * senf::detail::PacketImpl::annotation(AnnotationRegistry::key_type key)
00223 {
00224     return key >= 0 ? & simpleAnnotations_[key] : complexAnnotation(key);
00225 }
00226 
00227 //-/////////////////////////////////////////////////////////////////////////////////////////////////
00228 // senf::detail::PacketImpl::Guard
00229 
00230 prefix_ senf::detail::PacketImpl::Guard::Guard(PacketImpl * impl)
00231     : p (impl)
00232 {
00233     p->add_ref();
00234 }
00235 
00236 prefix_ senf::detail::PacketImpl::Guard::~Guard()
00237 {
00238     p->release();
00239 }
00240 
00241 //-/////////////////////////////////////////////////////////////////////////////////////////////////
00242 #undef prefix_
00243 
00244 
00245 // Local Variables:
00246 // mode: c++
00247 // fill-column: 100
00248 // c-file-style: "senf"
00249 // indent-tabs-mode: nil
00250 // ispell-local-dictionary: "american"
00251 // compile-command: "scons -u test"
00252 // comment-column: 40
00253 // End:

Contact: senf-dev@lists.berlios.de | © 2006-2010 Fraunhofer Institute for Open Communication Systems, Network Research