UniRec  3.3.1
unirecRecordView.hpp
Go to the documentation of this file.
1 
14 #pragma once
15 
16 #include "unirecArray.hpp"
17 #include "unirecTypeTraits.hpp"
18 #include "unirecTypes.hpp"
19 
20 #include <cstddef>
21 #include <exception>
22 #include <numeric>
23 #include <string>
24 #include <type_traits>
25 #include <unirec/unirec.h>
26 
27 namespace Nemea {
28 
29 class UnirecRecord;
30 
41 public:
48  UnirecRecordView(const void* unirecRecordData, ur_template_t* unirecTemplate, uint64_t sequenceNumber = 0)
49  : m_recordData(unirecRecordData)
50  , m_unirecTemplate(unirecTemplate)
51  , m_sequenceNumber(sequenceNumber)
52  {
53  }
54 
60  const void* data() const noexcept { return m_recordData; }
61 
67  size_t size() const noexcept { return ur_rec_size(m_unirecTemplate, m_recordData); }
68 
83  uint64_t getSequenceNumber() const noexcept
84  {
85  return m_sequenceNumber;
86  }
87 
116  template <typename T>
118  {
119  using BaseType = typename std::remove_cv_t<
120  typename std::remove_pointer_t<typename std::remove_reference_t<T>>>;
121  using RequiredType = typename std::conditional_t<is_string_v<BaseType>, T, BaseType>;
122 
123  if (getExpectedUnirecType<RequiredType>() != ur_get_type(fieldID)) {
124  throw std::runtime_error(
125  "UnirecRecord data type format mismatch: " + std::string(typeid(T).name()));
126  }
127 
128  if constexpr (is_string_v<T>) {
129  return {
130  static_cast<const char*>(ur_get_ptr_by_id(m_unirecTemplate, m_recordData, fieldID)),
132  };
133  } else if constexpr (std::is_pointer_v<T>) {
134  return static_cast<T>(ur_get_ptr_by_id(m_unirecTemplate, m_recordData, fieldID));
135  } else if constexpr (std::is_reference_v<T>) {
136  return *reinterpret_cast<BaseType*>(
138  } else {
139  return *static_cast<T*>(ur_get_ptr_by_id(m_unirecTemplate, m_recordData, fieldID));
140  }
141  }
142 
158  template <typename T>
160  {
161  return UnirecArray<T>(
162  static_cast<T*>(ur_get_ptr_by_id(m_unirecTemplate, m_recordData, fieldID)),
164  fieldID);
165  }
166 
167 private:
168  template <typename T>
170  {
171  return T(
172  static_cast<const char*>(ur_get_ptr_by_id(m_unirecTemplate, m_recordData, fieldID)),
174  }
175 
176  const void* m_recordData;
179 
180  friend class UnirecRecord;
181 };
182 
183 } // namespace Nemea
A wrapper class for a contiguous array of values with the same unirec fieldID.
Definition: unirecArray.hpp:30
Provides a view into a UniRec record.
ur_template_t * m_unirecTemplate
add_const_t< UnirecArray< T > > getFieldAsUnirecArray(ur_field_id_t fieldID) const
Gets the value of a field as a UnirecArray.
uint64_t getSequenceNumber() const noexcept
Gets the sequence number of the record.
add_const_t< T > getFieldAsType(ur_field_id_t fieldID) const
Gets the value of a field as a type T.
size_t size() const noexcept
Returns the size of the UniRec record.
UnirecRecordView(const void *unirecRecordData, ur_template_t *unirecTemplate, uint64_t sequenceNumber=0)
Constructs a UnirecRecordView object.
add_const_t< T > getFieldAsStringType(ur_field_id_t fieldID) const
const void * data() const noexcept
Returns a const pointer to the data of the UniRec record.
A class for working with UniRec records and their fields.
#define ur_rec_size(tmplt, rec)
Get size of UniRec record (static and variable length) Get total size of whole UniRec record.
Definition: unirec.h:680
#define ur_get_var_len(tmplt, rec, field_id)
Get size of a variable sized field in the record. Get size of a variable-length field in the record....
Definition: unirec.h:474
#define ur_array_get_elem_cnt(tmplt, rec, field_id)
Get number of elements stored in an UniRec array.
Definition: unirec.h:546
#define ur_get_type(field_id)
Get type of UniRec field Get type of any UniRec defined field.
Definition: unirec.h:388
#define ur_get_ptr_by_id(tmplt, data, field_id)
Get pointer to UniRec field Get pointer to fixed or varible length UniRec field. In contrast to ur_ge...
Definition: unirec.h:442
typename add_const< T >::type add_const_t
Header file containing the definition of the UnirecArray class and its Iterator subclass.
Provides a set of type traits and aliases for working with unirec++.
This file contains functions for determining the expected UniRec type for various C++ types.
Definition of UniRec structures and functions.
int16_t ur_field_id_t
Type of UniRec field identifiers.
Definition: unirec.h:136
UniRec template. It contains a table mapping a field to its position in an UniRec record.
Definition: unirec.h:191