summaryrefslogtreecommitdiff
path: root/src/compat/option.hpp
diff options
context:
space:
mode:
authorBen Longbons <b.r.longbons@gmail.com>2014-10-13 13:16:34 -0700
committerBen Longbons <b.r.longbons@gmail.com>2014-10-13 14:03:46 -0700
commit780a0d771edbe21dcfa3405163ffbdf7f7fa4604 (patch)
treeac202254d015d2a2a28ab5bca60c3f5474d168ba /src/compat/option.hpp
parenta5e0fe8204a8b3299507a645f3479e9ead6c6110 (diff)
downloadtmwa-780a0d771edbe21dcfa3405163ffbdf7f7fa4604.tar.gz
tmwa-780a0d771edbe21dcfa3405163ffbdf7f7fa4604.tar.bz2
tmwa-780a0d771edbe21dcfa3405163ffbdf7f7fa4604.tar.xz
tmwa-780a0d771edbe21dcfa3405163ffbdf7f7fa4604.zip
Convert container lookups to use Option<Borrowed<T>>
Diffstat (limited to 'src/compat/option.hpp')
-rw-r--r--src/compat/option.hpp54
1 files changed, 50 insertions, 4 deletions
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 T>
class Option
{
@@ -210,6 +211,10 @@ namespace option
{
return repr.is_some();
}
+ bool is_none() const
+ {
+ return !is_some();
+ }
template<class F>
auto move_map(F&& f) -> Option<decltype(std::forward<F>(f)(std::move(*repr.ptr())))>
@@ -262,6 +267,47 @@ namespace option
return None;
}
}
+ // wanting members is *so* common
+ template<class M, class B>
+ Option<M> pmd_get(const M B::*pmd) const
+ {
+ if (repr.is_some())
+ {
+ return Some((*repr.ptr()).*pmd);
+ }
+ else
+ {
+ return None;
+ }
+ }
+ template<class M, class B>
+ void pmd_set(M B::*pmd, M value)
+ {
+ if (repr.is_some())
+ {
+ ((*repr.ptr()).*pmd) = std::move(value);
+ }
+ }
+ template<class M, class B>
+ Option<M> pmd_pget(const M B::*pmd) const
+ {
+ if (repr.is_some())
+ {
+ return Some((**repr.ptr()).*pmd);
+ }
+ else
+ {
+ return None;
+ }
+ }
+ template<class M, class B>
+ void pmd_pset(M B::*pmd, M value)
+ {
+ if (repr.is_some())
+ {
+ ((**repr.ptr()).*pmd) = std::move(value);
+ }
+ }
};
template<class T>
@@ -371,16 +417,16 @@ namespace option
#define TRY_UNWRAP(opt, falsy) \
({ \
tmwa::option::RefWrapper<decltype((opt))> 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