UniRec 3.3.2
Loading...
Searching...
No Matches
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
18namespace Nemea {
19
29template<typename T>
31public:
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;
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
128private:
129 void checkDataType(ur_field_type_t fieldDataType) const
130 {
132 throw std::runtime_error("Unirec array data type format mismatch");
133 }
134 }
135
136 size_t m_size;
138};
139
140} // namespace Nemea
An iterator for the UnirecArray class.
reference operator*() const
bool operator==(const Iterator &other) const
bool operator!=(const Iterator &other) const
std::random_access_iterator_tag iterator_category
A wrapper class for a contiguous array of values with the same unirec fieldID.
constexpr T & operator[](size_t pos)
Returns a reference to the element at the specified position in the UniRec field array.
constexpr Iterator begin() const noexcept
Returns an iterator to the first element of 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.
constexpr size_t size() const noexcept
Returns the number of elements in the UniRec field array.
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
constexpr ur_field_type_t getExpectedUnirecType()
Determines the expected UniRec field type for a given C++ type T.
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