Cogs.Core
Public Member Functions | Private Member Functions | Private Attributes | List of all members
Cogs::ImmutableString Class Reference

Represents a non-copyable immutable string. More...

#include <ImmutableString.h>

Public Member Functions

constexpr ImmutableString () noexcept=default
 Create an empty string.
 
 ImmutableString (const ImmutableString &)=delete
 Non-copyable.
 
ImmutableStringoperator= (const ImmutableString &)=delete
 Non-copyable.
 
constexpr ImmutableString (ImmutableString &&other) noexcept
 Take ownership of another string.
 
constexpr ImmutableStringoperator= (ImmutableString &&other) noexcept
 Take ownership of another string.
 
COGSFOUNDATION_API ImmutableString (const Cogs::StringView &view)
 Create a new unique immutable string from a string view.
 
 ~ImmutableString ()
 Destructor.
 
constexpr operator const char * () const noexcept
 View the string as C-string.
 
constexpr operator Cogs::StringView () const noexcept
 View the string as a string view.
 
constexpr void swap (ImmutableString &other) noexcept
 Swap contents with another string.
 
constexpr Cogs::StringView asView () const noexcept
 Explicitly view the string as a string view.
 
constexpr bool empty () const noexcept
 Check if string is empty.
 
void clear ()
 Set contents to the empty string.
 
constexpr const char * c_str () const noexcept
 Get the string as C-string.
 
constexpr size_t size () const noexcept
 Get the string length.
 

Private Member Functions

COGSFOUNDATION_API void release ()
 

Private Attributes

char * ptr = nullptr
 

Detailed Description

Represents a non-copyable immutable string.

Essentially a heap-allocated string with unique_ptr-semantics.

Optimized for representing empty strings, in that case the object has the memory footprint of a single pointer.

Note
: Create these objects sparingly as creating this object wih any non-empty string will might incur a memory allocation. If you create such an object every frame you are using this class incorrectly.

Definition at line 21 of file ImmutableString.h.

Constructor & Destructor Documentation

◆ ImmutableString() [1/2]

constexpr Cogs::ImmutableString::ImmutableString ( ImmutableString &&  other)
inlineconstexprnoexcept

Take ownership of another string.

Definition at line 35 of file ImmutableString.h.

References swap().

◆ ImmutableString() [2/2]

Cogs::ImmutableString::ImmutableString ( const Cogs::StringView view)

Create a new unique immutable string from a string view.

Definition at line 37 of file ImmutableString.cpp.

References Cogs::StringView::data(), Cogs::StringView::empty(), and Cogs::StringView::size().

◆ ~ImmutableString()

Cogs::ImmutableString::~ImmutableString ( )
inline

Destructor.

Definition at line 44 of file ImmutableString.h.

References clear().

Member Function Documentation

◆ asView()

constexpr Cogs::StringView Cogs::ImmutableString::asView ( ) const
inlineconstexprnoexcept

Explicitly view the string as a string view.

Definition at line 56 of file ImmutableString.h.

Referenced by operator Cogs::StringView().

◆ c_str()

constexpr const char * Cogs::ImmutableString::c_str ( ) const
inlineconstexprnoexcept

Get the string as C-string.

Definition at line 65 of file ImmutableString.h.

Referenced by operator const char *().

◆ clear()

void Cogs::ImmutableString::clear ( )
inline

Set contents to the empty string.

Definition at line 62 of file ImmutableString.h.

Referenced by ~ImmutableString().

◆ empty()

constexpr bool Cogs::ImmutableString::empty ( ) const
inlineconstexprnoexcept

Check if string is empty.

Definition at line 59 of file ImmutableString.h.

◆ operator Cogs::StringView()

constexpr Cogs::ImmutableString::operator Cogs::StringView ( ) const
inlineconstexprnoexcept

View the string as a string view.

Definition at line 50 of file ImmutableString.h.

References asView().

◆ operator const char *()

constexpr Cogs::ImmutableString::operator const char * ( ) const
inlineconstexprnoexcept

View the string as C-string.

Definition at line 47 of file ImmutableString.h.

References c_str().

◆ operator=()

constexpr ImmutableString & Cogs::ImmutableString::operator= ( ImmutableString &&  other)
inlineconstexprnoexcept

Take ownership of another string.

Definition at line 38 of file ImmutableString.h.

References swap().

◆ release()

void Cogs::ImmutableString::release ( )
private

Definition at line 62 of file ImmutableString.cpp.

◆ size()

constexpr size_t Cogs::ImmutableString::size ( ) const
inlineconstexprnoexcept

Get the string length.

Definition at line 68 of file ImmutableString.h.

References size().

Referenced by size().

◆ swap()

constexpr void Cogs::ImmutableString::swap ( ImmutableString other)
inlineconstexprnoexcept

Swap contents with another string.

Definition at line 53 of file ImmutableString.h.

Referenced by ImmutableString(), and operator=().

Member Data Documentation

◆ ptr

char* Cogs::ImmutableString::ptr = nullptr
private

Definition at line 74 of file ImmutableString.h.


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