Skip to content

Commit 1d85d89

Browse files
Zainullin DamirZainullin Damir
authored andcommitted
++
1 parent 52e42d1 commit 1d85d89

33 files changed

+3325
-2
lines changed

include/ipfixprobe/pluginFactory/pluginFactory.hpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
#include <string_view>
2222
#include <type_traits>
2323

24+
#include "../api.hpp"
25+
2426
namespace ipxp {
2527

2628
/**
@@ -34,14 +36,14 @@ namespace ipxp {
3436
* @tparam Args The types of arguments that will be passed to the plugin constructors.
3537
*/
3638
template<typename Base, typename... Args>
37-
class PluginFactory {
39+
class IPXP_API PluginFactory {
3840
public:
3941
/**
4042
* @brief Retrieves the singleton instance of `PluginFactory`.
4143
*
4244
* @return A reference to the singleton `PluginFactory` instance.
4345
*/
44-
static PluginFactory& getInstance()
46+
inline static PluginFactory& getInstance()
4547
{
4648
static PluginFactory instance;
4749
return instance;
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#pragma once
2+
3+
#include <cstdint>
4+
5+
class Direction {
6+
private:
7+
enum class Value : std::size_t {Forward, Reverse};
8+
9+
constexpr Direction(const Value value) noexcept
10+
: m_value(value) {}
11+
12+
public:
13+
const static Direction Forward;
14+
const static Direction Reverse;
15+
16+
constexpr Direction(const bool value) noexcept
17+
: m_value(static_cast<Value>(value)) {}
18+
19+
constexpr operator bool() const noexcept
20+
{
21+
return static_cast<bool>(m_value);
22+
}
23+
24+
constexpr Direction operator!() const noexcept
25+
{
26+
return Direction(!static_cast<bool>(m_value));
27+
}
28+
29+
private:
30+
Value m_value;
31+
};
32+
33+
inline const Direction Direction::Forward = Direction(Value::Forward);
34+
inline const Direction Direction::Reverse = Direction(Value::Reverse);
35+
36+
template<typename T>
37+
struct DirectionalField {
38+
T values[2]{};
39+
40+
constexpr T& operator[](const Direction d)
41+
{
42+
return values[static_cast<bool>(d)];
43+
}
44+
45+
constexpr const T& operator[](const Direction d) const
46+
{
47+
return values[static_cast<bool>(d)];
48+
}
49+
};
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
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
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/**
2+
* @file
3+
* @author Pavel Siska <[email protected]>
4+
* @brief Provides the FieldDescriptor class for accessing field metadata and value getters.
5+
*
6+
* This class encapsulates information about a field in a FlowRecord, including its
7+
* name, group, bit index, and a generic value accessor. It allows checking if the
8+
* field is present in a specific FlowRecord. Construction is restricted to FieldManager.
9+
*
10+
* @copyright Copyright (c) 2025 CESNET, z.s.p.o.
11+
*/
12+
13+
#pragma once
14+
15+
#include "fieldGenericValueGetter.hpp"
16+
#include "fieldInfo.hpp"
17+
#include "flowRecord.hpp"
18+
19+
#include <cstdint>
20+
#include <string>
21+
#include <string_view>
22+
23+
namespace ipxp {
24+
25+
/**
26+
* @class FieldDescriptor
27+
* @brief Read-only access to field metadata and value getters.
28+
*
29+
* FieldDescriptor represents a single field within a FlowRecord. It provides
30+
* information about the field's name, logical group, bit index, and a generic
31+
* value accessor. It allows checking if the field is present in a specific
32+
* FlowRecord using the `isInRecord()` method.
33+
*
34+
* Instances are immutable and can only be created by FieldManager, ensuring
35+
* controlled registration and consistent state of fields.
36+
*/
37+
class FieldDescriptor {
38+
public:
39+
/**
40+
* @brief Returns the name of the field.
41+
* @return Field name as string_view.
42+
*/
43+
[[nodiscard]]
44+
std::string_view getName() const noexcept
45+
{
46+
return m_fieldInfo.name;
47+
}
48+
49+
/**
50+
* @brief Returns the logical group name of the field.
51+
* @return Group name (e.g., "tcp", "http").
52+
*/
53+
[[nodiscard]]
54+
std::string_view getGroup() const noexcept
55+
{
56+
return m_fieldInfo.group;
57+
}
58+
59+
/**
60+
* @brief Returns the generic value accessor for the field.
61+
* @return Reference to GenericValueGetter variant.
62+
*/
63+
[[nodiscard]]
64+
const GenericValueGetter& getValueGetter() const noexcept
65+
{
66+
return m_fieldInfo.getter;
67+
}
68+
69+
/**
70+
* @brief Returns the bit index used to check field presence in FlowRecord.
71+
* @return Bit index as size_t.
72+
*/
73+
[[nodiscard]]
74+
std::size_t getBitIndex() const noexcept
75+
{
76+
return m_fieldInfo.bitIndex;
77+
}
78+
79+
/**
80+
* @brief Checks whether this field is present in a given FlowRecord.
81+
* @param record Flow record to query.
82+
* @return True if the field is available in the record.
83+
*/
84+
[[nodiscard]]
85+
bool isInRecord(const FlowRecord& record) const
86+
{
87+
return record.fieldsAvailable.test(m_fieldInfo.bitIndex);
88+
}
89+
90+
private:
91+
// FieldDescriptor can only be constructed by FieldManager
92+
friend class FieldManager;
93+
94+
explicit FieldDescriptor(FieldInfo fieldInfo)
95+
: m_fieldInfo(std::move(fieldInfo))
96+
{
97+
}
98+
99+
const FieldInfo m_fieldInfo;
100+
};
101+
102+
} // namespace ipxp
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* @file
3+
* @author Pavel Siska <[email protected]>
4+
* @brief Type-erased variants for scalar and vector field accessors.
5+
*
6+
* Defines `ScalarValueGetter`, `VectorValueGetter`, and `GenericValueGetter` as `std::variant`
7+
* types that hold accessors for all supported scalar and vector field types.
8+
*
9+
* These variants allow uniform storage and usage of heterogeneous field accessors
10+
* without template overhead, enabling runtime polymorphism without virtual functions.
11+
*
12+
* @see ScalarAccessor
13+
* @see VectorAccessor
14+
*
15+
* @copyright Copyright (c) 2025 CESNET, z.s.p.o.
16+
*/
17+
18+
#pragma once
19+
20+
#include "fieldAccessor.hpp"
21+
#include "fieldSupportedTypes.hpp"
22+
#include "typeUtils.hpp"
23+
24+
namespace ipxp {
25+
26+
/**
27+
* @brief Variant holding any scalar field accessor for supported scalar types.
28+
*/
29+
using ScalarValueGetter = detail::variant_of_accessors_t<ScalarAccessor, SupportedScalarTypes>;
30+
31+
/**
32+
* @brief Variant holding any vector field accessor for supported vector types.
33+
*/
34+
using VectorValueGetter = detail::variant_of_accessors_t<VectorAccessor, SupportedVectorTypes>;
35+
36+
/**
37+
* @brief Variant holding either a scalar or a vector field accessor.
38+
*
39+
* Allows storing any supported accessor type in a single variable,
40+
* simplifying generic code paths.
41+
*/
42+
using GenericValueGetter = std::variant<ScalarValueGetter, VectorValueGetter>;
43+
44+
} // namespace ipxp

0 commit comments

Comments
 (0)