613fd8aac94b16f3cf65f3ca271dc2caa88abeb4
[senf.git] / Packets / PacketImpl.hh
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 public header */
25
26 #ifndef HH_PacketImpl_
27 #define HH_PacketImpl_ 1
28
29 // Custom includes
30 #include <memory>
31 #include <boost/utility.hpp>
32 #include "../Utils/pool_alloc_mixin.hh"
33 #include "PacketTypes.hh"
34
35 //#include "PacketImpl.mpp"
36 ///////////////////////////////hh.p////////////////////////////////////////
37
38 namespace senf {
39 namespace detail {
40     
41     /** \brief Internal: Packet data storage
42         
43         \internal
44
45         This is the class holding the packet data and the interpreter chain. All manipulations of
46         the packet data are performed via the interface exported here. This is very important, since
47         PacketImpl will update the interpreters (that is the vector indices stored therein) whenever
48         the data is changed.
49      */
50     class PacketImpl 
51         : boost::noncopyable,
52           public pool_alloc_mixin<PacketImpl>
53     {
54     public:
55         typedef senf::detail::packet::byte byte;
56         typedef senf::detail::packet::raw_container raw_container;
57         typedef senf::detail::packet::size_type size_type;
58         typedef senf::detail::packet::difference_type difference_type;
59         typedef senf::detail::packet::interpreter_list interpreter_list;
60         typedef senf::detail::packet::iterator iterator;
61         typedef senf::detail::packet::const_iterator const_iterator;
62         typedef senf::detail::packet::refcount_t refcount_t;
63
64         // structors
65
66         PacketImpl();
67         PacketImpl(size_type size, byte initValue);
68         template <class InputIterator>
69         PacketImpl(InputIterator b, InputIterator e);
70         ~PacketImpl();
71
72         // rerference/memory management
73  
74         void add_ref(refcount_t n=1);
75         void release(refcount_t n=1);
76         refcount_t refcount() const;
77
78         // Interpreter chain
79
80         PacketInterpreterBase * first();
81         PacketInterpreterBase * last();
82
83         PacketInterpreterBase * next(PacketInterpreterBase * p);
84         PacketInterpreterBase * prev(PacketInterpreterBase * p);
85
86         void appendInterpreter    (PacketInterpreterBase * p);
87         void prependInterpreter   (PacketInterpreterBase * p);
88         void truncateInterpreters (PacketInterpreterBase * p);
89         void truncateInterpretersBackwards (PacketInterpreterBase * p);
90
91         // Data container
92
93         iterator begin();
94         iterator end();
95         size_type size();
96
97         void insert(PacketData * self, iterator pos, byte v);
98         void insert(PacketData * self, iterator pos, size_type n, byte v);
99         template <class ForwardIterator>
100         void insert(PacketData * self, iterator pos, ForwardIterator f, ForwardIterator l);
101
102         void erase(PacketData * self, iterator pos);
103         void erase(PacketData * self, iterator first, iterator last);
104         void clear(PacketData * self);
105
106         /** \brief Internal: Keep PacketImpl instance alive
107
108             \internal
109
110             The Guard will keep the PacketImpl instance alive during a members execution time
111             It the refcount should drop to 0, PacketImpl will be deleted after the member
112             has completed executing.
113          */
114         struct Guard {
115             Guard(PacketImpl * impl);
116             ~Guard();
117             PacketImpl * p;
118         };
119
120     private:
121         refcount_t refcount_;
122         raw_container data_;
123         interpreter_list interpreters_;
124
125         void eraseInterpreters(interpreter_list::iterator b, interpreter_list::iterator e);
126         void updateIterators(PacketData * self, difference_type pos, difference_type n);
127     };
128
129 }}
130
131 ///////////////////////////////hh.e////////////////////////////////////////
132 #endif
133 #if !defined(HH_Packets__decls_) && !defined(HH_PacketImpl_i_)
134 #define HH_PacketImpl_i_
135 #include "PacketImpl.cci"
136 //#include "PacketImpl.ct"
137 #include "PacketImpl.cti"
138 #endif
139
140 \f
141 // Local Variables:
142 // mode: c++
143 // fill-column: 100
144 // c-file-style: "senf"
145 // indent-tabs-mode: nil
146 // ispell-local-dictionary: "american"
147 // compile-command: "scons -u test"
148 // comment-column: 40
149 // End:
150