Reapply changes missed during the previous merge (duh ...)
[senf.git] / Packets / PacketData.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 PacketData public header */
23
24 #ifndef HH_PacketData_
25 #define HH_PacketData_ 1
26
27 // Custom includes
28 #include <boost/utility.hpp>
29 #include <boost/type_traits.hpp>
30 #include <boost/iterator/iterator_facade.hpp>
31 #include "Utils/SafeBool.hh"
32 #include "PacketTypes.hh"
33
34 //#include "PacketData.mpp"
35 ///////////////////////////////hh.p////////////////////////////////////////
36
37 namespace senf {
38
39     /** \brief
40
41         PacketData only exists to separate out the container interface from PacketInterpreter.
42       */
43     class PacketData
44         : boost::noncopyable
45     {
46     public:
47         ///////////////////////////////////////////////////////////////////////////
48         // Types
49
50         typedef senf::detail::packet::smart_pointer<PacketData>::ptr_t ptr;
51         
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 byte value_type;
58         typedef byte & reference;
59         typedef byte const & const_reference;
60         typedef byte * pointer;
61         typedef byte const * const_pointer;
62
63         ///////////////////////////////////////////////////////////////////////////
64         ///\name Structors and default members
65         ///@{
66
67         // no public constructors
68         // no conversion constructors
69
70         ///@}
71         ///////////////////////////////////////////////////////////////////////////
72     
73         ///\name Sequence interface to raw data
74         ///@{
75
76         iterator begin() const;
77         iterator end() const;
78         size_type size() const;
79         bool empty() const;
80         byte operator[](size_type n) const;
81         byte & operator[](size_type n);
82
83         // Modifying the raw packet data
84
85         // IMPORTANT NOTE: It is not possible to insert data AFTER an empty packet
86         // since for an empty packet begin() == end(). However, I hope this problem is
87         // only academic since what should an empty packet be good for ?
88         void insert(iterator pos, byte v);
89         void insert(iterator pos, size_type n, byte v);
90         template <class InputIterator>
91         void insert(iterator pos, InputIterator f, InputIterator l,
92                     typename boost::disable_if< boost::is_convertible<InputIterator,size_type> >::type * = 0);
93
94         void erase(iterator pos);
95         void erase(iterator first, iterator last);
96         void clear();
97         
98         void resize(size_type n, byte v=0);
99
100         ///@}
101
102         bool valid();
103
104     protected:
105         PacketData(size_type b, size_type e);
106
107         detail::PacketImpl * impl_;
108
109         detail::PacketImpl & impl() const;
110
111     private:
112         size_type begin_;
113         size_type end_;
114
115         friend class detail::PacketImpl;
116     };
117
118     class PacketParserBase;
119
120     struct TruncatedPacketException : public std::exception
121     { virtual char const * what() const throw() { return "truncated packet"; } };
122
123     class safe_data_iterator
124         : public boost::iterator_facade< safe_data_iterator,
125                                          PacketData::value_type,
126                                          boost::random_access_traversal_tag >,
127           public ComparableSafeBool<safe_data_iterator>
128     {
129     public:
130         typedef PacketData::size_type size_type;
131
132         safe_data_iterator();
133         explicit safe_data_iterator(PacketData & data);
134         safe_data_iterator(PacketData & data, PacketData::iterator i);
135         explicit safe_data_iterator(PacketParserBase const & parser);
136
137         safe_data_iterator & operator=(PacketData::iterator i);
138         safe_data_iterator & operator=(PacketParserBase const & parser);
139         operator PacketData::iterator() const;
140
141         bool boolean_test() const;
142
143         PacketData & data() const;
144
145     private:
146         friend class boost::iterator_core_access;
147
148         // iterator_facade interface
149
150         value_type & dereference() const;
151         bool equal(safe_data_iterator const & other) const;
152         difference_type distance_to(safe_data_iterator const & other) const;
153         void increment();
154         void decrement();
155         void advance(difference_type n);
156
157         PacketData::iterator i() const;
158
159         PacketData * data_;
160         size_type i_;
161     };
162 }
163
164 ///////////////////////////////hh.e////////////////////////////////////////
165 #endif
166 #if !defined(HH_PacketData_DeclOnly) &&!defined(HH_PacketData_def)
167 #define HH_PacketData_def
168 #include "PacketData.cci"
169 //#include "PacketData.ct"
170 #include "PacketData.cti"
171 #endif
172
173 \f
174 // Local Variables:
175 // mode: c++
176 // fill-column: 100
177 // c-file-style: "senf"
178 // indent-tabs-mode: nil
179 // ispell-local-dictionary: "american"
180 // compile-command: "scons -u test"
181 // comment-column: 40
182 // End: