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
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
15 \brief PacketInterpreter inline template implementation */
17 //#include "PacketInterpreter.ih"
21 #define prefix_ inline
22 //-/////////////////////////////////////////////////////////////////////////////////////////////////
24 //-/////////////////////////////////////////////////////////////////////////////////////////////////
25 // senf::PacketInterpreterBase
27 // Interpreter chain access
30 prefix_ typename senf::PacketInterpreter<Type>::ptr senf::PacketInterpreterBase::as()
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));
38 template <class Annotation>
39 prefix_ Annotation & senf::PacketInterpreterBase::annotation()
41 return impl().annotation<Annotation>();
44 //-/////////////////////////////////////////////////////////////////////////////////////////////////
45 // senf::PacketInterpreter<PacketType>
47 template <class PacketType>
48 prefix_ senf::PacketInterpreterBase::factory_t
49 senf::PacketInterpreter<PacketType>::factory()
51 static const FactoryImpl factory;
55 // Create completely new packet
57 template <class PacketType>
58 prefix_ typename senf::PacketInterpreter<PacketType>::ptr
59 senf::PacketInterpreter<PacketType>::create()
61 return create(initSize());
64 template <class PacketType>
65 prefix_ typename senf::PacketInterpreter<PacketType>::ptr
66 senf::PacketInterpreter<PacketType>::create(senf::NoInit_t)
68 return create(0,senf::noinit);
71 template <class PacketType>
72 template <class ForwardReadableRange>
73 prefix_ typename senf::PacketInterpreter<PacketType>::ptr
74 senf::PacketInterpreter<PacketType>::create(ForwardReadableRange const & range)
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));
81 // Create packet as new packet after a given packet
83 template <class PacketType>
84 prefix_ typename senf::PacketInterpreter<PacketType>::ptr
85 senf::PacketInterpreter<PacketType>::createAfter(PacketInterpreterBase::ptr const & packet)
87 return createAfter(packet, initSize());
90 template <class PacketType>
91 prefix_ typename senf::PacketInterpreter<PacketType>::ptr
92 senf::PacketInterpreter<PacketType>::createAfter(PacketInterpreterBase::ptr const & packet, senf::NoInit_t)
94 return createAfter(packet, 0, senf::noinit);
97 // Create clone of current packet
99 template <class PacketType>
100 prefix_ typename senf::PacketInterpreter<PacketType>::ptr
101 senf::PacketInterpreter<PacketType>::clone()
103 return boost::static_pointer_cast<typename ptr::element_type>(PacketInterpreterBase::clone());
106 // Packet field access
108 template <class PacketType>
109 prefix_ typename senf::PacketInterpreter<PacketType>::parser
110 senf::PacketInterpreter<PacketType>::fields()
112 return parser(data().begin(),&data());
115 //-/////////////////////////////////////////////////////////////////////////////////////////////////
120 template <class PacketType>
121 prefix_ typename senf::PacketInterpreter<PacketType>::ptr
122 senf::PacketInterpreter<PacketType>::create(detail::PacketImpl * impl, iterator b, iterator e,
125 // PacketInterpreter constructor does not throw
126 void * storage (impl->allocateInterpreter());
127 return ptr(new (storage) PacketInterpreter(impl,b,e,Append));
130 template <class PacketType>
131 prefix_ typename senf::PacketInterpreter<PacketType>::ptr
132 senf::PacketInterpreter<PacketType>::create(detail::PacketImpl * impl, iterator b, iterator e,
135 // PacketInterpreter constructor does not throw
136 void * storage (impl->allocateInterpreter());
137 return ptr(new (storage) PacketInterpreter(impl,b,e,Prepend));
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)
145 // PacketInterpreter constructor does not throw
146 void * storage (impl->allocateInterpreter());
147 return ptr(new (storage) PacketInterpreter(impl,b,e,before));
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)
155 SENF_STATIC_ASSERT(sizeof(PacketInterpreter) == sizeof(PacketInterpreterBase),
156 invalid_size_of_packetinterpreter);
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)
164 SENF_STATIC_ASSERT(sizeof(PacketInterpreter) == sizeof(PacketInterpreterBase),
165 invalid_size_of_packetinterpreter);
168 template <class PacketType>
170 senf::PacketInterpreter<PacketType>::PacketInterpreter(detail::PacketImpl * impl, iterator b,
172 PacketInterpreterBase::ptr const & before)
173 : PacketInterpreterBase(impl,b,e,before)
175 SENF_STATIC_ASSERT(sizeof(PacketInterpreter) == sizeof(PacketInterpreterBase),
176 invalid_size_of_derived_packetinterpreter);
181 template <class PacketType>
182 prefix_ senf::PacketInterpreterBase::size_type
183 senf::PacketInterpreter<PacketType>::initSize()
185 return type::initSize();
188 template <class PacketType>
189 prefix_ senf::PacketInterpreterBase::size_type
190 senf::PacketInterpreter<PacketType>::initHeadSize()
192 size_type sz (type::initHeadSize());
193 return sz == size_type(-1) ? initSize() : sz ;
196 template <class PacketType>
197 prefix_ void senf::PacketInterpreter<PacketType>::init()
199 return type::init(ConcretePacket<PacketType>(ptr(this)));
202 //-/////////////////////////////////////////////////////////////////////////////////////////////////
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