Loading libs/binder/ndk/include_cpp/android/binder_auto_utils.h +9 −0 Original line number Diff line number Diff line Loading @@ -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 Loading libs/binder/ndk/tests/libbinder_ndk_unit_test.cpp +23 −0 Original line number Diff line number Diff line Loading @@ -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); Loading Loading
libs/binder/ndk/include_cpp/android/binder_auto_utils.h +9 −0 Original line number Diff line number Diff line Loading @@ -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 Loading
libs/binder/ndk/tests/libbinder_ndk_unit_test.cpp +23 −0 Original line number Diff line number Diff line Loading @@ -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); Loading