mirror of
https://github.com/fish-shell/fish-shell
synced 2024-12-26 12:53:13 +00:00
Add acquire() to maybe_t
Easy way to pull the value out.
This commit is contained in:
parent
5b489ca30f
commit
222a45f07a
2 changed files with 14 additions and 0 deletions
|
@ -4491,6 +4491,12 @@ void test_maybe() {
|
||||||
do_test(m4 && *m4 == "hi");
|
do_test(m4 && *m4 == "hi");
|
||||||
maybe_t<std::string> m5 = m0;
|
maybe_t<std::string> m5 = m0;
|
||||||
do_test(!m5);
|
do_test(!m5);
|
||||||
|
|
||||||
|
maybe_t<std::string> 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() {
|
void test_layout_cache() {
|
||||||
|
|
|
@ -64,6 +64,14 @@ class maybe_t {
|
||||||
return *reinterpret_cast<const T *>(storage);
|
return *reinterpret_cast<const T *>(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.
|
// Clear the value.
|
||||||
void reset() {
|
void reset() {
|
||||||
if (filled) {
|
if (filled) {
|
||||||
|
|
Loading…
Reference in a new issue