open62541pp 0.16.0
C++ wrapper of open62541
Loading...
Searching...
No Matches
string_utils.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <cstdarg> // va_list
4#include <string>
5#include <string_view>
6
7#include "open62541pp/detail/open62541/common.h" // UA_String
8
9namespace opcua::detail {
10
11/// Convert std::string_view to UA_String (no copy)
12UA_String toNativeString(std::string_view src) noexcept;
13
14/// Allocate UA_String from std::string_view
15[[nodiscard]] UA_String allocNativeString(std::string_view src);
16
17/// Convert UA_String to std::string_view
18/// Can be marked noexcept: https://stackoverflow.com/a/62061549/9967707
19inline std::string_view toStringView(const UA_String& src) noexcept {
20 if (src.data == nullptr || src.length == 0U) {
21 return {};
22 }
23 return {(const char*)src.data, src.length}; // NOLINT
24}
25
26/// Convert UA_String to std::string
27inline std::string toString(const UA_String& src) {
28 return std::string(toStringView(src));
29}
30
31/// Convert format string with args to std::string
32std::string toString(const char* format, va_list args);
33
34// NOLINTBEGIN
35inline std::string toString(const char* format, ...) {
36 va_list args;
37 va_start(args, format);
38 std::string result = toString(format, args);
39 va_end(args);
40 return result;
41}
42
43// NOLINTEND
44
45} // namespace opcua::detail
static UA_LogCategory const char va_list args
va_end(args)
va_start(args, msg)
UA_String allocNativeString(std::string_view src)
Allocate UA_String from std::string_view.
UA_String toNativeString(std::string_view src) noexcept
Convert std::string_view to UA_String (no copy)
std::string_view toStringView(const UA_String &src) noexcept
Convert UA_String to std::string_view Can be marked noexcept: https://stackoverflow....
std::string toString(const UA_String &src)
Convert UA_String to std::string.