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_
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"
38namespace inline_string_reference_detail {
41struct const_operations {
44 using string_base = inline_string_detail::inline_string_base;
46 using string_ptr = string_base
const*;
48 using size_type = string_base::size_type;
50 using const_iterator = string_base::const_iterator;
55 using data_op_type = detail::raw_c_string (*)(string_ptr);
57 using size_op_type = size_type (*)(string_ptr);
59 using capacity_op_type = size_type (*)(string_ptr);
61 using empty_op_type =
bool (*)(string_ptr);
63 using element_op_type = detail::character
const& (*)(string_ptr, size_type);
65 using at_op_type = detail::character
const& (*)(string_ptr, size_type);
67 using front_op_type = detail::character
const& (*)(string_ptr);
69 using back_op_type = detail::character
const& (*)(string_ptr);
71 using begin_op_type = const_iterator (*)(string_ptr);
73 using end_op_type = const_iterator (*)(string_ptr);
75 using to_string_view_op_type = string_view (*)(string_ptr);
83 capacity_op_type capacity;
87 element_op_type element;
99 to_string_view_op_type to_string_view;
104template <std::
size_t MaxSize>
105class const_operations_impl {
108 using this_string = inline_string<MaxSize>;
112 using string_ptr = const_operations::string_ptr;
114 using size_type = const_operations::size_type;
116 using const_iterator = const_operations::const_iterator;
122 static constexpr auto string(string_ptr ptr)
noexcept -> this_string
const* {
123 return static_cast<inline_string<MaxSize> const*
>(ptr);
129 static constexpr auto do_data(string_ptr ptr)
noexcept -> detail::raw_c_string {
return string(ptr)->data(); }
134 static constexpr auto do_size(string_ptr ptr)
noexcept -> size_type {
return string(ptr)->size(); }
139 static constexpr auto do_capacity(string_ptr ptr)
noexcept -> size_type {
return string(ptr)->capacity(); }
144 static constexpr auto do_empty(string_ptr ptr)
noexcept ->
bool {
return string(ptr)->empty(); }
150 static constexpr auto do_element(string_ptr ptr, size_type index)
noexcept -> detail::character
const& {
151 return (*
string(ptr))[index];
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);
168 bool AreExceptionsEnabled = detail::are_exceptions_enabled::value,
177 static constexpr auto do_front(string_ptr ptr)
noexcept -> detail::character
const& {
return string(ptr)->front(); }
181 static constexpr auto do_back(string_ptr ptr)
noexcept -> detail::character
const& {
return string(ptr)->back(); }
186 static constexpr auto do_begin(string_ptr ptr)
noexcept -> const_iterator {
return string(ptr)->begin(); }
190 static constexpr auto do_end(string_ptr ptr)
noexcept -> const_iterator {
return string(ptr)->end(); }
195 static constexpr auto do_to_string_view(string_ptr ptr)
noexcept -> string_view {
196 return {do_data(ptr), do_size(ptr)};
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,
220 using string_base = inline_string_detail::inline_string_base;
222 using string_ptr = string_base*;
225 using size_type = string_base::size_type;
227 using iterator = string_base::iterator;
229 using const_iterator = string_base::const_iterator;
234 using element_op_type = detail::character& (*)(string_ptr, size_type);
236 using at_op_type = detail::character& (*)(string_ptr, size_type);
238 using front_op_type = detail::character& (*)(string_ptr);
240 using back_op_type = detail::character& (*)(string_ptr);
242 using begin_op_type = iterator (*)(string_ptr);
244 using end_op_type = iterator (*)(string_ptr);
246 using to_string_view_op_type = string_view (*)(string_ptr);
248 using assign_op_type = string_ptr (*)(string_ptr, string_view);
250 using append_op_type = string_ptr (*)(string_ptr, string_view);
252 using append_count_char_op_type = string_ptr (*)(string_ptr, size_type, detail::character);
254 using erase_op_type = string_ptr (*)(string_ptr, size_type, size_type);
256 using erase_it_op_type = iterator (*)(string_ptr, const_iterator, const_iterator);
260 const_operations
const* const_ops;
262 element_op_type element;
274 to_string_view_op_type to_string_view;
276 assign_op_type assign;
278 append_op_type append;
280 append_count_char_op_type append_count_char;
284 erase_it_op_type erase_it;
289template <std::
size_t MaxSize>
290class operations_impl {
293 using this_string = inline_string<MaxSize>;
295 using string_ptr = inline_string_detail::inline_string_base*;
299 using size_type = operations::size_type;
301 using iterator = operations::iterator;
303 using const_iterator = operations::const_iterator;
309 static constexpr auto string(string_ptr ptr)
noexcept -> this_string* {
310 return static_cast<inline_string<MaxSize>*
>(ptr);
317 static constexpr auto do_element(string_ptr ptr, size_type index)
noexcept -> detail::character& {
318 return (*
string(ptr))[index];
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);
335 bool AreExceptionsEnabled = detail::are_exceptions_enabled::value,
344 static constexpr auto do_front(string_ptr ptr)
noexcept -> detail::character& {
return string(ptr)->front(); }
348 static constexpr auto do_back(string_ptr ptr)
noexcept -> detail::character& {
return string(ptr)->back(); }
353 static constexpr auto do_begin(string_ptr ptr)
noexcept -> iterator {
return string(ptr)->begin(); }
357 static constexpr auto do_end(string_ptr ptr)
noexcept -> iterator {
return string(ptr)->end(); }
362 static constexpr auto do_to_string_view(string_ptr ptr)
noexcept -> string_view {
363 return {string(ptr)->data(), string(ptr)->size()};
370 static constexpr auto do_assign(string_ptr ptr, string_view rhs)
noexcept -> string_ptr {
371 return &string(ptr)->assign(rhs);
378 static constexpr auto do_append(string_ptr ptr, string_view rhs)
noexcept -> string_ptr {
379 return &string(ptr)->append(rhs);
386 static constexpr auto do_append_count_char(string_ptr ptr, size_type count, detail::character chr)
noexcept
388 return &string(ptr)->append(count, chr);
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);
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);
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,
431 explicit ref_passkey() =
default;
434 friend class ::arene::base::const_inline_string_reference;
443 inline_string_detail::inline_string_base* string_ptr_;
445 inline_string_reference_detail::operations
const* op_ptrs_;
463 template <std::
size_t MaxSize>
467 op_ptrs_(&inline_string_reference_detail::string_operations<MaxSize>) {}
472 return op_ptrs_->const_ops->data(string_ptr_);
477 return op_ptrs_->const_ops->data(string_ptr_);
482 constexpr auto size() const noexcept ->
size_type {
return op_ptrs_->const_ops->size(string_ptr_); }
486 constexpr auto capacity() const noexcept ->
size_type {
return op_ptrs_->const_ops->capacity(string_ptr_); }
490 constexpr auto empty() const noexcept ->
bool {
return op_ptrs_->const_ops->empty(string_ptr_); }
496 return op_ptrs_->element(string_ptr_, index);
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);
511 constexpr auto front() const noexcept -> detail::character& {
return op_ptrs_->front(string_ptr_); }
514 constexpr auto back() const noexcept -> detail::character& {
return op_ptrs_->back(string_ptr_); }
518 constexpr auto begin() const noexcept ->
iterator {
return op_ptrs_->begin(string_ptr_); }
521 constexpr auto end() const noexcept ->
iterator {
return op_ptrs_->end(string_ptr_); }
546 constexpr auto append(
size_type const count, detail::character
const chr)
const noexcept
548 std::ignore = op_ptrs_->append_count_char(string_ptr_, count, chr);
583 std::ignore = op_ptrs_->erase(string_ptr_, pos, count);
591 return op_ptrs_->erase_it(string_ptr_, first, last);
613 constexpr auto get_string_ptr(inline_string_reference_detail::ref_passkey)
const noexcept
614 -> inline_string_detail::inline_string_base* {
621 -> inline_string_reference_detail::const_operations
const* {
622 return op_ptrs_->const_ops;
630 inline_string_detail::inline_string_base
const* string_ptr_;
632 inline_string_reference_detail::const_operations
const* const_op_ptrs_;
648 template <std::
size_t MaxSize>
652 const_op_ptrs_(&inline_string_reference_detail::const_string_operations<MaxSize>) {}
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{})) {}
664 return const_op_ptrs_->data(string_ptr_);
669 return const_op_ptrs_->data(string_ptr_);
674 constexpr auto size() const noexcept ->
size_type {
return const_op_ptrs_->size(string_ptr_); }
678 constexpr auto capacity() const noexcept ->
size_type {
return const_op_ptrs_->capacity(string_ptr_); }
682 constexpr auto empty() const noexcept ->
bool {
return const_op_ptrs_->empty(string_ptr_); }
688 return const_op_ptrs_->element(string_ptr_, index);
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);
703 constexpr auto front() const noexcept -> detail::character const& {
return const_op_ptrs_->front(string_ptr_); }
706 constexpr auto back() const noexcept -> detail::character const& {
return const_op_ptrs_->back(string_ptr_); }
713 constexpr auto end() const noexcept ->
const_iterator {
return const_op_ptrs_->end(string_ptr_); }
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
constexpr generic_ordering_from_three_way_compare() noexcept=default
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