5fc7bf9031bbd730772b3506c78bbee8b8179684
[senf.git] / senf / Packets / PacketImpl.cc
1 // $Id$
2 //
3 // Copyright (C) 2007
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 //     Stefan Bund <g0dil@berlios.de>
7 //
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 2 of the License, or
11 // (at your option) any later version.
12 //
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 // GNU General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the
20 // Free Software Foundation, Inc.,
21 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22
23 /** \file
24     \brief PacketImpl non-inline non-template implementation */
25
26 //#include "PacketImpl.ih"
27
28 // Custom includes
29 #include <iterator>
30 #include "Packets.hh"
31
32 //#include "PacketImpl.mpp"
33 #define prefix_
34 ///////////////////////////////cc.p////////////////////////////////////////
35
36 ///////////////////////////////////////////////////////////////////////////
37 // senf::detail::AnnotationIndexerBase
38
39 unsigned senf::detail::AnnotationIndexerBase::maxAnnotations (0);
40
41 prefix_ void senf::detail::AnnotationIndexerBase::dump(PacketImpl * p, std::ostream & os)
42 {
43     for(std::vector<AnnotationIndexerBase*>::const_iterator 
44             i (registry().begin()), i_end (registry().end());
45         i != i_end; ++i)
46         (*i)->v_dump(p,os);
47 }
48
49 ///////////////////////////////////////////////////////////////////////////
50 // senf::detail::PacketImpl
51
52 prefix_ senf::detail::PacketImpl::~PacketImpl()
53 {
54     // We increment refcount_ to ensure, release() won't call delete again
55     ++refcount_;
56     eraseInterpreters(interpreters_.begin(), interpreters_.end());
57     Annotations::const_iterator  i (annotations_.begin());
58     Annotations::const_iterator const i_end (annotations_.end());
59     std::vector<bool>::iterator small (AnnotationIndexerBase::small().begin());
60     for (; i != i_end; ++i, ++small)
61         if (! *small && i->p)
62             delete i->p;
63 }
64
65 // interpreter chain
66
67 prefix_ void senf::detail::PacketImpl::appendInterpreter(PacketInterpreterBase * p)
68 {
69     interpreters_.push_back(*p);
70     p->assignImpl(this);
71 }
72
73 prefix_ void senf::detail::PacketImpl::prependInterpreter(PacketInterpreterBase * p)
74 {
75     interpreters_.push_front(*p);
76     p->assignImpl(this);
77 }
78
79 prefix_ void senf::detail::PacketImpl::prependInterpreter(PacketInterpreterBase * p,
80                                                           PacketInterpreterBase * before)
81 {
82     interpreter_list::iterator i (interpreter_list::current(*before));
83     interpreters_.insert(i, *p);
84     p->assignImpl(this);
85 }
86
87 // Data container
88
89 prefix_ void senf::detail::PacketImpl::clear(PacketData * self)
90 {
91     PacketInterpreterBase * n (next(static_cast<PacketInterpreterBase*>(self)));
92     if (n)
93         truncateInterpreters(n);
94     iterator first (boost::next(begin(),self->begin_));
95     data_.erase(first, boost::next(begin(),self->end_));
96     updateIterators(self,self->begin_,-self->size());
97 }
98
99 // private members
100
101 prefix_ void senf::detail::PacketImpl::eraseInterpreters(interpreter_list::iterator b,
102                                                          interpreter_list::iterator e)
103 {
104     while (b!=e) {
105         interpreter_list::iterator i (b++);
106         PacketInterpreterBase * p (&(*i));
107         interpreters_.erase(i);
108         p->releaseImpl(); // might call PacketImpl::release and might delete p
109     }
110 }
111
112 prefix_ void senf::detail::PacketImpl::updateIterators(PacketData * self, difference_type pos,
113                                                        difference_type n)
114 {
115     // I hate to change the PacketData representation from here, I would have preferred to let
116     // PacketData have authority over this but trying that just get's to convoluted so I choose the
117     // simple solution and made PacketImpl a friend of PacketData.
118
119     interpreter_list::iterator i (interpreters_.begin());
120
121     // There are three types of packets
122     // a) Those which come before 'self' in the interpreter chain
123     // b) 'self'
124     // c) Those that come afterwards
125     // For a), the change must be inside the packet since 'self' must be within those packets
126     // For b), the change must also be within since that's the packet we are changeing
127     // For c), the change must be outside the packet (we don't allow an upper packet to mess with
128     // the the data owned by a packet further down the chain). It can be before or after the
129     // packet.
130
131     // a)
132     for (; &(*i) != static_cast<PacketInterpreterBase*>(self); ++i) i->end_ += n;
133
134     // b)
135     i->end_ += n;
136
137     // c)
138     interpreter_list::iterator const i_end (interpreters_.end());
139     if (++i != i_end)
140         if (pos <= difference_type(i->begin_))
141             // pos is before the packet, it must then be before all futher packets ...
142             for (; i != i_end; ++i) {
143                 i->begin_ += n;
144                 i->end_ += n;
145             }
146         // else pos is after the packet and we don't need to change anything ...
147 }
148
149 ///////////////////////////////cc.e////////////////////////////////////////
150 #undef prefix_
151 //#include "PacketImpl.mpp"
152
153 \f
154 // Local Variables:
155 // mode: c++
156 // fill-column: 100
157 // c-file-style: "senf"
158 // indent-tabs-mode: nil
159 // ispell-local-dictionary: "american"
160 // compile-command: "scons -u test"
161 // comment-column: 40
162 // End: