open62541pp 0.15.0
C++ wrapper of open62541
Loading...
Searching...
No Matches
types_conversion.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <string_view>
4#include <type_traits>
5
6#include "open62541pp/detail/string_utils.hpp" // allocNativeString
7#include "open62541pp/detail/types_handling.hpp" // copyArray
9#include "open62541pp/typeregistry.hpp" // getDataType
10#include "open62541pp/wrapper.hpp" // asNative, asWrapper
11
12namespace opcua::detail {
13
14template <typename T, typename = std::enable_if_t<std::is_enum_v<T>>>
15[[nodiscard]] inline auto toNative(T value) noexcept {
16 return static_cast<std::underlying_type_t<T>>(value);
17}
18
19[[nodiscard]] inline auto toNative(std::string_view value) {
20 return allocNativeString(value);
21}
22
23template <typename T, typename = std::enable_if_t<isWrapper<T>>>
24[[nodiscard]] inline auto toNative(T&& value) {
25 using NativeType = typename T::NativeType;
26 NativeType native{};
27 asWrapper<T>(native) = std::forward<T>(value);
28 return native;
29}
30
31template <typename T>
32[[nodiscard]] inline auto* toNativeArray(Span<const T> array) {
33 if constexpr (isWrapper<T>) {
34 return copyArray(asNative(array.data()), array.size(), getDataType<T>());
35 } else {
36 return copyArray(array.data(), array.size(), getDataType<T>());
37 }
38}
39
40} // namespace opcua::detail
View to a contiguous sequence of objects, similar to std::span in C++20.
Definition span.hpp:26
constexpr size_t size() const noexcept
Definition span.hpp:101
constexpr pointer data() const noexcept
Definition span.hpp:109
constexpr WrapperType * asWrapper(NativeType *native) noexcept
Cast native object pointers to Wrapper object pointers.
Definition wrapper.hpp:164
constexpr NativeType * asNative(WrapperType *wrapper) noexcept
Cast Wrapper object pointers to native object pointers.
Definition wrapper.hpp:190
UA_String allocNativeString(std::string_view src)
Allocate UA_String from std::string_view.
T * copyArray(const T *src, size_t size, const UA_DataType &type)
constexpr bool isWrapper
Definition wrapper.hpp:103
auto * toNativeArray(Span< const T > array)
auto toNative(T value) noexcept
const UA_DataType & getDataType() noexcept