iceoryx2
C++ Language Bindings
Loading...
Searching...
No Matches
semantic_string.hpp
Go to the documentation of this file.
1// Copyright (c) 2023 by Apex.AI Inc. All rights reserved.
2// Copyright (c) 2025 Contributors to the Eclipse Foundation
3//
4// See the NOTICE file(s) distributed with this work for additional
5// information regarding copyright ownership.
6//
7// This program and the accompanying materials are made available under the
8// terms of the Apache Software License 2.0 which is available at
9// https://www.apache.org/licenses/LICENSE-2.0, or the MIT license
10// which is available at https://opensource.org/licenses/MIT.
11//
12// SPDX-License-Identifier: Apache-2.0 OR MIT
13
14#ifndef IOX2_BB_SEMANTIC_STRING_HPP
15#define IOX2_BB_SEMANTIC_STRING_HPP
16
17#include "iox2/bb/expected.hpp"
20
21#include <cstdint>
22
23namespace iox2 {
24namespace bb {
26enum class SemanticStringError : uint8_t {
29};
30
61template <typename Child,
62 uint64_t Capacity,
63 DoesContainInvalidContent<Capacity> DoesContainInvalidContentCall,
64 DoesContainInvalidCharacter<Capacity> DoesContainInvalidCharacterCall>
66 private:
68
69 public:
75 template <uint64_t N>
76 // avoid-c-arrays: we would like to assign string literals, safe since it is known
77 // at compile time.
78 // NOLINTNEXTLINE(hicpp-avoid-c-arrays, cppcoreguidelines-avoid-c-arrays, hicpp-explicit-conversions, modernize-avoid-c-arrays)
79 static auto create(const char (&value)[N]) noexcept -> bb::Expected<Child, SemanticStringError>;
80
86 template <uint64_t N>
88
94 static auto create(const char* value) noexcept -> bb::Expected<Child, SemanticStringError>;
95
98 constexpr auto size() const noexcept -> uint64_t;
99
102 static constexpr auto capacity() noexcept -> uint64_t;
103
108 constexpr auto as_string() const noexcept -> const bb::StaticString<Capacity>&;
109
115 template <typename T>
116 auto append(const T& value) noexcept -> bb::Expected<void, SemanticStringError>;
117
125 template <typename T>
126 auto insert(uint64_t pos, const T& str, uint64_t count) noexcept -> bb::Expected<void, SemanticStringError>;
127
131 auto operator==(const SemanticString& rhs) const noexcept -> bool;
132
136 template <typename T>
137 auto operator==(const T& rhs) const noexcept -> bb::RequireStaticStringOrCharArray<T, bool>;
138
142 auto operator!=(const SemanticString& rhs) const noexcept -> bool;
143
147 template <typename T>
148 auto operator!=(const T& rhs) const noexcept -> bb::RequireStaticStringOrCharArray<T, bool>;
149
153 auto operator<=(const SemanticString& rhs) const noexcept -> bool;
154
158 template <typename T>
159 auto operator<=(const T& rhs) const noexcept -> bb::RequireStaticStringOrCharArray<T, bool>;
160
164 auto operator<(const SemanticString& rhs) const noexcept -> bool;
165
169 template <typename T>
170 auto operator<(const T& rhs) const noexcept -> bb::RequireStaticStringOrCharArray<T, bool>;
171
175 auto operator>=(const SemanticString& rhs) const noexcept -> bool;
176
180 template <typename T>
181 auto operator>=(const T& rhs) const noexcept -> bb::RequireStaticStringOrCharArray<T, bool>;
182
186 auto operator>(const SemanticString& rhs) const noexcept -> bool;
187
191 template <typename T>
192 auto operator>(const T& rhs) const noexcept -> bb::RequireStaticStringOrCharArray<T, bool>;
193
194 protected:
195 template <uint64_t N>
196 explicit SemanticString(const bb::StaticString<N>& value) noexcept;
197
198 private:
199 template <uint64_t N>
200 static auto create_impl(const char* value) noexcept -> bb::Expected<Child, SemanticStringError>;
201};
202
203
204template <typename Child,
205 uint64_t Capacity,
206 DoesContainInvalidContent<Capacity> DoesContainInvalidContentCall,
207 DoesContainInvalidCharacter<Capacity> DoesContainInvalidCharacterCall>
208template <uint64_t N>
209inline SemanticString<Child, Capacity, DoesContainInvalidContentCall, DoesContainInvalidCharacterCall>::SemanticString(
210 const bb::StaticString<N>& value) noexcept
211 : m_data { value } {
212}
213
214template <typename Child,
215 uint64_t Capacity,
216 DoesContainInvalidContent<Capacity> DoesContainInvalidContentCall,
217 DoesContainInvalidCharacter<Capacity> DoesContainInvalidCharacterCall>
218template <uint64_t N>
219inline auto
220SemanticString<Child, Capacity, DoesContainInvalidContentCall, DoesContainInvalidCharacterCall>::create_impl(
221 const char* value) noexcept -> bb::Expected<Child, SemanticStringError> {
222 if (N > Capacity) {
224 "Unable to create semantic string since the value \""
225 << value << "\" exceeds the maximum valid length of " << Capacity << ".");
227 }
228
230
231 if (DoesContainInvalidCharacterCall(str)) {
233 "Unable to create semantic string since the value \"" << value
234 << "\" contains invalid characters as content");
236 }
237
238 if (DoesContainInvalidContentCall(str)) {
240 "Unable to create semantic string since the value \"" << value << "\" contains invalid content");
242 }
243
244 return Child(str);
245}
246
247template <typename Child,
248 uint64_t Capacity,
249 DoesContainInvalidContent<Capacity> DoesContainInvalidContentCall,
250 DoesContainInvalidCharacter<Capacity> DoesContainInvalidCharacterCall>
251template <uint64_t N>
253 // avoid-c-arrays: justification in header
254 // NOLINTNEXTLINE(hicpp-avoid-c-arrays, cppcoreguidelines-avoid-c-arrays, hicpp-explicit-conversions, modernize-avoid-c-arrays)
255 const char (&value)[N]) noexcept -> bb::Expected<Child, SemanticStringError> {
257 template create_impl<N>(value);
258}
259
260template <typename Child,
261 uint64_t Capacity,
262 DoesContainInvalidContent<Capacity> DoesContainInvalidContentCall,
263 DoesContainInvalidCharacter<Capacity> DoesContainInvalidCharacterCall>
264template <uint64_t N>
270
271template <typename Child,
272 uint64_t Capacity,
273 DoesContainInvalidContent<Capacity> DoesContainInvalidContentCall,
274 DoesContainInvalidCharacter<Capacity> DoesContainInvalidCharacterCall>
276 const char* value) noexcept -> bb::Expected<Child, SemanticStringError> {
277 if (strnlen(value, Capacity + 1) > Capacity) {
279 "Unable to create semantic string since the value \""
280 << value << "\" exceeds the maximum valid length of " << Capacity << ".");
282 }
283
285 template create_impl<Capacity>(value);
286}
287
288template <typename Child,
289 uint64_t Capacity,
290 DoesContainInvalidContent<Capacity> DoesContainInvalidContentCall,
291 DoesContainInvalidCharacter<Capacity> DoesContainInvalidCharacterCall>
292constexpr auto
297
298template <typename Child,
299 uint64_t Capacity,
300 DoesContainInvalidContent<Capacity> DoesContainInvalidContentCall,
301 DoesContainInvalidCharacter<Capacity> DoesContainInvalidCharacterCall>
302constexpr auto
307
308template <typename Child,
309 uint64_t Capacity,
310 DoesContainInvalidContent<Capacity> DoesContainInvalidContentCall,
311 DoesContainInvalidCharacter<Capacity> DoesContainInvalidCharacterCall>
312constexpr auto
317
318template <typename Child,
319 uint64_t Capacity,
320 DoesContainInvalidContent<Capacity> DoesContainInvalidContentCall,
321 DoesContainInvalidCharacter<Capacity> DoesContainInvalidCharacterCall>
322template <typename T>
327
328template <typename Child,
329 uint64_t Capacity,
330 DoesContainInvalidContent<Capacity> DoesContainInvalidContentCall,
331 DoesContainInvalidCharacter<Capacity> DoesContainInvalidCharacterCall>
332template <typename T>
334 const uint64_t pos, const T& str, const uint64_t count) noexcept -> bb::Expected<void, SemanticStringError> {
335 auto temp = m_data;
336 if (!temp.unchecked_code_units().insert(pos, str, 0, count)) {
338 "Unable to insert the value \""
339 << str.unchecked_access().c_str()
340 << "\" to the semantic string since it would exceed the maximum valid length of " << Capacity
341 << ".");
343 }
344
345 if (DoesContainInvalidCharacterCall(temp)) {
347 "Unable to insert the value \""
348 << str.unchecked_access().c_str()
349 << "\" to the semantic string since it contains invalid characters as content.");
351 }
352
353 if (DoesContainInvalidContentCall(temp)) {
355 "Unable to insert the value \""
356 << str.unchecked_access().c_str()
357 << "\" to the semantic string since it would lead to invalid content.");
359 }
360
361 m_data = temp;
362 return {};
363}
364
365template <typename Child,
366 uint64_t Capacity,
367 DoesContainInvalidContent<Capacity> DoesContainInvalidContentCall,
368 DoesContainInvalidCharacter<Capacity> DoesContainInvalidCharacterCall>
370 const SemanticString& rhs) const noexcept -> bool {
371 return as_string() == rhs.as_string();
372}
373
374template <typename Child,
375 uint64_t Capacity,
376 DoesContainInvalidContent<Capacity> DoesContainInvalidContentCall,
377 DoesContainInvalidCharacter<Capacity> DoesContainInvalidCharacterCall>
378template <typename T>
383
384template <typename Child,
385 uint64_t Capacity,
386 DoesContainInvalidContent<Capacity> DoesContainInvalidContentCall,
387 DoesContainInvalidCharacter<Capacity> DoesContainInvalidCharacterCall>
389 const SemanticString& rhs) const noexcept -> bool {
390 return as_string() != rhs.as_string();
391}
392
393template <typename Child,
394 uint64_t Capacity,
395 DoesContainInvalidContent<Capacity> DoesContainInvalidContentCall,
396 DoesContainInvalidCharacter<Capacity> DoesContainInvalidCharacterCall>
397template <typename T>
402
403template <typename Child,
404 uint64_t Capacity,
405 DoesContainInvalidContent<Capacity> DoesContainInvalidContentCall,
406 DoesContainInvalidCharacter<Capacity> DoesContainInvalidCharacterCall>
408 const SemanticString& rhs) const noexcept -> bool {
409 return as_string() <= rhs.as_string();
410}
411
412template <typename Child,
413 uint64_t Capacity,
414 DoesContainInvalidContent<Capacity> DoesContainInvalidContentCall,
415 DoesContainInvalidCharacter<Capacity> DoesContainInvalidCharacterCall>
416template <typename T>
421
422template <typename Child,
423 uint64_t Capacity,
424 DoesContainInvalidContent<Capacity> DoesContainInvalidContentCall,
425 DoesContainInvalidCharacter<Capacity> DoesContainInvalidCharacterCall>
427 const SemanticString& rhs) const noexcept -> bool {
428 return as_string() < rhs.as_string();
429}
430
431template <typename Child,
432 uint64_t Capacity,
433 DoesContainInvalidContent<Capacity> DoesContainInvalidContentCall,
434 DoesContainInvalidCharacter<Capacity> DoesContainInvalidCharacterCall>
435template <typename T>
440
441template <typename Child,
442 uint64_t Capacity,
443 DoesContainInvalidContent<Capacity> DoesContainInvalidContentCall,
444 DoesContainInvalidCharacter<Capacity> DoesContainInvalidCharacterCall>
446 const SemanticString& rhs) const noexcept -> bool {
447 return as_string() >= rhs.as_string();
448}
449
450template <typename Child,
451 uint64_t Capacity,
452 DoesContainInvalidContent<Capacity> DoesContainInvalidContentCall,
453 DoesContainInvalidCharacter<Capacity> DoesContainInvalidCharacterCall>
454template <typename T>
459
460template <typename Child,
461 uint64_t Capacity,
462 DoesContainInvalidContent<Capacity> DoesContainInvalidContentCall,
463 DoesContainInvalidCharacter<Capacity> DoesContainInvalidCharacterCall>
465 const SemanticString& rhs) const noexcept -> bool {
466 return as_string() > rhs.as_string();
467}
468
469template <typename Child,
470 uint64_t Capacity,
471 DoesContainInvalidContent<Capacity> DoesContainInvalidContentCall,
472 DoesContainInvalidCharacter<Capacity> DoesContainInvalidCharacterCall>
473template <typename T>
478
479} // namespace bb
480} // namespace iox2
481
482#endif // IOX2_BB_SEMANTIC_STRING_HPP
The SemanticString is a string which has an inner syntax and restrictions to valid content....
auto operator!=(const SemanticString &rhs) const noexcept -> bool
checks if another SemanticString is not equal to this string
auto operator==(const SemanticString &rhs) const noexcept -> bool
checks if another SemanticString is equal to this string
auto operator<=(const SemanticString &rhs) const noexcept -> bool
checks if another SemanticString is less than or equal this string
constexpr auto size() const noexcept -> uint64_t
Returns the number of characters.
auto insert(uint64_t pos, const T &str, uint64_t count) noexcept -> bb::Expected< void, SemanticStringError >
Inserts another string into the SemanticString. If the value contains invalid characters or the resul...
auto append(const T &value) noexcept -> bb::Expected< void, SemanticStringError >
Appends another string to the SemanticString. If the value contains invalid characters or the result ...
constexpr auto as_string() const noexcept -> const bb::StaticString< Capacity > &
Returns a const reference to the underlying string. It is const and shall not be modified to guarante...
static constexpr auto capacity() noexcept -> uint64_t
Returns the capacity of the string.
static auto create(const bb::StaticString< N > &value) noexcept -> bb::Expected< Child, SemanticStringError >
Creates a new SemanticString from the provided string. If the value contains invalid characters or in...
auto operator>=(const SemanticString &rhs) const noexcept -> bool
checks if another SemanticString is greater than or equal this string
auto operator>(const SemanticString &rhs) const noexcept -> bool
checks if another SemanticString is greater than this string
auto operator<(const SemanticString &rhs) const noexcept -> bool
checks if another SemanticString is less than this string
static auto create(const char *value) noexcept -> bb::Expected< Child, SemanticStringError >
Creates a new SemanticString from the provided string literal. If the value contains invalid characte...
static auto create(const char(&value)[N]) noexcept -> bb::Expected< Child, SemanticStringError >
Creates a new SemanticString from the provided string literal. If the value contains invalid characte...
static auto from_utf8_null_terminated_unchecked_truncated(char const *utf8_str, SizeType count) -> StaticString
constexpr auto size() const noexcept -> SizeType
#define IOX2_LOG(level, msg_stream)
Macro for logging.
Definition logging.hpp:63
auto get_size(const StaticString< N > &data) -> uint64_t
bool(*)(const StaticString< Capacity > &value) DoesContainInvalidCharacter
SemanticStringError
Defines errors which can occur when modifying or creating a SemanticString.
constexpr auto err(const E &error) -> Unexpected< E >
Definition expected.hpp:33
bool(*)(const StaticString< Capacity > &value) DoesContainInvalidContent
iox2::bb::variation::Expected< T, E > Expected
Definition expected.hpp:22
typename std::enable_if_t< IsStaticString< T >::value||legacy::is_char_array< T >::value, ReturnType > RequireStaticStringOrCharArray