cb9e4de79c79558277f31fc8130780712b73ab87
[senf.git] / senf / Packets / Packet.cci
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 Packet inline non-template implementation */
25
26 // Custom includes
27 #include <senf/Utils/senfassert.hh>
28
29 #define prefix_ inline
30 ///////////////////////////////cci.p///////////////////////////////////////
31
32 ///////////////////////////////////////////////////////////////////////////
33 // senf::Packet
34
35 // protected members
36
37 prefix_  senf::Packet::Packet(PacketInterpreterBase::ptr const & packet)
38     : packet_(packet)
39 {}
40
41 prefix_ senf::PacketInterpreterBase::ptr const & senf::Packet::ptr()
42     const
43 {
44     SENF_ASSERT(packet_, "Invalid operation (dereferencing) on in-valid() Packet");
45     return packet_;
46 }
47
48 // public structors
49
50 prefix_ senf::Packet::Packet()
51 {}
52
53 // public members
54
55 prefix_ senf::Packet senf::Packet::clone()
56     const
57 {
58     return Packet(ptr()->clone());
59 }
60
61 // Interpreter chain access
62
63 prefix_ senf::Packet senf::Packet::next(NoThrow_t)
64     const
65 {
66     PacketInterpreterBase::ptr p (ptr()->next());
67     if (p) return Packet(p);
68     PacketInterpreterBase::optional_range r (ptr()->nextPacketRange());
69     return (r && ! r->empty()) ? getNext() : Packet();
70 }
71
72 prefix_ senf::Packet senf::Packet::next()
73     const
74 {
75     Packet p (next(nothrow));
76     if (!p) throw InvalidPacketChainException();
77     return p;
78 }
79
80 prefix_ senf::Packet senf::Packet::prev(NoThrow_t)
81     const
82 {
83     return Packet(ptr()->prev());
84 }
85
86 prefix_ senf::Packet senf::Packet::prev()
87     const
88 {
89     Packet p (prev(nothrow));
90     if (!p) throw InvalidPacketChainException();
91     return p;
92 }
93
94 prefix_ senf::Packet senf::Packet::first()
95     const
96 {
97     return Packet(ptr()->first());
98 }
99
100 prefix_ senf::Packet senf::Packet::last()
101     const
102 {
103     Packet p (ptr()->last());
104     return p.next(nothrow) ? getLast() : p;
105 }
106
107 prefix_ senf::Packet senf::Packet::parseNextAs(factory_t factory)
108     const
109 {
110     return Packet(ptr()->parseNextAs(factory));
111 }
112
113 prefix_ senf::Packet senf::Packet::append(Packet const & packet)
114     const
115 {
116     return Packet(ptr()->append(packet.ptr()));
117 }
118
119 // Data access
120
121 prefix_ senf::PacketData & senf::Packet::data()
122     const
123 {
124     return ptr()->data();
125 }
126
127 prefix_ senf::Packet::size_type senf::Packet::size()
128     const
129 {
130     return data().size();
131 }
132
133
134 // Other methods
135
136 prefix_ bool senf::Packet::operator==(Packet const & other)
137     const
138 {
139     return ptr() == other.ptr();
140 }
141
142 prefix_ void senf::Packet::finalizeThis()
143 {
144     ptr()->finalizeThis();
145 }
146
147 prefix_ void senf::Packet::finalizeTo(Packet const & other)
148 {
149     ptr()->finalizeTo(other.ptr());
150 }
151
152 prefix_ void senf::Packet::finalizeAll()
153 {
154     ptr()->finalizeTo(last().ptr());
155 }
156
157 prefix_ void senf::Packet::dump(std::ostream & os)
158     const
159 {
160     last(); // Make sure the packet is complete
161     ptr()->dump(os);
162 }
163
164 prefix_ senf::TypeIdValue senf::Packet::typeId()
165     const
166 {
167     return ptr()->typeId();
168 }
169
170 prefix_ senf::Packet::factory_t senf::Packet::factory()
171     const
172 {
173     return ptr()->factory();
174 }
175
176 prefix_ unsigned long senf::Packet::id()
177     const
178 {
179     return reinterpret_cast<unsigned long>(&ptr()->impl());
180 }
181
182 prefix_ bool senf::Packet::boolean_test()
183     const
184 {
185     return packet_ && packet_->valid();
186 }
187
188 prefix_ bool senf::Packet::valid()
189     const
190 {
191     return *this;
192 }
193
194 prefix_ bool senf::Packet::is_shared()
195     const
196 {
197     return ptr()->is_shared() || (ptr()->impl().refcount() > 1);
198 }
199
200 template <class PacketType, class Parser>
201 prefix_ Parser senf::operator<<(Parser target, ConcretePacket<PacketType> const & packet)
202 {
203     target << packet.parser();
204     return target;
205 }
206
207 ///////////////////////////////cci.e///////////////////////////////////////
208 #undef prefix_
209
210 \f
211 // Local Variables:
212 // mode: c++
213 // fill-column: 100
214 // c-file-style: "senf"
215 // indent-tabs-mode: nil
216 // ispell-local-dictionary: "american"
217 // compile-command: "scons -u test"
218 // comment-column: 40
219 // End: