gz-cpp-util 1.3
A c++20 library containing various utilities
|
This library provides utility for string conversion, from and to std::string
. The string utility is split into 3 files:
There also three important concepts, ConvertibleToString, ConstructibleFromString and StringConvertible which are used in the logger and the settings manager to log and store types.
If you want to use your own types with one of these, you can easily write your own string conversion function for your custom types.
You can use the gz::toString()
function to turn certain types into strings. Concepts are used to determine the correct overload of the function.
The concept ConvertibleToString is satisfied for types that can be converted to string using gz::toString()
.
The toString() function is implemented for these types (non-exhaustive list of examples in braces)
You can use the gz::fromString()
function to create certain types from a string.
The concept ConstructibleFromString is satisfied for types that can be constructed from a string with gz::fromString()
.
When constructing something from a string, there is of course the problem that errors occur at runtime when the string is unsuitable for construction. In this case, the fromString functions from this library throw an exception (either InvalidArgument, or std::invalid_argument / std::out_of_range).
If you want your custom type to be convertible to string, you have different options. You can either add an toString() const
member to your class or overload std::string toString(const T&)
in global or gz namespace. Example for a class called Custom
:
Writing an overload for fromString needs a little bit more boiler plate. Since fromString is a templated function, you need to declare it as such. The function declaration in this library uses a concept so that it is only declared for the types that are actully implemented. Example for a class called Custom
:
You should write your function so that the output of one can be used as input of the other. In other words:
SettingsManager expects the strings from toString() to be a single line. If it has multiple lines, it will not load it correctly when loading from a file.
To satisfy the concept(s) ConvertibleToString/ConstructibleFromString/StringConvertible, any overload must be visible to the compiler when the concept is processed, so you have to declare it before including string/to_string.hpp
/string/from_string.hpp
(which may also be included by string/conversion.hpp
, settings_manager.hpp
and log.hpp
). To ease troubleshooting the include order, string/to_string.hpp
/string_from_string.hpp
each define the macro GZ_UTIL_STRING_CONCEPTS
, so you can place an assertion like this before declaring your overloads:
Separate the declaration from the definition:
Define the macro GZ_TO_STRING_NO_VECTORS
before including <gz-util/string/to_string.hpp>
. This disables all overloads for Vector and Extent classes.
The functions toHexString(), toOctString() and toBinString() can be used to get a string of an integers representation in 16 / 8 / 2 basis.
The functions fromHexString<T>(), fromOctString<T>() and fromBinString<T>() can be used to get an integer from a string representation of an integer in 16 / 8 / 2 basis. These function can throw std::invalid_argument and std::out_of_range.
(unsigned) char
((u)int8_t
).