You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

233 lines
7.8 KiB

  1. #pragma once
  2. #include <string>
  3. #include <iostream>
  4. namespace cppcore
  5. {
  6. namespace __impl
  7. {
  8. /**
  9. * @brief Predicate class to convert any type to a string using a stream.
  10. */
  11. template<typename T, typename Enable = void>
  12. struct op_to_stream;
  13. /**
  14. * @brief Predicate class to convert any type to a string.
  15. */
  16. template<typename T, typename Enable = void>
  17. struct op_to_string;
  18. /**
  19. * @brief Predicate class to convert a string to any type.
  20. */
  21. template<typename T, typename Enable = void>
  22. struct op_from_string;
  23. /**
  24. * @brief Traits class to store needed data types.
  25. */
  26. struct string_conversion_traits
  27. {
  28. template<typename T>
  29. using to_stream_predicate_type = op_to_stream<T>;
  30. template<typename T>
  31. using to_string_predicate_type = op_to_string<T>;
  32. template<typename T>
  33. using from_string_predicate_type = op_from_string<T>;
  34. };
  35. }
  36. /**
  37. * @brief Convert the passed value to string.
  38. *
  39. * @tparam T Type of the passed value.
  40. * @tparam T_traits Traits type to use for conversion.
  41. *
  42. * @param[in] value Value to convert to string.
  43. *
  44. * @return Converted value.
  45. */
  46. template<typename T, typename T_traits = __impl::string_conversion_traits>
  47. inline std::string to_string(const T& value);
  48. /**
  49. * @brief Write the passed value as string representation to a stream.
  50. *
  51. * @tparam T Type of the passed value.
  52. * @tparam T_traits Traits type to use for conversion.
  53. *
  54. * @param[in] value Value to convert to write to stream.
  55. */
  56. template<typename T, typename T_traits = __impl::string_conversion_traits>
  57. inline void to_string(std::ostream& os, const T& value);
  58. /**
  59. * @brief Try to convert the given string to the passed value.
  60. *
  61. * @tparam T Type of the value to convert the stirng to.
  62. * @tparam T_traits Traits type to use for conversion.
  63. *
  64. * @param[in] s String to convert to value.
  65. * @param[out] value Parameter to store vconverted value in.
  66. *
  67. * @retval true If the conversion was successful.
  68. * @retval false If the conversion failed.
  69. */
  70. template<typename T, typename T_traits = __impl::string_conversion_traits>
  71. inline bool try_from_string(const std::string& s, T& value);
  72. /**
  73. * @brief Try to convert the given string to the passed type. Throws exception if conversion was failed.
  74. *
  75. * @tparam T Type of the value to convert the stirng to.
  76. * @tparam T_traits Traits type to use for conversion.
  77. *
  78. * @param[in] s String to convert to value.
  79. *
  80. * @return Converted value.
  81. */
  82. template<typename T, typename T_traits = __impl::string_conversion_traits>
  83. inline T from_string(const std::string& s);
  84. /**
  85. * @brief Try to convert the given string to the passed type. Returns the passed default value if conversion was failed.
  86. *
  87. * @tparam T Type of the value to convert the stirng to.
  88. * @tparam T_traits Traits type to use for conversion.
  89. *
  90. * @param[in] s String to convert to value.
  91. * @param[in] default_value Default value to return if the conversion failed.
  92. *
  93. * @return Converted value.
  94. */
  95. template<typename T, typename T_traits = __impl::string_conversion_traits>
  96. inline T from_string_default(const std::string& s, const T& default_value);
  97. /**
  98. * @brief Split string at the passed seperator and calls the passed predicate for each element.
  99. *
  100. * @tparam T_predicate Type of predicate to execute for each element.
  101. *
  102. * @param s String to split.
  103. * @param seperator Seperator to split string at.
  104. * @param predicate Predicate to execute for each element.
  105. *
  106. * @return Value returned from the predicate.
  107. */
  108. template<typename T_predicate>
  109. inline bool string_split(const std::string& s, char seperator, const T_predicate& predicate);
  110. /**
  111. * @brief Class to handle string conversions.
  112. *
  113. * @tparam T_traits Traits to use for converting.
  114. */
  115. template<typename T_traits>
  116. struct string_conversion_t
  117. {
  118. using traits_type = T_traits;
  119. /**
  120. * @brief Convert the passed value to string.
  121. *
  122. * @tparam T Type of the passed value.
  123. *
  124. * @param[in] value Value to convert to string.
  125. *
  126. * @return Converted value.
  127. */
  128. template<typename T>
  129. static inline std::string to_string(const T& value);
  130. /**
  131. * @brief Write the passed value as string representation to a stream.
  132. *
  133. * @tparam T Type of the passed value.
  134. *
  135. * @param[in] value Value to convert to write to stream.
  136. */
  137. template<typename T>
  138. static inline void to_string(std::ostream& os, const T& value);
  139. /**
  140. * @brief Try to convert the given string to the passed value.
  141. *
  142. * @tparam T Type of the value to convert the stirng to.
  143. *
  144. * @param[in] s String to convert to value.
  145. * @param[out] value Parameter to store vconverted value in.
  146. *
  147. * @retval true If the conversion was successful.
  148. * @retval false If the conversion failed.
  149. */
  150. template<typename T>
  151. static inline bool try_from_string(const std::string& s, T& value);
  152. /**
  153. * @brief Try to convert the given string to the passed type. Throws exception if conversion was failed.
  154. *
  155. * @tparam T Type of the value to convert the stirng to.
  156. *
  157. * @param[in] s String to convert to value.
  158. *
  159. * @return Converted value.
  160. */
  161. template<typename T>
  162. static inline T from_string(const std::string& s);
  163. /**
  164. * @brief Try to convert the given string to the passed type. Returns the passed default value if conversion was failed.
  165. *
  166. * @tparam T Type of the value to convert the stirng to.
  167. *
  168. * @param[in] s String to convert to value.
  169. * @param[in] default_value Default value to return if the conversion failed.
  170. *
  171. * @return Converted value.
  172. */
  173. template<typename T>
  174. static inline T from_string_default(const std::string& s, const T& default_value);
  175. };
  176. /**
  177. * @brief String conversion class with default traits.
  178. */
  179. struct string_conversion
  180. : public string_conversion_t<__impl::string_conversion_traits>
  181. { };
  182. }
  183. namespace std
  184. {
  185. /**
  186. * @brief Operator overload to write value to stream that supports the to_string method, with stream parameter.
  187. */
  188. template<typename T_char, typename T_traits, typename X>
  189. inline auto operator<<(basic_ostream<T_char, T_traits>& os, X&& x)
  190. -> decltype(
  191. std::forward<X>(x).to_string(std::declval<basic_ostream<T_char, T_traits>&>()),
  192. std::declval<basic_ostream<T_char, T_traits>&>());
  193. /**
  194. * @brief Operator overload to write value to stream that supports the to_string method, with stream parameter.
  195. */
  196. template<typename T_char, typename T_traits, typename X>
  197. inline auto operator<<(basic_ostream<T_char, T_traits>& os, X&& x)
  198. -> decltype(
  199. std::forward<X>(x).to_string(),
  200. std::declval<basic_ostream<T_char, T_traits>&>());
  201. }
  202. #include "string.inl"