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