ToyGine2 26.2.0
Game Engine for retro consoles
Loading...
Searching...
No Matches
toy::StringFixedStorage< AllocatedSize > Class Template Reference

Fixed-size character storage with a tracked length and implicit null terminator at data()[size()]. More...

Public Member Functions

constexpr size_t size () const noexcept
 Returns the current logical string length in bytes (not including the terminator).
constexpr char * data () noexcept
 Mutable pointer to the inline character buffer.
constexpr const char * data () const noexcept
 Const pointer to the same buffer as data().
constexpr void setSize (size_t newSize) noexcept
 Updates the stored length and writes a null terminator at data()[newSize].

Static Public Member Functions

static constexpr size_t capacity () noexcept
 Returns the maximum number of characters that fit before the mandatory null terminator.

Private Attributes

char _buffer [AllocatedSize] = {'\0'}
 Inline storage: characters live in [0, size()).
size_t _size {0}
 Logical length in bytes (not including the terminator).

Detailed Description

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
AllocatedSizeTotal 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

#include "core.hpp"
storage.data()[0] = 'h';
storage.data()[1] = 'i';
storage.setSize(2);
// storage.data() is "hi" (null-terminated)
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

Member Function Documentation

◆ capacity()

template<size_t AllocatedSize>
size_t toy::StringFixedStorage< AllocatedSize >::capacity ( )
staticnodiscardconstexprnoexcept

Returns the maximum number of characters that fit before the mandatory null terminator.

Evaluated entirely at compile time from AllocatedSize. The buffer holds AllocatedSize bytes; one byte is reserved for '\0' at index capacity(), so the usable span for character data is [0, capacity()).

Returns
AllocatedSize - 1.
Note
static constexpr; same for all instances of toy::StringFixedStorage with this AllocatedSize.

◆ data() [1/2]

template<size_t AllocatedSize>
const char * toy::StringFixedStorage< AllocatedSize >::data ( ) const
nodiscardconstexprnoexcept

Const pointer to the same buffer as data().

Returns
Pointer to the first character; after a valid setSize(n), safe to read n characters plus the following '\0' (i.e. [0, n] inclusive for the terminator).
Note
Does not synchronize with concurrent writers; use on const objects or when the string is not being modified.

◆ data() [2/2]

template<size_t AllocatedSize>
char * toy::StringFixedStorage< AllocatedSize >::data ( )
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.

◆ setSize()

template<size_t AllocatedSize>
void toy::StringFixedStorage< AllocatedSize >::setSize ( size_t newSize)
constexprnoexcept

Updates the stored length and writes a null terminator at data()[newSize].

Sets the tracked length to newSize and assigns '\0' to data()[newSize]. Does not clear or copy the range [0, newSize); callers must have written valid content there if newSize > 0.

Parameters
newSizeNew character count. Must be strictly less than AllocatedSize so the terminator fits in the buffer.
Precondition
newSize < AllocatedSize (checked with assert_message in debug builds).
Postcondition
size() equals newSize and data()[newSize] is '\0'.
Note
constexpr; invalid newSize is a debug assertion failure, not a throwing error.

◆ size()

template<size_t AllocatedSize>
size_t toy::StringFixedStorage< AllocatedSize >::size ( ) const
nodiscardconstexprnoexcept

Returns the current logical string length in bytes (not including the terminator).

The value is the newSize last passed to setSize(), or 0 after default construction. It does not inspect buffer contents.

Returns
Number of characters in [0, size()) before data()[size()] (the written '\0' after setSize).
Note
Runtime state; constexpr where the object is usable in constant evaluation.

Member Data Documentation

◆ _buffer

template<size_t AllocatedSize>
char toy::StringFixedStorage< AllocatedSize >::_buffer[AllocatedSize] = {'\0'}
private

Inline storage: characters live in [0, size()).

◆ _size

template<size_t AllocatedSize>
size_t toy::StringFixedStorage< AllocatedSize >::_size {0}
private

Logical length in bytes (not including the terminator).


The documentation for this class was generated from the following files: