From 222a45f07a7aa2d4ace50bb508935bc225e1d142 Mon Sep 17 00:00:00 2001 From: ridiculousfish Date: Sun, 1 Apr 2018 16:10:59 -0700 Subject: [PATCH] Add acquire() to maybe_t Easy way to pull the value out. --- src/fish_tests.cpp | 6 ++++++ src/maybe.h | 8 ++++++++ 2 files changed, 14 insertions(+) diff --git a/src/fish_tests.cpp b/src/fish_tests.cpp index d748e6e27..340d78b74 100644 --- a/src/fish_tests.cpp +++ b/src/fish_tests.cpp @@ -4491,6 +4491,12 @@ void test_maybe() { do_test(m4 && *m4 == "hi"); maybe_t m5 = m0; do_test(!m5); + + maybe_t acquire_test("def"); + do_test(acquire_test); + std::string res = acquire_test.acquire(); + do_test(!acquire_test); + do_test(res == "def"); } void test_layout_cache() { diff --git a/src/maybe.h b/src/maybe.h index 898731f9d..9e8301ed5 100644 --- a/src/maybe.h +++ b/src/maybe.h @@ -64,6 +64,14 @@ class maybe_t { return *reinterpret_cast(storage); } + // Transfer the value to the caller. + T acquire() { + assert(filled && "maybe_t does not have a value"); + T res = std::move(value()); + reset(); + return res; + } + // Clear the value. void reset() { if (filled) {