Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
inline_string_reference.hpp
Go to the documentation of this file.
1// Copyright 2026, Toyota Motor Corporation
2//
3// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4
5#ifndef INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_STRINGS_INLINE_STRING_REFERENCE_HPP_
6#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_STRINGS_INLINE_STRING_REFERENCE_HPP_
7
8// IWYU pragma: private
9// IWYU pragma: friend "(arene/base(?!/tests)|stdlib/include/stdlib_detail)/.*"
10
11// parasoft-begin-suppress AUTOSAR-A16_2_2-a-2 "Arene Base aggregate headers permitted by A16-2-2 Permit #1"
18#include "arene/base/detail/exceptions.hpp"
19#include "arene/base/detail/raw_c_string.hpp"
20#include "arene/base/stdlib_choice/cstddef.hpp"
21#include "arene/base/stdlib_choice/enable_if.hpp"
22#include "arene/base/stdlib_choice/ignore.hpp"
25// parasoft-end-suppress AUTOSAR-A16_2_2-a-2 "Arene Base aggregate headers permitted by A16-2-2 Permit #1"
26
27namespace arene {
28namespace base {
29
30// parasoft-begin-suppress AUTOSAR-A7_1_5-a "Trailing return syntax permitted by A7-1-5 Permit #1 v1.0.0"
31// parasoft-begin-suppress AUTOSAR-A13_5_1-a "False positive:" rule specifies
32// that a const overload of 'operator[]' is required if a non-const overload of
33// 'operator[]' is defined. These classes only define const overloads."
34
35// forward declaration
37
38namespace inline_string_reference_detail {
39
40/// @brief function pointers to be used inside const member functions
41struct const_operations {
42 // parasoft-begin-suppress AUTOSAR-A2_10_1-d "False positive: does not hide anything."
43 /// @brief The type of the base class
44 using string_base = inline_string_detail::inline_string_base;
45 /// @brief The type of a const pointer to the base class
46 using string_ptr = string_base const*;
47 /// @brief The type of a size at the base class
48 using size_type = string_base::size_type;
49 /// @brief The type of a const iterator at the base class
50 using const_iterator = string_base::const_iterator;
51 // parasoft-end-suppress AUTOSAR-A2_10_1-d "False positive: does not hide anything."
52
53 // parasoft-begin-suppress CERT_C-EXP37-b-3 "False positive: The rule does not mention naming all parameters"
54 /// @brief The function pointer type of the data operation
55 using data_op_type = detail::raw_c_string (*)(string_ptr);
56 /// @brief The function pointer type of the size operation
57 using size_op_type = size_type (*)(string_ptr);
58 /// @brief The function pointer type of the capacity operation
59 using capacity_op_type = size_type (*)(string_ptr);
60 /// @brief The function pointer type of the empty operation
61 using empty_op_type = bool (*)(string_ptr);
62 /// @brief The function pointer type of the element accessor
63 using element_op_type = detail::character const& (*)(string_ptr, size_type);
64 /// @brief The function pointer type of the at operation
65 using at_op_type = detail::character const& (*)(string_ptr, size_type);
66 /// @brief The function pointer type of the front operation
67 using front_op_type = detail::character const& (*)(string_ptr);
68 /// @brief The function pointer type of the back operation
69 using back_op_type = detail::character const& (*)(string_ptr);
70 /// @brief The function pointer type of the begin operation
71 using begin_op_type = const_iterator (*)(string_ptr);
72 /// @brief The function pointer type of the end operation
73 using end_op_type = const_iterator (*)(string_ptr);
74 /// @brief The function pointer type of the to_string_view operation
75 using to_string_view_op_type = string_view (*)(string_ptr);
76 // parasoft-end-suppress CERT_C-EXP37-b-3 "False positive: The rule does not mention naming all parameters"
77
78 /// @brief The function pointer of the data operation
79 data_op_type data;
80 /// @brief The function pointer of the size operation
81 size_op_type size;
82 /// @brief The function pointer of the capacity operation
83 capacity_op_type capacity;
84 /// @brief The function pointer of the empty operation
85 empty_op_type empty;
86 /// @brief The function pointer of the element accessor
87 element_op_type element;
88 /// @brief The function pointer of the at operation
89 at_op_type at;
90 /// @brief The function pointer of the front operation
91 front_op_type front;
92 /// @brief The function pointer of the back operation
93 back_op_type back;
94 /// @brief The function pointer of the begin iterator
95 begin_op_type begin;
96 /// @brief The function pointer of the end iterator
97 end_op_type end;
98 /// @brief The function pointer of the to_string_view operation
99 to_string_view_op_type to_string_view;
100};
101
102/// @brief Implementation of the const operations of a specific MaxSize
103/// @tparam MaxSize The maximum capacity of the string, _not_ including the null terminator
104template <std::size_t MaxSize>
105class const_operations_impl {
106 public:
107 /// @brief The type of the underlying string
108 using this_string = inline_string<MaxSize>;
109
110 // parasoft-begin-suppress AUTOSAR-A2_10_1-d "False positive: does not hide anything."
111 /// @brief The type of a pointer to the base class of the underlying string
112 using string_ptr = const_operations::string_ptr;
113 /// @brief The type of a size at the base class
114 using size_type = const_operations::size_type;
115 /// @brief The type of a const iterator at the base class
116 using const_iterator = const_operations::const_iterator;
117 // parasoft-end-suppress AUTOSAR-A2_10_1-d "False positive: does not hide anything."
118
119 /// @brief Cast base class pointer back to the underlying string type
120 /// @param ptr The base class pointer
121 /// @return The pointer as the underlying string type
122 static constexpr auto string(string_ptr ptr) noexcept -> this_string const* {
123 return static_cast<inline_string<MaxSize> const*>(ptr);
124 }
125
126 /// @brief Return a pointer to the NUL-terminated string held in @c *this
127 /// @param ptr The base class pointer
128 /// @return a pointer to the NUL-terminated string held in @c *this
129 static constexpr auto do_data(string_ptr ptr) noexcept -> detail::raw_c_string { return string(ptr)->data(); }
130
131 /// @brief Get the length of the string
132 /// @param ptr The base class pointer
133 /// @return The number of characters in the string, non-inclusive of the null-terminator
134 static constexpr auto do_size(string_ptr ptr) noexcept -> size_type { return string(ptr)->size(); }
135
136 /// @brief The maximum length of the string
137 /// @param ptr The base class pointer
138 /// @return size_type @c MaxSize
139 static constexpr auto do_capacity(string_ptr ptr) noexcept -> size_type { return string(ptr)->capacity(); }
140
141 /// @brief Check if the string is empty
142 /// @param ptr The base class pointer
143 /// @return @c true if the string is empty, @c false otherwise
144 static constexpr auto do_empty(string_ptr ptr) noexcept -> bool { return string(ptr)->empty(); }
145
146 /// @brief Retrieve the @c index -th character in the string
147 /// @param ptr The base class pointer
148 /// @param index The index of the character to retrieve
149 /// @return A reference to that character
150 static constexpr auto do_element(string_ptr ptr, size_type index) noexcept -> detail::character const& {
151 return (*string(ptr))[index];
152 }
153 /// @brief Retrieve the @c index -th character in the string
154 /// @tparam AreExceptionsEnabled Used to disable this overload if exceptions are not enabled.
155 /// @param ptr The base class pointer
156 /// @param index The index of the character to retrieve
157 /// @return A reference to that character
158 template <
159 bool AreExceptionsEnabled = detail::are_exceptions_enabled::value,
161 static constexpr auto do_at(string_ptr ptr, size_type index) -> detail::character const& {
162 return string(ptr)->at(index);
163 }
164 /// @brief When exceptions are disabled, invoking this is a precondition violation.
165 /// @tparam AreExceptionsEnabled Used to enable this overload if exceptions are not enabled.
166 /// @return Never returns
167 template <
168 bool AreExceptionsEnabled = detail::are_exceptions_enabled::value,
170 ARENE_NORETURN static auto do_at(string_ptr, std::size_t) -> detail::character const& {
171 ARENE_INVARIANT_UNREACHABLE("do_at() called when exceptions are disabled");
172 }
173
174 /// @brief Get the first character in the string
175 /// @param ptr The base class pointer
176 /// @return A reference to the first character
177 static constexpr auto do_front(string_ptr ptr) noexcept -> detail::character const& { return string(ptr)->front(); }
178 /// @brief Get the last character in the string
179 /// @param ptr The base class pointer
180 /// @return A reference to the last character
181 static constexpr auto do_back(string_ptr ptr) noexcept -> detail::character const& { return string(ptr)->back(); }
182
183 /// @brief Iterator to the first position in the string
184 /// @param ptr The base class pointer
185 /// @return An iterator to the first element if not empty, @c end() otherwise
186 static constexpr auto do_begin(string_ptr ptr) noexcept -> const_iterator { return string(ptr)->begin(); }
187 /// @brief The one-past-the-last position in the string
188 /// @param ptr The base class pointer
189 /// @return An iterator to the one-past-the-last position in the string
190 static constexpr auto do_end(string_ptr ptr) noexcept -> const_iterator { return string(ptr)->end(); }
191
192 /// @brief Convert to string view
193 /// @param ptr The base class pointer
194 /// @return A string_view object referencing the data
195 static constexpr auto do_to_string_view(string_ptr ptr) noexcept -> string_view {
196 return {do_data(ptr), do_size(ptr)};
197 }
198};
199
200/// @brief Instance of a holder of the const operations of a specific size
201/// @tparam MaxSize The maximum capacity of the string, _not_ including the null terminator
202template <std::size_t MaxSize>
203extern constexpr const_operations const_string_operations{
204 &const_operations_impl<MaxSize>::do_data,
205 &const_operations_impl<MaxSize>::do_size,
206 &const_operations_impl<MaxSize>::do_capacity,
207 &const_operations_impl<MaxSize>::do_empty,
208 &const_operations_impl<MaxSize>::do_element,
209 &const_operations_impl<MaxSize>::do_at,
210 &const_operations_impl<MaxSize>::do_front,
211 &const_operations_impl<MaxSize>::do_back,
212 &const_operations_impl<MaxSize>::do_begin,
213 &const_operations_impl<MaxSize>::do_end,
214 &const_operations_impl<MaxSize>::do_to_string_view,
215};
216
217/// @brief function pointers to be used inside member functions
218struct operations {
219 /// @brief The type of the base class
220 using string_base = inline_string_detail::inline_string_base;
221 /// @brief The type of a pointer to the base class
222 using string_ptr = string_base*;
223 // parasoft-begin-suppress AUTOSAR-A2_10_1-d "False positive: does not hide anything."
224 /// @brief The type of a size at the base class
225 using size_type = string_base::size_type;
226 /// @brief The type of an iterator at the base class
227 using iterator = string_base::iterator;
228 /// @brief The type of a const iterator at the base class
229 using const_iterator = string_base::const_iterator;
230 // parasoft-end-suppress AUTOSAR-A2_10_1-d "False positive: does not hide anything."
231
232 // parasoft-begin-suppress CERT_C-EXP37-b-3 "False positive: The rule does not mention naming all parameters"
233 /// @brief The function pointer type of the element accessor
234 using element_op_type = detail::character& (*)(string_ptr, size_type);
235 /// @brief The function pointer type of the at operation
236 using at_op_type = detail::character& (*)(string_ptr, size_type);
237 /// @brief The function pointer type of the front operation
238 using front_op_type = detail::character& (*)(string_ptr);
239 /// @brief The function pointer type of the back operation
240 using back_op_type = detail::character& (*)(string_ptr);
241 /// @brief The function pointer type of the begin operation
242 using begin_op_type = iterator (*)(string_ptr);
243 /// @brief The function pointer type of the end operation
244 using end_op_type = iterator (*)(string_ptr);
245 /// @brief The function pointer type of the to_string_view operation
246 using to_string_view_op_type = string_view (*)(string_ptr);
247 /// @brief The function pointer type of the assign operation
248 using assign_op_type = string_ptr (*)(string_ptr, string_view);
249 /// @brief The function pointer type of the append operation
250 using append_op_type = string_ptr (*)(string_ptr, string_view);
251 /// @brief The function pointer type of the append count char operation
252 using append_count_char_op_type = string_ptr (*)(string_ptr, size_type, detail::character);
253 /// @brief The function pointer type of the erase operation
254 using erase_op_type = string_ptr (*)(string_ptr, size_type, size_type);
255 /// @brief The function pointer type of the erase by iterator operation
256 using erase_it_op_type = iterator (*)(string_ptr, const_iterator, const_iterator);
257 // parasoft-end-suppress CERT_C-EXP37-b-3 "False positive: The rule does not mention naming all parameters"
258
259 /// @brief The pointer to the object holding the const operations
260 const_operations const* const_ops;
261 /// @brief The function pointer of the element accessor
262 element_op_type element;
263 /// @brief The function pointer of the at operation
264 at_op_type at;
265 /// @brief The function pointer of the front operation
266 front_op_type front;
267 /// @brief The function pointer of the back operation
268 back_op_type back;
269 /// @brief The function pointer of the begin iterator
270 begin_op_type begin;
271 /// @brief The function pointer of the end iterator
272 end_op_type end;
273 /// @brief The function pointer of the to_string_view operation
274 to_string_view_op_type to_string_view;
275 /// @brief The function pointer of the assign operation
276 assign_op_type assign;
277 /// @brief The function pointer of the append operation
278 append_op_type append;
279 /// @brief The function pointer of the append count char operation
280 append_count_char_op_type append_count_char;
281 /// @brief The function pointer of the erase operation
282 erase_op_type erase;
283 /// @brief The function pointer of the erase by iterator operation
284 erase_it_op_type erase_it;
285};
286
287/// @brief Implementation of the operations of a specific size
288/// @tparam MaxSize The maximum capacity of the string, _not_ including the null terminator
289template <std::size_t MaxSize>
290class operations_impl {
291 public:
292 /// @brief The type of the underlying string
293 using this_string = inline_string<MaxSize>;
294 /// @brief The type of a pointer to the base class of the underlying string
295 using string_ptr = inline_string_detail::inline_string_base*;
296
297 // parasoft-begin-suppress AUTOSAR-A2_10_1-d "False positive: does not hide anything."
298 /// @brief The type of a size at the base class
299 using size_type = operations::size_type;
300 /// @brief The type of an iterator at the base class
301 using iterator = operations::iterator;
302 /// @brief The type of a const iterator at the base class
303 using const_iterator = operations::const_iterator;
304 // parasoft-end-suppress AUTOSAR-A2_10_1-d "False positive: does not hide anything."
305
306 /// @brief Cast base class pointer back to the underlying string type
307 /// @param ptr The base class pointer
308 /// @return The pointer as the underlying string type
309 static constexpr auto string(string_ptr ptr) noexcept -> this_string* {
310 return static_cast<inline_string<MaxSize>*>(ptr);
311 }
312
313 /// @brief Retrieve the @c index -th character in the string
314 /// @param ptr The base class pointer
315 /// @param index The index of the character to retrieve
316 /// @return A reference to that character
317 static constexpr auto do_element(string_ptr ptr, size_type index) noexcept -> detail::character& {
318 return (*string(ptr))[index];
319 }
320 /// @brief Retrieve the @c index -th character in the string
321 /// @tparam AreExceptionsEnabled Used to disable this overload if exceptions are not enabled.
322 /// @param ptr The base class pointer
323 /// @param index The index of the character to retrieve
324 /// @return A reference to that character
325 template <
326 bool AreExceptionsEnabled = detail::are_exceptions_enabled::value,
328 static constexpr auto do_at(string_ptr ptr, size_type index) -> detail::character& {
329 return string(ptr)->at(index);
330 }
331 /// @brief When exceptions are disabled, invoking this is a precondition violation.
332 /// @tparam AreExceptionsEnabled Used to enable this overload if exceptions are not enabled.
333 /// @return Never returns
334 template <
335 bool AreExceptionsEnabled = detail::are_exceptions_enabled::value,
337 ARENE_NORETURN static auto do_at(string_ptr, std::size_t) -> detail::character& {
338 ARENE_INVARIANT_UNREACHABLE("do_at() called when exceptions are disabled");
339 }
340
341 /// @brief Get the first character in the string
342 /// @param ptr The base class pointer
343 /// @return A reference to the first character
344 static constexpr auto do_front(string_ptr ptr) noexcept -> detail::character& { return string(ptr)->front(); }
345 /// @brief Get the last character in the string
346 /// @param ptr The base class pointer
347 /// @return A reference to the last character
348 static constexpr auto do_back(string_ptr ptr) noexcept -> detail::character& { return string(ptr)->back(); }
349
350 /// @brief Iterator to the first position in the string
351 /// @param ptr The base class pointer
352 /// @return An iterator to the first element if not empty, @c end() otherwise
353 static constexpr auto do_begin(string_ptr ptr) noexcept -> iterator { return string(ptr)->begin(); }
354 /// @brief The one-past-the-last position in the string
355 /// @param ptr The base class pointer
356 /// @return An iterator to the one-past-the-last position in the string
357 static constexpr auto do_end(string_ptr ptr) noexcept -> iterator { return string(ptr)->end(); }
358
359 /// @brief Convert to string view
360 /// @param ptr The base class pointer
361 /// @return A string_view object referencing the data
362 static constexpr auto do_to_string_view(string_ptr ptr) noexcept -> string_view {
363 return {string(ptr)->data(), string(ptr)->size()};
364 }
365
366 /// @brief Assign from a string_view
367 /// @param ptr The source string
368 /// @param rhs The string to append
369 /// @return A pointer to the underlying object as the base class
370 static constexpr auto do_assign(string_ptr ptr, string_view rhs) noexcept -> string_ptr {
371 return &string(ptr)->assign(rhs);
372 }
373
374 /// @brief Append a string to the current string
375 /// @param ptr The source string
376 /// @param rhs The string to append
377 /// @return A pointer to the underlying object as the base class
378 static constexpr auto do_append(string_ptr ptr, string_view rhs) noexcept -> string_ptr {
379 return &string(ptr)->append(rhs);
380 }
381 /// @brief Append the specified number of copies of a character to the string
382 /// @param ptr The source string
383 /// @param count The number of characters to append
384 /// @param chr The character to append
385 /// @return A pointer to the underlying object as the base class
386 static constexpr auto do_append_count_char(string_ptr ptr, size_type count, detail::character chr) noexcept
387 -> string_ptr {
388 return &string(ptr)->append(count, chr);
389 }
390
391 /// @brief Erase the specified number of characters in the string starting at the specified position.
392 /// @param ptr The source string
393 /// @param pos The position of the characters to remove. If @c >size() is a noop.
394 /// @param count The maximum number of characters to remove.
395 /// @return A reference to this object
396 static constexpr auto do_erase(string_ptr ptr, size_type pos, size_type count) noexcept -> string_ptr {
397 return &string(ptr)->erase(pos, count);
398 }
399 /// @brief Erase the characters in the specified range.
400 /// @param ptr The source string
401 /// @param first The start of the range
402 /// @param last The end of the range
403 /// @return An iterator referring to the character after the removed range, or @c end()
404 static constexpr auto do_erase_it(string_ptr ptr, const_iterator first, const_iterator last) noexcept -> iterator {
405 return string(ptr)->erase(first, last);
406 }
407};
408
409/// @brief Instance of a holder of the operations of a specific size
410/// @tparam MaxSize The maximum capacity of the string, _not_ including the null terminator
411template <std::size_t MaxSize>
412extern constexpr operations string_operations{
413 &const_string_operations<MaxSize>,
414 &operations_impl<MaxSize>::do_element,
415 &operations_impl<MaxSize>::do_at,
416 &operations_impl<MaxSize>::do_front,
417 &operations_impl<MaxSize>::do_back,
418 &operations_impl<MaxSize>::do_begin,
419 &operations_impl<MaxSize>::do_end,
420 &operations_impl<MaxSize>::do_to_string_view,
421 &operations_impl<MaxSize>::do_assign,
422 &operations_impl<MaxSize>::do_append,
423 &operations_impl<MaxSize>::do_append_count_char,
424 &operations_impl<MaxSize>::do_erase,
425 &operations_impl<MaxSize>::do_erase_it,
426};
427
428/// @brief Passkey type for internal access
429class ref_passkey {
430 /// @brief Prevent construction from @c {}
431 explicit ref_passkey() = default;
432 // parasoft-begin-suppress AUTOSAR-A11_3_1-a "Passkey idiom permitted by A11-3-1 Permit #1"
433 /// @brief friend declaration to allow this class to construct the passkey
434 friend class ::arene::base::const_inline_string_reference;
435 // parasoft-end-suppress AUTOSAR-A11_3_1-a "Passkey idiom permitted by A11-3-1 Permit #1"
436};
437
438} // namespace inline_string_reference_detail
439
440/// @brief A reference class to an inline_string object, with size-erased type
442 /// @brief A pointer to the underlying object as the base class
443 inline_string_detail::inline_string_base* string_ptr_;
444 /// @brief A pointer to the struct holding operation implementations
445 inline_string_reference_detail::operations const* op_ptrs_;
446
447 public:
448 /// @brief The type of the base class
449 using string_base = inline_string_detail::inline_string_base;
450
451 // parasoft-begin-suppress AUTOSAR-A2_10_1-d "False positive: does not hide anything."
452 /// @brief The type of a size at the base class
453 using size_type = string_base::size_type;
454 /// @brief The type of an iterator at the base class
455 using iterator = string_base::iterator;
456 /// @brief The type of a const iterator at the base class
457 using const_iterator = string_base::const_iterator;
458 // parasoft-end-suppress AUTOSAR-A2_10_1-d "False positive: does not hide anything."
459
460 /// @brief Construct a reference to the supplied inline_string
461 /// @tparam MaxSize The maximum capacity of the string, _not_ including the null terminator
462 /// @param str Reference to the inline_string
463 template <std::size_t MaxSize>
464 constexpr explicit inline_string_reference(inline_string<MaxSize>& str) noexcept
466 string_ptr_(&str),
467 op_ptrs_(&inline_string_reference_detail::string_operations<MaxSize>) {}
468
469 /// @brief Return a pointer to the NUL-terminated string held in @c *this
470 /// @return a pointer to the NUL-terminated string held in @c *this
471 ARENE_NODISCARD constexpr auto c_str() const noexcept -> detail::raw_c_string {
472 return op_ptrs_->const_ops->data(string_ptr_);
473 }
474 /// @brief Return a pointer to the NUL-terminated string held in @c *this
475 /// @return A pointer to the NUL-terminated string held in @c *this
476 ARENE_NODISCARD constexpr auto data() const noexcept -> detail::raw_c_string {
477 return op_ptrs_->const_ops->data(string_ptr_);
478 }
479
480 /// @brief Get the length of the string
481 /// @return The number of characters in the string, non-inclusive of the null-terminator
482 constexpr auto size() const noexcept -> size_type { return op_ptrs_->const_ops->size(string_ptr_); }
483
484 /// @brief The maximum length of the string
485 /// @return size_type @c MaxSize
486 constexpr auto capacity() const noexcept -> size_type { return op_ptrs_->const_ops->capacity(string_ptr_); }
487
488 /// @brief Check if the string is empty
489 /// @return @c true if the string is empty, @c false otherwise
490 constexpr auto empty() const noexcept -> bool { return op_ptrs_->const_ops->empty(string_ptr_); }
491
492 /// @brief Retrieve the @c index -th character in the string
493 /// @param index The index of the character to retrieve
494 /// @return A reference to that character
495 ARENE_NODISCARD constexpr auto operator[](size_type const index) const noexcept -> detail::character& {
496 return op_ptrs_->element(string_ptr_, index);
497 }
498 /// @brief Retrieve the @c index -th character in the string
499 /// @tparam AreExceptionsEnabled Used to disable this overload if exceptions are not enabled.
500 /// @param index The index of the character to retrieve
501 /// @return A reference to that character
502 template <
503 bool AreExceptionsEnabled = detail::are_exceptions_enabled::value,
505 constexpr auto at(size_type const index) const -> detail::character& {
506 return op_ptrs_->at(string_ptr_, index);
507 }
508
509 /// @brief Get the first character in the string
510 /// @return A reference to the first character
511 constexpr auto front() const noexcept -> detail::character& { return op_ptrs_->front(string_ptr_); }
512 /// @brief Get the last character in the string
513 /// @return A reference to the last character
514 constexpr auto back() const noexcept -> detail::character& { return op_ptrs_->back(string_ptr_); }
515
516 /// @brief Return an iterator to the first position in the string
517 /// @return An iterator to the first element if not empty, @c end() otherwise
518 constexpr auto begin() const noexcept -> iterator { return op_ptrs_->begin(string_ptr_); }
519 /// @brief The one-past-the-last position in the string
520 /// @return An iterator to the one-past-the-last position in the string
521 constexpr auto end() const noexcept -> iterator { return op_ptrs_->end(string_ptr_); }
522
523 /// @brief Convert to string view
524 /// @return A string_view object referencing the data
525 constexpr auto to_string_view() const noexcept -> string_view { return op_ptrs_->to_string_view(string_ptr_); }
526
527 /// @brief Assign from a string_view
528 /// @param rhs The string to append
529 /// @return A reference to this object
530 constexpr auto assign(string_view const rhs) const noexcept -> inline_string_reference const& {
531 std::ignore = op_ptrs_->assign(string_ptr_, rhs);
532 return *this;
533 }
534
535 /// @brief Append a string to the current string
536 /// @param rhs The string to append
537 /// @return A reference to this object
538 constexpr auto append(string_view const rhs) const noexcept -> inline_string_reference const& {
539 std::ignore = op_ptrs_->append(string_ptr_, rhs);
540 return *this;
541 }
542 /// @brief Append the specified number of copies of a character to the string
543 /// @param count The number of characters to append
544 /// @param chr The character to append
545 /// @return A reference to this object
546 constexpr auto append(size_type const count, detail::character const chr) const noexcept
547 -> inline_string_reference const& {
548 std::ignore = op_ptrs_->append_count_char(string_ptr_, count, chr);
549 return *this;
550 }
551
552 /// @brief Append a string to the current string
553 /// @param rhs The string to append
554 /// @return A reference to this object
555 constexpr auto operator+=(string_view const rhs) const noexcept -> inline_string_reference const& {
556 return this->append(rhs);
557 }
558 /// @brief Append a character to the current string
559 /// @param rhs The character to append
560 /// @return A reference to this object
561 constexpr auto operator+=(detail::character const rhs) const noexcept -> inline_string_reference const& {
562 return this->append(string_view{&rhs, 1U});
563 }
564
565 /// @brief Append a string to the current string
566 /// @param rhs The string to append
567 /// @return A reference to this object
568 constexpr auto push_back(string_view const rhs) const noexcept -> inline_string_reference const& {
569 return this->operator+=(rhs);
570 }
571 /// @brief Append a character to the current string
572 /// @param rhs The character to append
573 /// @return A reference to this object
574 constexpr auto push_back(detail::character const rhs) const noexcept -> inline_string_reference const& {
575 return this->operator+=(rhs);
576 }
577
578 /// @brief Erase the specified number of characters in the string starting at the specified position.
579 /// @param pos The position of the characters to remove. If @c pos>size() this is a noop.
580 /// @param count The maximum number of characters to remove.
581 /// @return A reference to this object
582 constexpr auto erase(size_type const pos, size_type const count) const noexcept -> inline_string_reference const& {
583 std::ignore = op_ptrs_->erase(string_ptr_, pos, count);
584 return *this;
585 }
586 /// @brief Erase the characters in the specified range.
587 /// @param first The start of the range
588 /// @param last The end of the range
589 /// @return An iterator referring to the character after the removed range, or @c end()
590 constexpr auto erase(const_iterator const first, const_iterator const last) const noexcept -> iterator {
591 return op_ptrs_->erase_it(string_ptr_, first, last);
592 }
593
594 /// @brief Compare two strings for lexicographical ordering, forwarded to string_view's
595 /// @param lhs The first string representation
596 /// @param rhs The second string representation
597 /// @return strong_ordering equivalent to string_view's 3 way compare result
598 ARENE_NODISCARD static constexpr auto
600 return string_view::three_way_compare(lhs.to_string_view(), rhs.to_string_view());
601 }
602 /// @brief Compare two strings for lexicographical ordering, forwarded to string_view's
603 /// @param lhs The first string representation
604 /// @param rhs The second string representation
605 /// @return strong_ordering equivalent to string_view's 3 way compare result
606 ARENE_NODISCARD static constexpr auto
608 return string_view::three_way_compare(lhs.to_string_view(), rhs);
609 }
610
611 /// @brief Get a pointer to the underlying string object pointer
612 /// @return A pointer to the underlying string object pointer
613 constexpr auto get_string_ptr(inline_string_reference_detail::ref_passkey) const noexcept
614 -> inline_string_detail::inline_string_base* {
615 return string_ptr_;
616 }
617
618 /// @brief Get a pointer to the struct holding const operation implementations
619 /// @return A pointer to the struct holding const operation implementations
620 constexpr auto const_operations_ptr(inline_string_reference_detail::ref_passkey) const noexcept
621 -> inline_string_reference_detail::const_operations const* {
622 return op_ptrs_->const_ops;
623 }
624};
625
626// parasoft-begin-suppress AUTOSAR-A12_1_5-a "False positive: there is no common constructor to delegate to."
627/// @brief A const reference class to an inline_string object, with size-erased type
629 /// @brief A pointer to the underlying string object
630 inline_string_detail::inline_string_base const* string_ptr_;
631 /// @brief A pointer to the struct holding function pointers to the const operation implementations
632 inline_string_reference_detail::const_operations const* const_op_ptrs_;
633
634 public:
635 /// @brief The type of the base class
636 using string_base = inline_string_detail::inline_string_base;
637
638 // parasoft-begin-suppress AUTOSAR-A2_10_1-d "False positive: does not hide anything."
639 /// @brief The type of a size at the base class
640 using size_type = string_base::size_type;
641 /// @brief The type of a const iterator at the base class
642 using const_iterator = string_base::const_iterator;
643 // parasoft-end-suppress AUTOSAR-A2_10_1-d "False positive: does not hide anything."
644
645 /// @brief Construct a const reference to the supplied inline_string
646 /// @tparam MaxSize The maximum capacity of the string, _not_ including the null terminator
647 /// @param str Const reference to the inline_string
648 template <std::size_t MaxSize>
649 constexpr explicit const_inline_string_reference(inline_string<MaxSize> const& str) noexcept
651 string_ptr_(&str),
652 const_op_ptrs_(&inline_string_reference_detail::const_string_operations<MaxSize>) {}
653
654 /// @brief Construct a const reference out of a non-const one
655 /// @param ref The non-const reference class
656 constexpr explicit const_inline_string_reference(inline_string_reference const& ref) noexcept
658 string_ptr_(ref.get_string_ptr(inline_string_reference_detail::ref_passkey{})),
659 const_op_ptrs_(ref.const_operations_ptr(inline_string_reference_detail::ref_passkey{})) {}
660
661 /// @brief Return a pointer to the NUL-terminated string held in @c *this
662 /// @return A pointer to the NUL-terminated string held in @c *this
663 ARENE_NODISCARD constexpr auto c_str() const noexcept -> detail::raw_c_string {
664 return const_op_ptrs_->data(string_ptr_);
665 }
666 /// @brief Return a pointer to the NUL-terminated string held in @c *this
667 /// @return A pointer to the NUL-terminated string held in @c *this
668 ARENE_NODISCARD constexpr auto data() const noexcept -> detail::raw_c_string {
669 return const_op_ptrs_->data(string_ptr_);
670 }
671
672 /// @brief Get the length of the string
673 /// @return The number of characters in the string, non-inclusive of the null-terminator
674 constexpr auto size() const noexcept -> size_type { return const_op_ptrs_->size(string_ptr_); }
675
676 /// @brief The maximum length of the string
677 /// @return size_type @c MaxSize
678 constexpr auto capacity() const noexcept -> size_type { return const_op_ptrs_->capacity(string_ptr_); }
679
680 /// @brief Check if the string is empty
681 /// @return @c true if the string is empty, @c false otherwise
682 constexpr auto empty() const noexcept -> bool { return const_op_ptrs_->empty(string_ptr_); }
683
684 /// @brief Retrieve the @c index -th character in the string
685 /// @param index The index of the character to retrieve
686 /// @return A reference to that character
687 ARENE_NODISCARD constexpr auto operator[](size_type const index) const noexcept -> detail::character const& {
688 return const_op_ptrs_->element(string_ptr_, index);
689 }
690 /// @brief Retrieve the @c index -th character in the string
691 /// @tparam AreExceptionsEnabled Used to disable this overload if exceptions are not enabled.
692 /// @param index The index of the character to retrieve
693 /// @return A reference to that character
694 template <
695 bool AreExceptionsEnabled = detail::are_exceptions_enabled::value,
697 constexpr auto at(size_type const index) const -> detail::character const& {
698 return const_op_ptrs_->at(string_ptr_, index);
699 }
700
701 /// @brief Get the first character in the string
702 /// @return A reference to the first character
703 constexpr auto front() const noexcept -> detail::character const& { return const_op_ptrs_->front(string_ptr_); }
704 /// @brief Get the last character in the string
705 /// @return A reference to the last character
706 constexpr auto back() const noexcept -> detail::character const& { return const_op_ptrs_->back(string_ptr_); }
707
708 /// @brief Return an iterator to the first position in the string
709 /// @return A const iterator to the first element if not empty, @c end() otherwise
710 constexpr auto begin() const noexcept -> const_iterator { return const_op_ptrs_->begin(string_ptr_); }
711 /// @brief The one-past-the-last position in the string
712 /// @return A const iterator to one-past-the-last position in the string
713 constexpr auto end() const noexcept -> const_iterator { return const_op_ptrs_->end(string_ptr_); }
714
715 /// @brief Convert to string view
716 /// @return string_view A string_view object referencing the data
717 constexpr auto to_string_view() const noexcept -> string_view { return const_op_ptrs_->to_string_view(string_ptr_); }
718
719 /// @brief Compare two strings for lexicographical ordering, forwarded to string_view's
720 /// @param lhs The first string representation
721 /// @param rhs The second string representation
722 /// @return strong_ordering equivalent to string_view's 3 way compare result
723 ARENE_NODISCARD static constexpr auto three_way_compare(
726 ) noexcept -> strong_ordering {
727 return string_view::three_way_compare(lhs.to_string_view(), rhs.to_string_view());
728 }
729 /// @brief Compare two strings for lexicographical ordering, forwarded to string_view's
730 /// @param lhs The first string representation
731 /// @param rhs The second string representation
732 /// @return strong_ordering equivalent to string_view's 3 way compare result
733 ARENE_NODISCARD static constexpr auto three_way_compare(
736 ) noexcept -> strong_ordering {
737 return string_view::three_way_compare(lhs.to_string_view(), rhs.to_string_view());
738 }
739 /// @brief Compare two strings for lexicographical ordering, forwarded to string_view's
740 /// @param lhs The first string representation
741 /// @param rhs The second string representation
742 /// @return strong_ordering equivalent to string_view's 3 way compare result
743 ARENE_NODISCARD static constexpr auto
745 return string_view::three_way_compare(lhs.to_string_view(), rhs);
746 }
747};
748// parasoft-end-suppress AUTOSAR-A12_1_5-a "False positive: there is no common constructor to delegate to."
749
750} // namespace base
751} // namespace arene
752
753#endif // INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_STRINGS_INLINE_STRING_REFERENCE_HPP_
A const reference class to an inline_string object, with size-erased type.
Definition inline_string_reference.hpp:628
constexpr auto back() const noexcept -> detail::character const &
Get the last character in the string.
Definition inline_string_reference.hpp:706
string_base::size_type size_type
The type of a size at the base class.
Definition inline_string_reference.hpp:640
constexpr auto front() const noexcept -> detail::character const &
Get the first character in the string.
Definition inline_string_reference.hpp:703
constexpr auto at(size_type const index) const -> detail::character const &
Retrieve the index -th character in the string.
Definition inline_string_reference.hpp:697
ARENE_NODISCARD constexpr auto c_str() const noexcept -> detail::raw_c_string
Return a pointer to the NUL-terminated string held in *this.
Definition inline_string_reference.hpp:663
static ARENE_NODISCARD constexpr auto three_way_compare(const_inline_string_reference const lhs, inline_string_reference const rhs) noexcept -> strong_ordering
Compare two strings for lexicographical ordering, forwarded to string_view's.
Definition inline_string_reference.hpp:733
constexpr auto end() const noexcept -> const_iterator
The one-past-the-last position in the string.
Definition inline_string_reference.hpp:713
constexpr const_inline_string_reference(inline_string_reference const &ref) noexcept
Construct a const reference out of a non-const one.
Definition inline_string_reference.hpp:656
constexpr auto to_string_view() const noexcept -> string_view
Convert to string view.
Definition inline_string_reference.hpp:717
constexpr auto empty() const noexcept -> bool
Check if the string is empty.
Definition inline_string_reference.hpp:682
constexpr auto size() const noexcept -> size_type
Get the length of the string.
Definition inline_string_reference.hpp:674
static ARENE_NODISCARD constexpr auto three_way_compare(const_inline_string_reference const lhs, const_inline_string_reference const rhs) noexcept -> strong_ordering
Compare two strings for lexicographical ordering, forwarded to string_view's.
Definition inline_string_reference.hpp:723
static ARENE_NODISCARD constexpr auto three_way_compare(const_inline_string_reference const lhs, string_view const rhs) noexcept -> strong_ordering
Compare two strings for lexicographical ordering, forwarded to string_view's.
Definition inline_string_reference.hpp:744
constexpr auto begin() const noexcept -> const_iterator
Return an iterator to the first position in the string.
Definition inline_string_reference.hpp:710
string_base::const_iterator const_iterator
The type of a const iterator at the base class.
Definition inline_string_reference.hpp:642
constexpr const_inline_string_reference(inline_string< MaxSize > const &str) noexcept
Construct a const reference to the supplied inline_string.
Definition inline_string_reference.hpp:649
constexpr auto capacity() const noexcept -> size_type
The maximum length of the string.
Definition inline_string_reference.hpp:678
ARENE_NODISCARD constexpr auto data() const noexcept -> detail::raw_c_string
Return a pointer to the NUL-terminated string held in *this.
Definition inline_string_reference.hpp:668
ARENE_NODISCARD constexpr auto operator[](size_type const index) const noexcept -> detail::character const &
Retrieve the index -th character in the string.
Definition inline_string_reference.hpp:687
inline_string_detail::inline_string_base string_base
The type of the base class.
Definition inline_string_reference.hpp:636
A reference class to an inline_string object, with size-erased type.
Definition inline_string_reference.hpp:441
constexpr auto append(size_type const count, detail::character const chr) const noexcept -> inline_string_reference const &
Append the specified number of copies of a character to the string.
Definition inline_string_reference.hpp:546
constexpr inline_string_reference(inline_string< MaxSize > &str) noexcept
Construct a reference to the supplied inline_string.
Definition inline_string_reference.hpp:464
constexpr auto operator+=(detail::character const rhs) const noexcept -> inline_string_reference const &
Append a character to the current string.
Definition inline_string_reference.hpp:561
constexpr auto push_back(string_view const rhs) const noexcept -> inline_string_reference const &
Append a string to the current string.
Definition inline_string_reference.hpp:568
ARENE_NODISCARD constexpr auto c_str() const noexcept -> detail::raw_c_string
Return a pointer to the NUL-terminated string held in *this.
Definition inline_string_reference.hpp:471
constexpr auto operator+=(string_view const rhs) const noexcept -> inline_string_reference const &
Append a string to the current string.
Definition inline_string_reference.hpp:555
constexpr auto push_back(detail::character const rhs) const noexcept -> inline_string_reference const &
Append a character to the current string.
Definition inline_string_reference.hpp:574
constexpr auto at(size_type const index) const -> detail::character &
Retrieve the index -th character in the string.
Definition inline_string_reference.hpp:505
inline_string_detail::inline_string_base string_base
The type of the base class.
Definition inline_string_reference.hpp:449
constexpr auto begin() const noexcept -> iterator
Return an iterator to the first position in the string.
Definition inline_string_reference.hpp:518
constexpr auto erase(const_iterator const first, const_iterator const last) const noexcept -> iterator
Erase the characters in the specified range.
Definition inline_string_reference.hpp:590
constexpr auto erase(size_type const pos, size_type const count) const noexcept -> inline_string_reference const &
Erase the specified number of characters in the string starting at the specified position.
Definition inline_string_reference.hpp:582
constexpr auto const_operations_ptr(inline_string_reference_detail::ref_passkey) const noexcept -> inline_string_reference_detail::const_operations const *
Get a pointer to the struct holding const operation implementations.
Definition inline_string_reference.hpp:620
constexpr auto append(string_view const rhs) const noexcept -> inline_string_reference const &
Append a string to the current string.
Definition inline_string_reference.hpp:538
constexpr auto empty() const noexcept -> bool
Check if the string is empty.
Definition inline_string_reference.hpp:490
constexpr auto size() const noexcept -> size_type
Get the length of the string.
Definition inline_string_reference.hpp:482
constexpr auto capacity() const noexcept -> size_type
The maximum length of the string.
Definition inline_string_reference.hpp:486
constexpr auto to_string_view() const noexcept -> string_view
Convert to string view.
Definition inline_string_reference.hpp:525
constexpr auto end() const noexcept -> iterator
The one-past-the-last position in the string.
Definition inline_string_reference.hpp:521
string_base::size_type size_type
The type of a size at the base class.
Definition inline_string_reference.hpp:453
string_base::iterator iterator
The type of an iterator at the base class.
Definition inline_string_reference.hpp:455
static ARENE_NODISCARD constexpr auto three_way_compare(inline_string_reference const lhs, string_view const rhs) noexcept -> strong_ordering
Compare two strings for lexicographical ordering, forwarded to string_view's.
Definition inline_string_reference.hpp:607
constexpr auto assign(string_view const rhs) const noexcept -> inline_string_reference const &
Assign from a string_view.
Definition inline_string_reference.hpp:530
ARENE_NODISCARD constexpr auto operator[](size_type const index) const noexcept -> detail::character &
Retrieve the index -th character in the string.
Definition inline_string_reference.hpp:495
ARENE_NODISCARD constexpr auto data() const noexcept -> detail::raw_c_string
Return a pointer to the NUL-terminated string held in *this.
Definition inline_string_reference.hpp:476
constexpr auto front() const noexcept -> detail::character &
Get the first character in the string.
Definition inline_string_reference.hpp:511
constexpr auto back() const noexcept -> detail::character &
Get the last character in the string.
Definition inline_string_reference.hpp:514
string_base::const_iterator const_iterator
The type of a const iterator at the base class.
Definition inline_string_reference.hpp:457
static ARENE_NODISCARD constexpr auto three_way_compare(inline_string_reference const lhs, inline_string_reference const rhs) noexcept -> strong_ordering
Compare two strings for lexicographical ordering, forwarded to string_view's.
Definition inline_string_reference.hpp:599
constexpr auto get_string_ptr(inline_string_reference_detail::ref_passkey) const noexcept -> inline_string_detail::inline_string_base *
Get a pointer to the underlying string object pointer.
Definition inline_string_reference.hpp:613
A string type with a fixed maximum capacity, where the data is stored internally without allocations.
Definition inline_string.hpp:162
Backport of the C++17 std::string_view class.
Definition string_view.hpp:54
static constexpr auto three_way_compare(string_view const lhs, string_view const rhs) noexcept -> strong_ordering
Compare two string_view objects lexicographically.
Definition string_view.hpp:874
#define ARENE_INVARIANT_UNREACHABLE(message)
Terminate the application if this line is ever executed. The supplied message will be output in that ...
Definition contract.hpp:170
Definition accumulate.hpp:37
strong_ordering_detail::strong_ordering strong_ordering
The enumeration specifying the result of an ordering comparison.
Definition strong_ordering.hpp:147
typename constraints_impl::constraints< EnableIfs... >::type constraints
Helper used to express template constraints in a consistent way using std::enable_if_t.
Definition constraints.hpp:51
Definition accumulate.hpp:36
bool
Check if element Idx and all subsequent elements of the two tuples are equal.
Definition tuple.hpp:649
::size_t size_t
A type suitable for holding the size of an object.
Definition cstddef.hpp:29
static constexpr auto const & ignore
An object to which anything can be assigned, which ignores the assigned value.
Definition ignore.hpp:72
#define ARENE_NODISCARD
An alias for [[nodiscard]] , or a no-op if the attribute is not supported.
Definition nodiscard.hpp:26
#define ARENE_NORETURN
An alias for [[noreturn]] , or a no-op if the attribute is not supported.
Definition noreturn.hpp:24