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

ListBParser.ih

Go to the documentation of this file.
00001 // $Id: ListBParser.ih 1742 2010-11-04 14:51:56Z g0dil $
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 #ifndef IH_SENF_Packets_ListBParser_
00027 #define IH_SENF_Packets_ListBParser_ 1
00028 
00029 // Custom includes
00030 #include "ListParser.ih"
00031 
00032 //-/////////////////////////////////////////////////////////////////////////////////////////////////
00033 
00034 namespace senf {
00035 namespace detail {
00036 
00041     template <class ElementParser, class AuxPolicy>
00042     struct ListBParser_Policy
00043         : public AuxPolicy
00044     {
00045         // This policy needs to work around a serious problem with this type of list: When we change
00046         // the size of any (direct or indirect) sub-element of the list, this will change will
00047         // render the list completely invalid and un-parseable since the 'byte' field will now be
00048         // invalid.
00049         //
00050         // The solution we apply is to store the *size* (i.e. the number of elements) of the list
00051         // when creating the container wrapper. We also maintain this value across insert/erase
00052         // statements. Additionally we also safe the complete size of the data container (the vector
00053         // holding the bytes). Since we only allow packet changes through this container while it
00054         // exists, any change in the container size must be a change within this list and therefore
00055         // mandates an update of the 'bytes' field.
00056         //
00057         // The list container wrapper will call 'update' policy member before every access to the
00058         // container and also in the destructor. This gives us a chance to fix the bytes header
00059         // before the invalid header is seen by anyone (This is so, since we only allow access to
00060         // the list through the container wrapper ...). Since we know the number of list elements,
00061         // we can always find the correct 'bytes' value by traversing the list for that number of
00062         // elements.
00063         //
00064         // By caching the container size, all this can be made reasonably efficient and usable: The
00065         // updates are done automatically by only if needed. It would of course be more efficient to
00066         // just apply the size change directly to the bytes header (no need to traverse the
00067         // list). However, the implementation of this approach would be much more complex and even
00068         // more invasive and would probably suffer from the same restrictions to the user.
00069 
00070         struct container_policy;
00071 
00072         typedef PacketParserBase::data_iterator data_iterator;
00073         typedef PacketParserBase::state_type state_type;
00074         typedef PacketParserBase::size_type size_type;
00075 
00076         typedef ElementParser element_type;
00077         typedef ListParser< ListBParser_Policy > parser_type;
00078         typedef ListParser_Container< container_policy > container_type;
00079 
00080         static const size_type init_bytes = AuxPolicy::aux_bytes;
00081 
00082         ListBParser_Policy();
00083         template <class Arg> ListBParser_Policy(Arg const & arg);
00084 
00085         size_type bytes  (data_iterator i, state_type s) const;
00086         size_type size   (data_iterator i, state_type s) const;
00087         void      init   (data_iterator i, state_type s) const;
00088 
00090         struct container_policy
00091             : public AuxPolicy::WrapperPolicy
00092         {
00093             typedef PacketParserBase::data_iterator data_iterator;
00094             typedef PacketParserBase::state_type state_type;
00095             typedef PacketParserBase::size_type size_type;
00096 
00097             typedef ListBParser_Policy<ElementParser, AuxPolicy> parser_policy;
00098             typedef typename parser_policy::element_type element_type;
00099             typedef typename parser_policy::parser_type parser_type;
00100             typedef typename parser_policy::container_type container_type;
00101 
00102             static const size_type init_bytes = parser_policy::init_bytes;
00103 
00104             container_policy(parser_policy const & p);
00105 
00106             size_type bytes  (data_iterator i, state_type s) const;
00107             size_type size   (data_iterator i, state_type s) const;
00108             void      init   (data_iterator i, state_type s);
00109 
00110             void      construct (container_type & c) const;
00111             void      destruct  (container_type & c) const;
00112             void      erase     (container_type & c, data_iterator p);
00113             void      insert    (container_type & c, data_iterator p);
00114             void      update    (container_type const & c) const;
00115 
00117             struct iterator_data {};
00118 
00119             data_iterator setBegin        (container_type const & c, iterator_data & d) const;
00120             data_iterator setEnd          (container_type const & c, iterator_data & d) const;
00121             void          setFromPosition (container_type const & c, iterator_data & d,
00122                                            data_iterator p) const;
00123 
00124             data_iterator next            (container_type const & c, iterator_data & d) const;
00125             data_iterator raw             (container_type const & c, iterator_data const & d) const;
00126 
00127             size_type n_;
00128             mutable size_type container_size_;
00129         };
00130     };
00131 
00132 #ifndef DOXYGEN
00133 
00134     template <class ElementParser, class AuxPolicy>
00135     struct ListParserPolicy<ElementParser, AuxPolicy, senf::detail::auxtag::bytes>
00136     {
00137         typedef ListBParser_Policy<ElementParser, AuxPolicy> type;
00138     };
00139 
00140     template <class ElementParser, class AuxPolicy, class Transform>
00141     struct ListParserPolicy<ElementParser, AuxPolicy,
00142                             senf::detail::auxtag::transform<Transform,
00143                                                             senf::detail::auxtag::bytes> >
00144     {
00145         typedef ListBParser_Policy< ElementParser,
00146                                     TransformAuxParserPolicy<AuxPolicy, Transform> > type;
00147     };
00148 
00149 #endif
00150 
00151 }}
00152 
00153 //-/////////////////////////////////////////////////////////////////////////////////////////////////
00154 #endif
00155 
00156 
00157 // Local Variables:
00158 // mode: c++
00159 // fill-column: 100
00160 // comment-column: 40
00161 // c-file-style: "senf"
00162 // indent-tabs-mode: nil
00163 // ispell-local-dictionary: "american"
00164 // compile-command: "scons -u test"
00165 // End:

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