Merged revisions 262,264-265,267-282,284-298,300-311 via svnmerge from
[senf.git] / Packets / Packet.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 Packet public header */
23
24 #ifndef HH_Packet_
25 #define HH_Packet_ 1
26
27 // Custom includes
28 #include <boost/operators.hpp>
29
30 #include "Utils/Exception.hh"
31 #include "Utils/SafeBool.hh"
32 #include "PacketInterpreter.hh"
33
34 //#include "Packet.mpp"
35 ///////////////////////////////hh.p////////////////////////////////////////
36
37 namespace senf {
38
39     template <class PackeType> class ConcretePacket;
40     
41     /** \brief
42       */
43     class Packet
44         : public SafeBool<Packet>,
45           public boost::equality_comparable<Packet>
46     {
47     public:
48         ///////////////////////////////////////////////////////////////////////////
49         // Types
50         
51         typedef void type;
52         typedef senf::detail::packet::iterator iterator;
53         typedef senf::detail::packet::const_iterator const_iterator;
54         typedef senf::detail::packet::size_type size_type;
55         typedef senf::detail::packet::difference_type difference_type;
56         typedef senf::detail::packet::byte byte;
57         typedef PacketInterpreterBase::factory_t factory_t;
58
59         enum NoInit_t { noinit };
60
61         ///////////////////////////////////////////////////////////////////////////
62         ///\name Structors and default members
63         ///@{
64
65         // default copy constructor
66         // default copy assignment
67         // default destructor
68         
69         Packet();
70         Packet clone() const;
71
72         // conversion constructors
73
74         template <class PacketType>
75         Packet(ConcretePacket<PacketType> packet);
76
77         ///@}
78         ///////////////////////////////////////////////////////////////////////////
79
80         ///\name Interpreter chain access
81         ///@{
82
83                                      Packet      next() const;
84         template <class OtherPacket> OtherPacket next() const;
85         template <class OtherPacket> OtherPacket next(NoThrow_t) const;
86         template <class OtherPacket> OtherPacket findNext() const;
87         template <class OtherPacket> OtherPacket findNext(NoThrow_t) const;
88
89                                      Packet      prev() const;
90         template <class OtherPacket> OtherPacket prev() const;
91         template <class OtherPacket> OtherPacket prev(NoThrow_t) const;
92         template <class OtherPacket> OtherPacket findPrev() const;
93         template <class OtherPacket> OtherPacket findPrev(NoThrow_t) const;
94
95                                      Packet      first() const;
96         template <class OtherPacket> OtherPacket first() const;
97         template <class OtherPacket> OtherPacket first(NoThrow_t) const;
98
99                                      Packet      last() const;
100         template <class OtherPacket> OtherPacket last() const;
101         template <class OtherPacket> OtherPacket last(NoThrow_t) const;
102
103
104         template <class OtherPacket> OtherPacket parseNextAs() const;
105                                      Packet      parseNextAs(factory_t factory) const;
106         template <class OtherPacket> bool        is() const;
107         template <class OtherPacket> OtherPacket as() const;
108
109         Packet append(Packet packet) const;
110
111         ///@}
112
113         ///\name Data access
114         ///@{
115
116         PacketData & data() const;
117         size_type size() const;
118         
119         ///@}
120
121         ///\name Other methods
122         ///@{
123
124         bool operator==(Packet other) const;
125         bool boolean_test() const;
126
127         void finalize() const;
128
129         void dump(std::ostream & os) const;
130
131         TypeIdValue typeId() const;
132         factory_t factory() const;
133         
134         ///@}
135
136     protected:
137         explicit Packet(PacketInterpreterBase::ptr packet);
138
139         PacketInterpreterBase::ptr ptr() const;
140
141     private:
142         Packet checkNext() const;
143         Packet checkLast() const;
144         
145         PacketInterpreterBase::ptr packet_;
146         
147         template <class PacketType>
148         friend class ConcretePacket;
149     };
150
151     /** \brief
152       */
153     template <class PacketType>
154     class ConcretePacket 
155         : public Packet
156     {
157     public:
158         ///////////////////////////////////////////////////////////////////////////
159         // Types
160         
161         typedef PacketType type;
162         typedef PacketInterpreter<PacketType> interpreter;
163
164         ///////////////////////////////////////////////////////////////////////////
165         ///\name Structors and default members
166         ///@{
167
168         // default copy constructor
169         // default copy assignment
170         // default destructor
171         // no conversion constructors
172
173         ConcretePacket();
174
175         static factory_t factory();
176
177         // Create completely new packet
178
179         static ConcretePacket create();
180         static ConcretePacket create(NoInit_t);
181         static ConcretePacket create(size_type size);
182         static ConcretePacket create(size_type size, NoInit_t);
183         template <class ForwardReadableRange>
184         static ConcretePacket create(ForwardReadableRange const & range);
185
186         // Create packet as new packet after a given packet
187
188         static ConcretePacket createAfter(Packet packet);
189         static ConcretePacket createAfter(Packet packet, NoInit_t);
190         static ConcretePacket createAfter(Packet packet, size_type size);
191         static ConcretePacket createAfter(Packet packet, size_type size, NoInit_t);
192         template <class ForwardReadableRange>
193         static ConcretePacket createAfter(Packet packet, 
194                                           ForwardReadableRange const & range);
195
196         // Create packet as new packet (header) before a given packet
197
198         static ConcretePacket createBefore(Packet packet);
199         static ConcretePacket createBefore(Packet packet, NoInit_t);
200
201         // Create a clone of the current packet
202
203         ConcretePacket clone() const;
204
205         ///@}
206         ///////////////////////////////////////////////////////////////////////////
207
208         // Field access
209
210         typename interpreter::parser * operator->() const;
211
212     protected:
213
214     private:
215         ConcretePacket(typename interpreter::ptr packet_);
216         
217         typename interpreter::ptr ptr() const;
218
219         friend class Packet;
220         friend class PacketInterpreter<PacketType>;
221     };
222
223 }
224
225 ///////////////////////////////hh.e////////////////////////////////////////
226 #include "Packet.cci"
227 #include "Packet.ct"
228 #include "Packet.cti"
229 #endif
230
231 \f
232 // Local Variables:
233 // mode: c++
234 // fill-column: 100
235 // c-file-style: "senf"
236 // indent-tabs-mode: nil
237 // ispell-local-dictionary: "american"
238 // compile-command: "scons -u test"
239 // End: