From 00da6b5977574a0564169172227d8aab45be188f Mon Sep 17 00:00:00 2001 From: Ben Longbons Date: Sat, 3 Jan 2015 21:07:56 -0800 Subject: Switch MATCH to separate begin/end macros The for loop trick turned out to be very prone to infinite loops at runtime. It's better to force compiler errors even if it's ugly. --- src/compat/option.hpp | 49 ++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 40 insertions(+), 9 deletions(-) (limited to 'src/compat/option.hpp') diff --git a/src/compat/option.hpp b/src/compat/option.hpp index 7beef6f..b6e7655 100644 --- a/src/compat/option.hpp +++ b/src/compat/option.hpp @@ -24,8 +24,6 @@ #include -#include "attr.hpp" - namespace tmwa { @@ -423,13 +421,46 @@ namespace option 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_INLOOP(var, expr) \ - ((expr).is_some()) \ - WITH_VAR_INLOOP(auto&, var, *(expr).ptr_or(nullptr)) -#define OPTION_IS_SOME_NOLOOP(var, expr) \ - ((expr).is_some()) \ - WITH_VAR_NOLOOP(auto&, var, *(expr).ptr_or(nullptr)) + +#define OMATCH_BEGIN(expr) \ + { \ + auto&& _omatch_var = (expr); \ + switch (_omatch_var.is_some()) \ + { \ + { \ + { \ + /*}}}}*/ +#define OMATCH_END() \ + /*{{{{*/ \ + } \ + } \ + } \ + (void) _omatch_var; \ + } + +#define OMATCH_BEGIN_SOME(var, expr) \ + OMATCH_BEGIN (expr) \ + OMATCH_CASE_SOME (var) + +#define OMATCH_CASE_SOME(var) \ + /*{{{{*/ \ + } \ + break; \ + } \ + { \ + case true: \ + { \ + auto&& var = *_omatch_var.ptr_or(nullptr); \ + /*}}}}*/ +#define OMATCH_CASE_NONE() \ + /*{{{{*/ \ + } \ + break; \ + } \ + { \ + case false: \ + { \ + /*}}}}*/ } // namespace option //using option::Option; -- cgit v1.2.3-70-g09d2