diff options
Diffstat (limited to 'misc_support.h')
-rw-r--r-- | misc_support.h | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/misc_support.h b/misc_support.h new file mode 100644 index 0000000..b00f6a8 --- /dev/null +++ b/misc_support.h @@ -0,0 +1,49 @@ +#ifndef SOIL_MOISTURE_MISC_SUPPORT_H +#define SOIL_MOISTURE_MISC_SUPPORT_H + +#include <boost/lexical_cast.hpp> +#include <boost/program_options.hpp> +#include <boost/optional.hpp> +#include <string> + +namespace trygvis { +namespace misc_support { + +using namespace boost; +using namespace std; +namespace po = boost::program_options; + +template<typename Target, typename Source> +static +boost::optional<Target> map(boost::optional<Source> &a, std::function<Target(Source)> f) { + if (!a.is_initialized()) { + return boost::none; + } + + return make_optional(f(a)); +} + +template<typename Target, typename Source> +static +boost::optional<Target> flat_map(boost::optional<Source> &a, boost::optional<Target> (&f)(Source)) { + if (!a.is_initialized()) { + return boost::none; + } + + return f(a.get()); +} + +template<typename Target, typename Source = string> +static +boost::optional<Target> l_c(const Source source) { + try { + return boost::lexical_cast<Target>(source); + } catch (bad_lexical_cast &e) { + return boost::none; + } +}; + +} +} + +#endif |