PacketInterpreter.cti
Go to the documentation of this file.
1 //
2 // Copyright (c) 2020 Fraunhofer Institute for Applied Information Technology (FIT)
3 // Network Research Group (NET)
4 // Schloss Birlinghoven, 53754 Sankt Augustin, GERMANY
5 // Contact: support@wiback.org
6 //
7 // This file is part of the SENF code tree.
8 // It is licensed under the 3-clause BSD License (aka New BSD License).
9 // See LICENSE.txt in the top level directory for details or visit
10 // https://opensource.org/licenses/BSD-3-Clause
11 //
12 
13 
14 /** \file
15  \brief PacketInterpreter inline template implementation */
16 
17 //#include "PacketInterpreter.ih"
18 
19 // Custom includes
20 
21 #define prefix_ inline
22 //-/////////////////////////////////////////////////////////////////////////////////////////////////
23 
24 //-/////////////////////////////////////////////////////////////////////////////////////////////////
25 // senf::PacketInterpreterBase
26 
27 // Interpreter chain access
28 
29 template <class Type>
30 prefix_ typename senf::PacketInterpreter<Type>::ptr senf::PacketInterpreterBase::as()
31 {
32  // ensure that the template argument is included in the corresponding object file when linking:
33  { static typename PacketInterpreter<Type>::factory_t _ (PacketInterpreter<Type>::factory()); (void) _;}
34  return typename PacketInterpreter<Type>::ptr(
35  static_cast< PacketInterpreter<Type>* >(this));
36 }
37 
38 template <class Annotation>
39 prefix_ Annotation & senf::PacketInterpreterBase::annotation()
40 {
41  return impl().annotation<Annotation>();
42 }
43 
44 //-/////////////////////////////////////////////////////////////////////////////////////////////////
45 // senf::PacketInterpreter<PacketType>
46 
47 template <class PacketType>
48 prefix_ senf::PacketInterpreterBase::factory_t
49 senf::PacketInterpreter<PacketType>::factory()
50 {
51  static const FactoryImpl factory;
52  return & factory;
53 }
54 
55 // Create completely new packet
56 
57 template <class PacketType>
58 prefix_ typename senf::PacketInterpreter<PacketType>::ptr
59 senf::PacketInterpreter<PacketType>::create()
60 {
61  return create(initSize());
62 }
63 
64 template <class PacketType>
65 prefix_ typename senf::PacketInterpreter<PacketType>::ptr
66 senf::PacketInterpreter<PacketType>::create(senf::NoInit_t)
67 {
68  return create(0,senf::noinit);
69 }
70 
71 template <class PacketType>
72 template <class ForwardReadableRange>
73 prefix_ typename senf::PacketInterpreter<PacketType>::ptr
74 senf::PacketInterpreter<PacketType>::create(ForwardReadableRange const & range)
75 {
76  detail::PacketImpl::Guard p (new detail::PacketImpl(boost::begin(range),boost::end(range)));
77  ptr pi (create(p.p,p.p->begin(),p.p->end(),Append));
78  return pi;
79 }
80 
81 // Create packet as new packet after a given packet
82 
83 template <class PacketType>
84 prefix_ typename senf::PacketInterpreter<PacketType>::ptr
85 senf::PacketInterpreter<PacketType>::createAfter(PacketInterpreterBase::ptr const & packet)
86 {
87  return createAfter(packet, initSize());
88 }
89 
90 template <class PacketType>
91 prefix_ typename senf::PacketInterpreter<PacketType>::ptr
92 senf::PacketInterpreter<PacketType>::createAfter(PacketInterpreterBase::ptr const & packet, senf::NoInit_t)
93 {
94  return createAfter(packet, 0, senf::noinit);
95 }
96 
97 // Create clone of current packet
98 
99 template <class PacketType>
100 prefix_ typename senf::PacketInterpreter<PacketType>::ptr
101 senf::PacketInterpreter<PacketType>::clone()
102 {
103  return boost::static_pointer_cast<typename ptr::element_type>(PacketInterpreterBase::clone());
104 }
105 
106 // Packet field access
107 
108 template <class PacketType>
109 prefix_ typename senf::PacketInterpreter<PacketType>::parser
110 senf::PacketInterpreter<PacketType>::fields()
111 {
112  return parser(data().begin(),&data());
113 }
114 
115 //-/////////////////////////////////////////////////////////////////////////////////////////////////
116 // private members
117 
118 // Private structors
119 
120 template <class PacketType>
121 prefix_ typename senf::PacketInterpreter<PacketType>::ptr
122 senf::PacketInterpreter<PacketType>::create(detail::PacketImpl * impl, iterator b, iterator e,
123  Append_t)
124 {
125  // PacketInterpreter constructor does not throw
126  void * storage (impl->allocateInterpreter());
127  return ptr(new (storage) PacketInterpreter(impl,b,e,Append));
128 }
129 
130 template <class PacketType>
131 prefix_ typename senf::PacketInterpreter<PacketType>::ptr
132 senf::PacketInterpreter<PacketType>::create(detail::PacketImpl * impl, iterator b, iterator e,
133  Prepend_t)
134 {
135  // PacketInterpreter constructor does not throw
136  void * storage (impl->allocateInterpreter());
137  return ptr(new (storage) PacketInterpreter(impl,b,e,Prepend));
138 }
139 
140 template <class PacketType>
141 prefix_ typename senf::PacketInterpreter<PacketType>::ptr
142 senf::PacketInterpreter<PacketType>::create(detail::PacketImpl * impl, iterator b, iterator e,
143  PacketInterpreterBase::ptr const & before)
144 {
145  // PacketInterpreter constructor does not throw
146  void * storage (impl->allocateInterpreter());
147  return ptr(new (storage) PacketInterpreter(impl,b,e,before));
148 }
149 
150 template <class PacketType>
151 prefix_ senf::PacketInterpreter<PacketType>::PacketInterpreter(detail::PacketImpl * impl,
152  iterator b, iterator e, Append_t)
153  : PacketInterpreterBase(impl,b,e,Append)
154 {
155  SENF_STATIC_ASSERT(sizeof(PacketInterpreter) == sizeof(PacketInterpreterBase),
156  invalid_size_of_packetinterpreter);
157 }
158 
159 template <class PacketType>
160 prefix_ senf::PacketInterpreter<PacketType>::PacketInterpreter(detail::PacketImpl * impl,
161  iterator b, iterator e, Prepend_t)
162  : PacketInterpreterBase(impl,b,e,Prepend)
163 {
164  SENF_STATIC_ASSERT(sizeof(PacketInterpreter) == sizeof(PacketInterpreterBase),
165  invalid_size_of_packetinterpreter);
166 }
167 
168 template <class PacketType>
169 prefix_
170 senf::PacketInterpreter<PacketType>::PacketInterpreter(detail::PacketImpl * impl, iterator b,
171  iterator e,
172  PacketInterpreterBase::ptr const & before)
173  : PacketInterpreterBase(impl,b,e,before)
174 {
175  SENF_STATIC_ASSERT(sizeof(PacketInterpreter) == sizeof(PacketInterpreterBase),
176  invalid_size_of_derived_packetinterpreter);
177 }
178 
179 // PacketType access
180 
181 template <class PacketType>
182 prefix_ senf::PacketInterpreterBase::size_type
183 senf::PacketInterpreter<PacketType>::initSize()
184 {
185  return type::initSize();
186 }
187 
188 template <class PacketType>
189 prefix_ senf::PacketInterpreterBase::size_type
190 senf::PacketInterpreter<PacketType>::initHeadSize()
191 {
192  size_type sz (type::initHeadSize());
193  return sz == size_type(-1) ? initSize() : sz ;
194 }
195 
196 template <class PacketType>
197 prefix_ void senf::PacketInterpreter<PacketType>::init()
198 {
199  return type::init(ConcretePacket<PacketType>(ptr(this)));
200 }
201 
202 //-/////////////////////////////////////////////////////////////////////////////////////////////////
203 #undef prefix_
204 
205 
206 // Local Variables:
207 // mode: c++
208 // fill-column: 100
209 // c-file-style: "senf"
210 // indent-tabs-mode: nil
211 // ispell-local-dictionary: "american"
212 // compile-command: "scons -u test"
213 // comment-column: 40
214 // End: