summaryrefslogtreecommitdiff
path: root/AK/Try.h
blob: 772f0d66081a3c4c6345bcdbc9f8b067e4a4755c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/*
 * Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#pragma once

#include <AK/Diagnostics.h>

// NOTE: This macro works with any result type that has the expected APIs.
//       It's designed with AK::Result and AK::Error in mind.
//
//       It depends on a non-standard C++ extension, specifically
//       on statement expressions [1]. This is known to be implemented
//       by at least clang and gcc.
//       [1] https://gcc.gnu.org/onlinedocs/gcc/Statement-Exprs.html

#define TRY(expression)                                   \
    ({                                                    \
        /* Ignore -Wshadow to allow nesting the macro. */ \
        AK_IGNORE_DIAGNOSTIC("-Wshadow",                  \
            auto _temporary_result = (expression));       \
        if (_temporary_result.is_error()) [[unlikely]]    \
            return _temporary_result.release_error();     \
        _temporary_result.release_value();                \
    })

#define MUST(expression)                                  \
    ({                                                    \
        /* Ignore -Wshadow to allow nesting the macro. */ \
        AK_IGNORE_DIAGNOSTIC("-Wshadow",                  \
            auto _temporary_result = (expression));       \
        VERIFY(!_temporary_result.is_error());            \
        _temporary_result.release_value();                \
    })