|
- #pragma once
-
- #include <string>
- #include <iostream>
-
- namespace cppcore
- {
-
- namespace __impl
- {
-
- /**
- * @brief Predicate class to convert any type to a string using a stream.
- */
- template<typename T, typename Enable = void>
- struct op_to_stream;
-
- /**
- * @brief Predicate class to convert any type to a string.
- */
- template<typename T, typename Enable = void>
- struct op_to_string;
-
- /**
- * @brief Predicate class to convert a string to any type.
- */
- template<typename T, typename Enable = void>
- struct op_from_string;
-
- /**
- * @brief Traits class to store needed data types.
- */
- struct string_conversion_traits
- {
- template<typename T>
- using to_stream_predicate_type = op_to_stream<T>;
-
- template<typename T>
- using to_string_predicate_type = op_to_string<T>;
-
- template<typename T>
- using from_string_predicate_type = op_from_string<T>;
- };
-
- }
-
- /**
- * @brief Convert the passed value to string.
- *
- * @tparam T Type of the passed value.
- * @tparam T_traits Traits type to use for conversion.
- *
- * @param[in] value Value to convert to string.
- *
- * @return Converted value.
- */
- template<typename T, typename T_traits = __impl::string_conversion_traits>
- inline std::string to_string(const T& value);
-
- /**
- * @brief Write the passed value as string representation to a stream.
- *
- * @tparam T Type of the passed value.
- * @tparam T_traits Traits type to use for conversion.
- *
- * @param[in] value Value to convert to write to stream.
- */
- template<typename T, typename T_traits = __impl::string_conversion_traits>
- inline void to_string(std::ostream& os, const T& value);
-
- /**
- * @brief Try to convert the given string to the passed value.
- *
- * @tparam T Type of the value to convert the stirng to.
- * @tparam T_traits Traits type to use for conversion.
- *
- * @param[in] s String to convert to value.
- * @param[out] value Parameter to store vconverted value in.
- *
- * @retval true If the conversion was successful.
- * @retval false If the conversion failed.
- */
- template<typename T, typename T_traits = __impl::string_conversion_traits>
- inline bool try_from_string(const std::string& s, T& value);
-
- /**
- * @brief Try to convert the given string to the passed type. Throws exception if conversion was failed.
- *
- * @tparam T Type of the value to convert the stirng to.
- * @tparam T_traits Traits type to use for conversion.
- *
- * @param[in] s String to convert to value.
- *
- * @return Converted value.
- */
- template<typename T, typename T_traits = __impl::string_conversion_traits>
- inline T from_string(const std::string& s);
-
- /**
- * @brief Try to convert the given string to the passed type. Returns the passed default value if conversion was failed.
- *
- * @tparam T Type of the value to convert the stirng to.
- * @tparam T_traits Traits type to use for conversion.
- *
- * @param[in] s String to convert to value.
- * @param[in] default_value Default value to return if the conversion failed.
- *
- * @return Converted value.
- */
- template<typename T, typename T_traits = __impl::string_conversion_traits>
- inline T from_string_default(const std::string& s, const T& default_value);
-
- /**
- * @brief Split string at the passed seperator and calls the passed predicate for each element.
- *
- * @tparam T_predicate Type of predicate to execute for each element.
- *
- * @param s String to split.
- * @param seperator Seperator to split string at.
- * @param predicate Predicate to execute for each element.
- *
- * @return Value returned from the predicate.
- */
- template<typename T_predicate>
- inline bool string_split(const std::string& s, char seperator, const T_predicate& predicate);
-
- /**
- * @brief Class to handle string conversions.
- *
- * @tparam T_traits Traits to use for converting.
- */
- template<typename T_traits>
- struct string_conversion_t
- {
- using traits_type = T_traits;
-
- /**
- * @brief Convert the passed value to string.
- *
- * @tparam T Type of the passed value.
- *
- * @param[in] value Value to convert to string.
- *
- * @return Converted value.
- */
- template<typename T>
- static inline std::string to_string(const T& value);
-
- /**
- * @brief Write the passed value as string representation to a stream.
- *
- * @tparam T Type of the passed value.
- *
- * @param[in] value Value to convert to write to stream.
- */
- template<typename T>
- static inline void to_string(std::ostream& os, const T& value);
-
- /**
- * @brief Try to convert the given string to the passed value.
- *
- * @tparam T Type of the value to convert the stirng to.
- *
- * @param[in] s String to convert to value.
- * @param[out] value Parameter to store vconverted value in.
- *
- * @retval true If the conversion was successful.
- * @retval false If the conversion failed.
- */
- template<typename T>
- static inline bool try_from_string(const std::string& s, T& value);
-
- /**
- * @brief Try to convert the given string to the passed type. Throws exception if conversion was failed.
- *
- * @tparam T Type of the value to convert the stirng to.
- *
- * @param[in] s String to convert to value.
- *
- * @return Converted value.
- */
- template<typename T>
- static inline T from_string(const std::string& s);
-
- /**
- * @brief Try to convert the given string to the passed type. Returns the passed default value if conversion was failed.
- *
- * @tparam T Type of the value to convert the stirng to.
- *
- * @param[in] s String to convert to value.
- * @param[in] default_value Default value to return if the conversion failed.
- *
- * @return Converted value.
- */
- template<typename T>
- static inline T from_string_default(const std::string& s, const T& default_value);
- };
-
- /**
- * @brief String conversion class with default traits.
- */
- struct string_conversion
- : public string_conversion_t<__impl::string_conversion_traits>
- { };
-
- }
-
-
- namespace std
- {
-
- /**
- * @brief Operator overload to write value to stream that supports the to_string method, with stream parameter.
- */
- template<typename T_char, typename T_traits, typename X>
- inline auto operator<<(basic_ostream<T_char, T_traits>& os, X&& x)
- -> decltype(
- std::forward<X>(x).to_string(std::declval<basic_ostream<T_char, T_traits>&>()),
- std::declval<basic_ostream<T_char, T_traits>&>());
-
- /**
- * @brief Operator overload to write value to stream that supports the to_string method, with stream parameter.
- */
- template<typename T_char, typename T_traits, typename X>
- inline auto operator<<(basic_ostream<T_char, T_traits>& os, X&& x)
- -> decltype(
- std::forward<X>(x).to_string(),
- std::declval<basic_ostream<T_char, T_traits>&>());
-
- }
-
- #include "string.inl"
|