@ -14,11 +14,11 @@
# include "string_func.h"
# include "string_func.h"
# include "core/span_type.hpp"
# include "core/span_type.hpp"
# include <variant>
/** The data required to format and validate a single parameter of a string. */
/** The data required to format and validate a single parameter of a string. */
struct StringParameter {
struct StringParameter {
uint64_t data ; ///< The data of the parameter.
std : : variant < uint64_t , const char * , std : : unique_ptr < std : : string > > data ; ///< The data of the parameter: integer, non-owning string value, or owned string value.
const char * string_view ; ///< The string value, if it has any.
std : : unique_ptr < std : : string > string ; ///< Copied string value, if it has any.
char32_t type ; ///< The #StringControlCode to interpret this data with when it's the first parameter, otherwise '\0'.
char32_t type ; ///< The #StringControlCode to interpret this data with when it's the first parameter, otherwise '\0'.
} ;
} ;
@ -95,7 +95,12 @@ public:
T GetNextParameter ( )
T GetNextParameter ( )
{
{
auto ptr = GetNextParameterPointer ( ) ;
auto ptr = GetNextParameterPointer ( ) ;
return static_cast < T > ( ptr - > data ) ;
if ( std : : holds_alternative < uint64_t > ( ptr - > data ) ) {
return static_cast < T > ( std : : get < uint64_t > ( ptr - > data ) ) ;
} else {
throw std : : out_of_range ( " Trying to read string parameter as integer " ) ;
}
}
}
/**
/**
@ -107,7 +112,14 @@ public:
const char * GetNextParameterString ( )
const char * GetNextParameterString ( )
{
{
auto ptr = GetNextParameterPointer ( ) ;
auto ptr = GetNextParameterPointer ( ) ;
return ptr - > string ! = nullptr ? ptr - > string - > c_str ( ) : ptr - > string_view ;
if ( std : : holds_alternative < const char * > ( ptr - > data ) ) {
return std : : get < const char * > ( ptr - > data ) ;
} else if ( std : : holds_alternative < std : : unique_ptr < std : : string > > ( ptr - > data ) ) {
return std : : get < std : : unique_ptr < std : : string > > ( ptr - > data ) - > c_str ( ) ;
} else {
throw std : : out_of_range ( " Trying to read string parameter as integer " ) ;
}
}
}
/**
/**
@ -152,8 +164,6 @@ public:
{
{
assert ( n < this - > parameters . size ( ) ) ;
assert ( n < this - > parameters . size ( ) ) ;
this - > parameters [ n ] . data = v ;
this - > parameters [ n ] . data = v ;
this - > parameters [ n ] . string . reset ( ) ;
this - > parameters [ n ] . string_view = nullptr ;
}
}
template < typename T , std : : enable_if_t < std : : is_base_of < StrongTypedefBase , T > : : value , int > = 0 >
template < typename T , std : : enable_if_t < std : : is_base_of < StrongTypedefBase , T > : : value , int > = 0 >
@ -165,9 +175,7 @@ public:
void SetParam ( size_t n , const char * str )
void SetParam ( size_t n , const char * str )
{
{
assert ( n < this - > parameters . size ( ) ) ;
assert ( n < this - > parameters . size ( ) ) ;
this - > parameters [ n ] . data = 0 ;
this - > parameters [ n ] . data = str ;
this - > parameters [ n ] . string . reset ( ) ;
this - > parameters [ n ] . string_view = str ;
}
}
void SetParam ( size_t n , const std : : string & str ) { this - > SetParam ( n , str . c_str ( ) ) ; }
void SetParam ( size_t n , const std : : string & str ) { this - > SetParam ( n , str . c_str ( ) ) ; }
@ -175,16 +183,14 @@ public:
void SetParam ( size_t n , std : : string & & str )
void SetParam ( size_t n , std : : string & & str )
{
{
assert ( n < this - > parameters . size ( ) ) ;
assert ( n < this - > parameters . size ( ) ) ;
this - > parameters [ n ] . data = 0 ;
this - > parameters [ n ] . data = std : : make_unique < std : : string > ( std : : move ( str ) ) ;
this - > parameters [ n ] . string = std : : make_unique < std : : string > ( std : : move ( str ) ) ;
this - > parameters [ n ] . string_view = nullptr ;
}
}
uint64_t GetParam ( size_t n ) const
uint64_t GetParam ( size_t n ) const
{
{
assert ( n < this - > parameters . size ( ) ) ;
assert ( n < this - > parameters . size ( ) ) ;
assert ( this- > parameters [ n ] . string_view = = nullptr & & this - > parameters [ n ] . string = = nullptr ) ;
assert ( std: : holds_alternative < uint64_t > ( this - > parameters [ n ] . data ) ) ;
return this- > parameters [ n ] . data ;
return std: : get < uint64_t > ( this- > parameters [ n ] . data ) ;
}
}
/**
/**
@ -195,8 +201,14 @@ public:
const char * GetParamStr ( size_t n ) const
const char * GetParamStr ( size_t n ) const
{
{
assert ( n < this - > parameters . size ( ) ) ;
assert ( n < this - > parameters . size ( ) ) ;
auto & param = this - > parameters [ n ] ;
return param . string ! = nullptr ? param . string - > c_str ( ) : param . string_view ;
if ( std : : holds_alternative < const char * > ( this - > parameters [ n ] . data ) ) {
return std : : get < const char * > ( this - > parameters [ n ] . data ) ;
} else if ( std : : holds_alternative < std : : unique_ptr < std : : string > > ( this - > parameters [ n ] . data ) ) {
return std : : get < std : : unique_ptr < std : : string > > ( this - > parameters [ n ] . data ) - > c_str ( ) ;
} else {
return nullptr ;
}
}
}
} ;
} ;