8dfd75e10c653b5a1a5ffde744aa4463df23bee6
[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.ih"
25
26 // Custom includes
27 #include <iterator>
28 #include "Packets.hh"
29
30 //#include "PacketImpl.mpp"
31 #define prefix_
32 ///////////////////////////////cc.p////////////////////////////////////////
33
34 ///////////////////////////////////////////////////////////////////////////
35 // senf::detail::PacketImpl
36
37 // interpreter chain
38
39 prefix_ void senf::detail::PacketImpl::appendInterpreter(PacketInterpreterBase * p)
40 {
41     interpreters_.push_back(*p);
42     p->assignImpl(this);
43 }
44
45 prefix_ void senf::detail::PacketImpl::prependInterpreter(PacketInterpreterBase * p)
46 {
47     interpreters_.push_front(*p);
48     p->assignImpl(this);
49 }
50
51 // Data container
52
53 prefix_ void senf::detail::PacketImpl::clear(PacketData * self)
54 {
55     PacketInterpreterBase * n (next(static_cast<PacketInterpreterBase*>(self)));
56     if (n)
57         truncateInterpreters(n);
58     iterator first (boost::next(begin(),self->begin_));
59     data_.erase(first, boost::next(begin(),self->end_));
60     updateIterators(self,first,-self->size());
61 }
62
63 // private members
64
65 prefix_ void senf::detail::PacketImpl::eraseInterpreters(interpreter_list::iterator b,
66                                                          interpreter_list::iterator e)
67 {
68     while (b!=e) {
69         interpreter_list::iterator i (b++);
70         PacketInterpreterBase * p (&(*i));
71         interpreters_.erase(i);
72         p->releaseImpl(); // might call PacketImpl::release and might delete p
73     }
74 }
75
76 prefix_ void senf::detail::PacketImpl::updateIterators(PacketData * self, iterator pos,
77                                                        difference_type n)
78 {
79     // I hate to change the PacketData representation from here, I would have preferred to let
80     // PacketData have authority over this but trying that just get's to convoluted so I choose the
81     // simple solution and made PacketImpl a friend of PacketData.
82
83     interpreter_list::iterator i (interpreters_.begin());
84
85     // There are three types of packets
86     // a) Those which come before 'self' in the interpreter chain
87     // b) 'self'
88     // c) Those that come afterwards
89     // For a), the change must be inside the packet since 'self' must be within those packets
90     // For b), the change must also be within since that's the packet we are changeing
91     // For c), the change must be outside the packet (we don't allow an upper packet to mess with
92     // the the data owned by a packet further down the chain). It can be before or after the
93     // packet.
94
95     // a)
96     for (; &(*i) != static_cast<PacketInterpreterBase*>(self); ++i) i->end_ += n;
97
98     // b)
99     i->end_ += n;
100
101     // c)
102     interpreter_list::iterator const i_end (interpreters_.end());
103     if (++i != i_end)
104         if (std::distance(begin(), pos) < difference_type(i->begin_))
105             // pos is before the packet, it must then be before all futher packets ...
106             for (; i != i_end; ++i) {
107                 i->begin_ += n;
108                 i->end_ += n;
109             }
110         // else pos is after the packet and we don't need to change anything ...
111 }
112
113 ///////////////////////////////cc.e////////////////////////////////////////
114 #undef prefix_
115 //#include "PacketImpl.mpp"
116
117 \f
118 // Local Variables:
119 // mode: c++
120 // fill-column: 100
121 // c-file-style: "senf"
122 // indent-tabs-mode: nil
123 // ispell-local-dictionary: "american"
124 // compile-command: "scons -u test"
125 // comment-column: 40
126 // End: