SofaBuffers C++ 0.12.0
Pure-C++20 SofaBuffers โ€” API Documentation
Loading...
Searching...
No Matches
sofab::FixedString< N > Class Template Reference

Fixed-capacity, heap-free string of up to N characters. More...

#include <sofab.hpp>

Public Types

using value_type = char
 Character type (mirrors std::string).
 
using size_type = std::size_t
 Size type (mirrors std::string).
 

Public Member Functions

 FixedString () noexcept=default
 Construct an empty string.
 
 FixedString (const char *s) noexcept
 Construct from a NUL-terminated C string (clamped to N, see assign).
 
 FixedString (std::string_view sv) noexcept
 Construct from a string view (clamped to N, see assign).
 
 FixedString (const std::string &s) noexcept
 Construct from a std::string (the easy on-ramp; truncated to N).
 
FixedString & operator= (const char *s) noexcept
 Assign from a NUL-terminated C string (clamped to N, see assign).
 
FixedString & operator= (std::string_view sv) noexcept
 Assign from a string view (clamped to N, see assign).
 
FixedString & operator= (const std::string &s) noexcept
 Assign from a std::string (clamped to N, see assign).
 
FixedString & assign (std::string_view sv) noexcept
 Replace the contents with sv, clamped to N bytes.
 
void set_len (std::size_t n) noexcept
 Decode hook: set the logical length and (re)place the trailing NUL.
 
char * data () noexcept
 Mutable pointer to the character buffer (decode target).
 
const char * data () const noexcept
 Const pointer to the character buffer.
 
const char * c_str () const noexcept
 NUL-terminated view of the contents.
 
std::size_t size () const noexcept
 Number of characters currently stored.
 
std::size_t length () const noexcept
 Alias of size.
 
bool empty () const noexcept
 True if the string is empty.
 
char & operator[] (std::size_t i) noexcept
 Access the character at i (no bounds checking).
 
const char & operator[] (std::size_t i) const noexcept
 Access the character at i (no bounds checking).
 
char * begin () noexcept
 Iterator to the first character.
 
char * end () noexcept
 Iterator past the last character.
 
const char * begin () const noexcept
 Const iterator to the first character.
 
const char * end () const noexcept
 Const iterator past the last character.
 
void clear () noexcept
 Reset to an empty string.
 
std::string_view view () const noexcept
 Non-owning view over the current characters.
 
 operator std::string_view () const noexcept
 Implicit conversion to std::string_view.
 
std::string str () const
 Copy the contents into an owning std::string (allocates).
 
bool operator== (std::string_view rhs) const noexcept
 Equality against any string view-like operand.
 
bool operator!= (std::string_view rhs) const noexcept
 Inequality against any string view-like operand.
 

Static Public Member Functions

static constexpr std::size_t capacity () noexcept
 Maximum number of characters (the template parameter N).
 
static constexpr std::size_t max_size () noexcept
 Alias of capacity.
 

Detailed Description

template<std::size_t N>
class sofab::FixedString< N >

Fixed-capacity, heap-free string of up to N characters.

A drop-in, embedded-friendly stand-in for std::string on both the encode and decode paths. The characters live in an inline std::array, so an instance allocates nothing, never throws (an over-long value is clamped to N bytes at a UTF-8 character boundary, see assign), and compiles cleanly under -fno-exceptions / -fno-rtti. The buffer never moves, so an instance stays a valid decode destination across every IStreamImpl::feed chunk.

The storage is N+1 bytes: one extra slot always holds a trailing NUL so c_str and the std::string_view encode path remain valid even at full length N. The buffer is zero-initialised, so the NUL is present from construction and is re-placed by every length-changing operation.

Generator integration contract
Generated code is spelled exactly as for std::string, so this type only has to keep the surface the typed calls look for:
  • decode emits is.readString(s, maxlen); โ€” IStreamImpl::readString picks the heap-free branch off set_len / capacity, rejects an over-N payload as INVALID (ยง7.1) rather than truncating it, and fills data before fixing size;
  • encode emits os.write(id, s); โ€” the implicit operator std::string_view routes it through the existing string encode branch, byte-for-byte identical to the same-content std::string.
Template Parameters
NMaximum number of characters (excluding the reserved NUL slot).

Definition at line 799 of file sofab.hpp.

Member Typedef Documentation

◆ size_type

template<std::size_t N>
using sofab::FixedString< N >::size_type = std::size_t

Size type (mirrors std::string).

Definition at line 808 of file sofab.hpp.

◆ value_type

template<std::size_t N>
using sofab::FixedString< N >::value_type = char

Character type (mirrors std::string).

Definition at line 806 of file sofab.hpp.

Constructor & Destructor Documentation

◆ FixedString() [1/4]

template<std::size_t N>
sofab::FixedString< N >::FixedString ( )
defaultnoexcept

Construct an empty string.

◆ FixedString() [2/4]

template<std::size_t N>
sofab::FixedString< N >::FixedString ( const char *  s)
inlinenoexcept

Construct from a NUL-terminated C string (clamped to N, see assign).

Parameters
sSource string, or nullptr for an empty string.

Definition at line 817 of file sofab.hpp.

◆ FixedString() [3/4]

template<std::size_t N>
sofab::FixedString< N >::FixedString ( std::string_view  sv)
inlinenoexcept

Construct from a string view (clamped to N, see assign).

Parameters
svSource characters (may contain embedded NULs).

Definition at line 826 of file sofab.hpp.

◆ FixedString() [4/4]

template<std::size_t N>
sofab::FixedString< N >::FixedString ( const std::string &  s)
inlinenoexcept

Construct from a std::string (the easy on-ramp; truncated to N).

Parameters
sSource string.

Definition at line 835 of file sofab.hpp.

Member Function Documentation

◆ assign()

template<std::size_t N>
FixedString & sofab::FixedString< N >::assign ( std::string_view  sv)
inlinenoexcept

Replace the contents with sv, clamped to N bytes.

The assignment-time clamp is a documented contract of the static storage (the generator's allow_dynamic: false): a value longer than N is cut, not refused, before any encoder sees it. The cut never lands inside a UTF-8 character – it moves back to the start of the character that would not fit whole, so the kept prefix is valid UTF-8 whenever sv is.

Parameters
svSource characters (may contain embedded NULs).
Returns
Reference to *this.

Definition at line 874 of file sofab.hpp.

◆ begin() [1/2]

template<std::size_t N>
const char * sofab::FixedString< N >::begin ( ) const
inlinenoexcept

Const iterator to the first character.

Definition at line 931 of file sofab.hpp.

◆ begin() [2/2]

template<std::size_t N>
char * sofab::FixedString< N >::begin ( )
inlinenoexcept

Iterator to the first character.

Definition at line 927 of file sofab.hpp.

◆ c_str()

template<std::size_t N>
const char * sofab::FixedString< N >::c_str ( ) const
inlinenoexcept

NUL-terminated view of the contents.

Definition at line 908 of file sofab.hpp.

◆ capacity()

template<std::size_t N>
static constexpr std::size_t sofab::FixedString< N >::capacity ( )
inlinestaticconstexprnoexcept

Maximum number of characters (the template parameter N).

Definition at line 917 of file sofab.hpp.

◆ clear()

template<std::size_t N>
void sofab::FixedString< N >::clear ( )
inlinenoexcept

Reset to an empty string.

Definition at line 936 of file sofab.hpp.

◆ data() [1/2]

template<std::size_t N>
const char * sofab::FixedString< N >::data ( ) const
inlinenoexcept

Const pointer to the character buffer.

Definition at line 906 of file sofab.hpp.

◆ data() [2/2]

template<std::size_t N>
char * sofab::FixedString< N >::data ( )
inlinenoexcept

Mutable pointer to the character buffer (decode target).

Definition at line 904 of file sofab.hpp.

◆ empty()

template<std::size_t N>
bool sofab::FixedString< N >::empty ( ) const
inlinenoexcept

True if the string is empty.

Definition at line 915 of file sofab.hpp.

◆ end() [1/2]

template<std::size_t N>
const char * sofab::FixedString< N >::end ( ) const
inlinenoexcept

Const iterator past the last character.

Definition at line 933 of file sofab.hpp.

◆ end() [2/2]

template<std::size_t N>
char * sofab::FixedString< N >::end ( )
inlinenoexcept

Iterator past the last character.

Definition at line 929 of file sofab.hpp.

◆ length()

template<std::size_t N>
std::size_t sofab::FixedString< N >::length ( ) const
inlinenoexcept

Alias of size.

Definition at line 913 of file sofab.hpp.

◆ max_size()

template<std::size_t N>
static constexpr std::size_t sofab::FixedString< N >::max_size ( )
inlinestaticconstexprnoexcept

Alias of capacity.

Definition at line 919 of file sofab.hpp.

◆ operator std::string_view()

template<std::size_t N>
sofab::FixedString< N >::operator std::string_view ( ) const
inlinenoexcept

Implicit conversion to std::string_view.

Gives a cheap non-owning view and makes the existing string encode branch (OStreamImpl::write) match a FixedString automatically.

Definition at line 954 of file sofab.hpp.

◆ operator!=()

template<std::size_t N>
bool sofab::FixedString< N >::operator!= ( std::string_view  rhs) const
inlinenoexcept

Inequality against any string view-like operand.

Definition at line 972 of file sofab.hpp.

◆ operator=() [1/3]

template<std::size_t N>
FixedString & sofab::FixedString< N >::operator= ( const char *  s)
inlinenoexcept

Assign from a NUL-terminated C string (clamped to N, see assign).

Definition at line 841 of file sofab.hpp.

◆ operator=() [2/3]

template<std::size_t N>
FixedString & sofab::FixedString< N >::operator= ( const std::string &  s)
inlinenoexcept

Assign from a std::string (clamped to N, see assign).

Definition at line 855 of file sofab.hpp.

◆ operator=() [3/3]

template<std::size_t N>
FixedString & sofab::FixedString< N >::operator= ( std::string_view  sv)
inlinenoexcept

Assign from a string view (clamped to N, see assign).

Definition at line 848 of file sofab.hpp.

◆ operator==()

template<std::size_t N>
bool sofab::FixedString< N >::operator== ( std::string_view  rhs) const
inlinenoexcept

Equality against any string view-like operand.

Definition at line 966 of file sofab.hpp.

◆ operator[]() [1/2]

template<std::size_t N>
const char & sofab::FixedString< N >::operator[] ( std::size_t  i) const
inlinenoexcept

Access the character at i (no bounds checking).

Definition at line 924 of file sofab.hpp.

◆ operator[]() [2/2]

template<std::size_t N>
char & sofab::FixedString< N >::operator[] ( std::size_t  i)
inlinenoexcept

Access the character at i (no bounds checking).

Definition at line 922 of file sofab.hpp.

◆ set_len()

template<std::size_t N>
void sofab::FixedString< N >::set_len ( std::size_t  n)
inlinenoexcept

Decode hook: set the logical length and (re)place the trailing NUL.

Fixes size() to the decoded payload length (clamped to N) and writes the NUL at buf_[len_]. IStreamImpl::readString calls it after copying [0, size()) and never touches buf_[len_], so the NUL survives and c_str stays valid. Re-decoding a shorter value re-terminates here. Its presence is also what marks this type as a heap-free destination to the typed reads (requires { set_len(...) }).

Parameters
nRequested logical length (clamped to N).

Definition at line 897 of file sofab.hpp.

◆ size()

template<std::size_t N>
std::size_t sofab::FixedString< N >::size ( ) const
inlinenoexcept

Number of characters currently stored.

Definition at line 911 of file sofab.hpp.

◆ str()

template<std::size_t N>
std::string sofab::FixedString< N >::str ( ) const
inline

Copy the contents into an owning std::string (allocates).

Definition at line 960 of file sofab.hpp.

◆ view()

template<std::size_t N>
std::string_view sofab::FixedString< N >::view ( ) const
inlinenoexcept

Non-owning view over the current characters.

Definition at line 943 of file sofab.hpp.


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