Converter Structs & Specialisations
The converter function is template programming sorcery. This is how it was achieved.
Converter Backend
-
template<typename T>
struct Converter The backend structure for the convert function.
Required because functions are not able to undergo partial specialisation, whilst classes can. This therefore amounts to some compile-time wizardry.
Public Static Functions
-
static inline T internalConvert(std::string_view sv)
The default conversion operator — where the actual conversion happens.
Specialisations (i.e. for new convert-types) must define their own version of this function
- Parameters:
sv – A string_view (implicitly converted from std::strings) representing a value to be converted
- Returns:
A value of type T corresponding to the input string
Private Static Functions
-
static inline void CheckErrors(std::from_chars_result &result, std::string_view sv)
Throws errors if the internalConvert reached some undesirable state
- Throws:
logic_error – If the characters could not be converted into numerics (i.e. ab)
logic_error – If some, but not all, characters could be converted into numerics (i.e. 123ab)
logic_error – If the result is out of range (i.e. convert<int>(INT_MAX + 10))
-
static inline void RejectEmpty(std::string_view sv)
Check if converted string is empty.
- Throws:
logic_error – If target string is empty
-
static inline T internalConvert(std::string_view sv)
Template Specialisations
-
template<>
struct Converter<std::string> Full specialization for std::string (because from_chars can’t do strings!)
Could just rely on user to call this manually, but it’s nicer to have a unified interface
Public Static Functions
-
static inline std::string internalConvert(std::string_view sv)
Performs a basic string-initialisation.
-
static inline std::string internalConvert(std::string_view sv)
-
template<>
struct Converter<bool> Full specialization for boolean (because from_chars can’t do boolean)
Public Static Functions
-
static inline bool internalConvert(std::string_view sv)
Checks if the input string is 1,0, true or false (case insensitive), and converts to the relevant bool
- Throws:
runtime_error – If the string is not in [“1”,”0”, “true” ,”false”]
-
static inline bool internalConvert(std::string_view sv)
-
template<>
struct Converter<char> Full specialisation for single chars
Converting between strings and chars is notoriously more difficult than it feels it should be
Public Static Functions
-
static inline char internalConvert(std::string_view sv)
Converts a string into a char
- Throws:
logic_error – if the string is not a single character long
-
static inline char internalConvert(std::string_view sv)
-
template<>
struct Converter<double> Specialization for double, activated on Apple-Clang compilers
For reasons unknown to me, Apple-Clang does not fully implement the C++ standard. std::from_chars does not work on non-integral types. This is really, really annoying that this is necessary. Must therefore resort to using the (slower, worse) std::stod for Apple people (which includes me!)
Public Static Functions
-
static inline double internalConvert(std::string_view sv)
Performs a basic std::stod check, and then checks the validity of the argument. Has its own nested version of Converter<T>::RejectEmpty and Converter<T>::CheckErrors.
-
static inline double internalConvert(std::string_view sv)