PacketImpl.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 PacketImpl inline template implementation */
16 
17 #include "PacketImpl.ih"
18 
19 // Custom includes
20 
21 #define prefix_ inline
22 //-/////////////////////////////////////////////////////////////////////////////////////////////////
23 
24 //-/////////////////////////////////////////////////////////////////////////////////////////////////
25 // senf::detail::AnnotationRegistry
26 
27 template <class Annotation>
28 prefix_ senf::detail::AnnotationRegistry::key_type
29 senf::detail::AnnotationRegistry::registerAnnotation()
30 {
31 #ifndef SENF_PACKET_NO_COMPLEX_ANNOTATIONS
32  key_type key (simpleAnnotationCount_ >= SENF_PACKET_ANNOTATION_SLOTS
33  || IsComplexAnnotation<Annotation>::value
34  ? - ++complexAnnotationCount_
35  : simpleAnnotationCount_ ++);
36 #else
37  key_type key (simpleAnnotationCount_ ++);
38  SENF_ASSERT(simpleAnnotationCount_ <= SENF_PACKET_ANNOTATION_SLOTS,
39  "to many annotations defined and complex annotations disabled");
40  SENF_STATIC_ASSERT(! IsComplexAnnotation<Annotation>::value,
41  annotation_is_complex_but_complex_annotations_disabled);
42 #endif
43  std::pair<Registry::iterator, bool> reg (
44  registry_.insert(key, new Registration<Annotation>()));
45  SENF_ASSERT(reg.second, "internal error: duplicate annotation key");
46  index_.insert(std::make_pair(reg.first->second->v_name(), key));
47  return key;
48 }
49 
50 template <class Annotation>
51 prefix_ senf::detail::AnnotationRegistry::key_type senf::detail::AnnotationRegistry::lookup()
52 {
53 #ifndef SENF_PACKET_NO_COMPLEX_ANNOTATIONS
54  SENF_ASSERT(
55  -instance().complexAnnotationCount_ <= AnnotationRegistry::Entry<Annotation>::key()
56  && AnnotationRegistry::Entry<Annotation>::key() < instance().simpleAnnotationCount_,
57  "internal error: annotation key not registered" );
58  SENF_ASSERT(
59  AnnotationRegistry::Entry<Annotation>::key() < 0
60  || ! IsComplexAnnotation<Annotation>::value,
61  "internal error: complex annotation registered with invalid key" );
62 #else
63  SENF_ASSERT(
64  AnnotationRegistry::Entry<Annotation>::key() >= 0,
65  "internal error: annotation key out of valid range" );
66 #endif
67  SENF_ASSERT(
68  AnnotationRegistry::Entry<Annotation>::key() < SENF_PACKET_ANNOTATION_SLOTS,
69  "internal error: annotation key out of valid range" );
70  return AnnotationRegistry::Entry<Annotation>::key();
71 }
72 
73 //-/////////////////////////////////////////////////////////////////////////////////////////////////
74 // senf::detail::PacketImpl
75 
76 // Data container
77 
78 template <class ForwardIterator>
79 prefix_ void senf::detail::PacketImpl::insert(PacketData * self, iterator pos, ForwardIterator f,
80  ForwardIterator l)
81 {
82  difference_type ix (std::distance(begin(),pos));
83  data_.insert(pos,f,l);
84  updateIterators(self,ix,std::distance(f,l));
85 }
86 
87 template <class InputIterator>
88 prefix_ senf::detail::PacketImpl::PacketImpl(InputIterator first, InputIterator last)
89  : refcount_(0), data_(first,last)
90 #ifdef SENF_PACKET_ALTERNATIVE_PREALLOC
91  , preallocHigh_(0), preallocFree_(0)
92 #else
93  , preallocFree_(prealloc_)
94 #endif
95 #ifndef SENF_PACKET_NO_HEAP_INTERPRETERS
96  , preallocHeapcount_(0)
97 #endif
98 {
99  ::memset(simpleAnnotations_, 0, sizeof(simpleAnnotations_));
100 #ifndef SENF_PACKET_ALTERNATIVE_PREALLOC
101  ::memset(prealloc_, 0, sizeof(prealloc_));
102 #endif
103 }
104 
105 // Annotations
106 
107 template <class Annotation>
108 prefix_ Annotation & senf::detail::PacketImpl::annotation()
109 {
110  AnnotationRegistry::key_type key (AnnotationRegistry::lookup<Annotation>());
111 #ifndef SENF_PACKET_NO_COMPLEX_ANNOTATIONS
112  void * antn (key >= 0 ? & simpleAnnotations_[key] : complexAnnotation<Annotation>());
113 #else
114  void * antn (& simpleAnnotations_[key]);
115 #endif
116  SENF_ASSERT( antn, "internal error: null annotation pointer" );
117  return * static_cast<Annotation*>(antn);
118 }
119 
120 //-/////////////////////////////////////////////////////////////////////////////////////////////////
121 #undef prefix_
122 
123 
124 // Local Variables:
125 // mode: c++
126 // fill-column: 100
127 // c-file-style: "senf"
128 // indent-tabs-mode: nil
129 // ispell-local-dictionary: "american"
130 // compile-command: "scons -u test"
131 // comment-column: 40
132 // End: