2bd41bea6db88ddc02216d5ac646f83d693ff203
[senf.git] / 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::PacketImpl
38
39 // This function has a problem being inlined. Somehow, often when calling this, the size of the 
40 // resulting inlined code would be huge. Need to further debug this.
41
42 prefix_ void senf::detail::PacketImpl::release(refcount_t n)
43 {
44     SENF_ASSERT(refcount_ >= n);
45     // uah ... we need to be extremely careful here. If refcount_ is n, we want to commit suicide,
46     // however the destructor will remove all PacketInterpreters from the list and will thereby
47     // decrement refcount -> only decrenebt refcount_ when *not* caling delete
48     if (refcount_ == n)
49         delete this;
50     else
51         refcount_ -= n;
52 }
53
54 // interpreter chain
55
56 prefix_ void senf::detail::PacketImpl::appendInterpreter(PacketInterpreterBase * p)
57 {
58     interpreters_.push_back(*p);
59     p->assignImpl(this);
60 }
61
62 prefix_ void senf::detail::PacketImpl::prependInterpreter(PacketInterpreterBase * p)
63 {
64     interpreters_.push_front(*p);
65     p->assignImpl(this);
66 }
67
68 // Data container
69
70 prefix_ void senf::detail::PacketImpl::clear(PacketData * self)
71 {
72     PacketInterpreterBase * n (next(static_cast<PacketInterpreterBase*>(self)));
73     if (n)
74         truncateInterpreters(n);
75     iterator first (boost::next(begin(),self->begin_));
76     data_.erase(first, boost::next(begin(),self->end_));
77     updateIterators(self,self->begin_,-self->size());
78 }
79
80 // private members
81
82 prefix_ void senf::detail::PacketImpl::eraseInterpreters(interpreter_list::iterator b,
83                                                          interpreter_list::iterator e)
84 {
85     while (b!=e) {
86         interpreter_list::iterator i (b++);
87         PacketInterpreterBase * p (&(*i));
88         interpreters_.erase(i);
89         p->releaseImpl(); // might call PacketImpl::release and might delete p
90     }
91 }
92
93 prefix_ void senf::detail::PacketImpl::updateIterators(PacketData * self, difference_type pos,
94                                                        difference_type n)
95 {
96     // I hate to change the PacketData representation from here, I would have preferred to let
97     // PacketData have authority over this but trying that just get's to convoluted so I choose the
98     // simple solution and made PacketImpl a friend of PacketData.
99
100     interpreter_list::iterator i (interpreters_.begin());
101
102     // There are three types of packets
103     // a) Those which come before 'self' in the interpreter chain
104     // b) 'self'
105     // c) Those that come afterwards
106     // For a), the change must be inside the packet since 'self' must be within those packets
107     // For b), the change must also be within since that's the packet we are changeing
108     // For c), the change must be outside the packet (we don't allow an upper packet to mess with
109     // the the data owned by a packet further down the chain). It can be before or after the
110     // packet.
111
112     // a)
113     for (; &(*i) != static_cast<PacketInterpreterBase*>(self); ++i) i->end_ += n;
114
115     // b)
116     i->end_ += n;
117
118     // c)
119     interpreter_list::iterator const i_end (interpreters_.end());
120     if (++i != i_end)
121         if (pos <= difference_type(i->begin_))
122             // pos is before the packet, it must then be before all futher packets ...
123             for (; i != i_end; ++i) {
124                 i->begin_ += n;
125                 i->end_ += n;
126             }
127         // else pos is after the packet and we don't need to change anything ...
128 }
129
130 ///////////////////////////////cc.e////////////////////////////////////////
131 #undef prefix_
132 //#include "PacketImpl.mpp"
133
134 \f
135 // Local Variables:
136 // mode: c++
137 // fill-column: 100
138 // c-file-style: "senf"
139 // indent-tabs-mode: nil
140 // ispell-local-dictionary: "american"
141 // compile-command: "scons -u test"
142 // comment-column: 40
143 // End: