UniRec  3.0.0
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 NemeaPlusPlus {
28 
29 class UnirecRecord;
30 
41 public:
47  UnirecRecordView(const void* unirecRecordData, ur_template_t* unirecTemplate)
48  : m_recordData(unirecRecordData)
49  , m_unirecTemplate(unirecTemplate)
50  {
51  }
52 
58  const void* data() const noexcept { return m_recordData; }
59 
65  size_t size() const noexcept { return ur_rec_size(m_unirecTemplate, m_recordData); }
66 
95  template<typename T>
97  {
98  using BaseType = typename std::remove_cv_t<
99  typename std::remove_pointer_t<typename std::remove_reference_t<T>>>;
100  using RequiredType = typename std::conditional_t<is_string_v<BaseType>, T, BaseType>;
101 
102  if (getExpectedUnirecType<RequiredType>() != ur_get_type(fieldID)) {
103  throw std::runtime_error(
104  "UnirecRecord data type format mismatch: " + std::string(typeid(T).name()));
105  }
106 
107  if constexpr (is_string_v<T>) {
108  return {
109  static_cast<const char*>(ur_get_ptr_by_id(m_unirecTemplate, m_recordData, fieldID)),
111  } else if constexpr (std::is_pointer_v<T>) {
112  return static_cast<T>(ur_get_ptr_by_id(m_unirecTemplate, m_recordData, fieldID));
113  } else if constexpr (std::is_reference_v<T>) {
114  return *reinterpret_cast<BaseType*>(
116  } else {
117  return *static_cast<T*>(ur_get_ptr_by_id(m_unirecTemplate, m_recordData, fieldID));
118  }
119  }
120 
136  template<typename T>
138  {
139  return UnirecArray<T>(
140  static_cast<T*>(ur_get_ptr_by_id(m_unirecTemplate, m_recordData, fieldID)),
142  fieldID);
143  }
144 
145 private:
146  template<typename T>
148  {
149  return T(
150  static_cast<const char*>(ur_get_ptr_by_id(m_unirecTemplate, m_recordData, fieldID)),
152  }
153 
154  const void* m_recordData;
156 
157  friend class UnirecRecord;
158 };
159 
160 } // namespace NemeaPlusPlus
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.
add_const_t< T > getFieldAsStringType(ur_field_id_t fieldID) const
size_t size() const noexcept
Returns the size of the UniRec record.
add_const_t< UnirecArray< T > > getFieldAsUnirecArray(ur_field_id_t fieldID) const
Gets the value of a field as a UnirecArray.
const void * data() const noexcept
Returns a const pointer to the data of the UniRec record.
UnirecRecordView(const void *unirecRecordData, ur_template_t *unirecTemplate)
Constructs a UnirecRecordView object.
add_const_t< T > getFieldAsType(ur_field_id_t fieldID) const
Gets the value of a field as a type T.
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