open62541pp 0.19.0
C++ wrapper of open62541
Loading...
Searching...
No Matches
typeregistry.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <type_traits>
4
6#include "open62541pp/detail/traits.hpp" // AlwaysFalse
7
8namespace opcua {
9
10/**
11 * Type registry.
12 *
13 * The type registry is used to derive the corresponding `UA_DataType` object from template types.
14 *
15 * Custom data types can be registered with template specializations:
16 * @code
17 * namespace ::opcua {
18 * template <>
19 * struct TypeRegistry<MyCustomType> {
20 * static const UA_DataType& getDataType() noexcept {
21 * // ...
22 * }
23 * };
24 * }
25 * @endcode
26 */
27template <typename T, typename Enabled = void>
29
30/* -------------------------------------- Traits and helper ------------------------------------- */
31
32template <typename T, typename = void>
33struct IsRegistered : std::false_type {};
34
35template <typename T>
36struct IsRegistered<T, std::void_t<decltype(TypeRegistry<T>{})>> : std::true_type {};
37
38template <typename T>
39const UA_DataType& getDataType() noexcept {
40 using ValueType = typename std::remove_cv_t<T>;
41 static_assert(
43 "The provided template type is not registered. "
44 "Specify the data type manually or add a template specialization for TypeRegistry."
45 );
47}
48
49/* ---------------------------------- Template specializations ---------------------------------- */
50
51// NOLINTNEXTLINE
52#define UAPP_TYPEREGISTRY_NATIVE(NativeType, typeIndex) \
53 template <> \
54 struct TypeRegistry<NativeType> { \
55 static const auto& getDataType() noexcept { \
56 return UA_TYPES[typeIndex]; \
57 } \
58 };
59
60// builtin types
61// @cond HIDDEN_SYMBOLS
62UAPP_TYPEREGISTRY_NATIVE(UA_Boolean, UA_TYPES_BOOLEAN)
66UAPP_TYPEREGISTRY_NATIVE(UA_UInt16, UA_TYPES_UINT16)
68UAPP_TYPEREGISTRY_NATIVE(UA_UInt32, UA_TYPES_UINT32)
70UAPP_TYPEREGISTRY_NATIVE(UA_UInt64, UA_TYPES_UINT64)
72UAPP_TYPEREGISTRY_NATIVE(UA_Double, UA_TYPES_DOUBLE)
73// UAPP_TYPEREGISTRY_NATIVE(UA_String, UA_TYPES_STRING) // manual implementation below
74// UAPP_TYPEREGISTRY_NATIVE(UA_DateTime, UA_TYPES_DATETIME) // alias for int64_t
76// UAPP_TYPEREGISTRY_NATIVE(UA_ByteString, UA_TYPES_BYTESTRING) // alias for UA_String
77// UAPP_TYPEREGISTRY_NATIVE(UA_XmlElement, UA_TYPES_XMLELEMENT) // alias for UA_String
78UAPP_TYPEREGISTRY_NATIVE(UA_NodeId, UA_TYPES_NODEID)
79UAPP_TYPEREGISTRY_NATIVE(UA_ExpandedNodeId, UA_TYPES_EXPANDEDNODEID)
80// UAPP_TYPEREGISTRY_NATIVE(UA_StatusCode, UA_TYPES_STATUSCODE) // alias for uint32_t
81UAPP_TYPEREGISTRY_NATIVE(UA_QualifiedName, UA_TYPES_QUALIFIEDNAME)
82UAPP_TYPEREGISTRY_NATIVE(UA_LocalizedText, UA_TYPES_LOCALIZEDTEXT)
83UAPP_TYPEREGISTRY_NATIVE(UA_ExtensionObject, UA_TYPES_EXTENSIONOBJECT)
84UAPP_TYPEREGISTRY_NATIVE(UA_DataValue, UA_TYPES_DATAVALUE)
85UAPP_TYPEREGISTRY_NATIVE(UA_Variant, UA_TYPES_VARIANT)
86UAPP_TYPEREGISTRY_NATIVE(UA_DiagnosticInfo, UA_TYPES_DIAGNOSTICINFO)
87
88template <>
89struct TypeRegistry<UA_String> {
90 static_assert(std::is_same_v<UA_String, UA_ByteString>);
91 static_assert(std::is_same_v<UA_String, UA_XmlElement>);
92
93 template <typename... Ts>
94 static const auto& getDataType([[maybe_unused]] Ts... args) noexcept {
95 static_assert(
96 detail::AlwaysFalse<Ts...>::value,
97 "Data type of UA_String is ambiguous (alias for UA_ByteString and UA_XmlElement). "
98 "Please specify data type manually."
99 );
100 }
101};
102
103// @endcond
104
105} // namespace opcua
static UA_LogCategory const char va_list args
const UA_DataType & getDataType() noexcept
#define UAPP_TYPEREGISTRY_NATIVE(NativeType, typeIndex)
int32_t UA_Int32
uint16_t UA_UInt16
int16_t UA_Int16
int8_t UA_SByte
uint32_t UA_UInt32
float UA_Float
double UA_Double
uint8_t UA_Byte
uint64_t UA_UInt64
int64_t UA_Int64