From f48e6b74b7211f4558e910c98e93c38290d2bf28 Mon Sep 17 00:00:00 2001 From: Corentin Wallez Date: Tue, 4 Dec 2018 12:13:03 +0000 Subject: [PATCH] dawncpp.h: Add nullptr ObjectBase constructor and assignment BUG=dawn:3 Change-Id: I9f7ec0ae1c7885809888a603ed056c6565751bec Reviewed-on: https://dawn-review.googlesource.com/c/2840 Commit-Queue: Corentin Wallez Reviewed-by: Stephen White Reviewed-by: Kai Ninomiya --- generator/templates/apicpp.h | 9 +++++++++ src/tests/unittests/ObjectBaseTests.cpp | 18 ++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/generator/templates/apicpp.h b/generator/templates/apicpp.h index af142b04c0..87bd71b5e7 100644 --- a/generator/templates/apicpp.h +++ b/generator/templates/apicpp.h @@ -97,6 +97,15 @@ namespace dawn { return static_cast(*this); } + ObjectBase(nullptr_t) {} + Derived& operator=(nullptr_t) { + if (mHandle != nullptr) { + Derived::DawnRelease(mHandle); + mHandle = nullptr; + } + return static_cast(*this); + } + explicit operator bool() const { return mHandle != nullptr; } diff --git a/src/tests/unittests/ObjectBaseTests.cpp b/src/tests/unittests/ObjectBaseTests.cpp index 2c9dccbffb..b42920f157 100644 --- a/src/tests/unittests/ObjectBaseTests.cpp +++ b/src/tests/unittests/ObjectBaseTests.cpp @@ -177,3 +177,21 @@ TEST(ObjectBase, MoveAssignmentSelf) { ASSERT_EQ(obj.Get(), &refcount); ASSERT_EQ(refcount, 2); } + +// Test the constructor using nullptr +TEST(ObjectBase, NullptrConstructor) { + Object obj(nullptr); + ASSERT_EQ(obj.Get(), nullptr); +} + +// Test assigning nullptr to the object +TEST(ObjectBase, AssignNullptr) { + int refcount = 1; + + Object obj(&refcount); + ASSERT_EQ(refcount, 2); + + obj = nullptr; + ASSERT_EQ(refcount, 1); +} +