Packets: not really a smart but efficient 'shortcut' for ConcretePacket::next() ...
[senf.git] / senf / Packets / Packet.cci
1 // $Id$
2 //
3 // Copyright (C) 2007
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 //
6 // The contents of this file are subject to the Fraunhofer FOKUS Public License
7 // Version 1.0 (the "License"); you may not use this file except in compliance
8 // with the License. You may obtain a copy of the License at 
9 // http://senf.berlios.de/license.html
10 //
11 // The Fraunhofer FOKUS Public License Version 1.0 is based on, 
12 // but modifies the Mozilla Public License Version 1.1.
13 // See the full license text for the amendments.
14 //
15 // Software distributed under the License is distributed on an "AS IS" basis, 
16 // WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 
17 // for the specific language governing rights and limitations under the License.
18 //
19 // The Original Code is Fraunhofer FOKUS code.
20 //
21 // The Initial Developer of the Original Code is Fraunhofer-Gesellschaft e.V. 
22 // (registered association), Hansastraße 27 c, 80686 Munich, Germany.
23 // All Rights Reserved.
24 //
25 // Contributor(s):
26 //   Stefan Bund <g0dil@berlios.de>
27
28 /** \file
29     \brief Packet inline non-template implementation */
30
31 // Custom includes
32 #include <senf/Utils/senfassert.hh>
33
34 #define prefix_ inline
35 //-/////////////////////////////////////////////////////////////////////////////////////////////////
36
37 //-/////////////////////////////////////////////////////////////////////////////////////////////////
38 // senf::Packet
39
40 // protected members
41
42 prefix_  senf::Packet::Packet(PacketInterpreterBase::ptr const & packet)
43     : packet_(packet)
44 {}
45
46 prefix_ senf::PacketInterpreterBase::ptr const & senf::Packet::ptr()
47     const
48 {
49     SENF_ASSERT(packet_, "Invalid operation (dereferencing) on in-valid() Packet");
50     return packet_;
51 }
52
53 // public structors
54
55 prefix_ senf::Packet::Packet()
56 {}
57
58 // public members
59
60 prefix_ senf::Packet senf::Packet::clone()
61     const
62 {
63     return Packet(ptr()->clone());
64 }
65
66 // Interpreter chain access
67
68 prefix_ senf::Packet senf::Packet::next(NoThrow_t)
69     const
70 {
71     PacketInterpreterBase::ptr p (ptr()->next());
72     if (p) return Packet(p);
73     PacketInterpreterBase::optional_range r (ptr()->nextPacketRange());
74     return (r && ! r->empty()) ? getNext(r) : Packet();
75 }
76
77 prefix_ senf::Packet senf::Packet::next()
78     const
79 {
80     Packet p (next(nothrow));
81     if (!p) throw InvalidPacketChainException();
82     return p;
83 }
84
85 prefix_ senf::Packet senf::Packet::prev(NoThrow_t)
86     const
87 {
88     return Packet(ptr()->prev());
89 }
90
91 prefix_ senf::Packet senf::Packet::prev()
92     const
93 {
94     Packet p (prev(nothrow));
95     if (!p) throw InvalidPacketChainException();
96     return p;
97 }
98
99 prefix_ senf::Packet senf::Packet::first()
100     const
101 {
102     return Packet(ptr()->first());
103 }
104
105 prefix_ senf::Packet senf::Packet::last()
106     const
107 {
108     Packet p (ptr()->last());
109     return p.next(nothrow) ? getLast() : p;
110 }
111
112 prefix_ senf::Packet senf::Packet::parseNextAs(factory_t factory)
113     const
114 {
115     return Packet(ptr()->parseNextAs(factory, ptr()->nextPacketRange()));
116 }
117
118 prefix_ senf::Packet senf::Packet::parseNextAs(factory_t factory, PacketInterpreterBase::optional_range const & range)
119     const
120 {
121     return Packet(ptr()->parseNextAs(factory, range));
122 }
123
124 prefix_ senf::Packet senf::Packet::append(Packet const & packet)
125     const
126 {
127     return Packet(ptr()->append(packet.ptr()));
128 }
129
130 // Data access
131
132 prefix_ senf::PacketData & senf::Packet::data()
133     const
134 {
135     return ptr()->data();
136 }
137
138 prefix_ senf::Packet::size_type senf::Packet::size()
139     const
140 {
141     return data().size();
142 }
143
144 // Other methods
145
146 prefix_ bool senf::Packet::operator==(Packet const & other)
147     const
148 {
149     return ptr() == other.ptr();
150 }
151
152 prefix_ void senf::Packet::finalizeThis()
153 {
154     ptr()->finalizeThis();
155 }
156
157 prefix_ void senf::Packet::finalizeTo(Packet const & other)
158 {
159     ptr()->finalizeTo(other.ptr());
160 }
161
162 prefix_ void senf::Packet::finalizeAll()
163 {
164     ptr()->finalizeTo(last().ptr());
165 }
166
167 prefix_ senf::TypeIdValue senf::Packet::typeId()
168     const
169 {
170     return ptr()->typeId();
171 }
172
173 prefix_ senf::Packet::factory_t senf::Packet::factory()
174     const
175 {
176     return ptr()->factory();
177 }
178
179 prefix_ unsigned long senf::Packet::id()
180     const
181 {
182     return reinterpret_cast<unsigned long>(&ptr()->impl());
183 }
184
185 prefix_ bool senf::Packet::boolean_test()
186     const
187 {
188     return packet_ && packet_->valid();
189 }
190
191 prefix_ bool senf::Packet::valid()
192     const
193 {
194     return *this;
195 }
196
197 prefix_ bool senf::Packet::is_shared()
198     const
199 {
200     return ptr()->is_shared() || (ptr()->impl().refcount() > 1);
201 }
202
203 prefix_ void senf::Packet::reparse()
204     const
205 {
206     return ptr()->reparse();
207 }
208
209 prefix_ void senf::Packet::clearAnnotations()
210 {
211     return ptr()->clearAnnotations();
212 }
213
214 template <class PacketType, class Parser>
215 prefix_ Parser senf::operator<<(Parser target, ConcretePacket<PacketType> const & packet)
216 {
217     target << packet.parser();
218     return target;
219 }
220
221 //-/////////////////////////////////////////////////////////////////////////////////////////////////
222 #undef prefix_
223
224 \f
225 // Local Variables:
226 // mode: c++
227 // fill-column: 100
228 // c-file-style: "senf"
229 // indent-tabs-mode: nil
230 // ispell-local-dictionary: "american"
231 // compile-command: "scons -u test"
232 // comment-column: 40
233 // End: