|
|
@@ -9,6 +9,7 @@
|
|
|
#pragma once
|
|
|
|
|
|
#include <cstdlib>
|
|
|
+#include <optional>
|
|
|
#include <string>
|
|
|
#include <type_traits>
|
|
|
#include <utility>
|
|
|
@@ -49,7 +50,14 @@ struct is_stringy<
|
|
|
|
|
|
namespace string_utils {
|
|
|
template <typename T> std::pair<T, bool> cast(std::string const & str);
|
|
|
+template <typename T> bool cast(std::string const & str, std::optional<T> & to);
|
|
|
+#if defined(STRING_UTIL_CAST_STD_VARIANT)
|
|
|
+template <typename... Ts>
|
|
|
+bool cast(std::string const & str, std::variant<Ts...> & to);
|
|
|
+#endif
|
|
|
+}
|
|
|
|
|
|
+namespace string_utils {
|
|
|
template <typename T, typename = std::enable_if_t<traits::is_stringy<T>{}>>
|
|
|
bool cast(std::string const & str, T & to) {
|
|
|
to = T(str);
|
|
|
@@ -110,6 +118,15 @@ bool cast(std::string const & str, std::variant<Ts...> & to) {
|
|
|
}
|
|
|
#endif
|
|
|
|
|
|
+namespace string_utils {
|
|
|
+template <typename T>
|
|
|
+bool cast(std::string const & str, std::optional<T> & to) {
|
|
|
+ auto [value, success] = cast<T>(str);
|
|
|
+ if (success) { to = std::move(value); }
|
|
|
+ return true;
|
|
|
+}
|
|
|
+}
|
|
|
+
|
|
|
// This should be placed last in the file
|
|
|
namespace string_utils {
|
|
|
template <typename T> std::pair<T, bool> cast(std::string const & str) {
|