FSMgine
High-performance finite state machine library for C++17 with single-threaded and multi-threaded variants
Loading...
Searching...
No Matches
fsmgine::StringInterner Class Reference

Provides memory-efficient string storage through string interning. More...

#include <StringInterner.hpp>

Public Member Functions

std::string_view intern (const std::string &str)
 Interns a string and returns a persistent string_view.
 
std::string_view intern (std::string_view sv)
 Interns a string_view and returns a persistent string_view.
 
void clear ()
 Clears all interned strings (TEST ONLY - DO NOT USE IN PRODUCTION)
 

Static Public Member Functions

static StringInternerinstance ()
 Gets the singleton instance of StringInterner.
 

Detailed Description

Provides memory-efficient string storage through string interning.

StringInterner implements the string interning pattern to optimize string storage and comparison in the FSM. By interning strings, we achieve:

  • Fast pointer-based string comparisons (O(1) instead of O(n))
  • Reduced memory usage when the same state names are used multiple times
  • Guaranteed string_view safety throughout the FSM lifetime
  • Thread-safe operations when using the FSMgineMT library variant
Note
This is a singleton class - use StringInterner::instance() to access
Thread Safety
Thread-safety depends on which library variant you're using:
  • FSMgine: No thread synchronization, must be used from a single thread
  • FSMgineMT: All operations are protected by mutexes for thread-safe access
Warning
The clear() method is NOT thread-safe in either variant and should only be used in single-threaded test scenarios.

Member Function Documentation

◆ clear()

void fsmgine::StringInterner::clear ( )

Clears all interned strings (TEST ONLY - DO NOT USE IN PRODUCTION)

Warning
This method is for testing purposes only and is NOT thread-safe
After calling clear(), all previously returned string_views become invalid
Using this in production code will cause undefined behavior
Note
This method exists solely to reset state between tests

◆ instance()

static StringInterner & fsmgine::StringInterner::instance ( )
static

Gets the singleton instance of StringInterner.

Returns
Reference to the global StringInterner instance

◆ intern() [1/2]

std::string_view fsmgine::StringInterner::intern ( const std::string &  str)

Interns a string and returns a persistent string_view.

Parameters
strThe string to intern
Returns
A string_view that remains valid for the lifetime of the StringInterner
Note
The returned string_view points to the interned string stored internally

◆ intern() [2/2]

std::string_view fsmgine::StringInterner::intern ( std::string_view  sv)

Interns a string_view and returns a persistent string_view.

Parameters
svThe string_view to intern
Returns
A string_view that remains valid for the lifetime of the StringInterner
Note
The input string_view's data is copied and stored internally

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