Functions for printing custom classes.
More...
Functions for printing custom classes.
Since the repository supports any type, including custom classes, these functions for converting objects of custom classes must be defined by users. This is implemented using the specialization of a partial function. The library defines a template function ToString, which accepts an object of any type and returns an empty string. This is necessary in order for the code to work with any types, even if conversion to a string is not defined for them. However, if you want to add to your type the ability to convert to a string, then you must define the ToString function for your type.
For example, like this
template <>
{
if (data.size() == 0) return "{}";
std::string res = "{}";
for (const int& dataIt : data)
{
res += std::to_string(dataIt) + ", ";
}
res.pop_back();
res.pop_back();
res += "}";
return res;
}
std::string ToString(const T &data) noexcept
A template method for providing an interface converting any type to a string.
Definition ToString.h:75
This code defines the ToString function for a vector of integers.
By calling this function:
std::vector<int> vec = {1, 2, 3, 4, 5};
std::cout <<
ToString(vec) << std::endl;
The output will be as follows:
By default, the function is defined for the following types:
- int
- bool
- float
- std::string
◆ ToString() [1/14]
template<>
std::string mvlt::ToString |
( |
const bool & |
data | ) |
|
|
inlinenoexcept |
Specialization of the ToString method for the bool type.
- Template Parameters
-
- Parameters
-
[in] | data | the variable to be converted to a string |
- Returns
- string with true if data true, otherwise retutn string with false
◆ ToString() [2/14]
template<>
std::string mvlt::ToString |
( |
const char & |
data | ) |
|
|
inlinenoexcept |
Specialization of the ToString method for the float type.
- Template Parameters
-
- Parameters
-
[in] | data | the variable to be converted to a string |
- Returns
- string with one char
◆ ToString() [3/14]
Specialization of the ToString method for the double type.
- Template Parameters
-
- Parameters
-
[in] | data | the variable to be converted to a string |
- Returns
- result of std::to_string function
◆ ToString() [4/14]
Specialization of the ToString method for the float type.
- Template Parameters
-
- Parameters
-
[in] | data | the variable to be converted to a string |
- Returns
- result of std::to_string function
◆ ToString() [5/14]
template<>
std::string mvlt::ToString |
( |
const int & |
data | ) |
|
|
inlinenoexcept |
Specialization of the ToString method for the int type.
- Template Parameters
-
- Parameters
-
[in] | data | the variable to be converted to a string |
- Returns
- result of std::to_string function
◆ ToString() [6/14]
Specialization of the ToString method for the int type.
- Template Parameters
-
<long | int> integer variable |
- Parameters
-
[in] | data | the variable to be converted to a string |
- Returns
- result of std::to_string function
◆ ToString() [7/14]
Specialization of the ToString method for the long long int type.
- Template Parameters
-
<long | long int> integer variable |
- Parameters
-
[in] | data | the variable to be converted to a string |
- Returns
- result of std::to_string function
◆ ToString() [8/14]
Specialization of the ToString method for the short int type.
- Template Parameters
-
<short | int> integer variable |
- Parameters
-
[in] | data | the variable to be converted to a string |
- Returns
- result of std::to_string function
◆ ToString() [9/14]
template<>
std::string mvlt::ToString |
( |
const std::string & |
data | ) |
|
|
inlinenoexcept |
Specialization of the ToString method for the std::string type.
- Template Parameters
-
<std::string> | string variable |
- Parameters
-
[in] | data | the variable to be converted to a string |
- Returns
- data
◆ ToString() [10/14]
std::string mvlt::ToString |
( |
const T & |
data | ) |
|
|
noexcept |
A template method for providing an interface converting any type to a string.
This function allows you to use the same interface inside the Vault for any class, since this function accepts any type. Therefore, if you do not plan to use the functionality of saving to files or printing data, then there will be no problems when using a custom type. At the same time, if you plan to work with files or printing data, you can specialize this function for each required type.
- Template Parameters
-
- Parameters
-
[in] | data | the variable to be converted to a string |
- Returns
- This function returns an empty string. The specialization of this function for types will return strings with data from data.
◆ ToString() [11/14]
Specialization of the ToString method for the unsigned int type.
- Template Parameters
-
<unsigned | int> integer variable |
- Parameters
-
[in] | data | the variable to be converted to a string |
- Returns
- result of std::to_string function
◆ ToString() [12/14]
Specialization of the ToString method for the int type.
- Template Parameters
-
<unsigned | long int> integer variable |
- Parameters
-
[in] | data | the variable to be converted to a string |
- Returns
- result of std::to_string function
◆ ToString() [13/14]
Specialization of the ToString method for the unsigned long long int type.
- Template Parameters
-
<unsigned | long long int> integer variable |
- Parameters
-
[in] | data | the variable to be converted to a string |
- Returns
- result of std::to_string function
◆ ToString() [14/14]
Specialization of the ToString method for the unsigned short int type.
- Template Parameters
-
<unsigned | short int> integer variable |
- Parameters
-
[in] | data | the variable to be converted to a string |
- Returns
- result of std::to_string function