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
91 constexpr auto size() const noexcept -> uint64_t;
92
95 static constexpr auto capacity() noexcept -> uint64_t;
96
101 constexpr auto as_string() const noexcept -> const bb::StaticString<Capacity>&;
102
108 template <typename T>
109 auto append(const T& value) noexcept -> bb::Expected<void, SemanticStringError>;
110
118 template <typename T>
119 auto insert(uint64_t pos, const T& str, uint64_t count) noexcept -> bb::Expected<void, SemanticStringError>;
120
124 auto operator==(const SemanticString& rhs) const noexcept -> bool;
125
129 template <typename T>
130 auto operator==(const T& rhs) const noexcept -> bb::RequireStaticStringOrCharArray<T, bool>;
131
135 auto operator!=(const SemanticString& rhs) const noexcept -> bool;
136
140 template <typename T>
141 auto operator!=(const T& rhs) const noexcept -> bb::RequireStaticStringOrCharArray<T, bool>;
142
146 auto operator<=(const SemanticString& rhs) const noexcept -> bool;
147
151 template <typename T>
152 auto operator<=(const T& rhs) const noexcept -> bb::RequireStaticStringOrCharArray<T, bool>;
153
157 auto operator<(const SemanticString& rhs) const noexcept -> bool;
158
162 template <typename T>
163 auto operator<(const T& rhs) const noexcept -> bb::RequireStaticStringOrCharArray<T, bool>;
164
168 auto operator>=(const SemanticString& rhs) const noexcept -> bool;
169
173 template <typename T>
174 auto operator>=(const T& rhs) const noexcept -> bb::RequireStaticStringOrCharArray<T, bool>;
175
179 auto operator>(const SemanticString& rhs) const noexcept -> bool;
180
184 template <typename T>
185 auto operator>(const T& rhs) const noexcept -> bb::RequireStaticStringOrCharArray<T, bool>;
186
187 protected:
188 template <uint64_t N>
189 explicit SemanticString(const bb::StaticString<N>& value) noexcept;
190
191 private:
192 template <uint64_t N>
193 static auto create_impl(const char* value) noexcept -> bb::Expected<Child, SemanticStringError>;
194};
195
196
197template <typename Child,
198 uint64_t Capacity,
199 DoesContainInvalidContent<Capacity> DoesContainInvalidContentCall,
200 DoesContainInvalidCharacter<Capacity> DoesContainInvalidCharacterCall>
201template <uint64_t N>
202inline SemanticString<Child, Capacity, DoesContainInvalidContentCall, DoesContainInvalidCharacterCall>::SemanticString(
203 const bb::StaticString<N>& value) noexcept
204 : m_data { value } {
205}
206
207template <typename Child,
208 uint64_t Capacity,
209 DoesContainInvalidContent<Capacity> DoesContainInvalidContentCall,
210 DoesContainInvalidCharacter<Capacity> DoesContainInvalidCharacterCall>
211template <uint64_t N>
212inline auto
213SemanticString<Child, Capacity, DoesContainInvalidContentCall, DoesContainInvalidCharacterCall>::create_impl(
214 const char* value) noexcept -> bb::Expected<Child, SemanticStringError> {
215 if (N > Capacity) {
217 "Unable to create semantic string since the value \""
218 << value << "\" exceeds the maximum valid length of " << Capacity << ".");
220 }
221
223
224 if (DoesContainInvalidCharacterCall(str)) {
226 "Unable to create semantic string since the value \"" << value
227 << "\" contains invalid characters as content");
229 }
230
231 if (DoesContainInvalidContentCall(str)) {
233 "Unable to create semantic string since the value \"" << value << "\" contains invalid content");
235 }
236
237 return Child(str);
238}
239
240template <typename Child,
241 uint64_t Capacity,
242 DoesContainInvalidContent<Capacity> DoesContainInvalidContentCall,
243 DoesContainInvalidCharacter<Capacity> DoesContainInvalidCharacterCall>
244template <uint64_t N>
246 // avoid-c-arrays: justification in header
247 // NOLINTNEXTLINE(hicpp-avoid-c-arrays, cppcoreguidelines-avoid-c-arrays, hicpp-explicit-conversions, modernize-avoid-c-arrays)
248 const char (&value)[N]) noexcept -> bb::Expected<Child, SemanticStringError> {
250 template create_impl<N>(value);
251}
252
253template <typename Child,
254 uint64_t Capacity,
255 DoesContainInvalidContent<Capacity> DoesContainInvalidContentCall,
256 DoesContainInvalidCharacter<Capacity> DoesContainInvalidCharacterCall>
257template <uint64_t N>
263
264template <typename Child,
265 uint64_t Capacity,
266 DoesContainInvalidContent<Capacity> DoesContainInvalidContentCall,
267 DoesContainInvalidCharacter<Capacity> DoesContainInvalidCharacterCall>
268constexpr auto
273
274template <typename Child,
275 uint64_t Capacity,
276 DoesContainInvalidContent<Capacity> DoesContainInvalidContentCall,
277 DoesContainInvalidCharacter<Capacity> DoesContainInvalidCharacterCall>
278constexpr auto
283
284template <typename Child,
285 uint64_t Capacity,
286 DoesContainInvalidContent<Capacity> DoesContainInvalidContentCall,
287 DoesContainInvalidCharacter<Capacity> DoesContainInvalidCharacterCall>
288constexpr auto
293
294template <typename Child,
295 uint64_t Capacity,
296 DoesContainInvalidContent<Capacity> DoesContainInvalidContentCall,
297 DoesContainInvalidCharacter<Capacity> DoesContainInvalidCharacterCall>
298template <typename T>
303
304template <typename Child,
305 uint64_t Capacity,
306 DoesContainInvalidContent<Capacity> DoesContainInvalidContentCall,
307 DoesContainInvalidCharacter<Capacity> DoesContainInvalidCharacterCall>
308template <typename T>
310 const uint64_t pos, const T& str, const uint64_t count) noexcept -> bb::Expected<void, SemanticStringError> {
311 auto temp = m_data;
312 if (!temp.unchecked_code_units().insert(pos, str, 0, count)) {
314 "Unable to insert the value \""
315 << str.unchecked_access().c_str()
316 << "\" to the semantic string since it would exceed the maximum valid length of " << Capacity
317 << ".");
319 }
320
321 if (DoesContainInvalidCharacterCall(temp)) {
323 "Unable to insert the value \""
324 << str.unchecked_access().c_str()
325 << "\" to the semantic string since it contains invalid characters as content.");
327 }
328
329 if (DoesContainInvalidContentCall(temp)) {
331 "Unable to insert the value \""
332 << str.unchecked_access().c_str()
333 << "\" to the semantic string since it would lead to invalid content.");
335 }
336
337 m_data = temp;
338 return {};
339}
340
341template <typename Child,
342 uint64_t Capacity,
343 DoesContainInvalidContent<Capacity> DoesContainInvalidContentCall,
344 DoesContainInvalidCharacter<Capacity> DoesContainInvalidCharacterCall>
346 const SemanticString& rhs) const noexcept -> bool {
347 return as_string() == rhs.as_string();
348}
349
350template <typename Child,
351 uint64_t Capacity,
352 DoesContainInvalidContent<Capacity> DoesContainInvalidContentCall,
353 DoesContainInvalidCharacter<Capacity> DoesContainInvalidCharacterCall>
354template <typename T>
359
360template <typename Child,
361 uint64_t Capacity,
362 DoesContainInvalidContent<Capacity> DoesContainInvalidContentCall,
363 DoesContainInvalidCharacter<Capacity> DoesContainInvalidCharacterCall>
365 const SemanticString& rhs) const noexcept -> bool {
366 return as_string() != rhs.as_string();
367}
368
369template <typename Child,
370 uint64_t Capacity,
371 DoesContainInvalidContent<Capacity> DoesContainInvalidContentCall,
372 DoesContainInvalidCharacter<Capacity> DoesContainInvalidCharacterCall>
373template <typename T>
378
379template <typename Child,
380 uint64_t Capacity,
381 DoesContainInvalidContent<Capacity> DoesContainInvalidContentCall,
382 DoesContainInvalidCharacter<Capacity> DoesContainInvalidCharacterCall>
384 const SemanticString& rhs) const noexcept -> bool {
385 return as_string() <= rhs.as_string();
386}
387
388template <typename Child,
389 uint64_t Capacity,
390 DoesContainInvalidContent<Capacity> DoesContainInvalidContentCall,
391 DoesContainInvalidCharacter<Capacity> DoesContainInvalidCharacterCall>
392template <typename T>
397
398template <typename Child,
399 uint64_t Capacity,
400 DoesContainInvalidContent<Capacity> DoesContainInvalidContentCall,
401 DoesContainInvalidCharacter<Capacity> DoesContainInvalidCharacterCall>
403 const SemanticString& rhs) const noexcept -> bool {
404 return as_string() < rhs.as_string();
405}
406
407template <typename Child,
408 uint64_t Capacity,
409 DoesContainInvalidContent<Capacity> DoesContainInvalidContentCall,
410 DoesContainInvalidCharacter<Capacity> DoesContainInvalidCharacterCall>
411template <typename T>
416
417template <typename Child,
418 uint64_t Capacity,
419 DoesContainInvalidContent<Capacity> DoesContainInvalidContentCall,
420 DoesContainInvalidCharacter<Capacity> DoesContainInvalidCharacterCall>
422 const SemanticString& rhs) const noexcept -> bool {
423 return as_string() >= rhs.as_string();
424}
425
426template <typename Child,
427 uint64_t Capacity,
428 DoesContainInvalidContent<Capacity> DoesContainInvalidContentCall,
429 DoesContainInvalidCharacter<Capacity> DoesContainInvalidCharacterCall>
430template <typename T>
435
436template <typename Child,
437 uint64_t Capacity,
438 DoesContainInvalidContent<Capacity> DoesContainInvalidContentCall,
439 DoesContainInvalidCharacter<Capacity> DoesContainInvalidCharacterCall>
441 const SemanticString& rhs) const noexcept -> bool {
442 return as_string() > rhs.as_string();
443}
444
445template <typename Child,
446 uint64_t Capacity,
447 DoesContainInvalidContent<Capacity> DoesContainInvalidContentCall,
448 DoesContainInvalidCharacter<Capacity> DoesContainInvalidCharacterCall>
449template <typename T>
454
455} // namespace bb
456} // namespace iox2
457
458#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)[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