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

Wrapper around a toy::CStringView pattern with consteval validation of placeholders. More...

Collaboration diagram for toy::FormatString< Args >:

Public Member Functions

consteval explicit (false) FormatString(const CStringView &string) noexcept
 Validates string at compile time and stores it as a toy::CStringView.
 FormatString (const FormatString &other) noexcept=default
 Default copy constructor.
FormatStringoperator= (const FormatString &)=delete
 Copy assignment is deleted.
constexpr CStringView get () const noexcept
 Returns the stored format pattern as a toy::CStringView.

Static Private Member Functions

static void _compileTimeError (const char *message) noexcept
 Stub used when validation fails; the program should be ill-formed before a call is needed.

Private Attributes

const CStringView _string
 The stored format string.

Detailed Description

template<class... Args>
class toy::FormatString< Args >

Wrapper around a toy::CStringView pattern with consteval validation of placeholders.

Two modes are supported, not mixed in one pattern: auto-indexed placeholders are exactly {}; positional placeholders are {0}, {1}, … (decimal digits only). {{ and }} are literal braces. In auto mode the number of {} must equal sizeof...(Args). In positional mode each index must be < sizeof...(Args); repeating an index is allowed and not every index need appear. Unmatched { or } makes construction ill-formed.

Template Parameters
ArgsTemplate parameter pack representing the types of arguments that will be formatted.

Key Features

  • Compile-time checks: consteval constructor; invalid patterns fail at compile time.
  • Auto and positional modes: {} vs {N}; mixing both in one string is ill-formed.
  • Storage: Holds a toy::CStringView to the pattern; no allocation.
  • get(): Returns the same view; constexpr noexcept.

Usage Example

#include "core.hpp"
constexpr toy::FormatString<int, float> autoFmt(toy::CStringView("Value: {}, Float: {}"));
constexpr toy::FormatString<int, float> posFmt(toy::CStringView("Float: {1}, Value: {0}"));
// Ill-formed examples (do not compile):
// toy::FormatString<int, int>(toy::CStringView("{} {0}")); // mixed auto and positional
// toy::FormatString<int>(toy::CStringView("{")); // unmatched brace
Non-owning string view class for C-style strings.
Definition c_string_view.hpp:109
Wrapper around a toy::CStringView pattern with consteval validation of placeholders.
Definition format_string.hpp:87
Umbrella header for the engine core module.

Performance Characteristics

  • Construction: Work is done at compile time (consteval).
  • get(): O(1); returns the stored toy::CStringView.

Safety Guarantees

  • Invalid patterns: Diagnosed at compile time when the consteval constructor runs.
  • Exception safety: All operations are noexcept; this type does not throw.

Compatibility

See also
CStringView

Constructor & Destructor Documentation

◆ FormatString()

template<class... Args>
toy::FormatString< Args >::FormatString ( const FormatString< Args > & other)
constevaldefaultnoexcept

Default copy constructor.

Parameters
otherFormatString to copy.

Member Function Documentation

◆ _compileTimeError()

template<class... Args>
void toy::FormatString< Args >::_compileTimeError ( const char * message)
inlinestaticprivatenoexcept

Stub used when validation fails; the program should be ill-formed before a call is needed.

Parameters
messageDiagnostic text for implementations that surface it.
Note
Intended only from the consteval constructor path.
See also
FormatString(const CStringView &)

◆ explicit()

template<class... Args>
toy::FormatString< Args >::explicit ( false ) const &
constevalnoexcept

Validates string at compile time and stores it as a toy::CStringView.

Parameters
stringFormat pattern to validate and store.
Precondition
Braces in string are balanced; {{ and }} are literals.
Either: auto mode — number of {} equals sizeof...(Args); or positional mode — each {N} has N < sizeof...(Args), and the pattern does not mix {} with {N}.
Postcondition
The pattern is stored and can be read with get().
Note
consteval; ill-formed patterns make the program ill-formed.
Escaped {{ and }} are not placeholders.
See also
get()

◆ get()

template<class... Args>
CStringView toy::FormatString< Args >::get ( ) const
nodiscardconstexprnoexcept

Returns the stored format pattern as a toy::CStringView.

Returns
The same view stored at construction time.
Note
constexpr; safe in constant evaluation.
See also
FormatString(const CStringView &)

◆ operator=()

template<class... Args>
FormatString & toy::FormatString< Args >::operator= ( const FormatString< Args > & )
delete

Copy assignment is deleted.

Member Data Documentation

◆ _string

template<class... Args>
const CStringView toy::FormatString< Args >::_string
private

The stored format string.


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