Merged revisions 262,264-265,267-282,284-298,300-311 via svnmerge from
[senf.git] / Packets / PacketImpl.cc
1 // Copyright (C) 2007 
2 // Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
3 // Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
4 //     Stefan Bund <g0dil@berlios.de>
5 //
6 // This program is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 2 of the License, or
9 // (at your option) any later version.
10 //
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the
18 // Free Software Foundation, Inc.,
19 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
20
21 /** \file
22     \brief PacketImpl non-inline non-template implementation */
23
24 #include "PacketImpl.hh"
25 //#include "PacketImpl.ih"
26
27 // Custom includes
28 #include <iterator>
29 #include "PacketInterpreter.hh"
30
31 //#include "PacketImpl.mpp"
32 #define prefix_
33 ///////////////////////////////cc.p////////////////////////////////////////
34
35 ///////////////////////////////////////////////////////////////////////////
36 // senf::detail::PacketImpl
37
38 // interpreter chain
39
40 prefix_ void senf::detail::PacketImpl::appendInterpreter(PacketInterpreterBase * p)
41 {
42     interpreters_.push_back(*p);
43     p->assignImpl(this);
44 }
45
46 prefix_ void senf::detail::PacketImpl::prependInterpreter(PacketInterpreterBase * p)
47 {
48     interpreters_.push_front(*p);
49     p->assignImpl(this);
50 }
51
52 // Data container
53
54 prefix_ void senf::detail::PacketImpl::clear(PacketData * self)
55 {
56     PacketInterpreterBase * n (next(static_cast<PacketInterpreterBase*>(self)));
57     if (n)
58         truncateInterpreters(n);
59     iterator first (boost::next(begin(),self->begin_));
60     data_.erase(first, boost::next(begin(),self->end_));
61     updateIterators(self,first,-self->size());
62 }
63
64 // private members
65
66 prefix_ void senf::detail::PacketImpl::eraseInterpreters(interpreter_list::iterator b,
67                                                          interpreter_list::iterator e)
68 {
69     while (b!=e) {
70         interpreter_list::iterator i (b++);
71         PacketInterpreterBase * p (&(*i));
72         interpreters_.erase(i);
73         p->releaseImpl(); // might call PacketImpl::release and might delete p
74     }
75 }
76
77 prefix_ void senf::detail::PacketImpl::updateIterators(PacketData * self, iterator pos,
78                                                        difference_type n)
79 {
80     // I hate to change the PacketData representation from here, I would have preferred to let
81     // PacketData have authority over this but trying that just get's to convoluted so I choose the
82     // simple solution and made PacketImpl a friend of PacketData.
83
84     interpreter_list::iterator i (interpreters_.begin());
85
86     // There are three types of packets
87     // a) Those which come before 'self' in the interpreter chain
88     // b) 'self'
89     // c) Those that come afterwards
90     // For a), the change must be inside the packet since 'self' must be within those packets
91     // For b), the change must also be within since that's the packet we are changeing
92     // For c), the change must be outside the packet (we don't allow an upper packet to mess with
93     // the the data owned by a packet further down the chain). It can be before or after the
94     // packet.
95
96     // a)
97     for (; &(*i) != static_cast<PacketInterpreterBase*>(self); ++i) i->end_ += n;
98
99     // b)
100     i->end_ += n;
101
102     // c)
103     interpreter_list::iterator const i_end (interpreters_.end());
104     if (++i != i_end)
105         if (std::distance(begin(), pos) < difference_type(i->begin_))
106             // pos is before the packet, it must then be before all futher packets ...
107             for (; i != i_end; ++i) {
108                 i->begin_ += n;
109                 i->end_ += n;
110             }
111         // else pos is after the packet and we don't need to change anything ...
112 }
113
114 ///////////////////////////////cc.e////////////////////////////////////////
115 #undef prefix_
116 //#include "PacketImpl.mpp"
117
118 \f
119 // Local Variables:
120 // mode: c++
121 // fill-column: 100
122 // c-file-style: "senf"
123 // indent-tabs-mode: nil
124 // ispell-local-dictionary: "american"
125 // End: