UniRec  3.3.1
unirecArray.hpp
Go to the documentation of this file.
1 
9 #pragma once
10 
11 #include "unirecTypes.hpp"
12 
13 #include <cstddef>
14 #include <exception>
15 #include <iterator>
16 #include <unirec/unirec.h>
17 
18 namespace Nemea {
19 
29 template<typename T>
30 class UnirecArray {
31 public:
35  class Iterator {
36  public:
37  using iterator_category = std::random_access_iterator_tag;
38  using difference_type = std::ptrdiff_t;
39  using value_type = T;
40  using pointer = value_type*;
42 
44  : m_ptr(ptr)
45  {
46  }
47 
48  reference operator*() const { return *m_ptr; }
49  pointer operator->() { return m_ptr; }
51  {
52  m_ptr++;
53  return *this;
54  }
55 
57  {
58  Iterator tmp = *this;
59  ++(*this);
60  return tmp;
61  }
62 
63  T* data() { return m_ptr; }
64 
65  bool operator==(const Iterator& other) const { return this->m_ptr == other.m_ptr; };
66  bool operator!=(const Iterator& other) const { return this->m_ptr != other.m_ptr; };
67 
68  private:
70  };
71 
79  UnirecArray(T* dataPointer, size_t size, ur_field_id_t fieldID)
80  : m_data(dataPointer)
81  , m_size(size)
82  {
83  checkDataType(ur_get_type(fieldID));
84  }
85 
89  constexpr size_t size() const noexcept { return m_size; }
90 
94  constexpr Iterator begin() const noexcept { return Iterator(m_data); }
95 
100  constexpr Iterator end() const noexcept { return Iterator(m_data + m_size); }
101 
108  constexpr T& operator[](size_t pos) { return m_data[pos]; }
109 
118  constexpr T& at(size_t pos) const
119  {
120  if (pos >= m_size)
121  throw std::out_of_range(
122  "UnirecArray::at: pos (which is %zu) "
123  ">= m_size (which is %zu)"),
124  pos, m_size;
125  return m_data[pos];
126  }
127 
128 private:
129  void checkDataType(ur_field_type_t fieldDataType) const
130  {
131  if (getExpectedUnirecType<T*>() != fieldDataType) {
132  throw std::runtime_error("Unirec array data type format mismatch");
133  }
134  }
135 
136  size_t m_size;
137  T* m_data;
138 };
139 
140 } // namespace Nemea
An iterator for the UnirecArray class.
Definition: unirecArray.hpp:35
std::ptrdiff_t difference_type
Definition: unirecArray.hpp:38
reference operator*() const
Definition: unirecArray.hpp:48
bool operator==(const Iterator &other) const
Definition: unirecArray.hpp:65
bool operator!=(const Iterator &other) const
Definition: unirecArray.hpp:66
std::random_access_iterator_tag iterator_category
Definition: unirecArray.hpp:37
A wrapper class for a contiguous array of values with the same unirec fieldID.
Definition: unirecArray.hpp:30
constexpr Iterator begin() const noexcept
Returns an iterator to the first element of the UniRec field array.
Definition: unirecArray.hpp:94
constexpr T & operator[](size_t pos)
Returns a reference to the element at the specified position in the UniRec field array.
constexpr Iterator end() const noexcept
Returns an iterator to the element following the last element of the UniRec field array.
void checkDataType(ur_field_type_t fieldDataType) const
UnirecArray(T *dataPointer, size_t size, ur_field_id_t fieldID)
Constructs a UnirecArray object.
Definition: unirecArray.hpp:79
constexpr size_t size() const noexcept
Returns the number of elements in the UniRec field array.
Definition: unirecArray.hpp:89
constexpr T & at(size_t pos) const
Returns a reference to the element at the specified position in the UniRec field array,...
#define ur_get_type(field_id)
Get type of UniRec field Get type of any UniRec defined field.
Definition: unirec.h:388
This file contains functions for determining the expected UniRec type for various C++ types.
Definition of UniRec structures and functions.
ur_field_type_t
Definition: unirec.h:95
int16_t ur_field_id_t
Type of UniRec field identifiers.
Definition: unirec.h:136