Fixed-size character storage with a tracked length and implicit null terminator at data()[size()].
More...
template<size_t AllocatedSize>
class toy::StringFixedStorage< AllocatedSize >
Fixed-size character storage with a tracked length and implicit null terminator at data()[size()].
Holds AllocatedSize bytes (including space for a terminating '\0' after the logical string). The usable character count is capacity() = AllocatedSize - 1. size() is the number of characters before that terminator; setSize() updates the length and writes '\0' at data()[size()]. Callers fill data()[0] … data()[size()-1] when size() > 0.
- Template Parameters
-
| AllocatedSize | Total byte count of the internal buffer. Must be > 0 (compile-time check). |
Key Features
- No heap: Storage is inline; suitable for embedded and deterministic code paths.
- Explicit length: size() and setSize() separate from buffer contents (caller maintains valid UTF-8 or other encoding as needed).
- Null-terminated: After setSize(n),
data()[n] is '\0' when n < AllocatedSize.
- Compile-time capacity:
AllocatedSize is a template constant validated with a compile-time assertion.
Usage Example
Fixed-size character storage with a tracked length and implicit null terminator at data()[size()].
Definition string_fixed_storage.hpp:82
constexpr void setSize(size_t newSize) noexcept
Updates the stored length and writes a null terminator at data()[newSize].
Definition string_fixed_storage.inl:53
constexpr char * data() noexcept
Mutable pointer to the inline character buffer.
Definition string_fixed_storage.inl:43
Umbrella header for the engine core module.
Performance Characteristics
capacity(), size(), and data() are O(1). setSize() is O(1). No allocations.
Safety Guarantees
- Preconditions: setSize(newSize) requires newSize
< AllocatedSize (asserted in debug) so the written terminator stays within the buffer.
- Exception safety: All operations are noexcept.
- Note
- This type does not validate encoding or copy characters; higher-level types (e.g. toy::FixedString) build on similar storage patterns.
- See also
- toy::FixedString
template<size_t AllocatedSize>
|
|
nodiscardconstexprnoexcept |
Mutable pointer to the inline character buffer.
Use this to fill the bytes that will form the logical string: write payload in [0, newSize), then call setSize(newSize) with the same newSize (with newSize <= capacity()). Do not write a null terminator in place of setSize(): setSize(newSize) sets data()[newSize] = '\0'.
Until setSize() runs after your writes, only bytes you have written are defined; default construction zero-initializes the whole buffer. After setSize(n), [0, n) is the string and data()[n] is '\0'.
- Returns
- Pointer to the first element of the storage array.
- Precondition
- Writing payload past index
capacity()-1, or calling setSize(newSize) with newSize > capacity(), is undefined behavior.
- Note
- Same address as the const overload on the same object.