halcheck 1.0
Loading...
Searching...
No Matches
string.hpp
1#ifndef HALCHECK_LIB_STRING_HPP
2#define HALCHECK_LIB_STRING_HPP
3
11#include <halcheck/lib/optional.hpp>
12#include <halcheck/lib/type_traits.hpp>
13
14#include <sstream>
15#include <string> // IWYU pragma: export
16
17namespace halcheck { namespace lib {
18
27template<typename T, HALCHECK_REQUIRE(lib::is_printable<T>())>
28std::string to_string(const T &value) {
30 os << value;
31 return os.str();
32}
33
42template<typename T, HALCHECK_REQUIRE(lib::is_parsable<T>())>
44 T output;
45 if (std::istringstream(value) >> output)
46 return output;
47 else
48 return lib::nullopt;
49}
50
59
68template<typename T, HALCHECK_REQUIRE(lib::is_parsable<T>())>
70 if (auto value = lib::getenv(name))
71 return lib::of_string<T>(*value);
72 else
73 return lib::nullopt;
74}
75
76}} // namespace halcheck::lib
77
78#endif
An implementation of std::optional.
Definition optional.hpp:338
static constexpr nullopt_t nullopt
An implementation of std::nullopt.
Definition optional.hpp:41
std::string to_string(const T &value)
Converts a value to a std::string using operator<<.
Definition string.hpp:28
lib::optional< std::string > getenv(const std::string &name)
Gets the value of an environment variable.
lib::optional< T > of_string(const std::string &value)
Converts a std::string to a value using operator>>.
Definition string.hpp:43
T str(T... args)