Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 599a8b82 authored by Ytai Ben-Tsvi's avatar Ytai Ben-Tsvi
Browse files

Use liberror in AidlConversionUtil

This completes the extraction of the Result type from the AIDL
conversion library into liberror, for broader use.

Test: m -j
Change-Id: I7dba09460e1d357105e8a965b5df20aa2ea009d8
parent 4e0dcac2
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -203,9 +203,11 @@ cc_library_headers {
    ],
    header_libs: [
        "libbase_headers",
        "liberror_headers",
    ],
    export_header_lib_headers: [
        "libbase_headers",
        "liberror_headers",
    ],
    apex_available: [
        "//apex_available:platform",
+2 −34
Original line number Diff line number Diff line
@@ -20,45 +20,13 @@
#include <type_traits>
#include <utility>

#include <android-base/expected.h>
#include <binder/Status.h>
#include <error/Result.h>

namespace android {

template <typename T>
using ConversionResult = base::expected<T, status_t>;

// Convenience macros for working with ConversionResult, useful for writing converted for aggregate
// types.

#define VALUE_OR_RETURN(result)                                \
    ({                                                         \
        auto _tmp = (result);                                  \
        if (!_tmp.ok()) return base::unexpected(_tmp.error()); \
        std::move(_tmp.value());                               \
    })

#define RETURN_IF_ERROR(result) \
    if (status_t _tmp = (result); _tmp != OK) return base::unexpected(_tmp);

#define RETURN_STATUS_IF_ERROR(result) \
    if (status_t _tmp = (result); _tmp != OK) return _tmp;

#define VALUE_OR_RETURN_STATUS(x)           \
    ({                                      \
       auto _tmp = (x);                     \
       if (!_tmp.ok()) return _tmp.error(); \
       std::move(_tmp.value());             \
     })

#define VALUE_OR_FATAL(result)                                        \
    ({                                                                \
       auto _tmp = (result);                                          \
       LOG_ALWAYS_FATAL_IF(!_tmp.ok(),                                \
                           "Function: %s Line: %d Failed result (%d)",\
                           __FUNCTION__, __LINE__, _tmp.error());     \
       std::move(_tmp.value());                                       \
     })
using ConversionResult = error::Result<T>;

/**
 * A generic template to safely cast between integral types, respecting limits of the destination