|
| 1 | +/** |
| 2 | + * @file |
| 3 | + * @author Pavel Siska <[email protected]> |
| 4 | + * @brief Accessor templates for scalar and vector fields with type-safe getters. |
| 5 | + * |
| 6 | + * This header defines the `ScalarAccessor` and `VectorAccessor` templates, which provide |
| 7 | + * a type-safe and performant way to access scalar and vector field values from external |
| 8 | + * data structures using function pointers. |
| 9 | + * |
| 10 | + * These accessors encapsulate a getter function pointer to retrieve the value from |
| 11 | + * a void pointer to the external data, ensuring compile-time type safety via concepts. |
| 12 | + * |
| 13 | + * @copyright Copyright (c) 2025 CESNET, z.s.p.o. |
| 14 | + */ |
| 15 | + |
| 16 | +#pragma once |
| 17 | + |
| 18 | +#include "fieldTypeConcepts.hpp" |
| 19 | + |
| 20 | +#include <cassert> |
| 21 | +#include <span> |
| 22 | + |
| 23 | +namespace ipxp { |
| 24 | + |
| 25 | +/** |
| 26 | + * @brief Accessor for scalar values. |
| 27 | + * |
| 28 | + * Provides a mechanism to access a scalar value of type `T` from an external data |
| 29 | + * structure via a getter function pointer. |
| 30 | + * |
| 31 | + * @tparam T Type of scalar value (must satisfy `FlowDataTypeScalar ` concept). |
| 32 | + */ |
| 33 | +template<FlowDataTypeScalar T> |
| 34 | +class ScalarAccessor { |
| 35 | +public: |
| 36 | + /// Function pointer type returning a scalar value of type T from a data pointer. |
| 37 | + using GetterFunction = T (*)(const void*); |
| 38 | + |
| 39 | + /** |
| 40 | + * @brief Constructs the ScalarAccessor with a getter function. |
| 41 | + * |
| 42 | + * @param getterFunction Function pointer used to extract the scalar value. |
| 43 | + * |
| 44 | + * @note The getter function pointer must not be null. |
| 45 | + */ |
| 46 | + explicit constexpr ScalarAccessor(GetterFunction getterFunction) |
| 47 | + : m_getterFunction(getterFunction) |
| 48 | + { |
| 49 | + assert(m_getterFunction && "ScalarAccessor: getter function must not be nullptr"); |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * @brief Retrieves the scalar value by invoking the getter function. |
| 54 | + * |
| 55 | + * @param data Pointer to the external data structure. |
| 56 | + * @return Extracted scalar value of type T. |
| 57 | + */ |
| 58 | + [[nodiscard]] T operator()(const void* data) const { return m_getterFunction(data); } |
| 59 | + |
| 60 | + ScalarAccessor() = delete; |
| 61 | + ScalarAccessor(const ScalarAccessor&) = default; |
| 62 | + ScalarAccessor& operator=(const ScalarAccessor&) = default; |
| 63 | + ScalarAccessor(ScalarAccessor&&) = default; |
| 64 | + ScalarAccessor& operator=(ScalarAccessor&&) = default; |
| 65 | + |
| 66 | +private: |
| 67 | + const GetterFunction m_getterFunction; |
| 68 | +}; |
| 69 | + |
| 70 | +/** |
| 71 | + * @brief Accessor for vector values. |
| 72 | + * |
| 73 | + * Provides a mechanism to access a span of values of type `T` from an external data |
| 74 | + * structure via a getter function pointer. |
| 75 | + * |
| 76 | + * @tparam T Type of vector element (must satisfy `FlowDataTypeVector` concept). |
| 77 | + */ |
| 78 | +template<FlowDataTypeVector T> |
| 79 | +class VectorAccessor { |
| 80 | +public: |
| 81 | + /// Function pointer type returning a span of constant values of type T from a data pointer. |
| 82 | + using GetterFunction = std::span<const T> (*)(const void*); |
| 83 | + |
| 84 | + /** |
| 85 | + * @brief Constructs the VectorAccessor with a getter function. |
| 86 | + * |
| 87 | + * @param getterFunction Function pointer used to extract the vector span. |
| 88 | + * |
| 89 | + * @note The getter function pointer must not be null. |
| 90 | + */ |
| 91 | + explicit constexpr VectorAccessor(GetterFunction getterFunction) |
| 92 | + : m_getterFunction(getterFunction) |
| 93 | + { |
| 94 | + assert(m_getterFunction && "VectorAccessor: getter function must not be nullptr"); |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * @brief Retrieves the vector span by invoking the getter function. |
| 99 | + * |
| 100 | + * @param data Pointer to the external data structure. |
| 101 | + * @return Extracted span of constant values of type T. |
| 102 | + */ |
| 103 | + [[nodiscard]] std::span<const T> operator()(const void* data) const |
| 104 | + { |
| 105 | + return m_getterFunction(data); |
| 106 | + } |
| 107 | + |
| 108 | + VectorAccessor() = delete; |
| 109 | + VectorAccessor(const VectorAccessor&) = default; |
| 110 | + VectorAccessor& operator=(const VectorAccessor&) = default; |
| 111 | + VectorAccessor(VectorAccessor&&) = default; |
| 112 | + VectorAccessor& operator=(VectorAccessor&&) = default; |
| 113 | + |
| 114 | +private: |
| 115 | + const GetterFunction m_getterFunction; |
| 116 | +}; |
| 117 | + |
| 118 | +} // namespace ipxp |
0 commit comments