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

Commit 7c8892c2 authored by Steven Moreland's avatar Steven Moreland
Browse files

libbinder_ndk: Add ScopedAResource release

Allow resources like ScopedFileDescriptor and ScopedAIBinder to be
released.

Fixed: 214390526
Test: libbinder_ndk_unit_test
Change-Id: I4a9b9b78fd1765b95047bacf5c8b99f07c3013cf
parent aee8706d
Loading
Loading
Loading
Loading
+9 −0
Original line number Diff line number Diff line
@@ -162,6 +162,15 @@ class ScopedAResource {
     */
    const T get() const { return mT; }

    /**
     * Release the underlying resource.
     */
    [[nodiscard]] T release() {
        T a = mT;
        mT = DEFAULT;
        return a;
    }

    /**
     * This allows the value in this class to be set from beneath it. If you call this method and
     * then change the value of T*, you must take ownership of the value you are replacing and add
+23 −0
Original line number Diff line number Diff line
@@ -752,6 +752,29 @@ TEST(NdkBinder, GetClassInterfaceDescriptor) {
    ASSERT_STREQ(IFoo::kIFooDescriptor, AIBinder_Class_getDescriptor(IFoo::kClass));
}

static void addOne(int* to) {
    if (!to) return;
    ++(*to);
}
struct FakeResource : public ndk::impl::ScopedAResource<int*, addOne, nullptr> {
    explicit FakeResource(int* a) : ScopedAResource(a) {}
};

TEST(NdkBinder_ScopedAResource, GetDelete) {
    int deleteCount = 0;
    { FakeResource resource(&deleteCount); }
    EXPECT_EQ(deleteCount, 1);
}

TEST(NdkBinder_ScopedAResource, Release) {
    int deleteCount = 0;
    {
        FakeResource resource(&deleteCount);
        (void)resource.release();
    }
    EXPECT_EQ(deleteCount, 0);
}

int main(int argc, char* argv[]) {
    ::testing::InitGoogleTest(&argc, argv);