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

Commit 7af6853c authored by Ytai Ben-tsvi's avatar Ytai Ben-tsvi Committed by Android (Google) Code Review
Browse files

Merge changes from topic "libaudiohal-parameters"

* changes:
  Use liberror in AidlConversionUtil
  A simple error handling library
parents 12935731 599a8b82
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
+67 −0
Original line number Diff line number Diff line
package {
    // See: http://go/android-license-faq
    // A large-scale-change added 'default_applicable_licenses' to import
    // all of the 'license_kinds' from "frameworks_av_license"
    // to get the below license kinds:
    //   SPDX-license-identifier-Apache-2.0
    default_applicable_licenses: ["frameworks_av_license"],
}

cc_library_headers {
    name: "libexpectedutils_headers",
    host_supported: true,
    vendor_available: true,
    min_sdk_version: "29",
    export_include_dirs: [
        "include",
    ],
    header_libs: [
        "libbase_headers",
        "libutils_headers",
    ],
    export_header_lib_headers: [
        "libbase_headers",
        "libutils_headers",
    ],
    apex_available: [
        "//apex_available:platform",
        "com.android.bluetooth",
        "com.android.media",
        "com.android.media.swcodec",
    ],
}

cc_test_host {
    name: "libexpectedutils_test",
    srcs: [
        "expected_utils_test.cpp",
    ],
    shared_libs: [
        "liblog",
    ],
    header_libs: [
        "libexpectedutils_headers",
    ],
}

cc_library_headers {
    name: "liberror_headers",
    host_supported: true,
    vendor_available: true,
    min_sdk_version: "29",
    apex_available: [
        "//apex_available:platform",
        "com.android.bluetooth",
        "com.android.media",
        "com.android.media.swcodec",
    ],
    export_include_dirs: [
        "include",
    ],
    header_libs: [
        "libexpectedutils_headers",
    ],
    export_header_lib_headers: [
        "libexpectedutils_headers",
    ],
}
+157 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2021 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <error/expected_utils.h>
#include <gtest/gtest.h>

#define LOG_TAG "Result-test"

namespace android {
namespace foo {

class Value {
  public:
    explicit Value(int i) : mInt(i) {}
    Value(const Value&) = delete;
    Value(Value&&) = default;

    operator int() const { return mInt; }

  private:
    const int mInt;
};

class Status {
  public:
    explicit Status(int i) : mInt(i) {}
    Status(const Status&) = delete;
    Status(Status&&) = default;

    operator int() const { return mInt; }

  private:
    const int mInt;
};

bool errorIsOk(const Status& e) {
    return e == 0;
}

std::string errorToString(const Status& e) {
    std::ostringstream str;
    str << e;
    return str.str();
}

using Result = base::expected<Value, Status>;

}  // namespace foo

namespace {

using foo::Result;
using foo::Status;
using foo::Value;

TEST(Result, ValueOrReturnSuccess) {
    Result result = []() -> Result {
        Value intermediate = VALUE_OR_RETURN(Result(Value(3)));
        return Value(intermediate + 1);
    }();
    ASSERT_TRUE(result.ok());
    EXPECT_EQ(4, result.value());
}

TEST(Result, ValueOrReturnFailure) {
    Result result = []() -> Result {
        Value intermediate = VALUE_OR_RETURN(Result(base::unexpected(Status(2))));
        return Value(intermediate + 1);
    }();
    ASSERT_FALSE(result.ok());
    EXPECT_EQ(2, result.error());
}

TEST(Result, ValueOrReturnStatusSuccess) {
    Status status = []() -> Status {
        Value intermediate = VALUE_OR_RETURN_STATUS(Result(Value(3)));
        (void) intermediate;
        return Status(0);
    }();
    EXPECT_EQ(0, status);
}

TEST(Result, ValueOrReturnStatusFailure) {
    Status status = []() -> Status {
        Value intermediate = VALUE_OR_RETURN_STATUS(Result(base::unexpected(Status(1))));
        (void) intermediate;
        return Status(0);
    }();
    EXPECT_EQ(1, status);
}

TEST(Result, ReturnIfErrorSuccess) {
    Result result = []() -> Result {
        RETURN_IF_ERROR(Status(0));
        return Value(5);
    }();
    ASSERT_TRUE(result.ok());
    EXPECT_EQ(5, result.value());
}

TEST(Result, ReturnIfErrorFailure) {
    Result result = []() -> Result {
        RETURN_IF_ERROR(Status(4));
        return Value(5);
    }();
    ASSERT_FALSE(result.ok());
    EXPECT_EQ(4, result.error());
}

TEST(Result, ReturnStatusIfErrorSuccess) {
    Status status = []() -> Status {
        RETURN_STATUS_IF_ERROR(Status(0));
        return Status(7);
    }();
    EXPECT_EQ(7, status);
}

TEST(Result, ReturnStatusIfErrorFailure) {
    Status status = []() -> Status {
        RETURN_STATUS_IF_ERROR(Status(3));
        return Status(0);
    }();
    EXPECT_EQ(3, status);
}

TEST(Result, ValueOrFatalSuccess) {
    Value value = VALUE_OR_FATAL(Result(Value(7)));
    EXPECT_EQ(7, value);
}

TEST(Result, ValueOrFatalFailure) {
    EXPECT_DEATH(VALUE_OR_FATAL(Result(base::unexpected(Status(3)))), "");
}

TEST(Result, FatalIfErrorSuccess) {
    FATAL_IF_ERROR(Status(0));
}

TEST(Result, FatalIfErrorFailure) {
    EXPECT_DEATH(FATAL_IF_ERROR(Status(3)), "");
}

}  // namespace
}  // namespace android
+44 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2021 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#pragma once

#include <error/expected_utils.h>
#include <utils/Errors.h>

namespace android {
namespace error {

/**
 * A convenience short-hand for base::expected, where the error type is a status_t.
 */
template <typename T>
using Result = base::expected<T, status_t>;

}  // namespace error
}  // namespace android

// Below are the implementations of errorIsOk and errorToString for status_t .
// This allows status_t to be used in conjunction with the expected_utils.h macros.
// Unfortuantely, since status_t is merely a typedef for int rather than a unique type, we have to
// overload these methods for any int, and do so in the global namespace for ADL to work.

inline bool errorIsOk(int status) {
    return status == android::OK;
}

inline std::string errorToString(int status) {
    return android::statusToString(status);
}
Loading