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

Commit 48165b9c authored by Treehugger Robot's avatar Treehugger Robot Committed by Gerrit Code Review
Browse files

Merge "libbinder_ndk: Add ScopedAResource release"

parents b31faa6c 7c8892c2
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);