From a5e0fe8204a8b3299507a645f3479e9ead6c6110 Mon Sep 17 00:00:00 2001 From: Ben Longbons Date: Thu, 9 Oct 2014 19:54:23 -0700 Subject: Add OPTION_IS_SOME branching macro --- src/compat/option.hpp | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) (limited to 'src/compat/option.hpp') diff --git a/src/compat/option.hpp b/src/compat/option.hpp index 25c23eb..ad83395 100644 --- a/src/compat/option.hpp +++ b/src/compat/option.hpp @@ -24,6 +24,8 @@ #include +#include "attr.hpp" + namespace tmwa { @@ -204,6 +206,10 @@ namespace option { return repr.is_some() ? repr.ptr() : def; } + bool is_some() const + { + return repr.is_some(); + } template auto move_map(F&& f) -> Option(f)(std::move(*repr.ptr())))> @@ -365,9 +371,13 @@ namespace option #define TRY_UNWRAP(opt, falsy) \ ({ \ tmwa::option::RefWrapper o = {(opt)}; \ - if (!o.maybe_ref.ptr_or(nullptr)) falsy; \ + if (!o.maybe_ref.is_some()) 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()) \ + WITH_VAR(auto&, var, *(expr).ptr_or(nullptr)) } // namespace option using option::Option; -- cgit v1.2.3-70-g09d2