6d1b3f780b84aef5f33d76b32e531d392046852f
[senf.git] / senf / Packets / PacketData.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 PacketData public header */
25
26 #ifndef HH_SENF_Packets_PacketData_
27 #define HH_SENF_Packets_PacketData_ 1
28
29 // Custom includes
30 #include <boost/utility.hpp>
31 #include <boost/type_traits.hpp>
32 #include <senf/Utils/Exception.hh>
33
34 //#include "PacketData.mpp"
35 ///////////////////////////////hh.p////////////////////////////////////////
36
37 namespace senf {
38
39     /** \brief Packet data STL-sequence view
40
41         The PacketData class provides an STL-sequence compatible view of the raw packet data. Each
42         packet/header/interpreter in the chain references the same storage area, presenting a
43         different (but nested/overlapping) section of the data.
44
45         Whenever the data is manipulated through PacketData, the change is assumed to be within the
46         data range of that packet: All insertions take place \e inside \c this packet and \e outside
47         any following packets in the packet chain.
48
49         \warning It is not permissible to change data belonging to a following
50             packet/header/interpreter even though this data is part of \c this sequence. Doing so
51             will corrupt the packet data.
52
53         \par
54
55         \warning When accessing packet data via the PacketData interface you are on your own: The
56             packet is not validated in any way, you bypass all parsers.
57
58         All public members are those of an STL random-access sequence.
59
60         \implementation This class is very tightly integrated with PacketInterpreterBase /
61             PacketInterpreter. It is separated out of those classes primarily to provide a clean
62             sequence interface to the library user and not for implementation reasons (it would have
63             been simpler to implement all these members in PacketInterpreterBase).
64
65         \ingroup packet_module
66       */
67     class PacketData
68         : boost::noncopyable
69     {
70     public:
71         ///////////////////////////////////////////////////////////////////////////
72         // Types
73
74         typedef senf::detail::packet::iterator iterator;
75         typedef senf::detail::packet::const_iterator const_iterator;
76         typedef senf::detail::packet::size_type size_type;
77         typedef senf::detail::packet::difference_type difference_type;
78         typedef senf::detail::packet::byte byte;
79         typedef byte value_type;
80         typedef byte & reference;
81         typedef byte const & const_reference;
82         typedef byte * pointer;
83         typedef byte const * const_pointer;
84
85         ///////////////////////////////////////////////////////////////////////////
86         ///\name Structors and default members
87         ///@{
88
89         // no public constructors
90         // no conversion constructors
91
92         ///@}
93         ///////////////////////////////////////////////////////////////////////////
94
95         ///\name Sequence interface to raw data
96         ///@{
97
98         iterator begin() const; ///< Return iterator to beginning
99                                 /**< Returns an <em>random access iterator</em> referring to the
100                                      first byte of the packet data. */
101         iterator end() const; ///< Return iterator to end
102                               /**< Returns an <em>random access iterator</em> referring to the
103                                    byte past the end of the packet data. */
104         size_type size() const; ///< Returns the number of bytes in the packet data.
105         bool empty() const; ///< Test whether the packet data is empty.
106                             /**< Returns whether the packet data is empty, i.e. whether its size
107                                  is 0. This function does not modify the content of the packet
108                                  data in any way. To clear the content use clear() */
109         byte operator[](size_type n) const; ///< Access byte in the packet data
110         byte & operator[](size_type n); ///< Access byte in the packet data
111
112         // Modifying the raw packet data
113
114         // IMPORTANT NOTE: It is not possible to insert data AFTER an empty packet
115         // since for an empty packet begin() == end(). However, I hope this problem is
116         // only academic since what should an empty packet be good for ?
117         void insert(iterator pos, byte v);
118         void insert(iterator pos, size_type n, byte v);
119 #       ifndef DOXYGEN
120         template <class InputIterator>
121         void insert(iterator pos, InputIterator f, InputIterator l,
122                     typename boost::disable_if< boost::is_convertible<InputIterator,size_type> >::type * = 0);
123 #       else
124         template <class InputIterator>
125         void insert(iterator pos, InputIterator f, InputIterator l);
126 #       endif
127
128         void erase(iterator pos);
129         void erase(iterator first, iterator last);
130         void clear(); ///< All bytes of the packet data dropped, leaving the container with a size of 0. */
131
132         void resize(size_type n, byte v=0);
133
134         void reserve(size_type n);
135         size_type capacity() const;
136
137         ///@}
138
139     protected:
140         PacketData(size_type b, size_type e);
141
142         /// Need to make this protected so we can change it in the derived class
143         detail::PacketImpl * impl_;
144
145         detail::PacketImpl & impl() const;
146
147         bool valid();
148
149     private:
150         size_type begin_;
151         size_type end_;
152
153         friend class detail::PacketImpl;
154     };
155
156     class PacketParserBase;
157
158     /** \brief Invalid packet data access
159
160         This exception is signaled whenever an operation tries to access an out-of-bounds data
161         byte. If the packet has been implemented correctly, this signals a malformed packet.
162      */
163     struct TruncatedPacketException : public senf::Exception
164     { TruncatedPacketException() : senf::Exception("truncated packet") {} };
165
166 }
167
168 ///////////////////////////////hh.e////////////////////////////////////////
169 #endif
170 #if !defined(HH_SENF_Packets_Packets__decls_) && !defined(HH_SENF_Packets_PacketData_i_)
171 #define HH_SENF_Packets_PacketData_i_
172 #include "PacketData.cci"
173 //#include "PacketData.ct"
174 #include "PacketData.cti"
175 #endif
176
177 \f
178 // Local Variables:
179 // mode: c++
180 // fill-column: 100
181 // c-file-style: "senf"
182 // indent-tabs-mode: nil
183 // ispell-local-dictionary: "american"
184 // compile-command: "scons -u test"
185 // comment-column: 40
186 // End:
187