From 780a0d771edbe21dcfa3405163ffbdf7f7fa4604 Mon Sep 17 00:00:00 2001 From: Ben Longbons Date: Mon, 13 Oct 2014 13:16:34 -0700 Subject: Convert container lookups to use Option> --- src/compat/option.hpp | 54 +++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 50 insertions(+), 4 deletions(-) (limited to 'src/compat/option.hpp') diff --git a/src/compat/option.hpp b/src/compat/option.hpp index ad83395..27ee0bc 100644 --- a/src/compat/option.hpp +++ b/src/compat/option.hpp @@ -66,6 +66,7 @@ namespace option return rv; } + // TODO all *_or and *_set methods should have a lazy version too template class Option { @@ -210,6 +211,10 @@ namespace option { return repr.is_some(); } + bool is_none() const + { + return !is_some(); + } template auto move_map(F&& f) -> Option(f)(std::move(*repr.ptr())))> @@ -262,6 +267,47 @@ namespace option return None; } } + // wanting members is *so* common + template + Option pmd_get(const M B::*pmd) const + { + if (repr.is_some()) + { + return Some((*repr.ptr()).*pmd); + } + else + { + return None; + } + } + template + void pmd_set(M B::*pmd, M value) + { + if (repr.is_some()) + { + ((*repr.ptr()).*pmd) = std::move(value); + } + } + template + Option pmd_pget(const M B::*pmd) const + { + if (repr.is_some()) + { + return Some((**repr.ptr()).*pmd); + } + else + { + return None; + } + } + template + void pmd_pset(M B::*pmd, M value) + { + if (repr.is_some()) + { + ((**repr.ptr()).*pmd) = std::move(value); + } + } }; template @@ -371,16 +417,16 @@ namespace option #define TRY_UNWRAP(opt, falsy) \ ({ \ tmwa::option::RefWrapper o = {(opt)}; \ - if (!o.maybe_ref.is_some()) falsy; \ + if (o.maybe_ref.is_none()) falsy; \ tmwa::option::option_unwrap(std::move(o)); \ }).maybe_ref_fun() // immediately preceded by 'if'; not double-eval-safe -#define OPTION_IS_SOME(var, expr) \ - ((expr).is_some()) \ +#define OPTION_IS_SOME(var, expr) \ + ((expr).is_some()) \ WITH_VAR(auto&, var, *(expr).ptr_or(nullptr)) } // namespace option -using option::Option; +//using option::Option; using option::None; using option::Some; } // namespace tmwa -- cgit v1.2.3-70-g09d2