From fede1dfcd3dee5076f26cb48d009f2375944e467 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Artur=20=C5=9Awigo=C5=84?= Date: Thu, 13 Jan 2022 15:29:55 +0100 Subject: [PATCH] Replace obsolete safe bool idiom with explicit operator bool C++11 introduces 'explicit operator bool' to prevent unintended implicit conversions to 'bool', thus making the trick with converting to a pointer-to-member (a.k.a. "safe bool idiom") obsolete. The explicit operator is more restrictive than 'safe bool', and it helped uncover a bug in the test suite where object handles were implicitly converted to bool before being sent to an std::ostream. Change-Id: I2bbb60d6b97e920dc08b641031304b1e07f8a2cd --- .../dali-toolkit-test-utils/dali-test-suite-utils.cpp | 5 +++++ .../dali-toolkit-test-utils/dali-test-suite-utils.h | 1 + .../dali-toolkit-test-utils/test-graphics-controller.cpp | 2 +- automated-tests/src/dali-toolkit/utc-Dali-ItemView.cpp | 6 +++--- .../src/dali-toolkit/utc-Dali-SyncImageLoader.cpp | 4 +--- automated-tests/src/dali-toolkit/utc-Dali-WebView.cpp | 5 ++--- dali-toolkit/internal/builder/optional-value.h | 9 ++------- .../canvas-view/canvas-view-rasterize-thread.cpp | 2 +- .../internal/controls/magnifier/magnifier-impl.cpp | 2 +- .../internal/visuals/svg/svg-rasterize-thread.cpp | 2 +- 10 files changed, 18 insertions(+), 20 deletions(-) diff --git a/automated-tests/src/dali-toolkit/dali-toolkit-test-utils/dali-test-suite-utils.cpp b/automated-tests/src/dali-toolkit/dali-toolkit-test-utils/dali-test-suite-utils.cpp index 523bd8ce22..299a0e1a0b 100644 --- a/automated-tests/src/dali-toolkit/dali-toolkit-test-utils/dali-test-suite-utils.cpp +++ b/automated-tests/src/dali-toolkit/dali-toolkit-test-utils/dali-test-suite-utils.cpp @@ -78,6 +78,11 @@ std::ostream& operator<<(std::ostream& ostream, Degree angle) return ostream; } +std::ostream& operator<<(std::ostream& ostream, BaseHandle handle) +{ + return ostream << static_cast(handle.GetObjectPtr()); +} + void DALI_TEST_EQUALS(const BaseHandle& baseHandle1, const BaseHandle& baseHandle2, const char* location) { DALI_TEST_EQUALS(baseHandle1, baseHandle2, location); diff --git a/automated-tests/src/dali-toolkit/dali-toolkit-test-utils/dali-test-suite-utils.h b/automated-tests/src/dali-toolkit/dali-toolkit-test-utils/dali-test-suite-utils.h index c6e63e7e47..a070298379 100644 --- a/automated-tests/src/dali-toolkit/dali-toolkit-test-utils/dali-test-suite-utils.h +++ b/automated-tests/src/dali-toolkit/dali-toolkit-test-utils/dali-test-suite-utils.h @@ -99,6 +99,7 @@ bool operator==(TimePeriod a, TimePeriod b); std::ostream& operator<<(std::ostream& ostream, TimePeriod value); std::ostream& operator<<(std::ostream& ostream, Radian angle); std::ostream& operator<<(std::ostream& ostream, Degree angle); +std::ostream& operator<<(std::ostream& ostream, BaseHandle handle); /** * Test whether two values are equal. diff --git a/automated-tests/src/dali-toolkit/dali-toolkit-test-utils/test-graphics-controller.cpp b/automated-tests/src/dali-toolkit/dali-toolkit-test-utils/test-graphics-controller.cpp index d2d8b161b9..fb2afdaa63 100644 --- a/automated-tests/src/dali-toolkit/dali-toolkit-test-utils/test-graphics-controller.cpp +++ b/automated-tests/src/dali-toolkit/dali-toolkit-test-utils/test-graphics-controller.cpp @@ -78,7 +78,7 @@ std::ostream& operator<<(std::ostream& o, const Graphics::TextureCreateInfo& cre << " usageFlags:" << std::hex << createInfo.usageFlags << " data:" << std::hex << createInfo.data << " dataSize:" << std::dec << createInfo.dataSize - << " nativeImagePtr:" << std::hex << createInfo.nativeImagePtr; + << " nativeImagePtr:" << std::hex << createInfo.nativeImagePtr.Get(); return o; } diff --git a/automated-tests/src/dali-toolkit/utc-Dali-ItemView.cpp b/automated-tests/src/dali-toolkit/utc-Dali-ItemView.cpp index da5ce83797..e1273952bf 100644 --- a/automated-tests/src/dali-toolkit/utc-Dali-ItemView.cpp +++ b/automated-tests/src/dali-toolkit/utc-Dali-ItemView.cpp @@ -362,7 +362,7 @@ int UtcDaliItemViewActivateLayoutAndGetActiveLayout(void) DALI_TEST_CHECK(view.GetLayoutCount() == 3); // Check there is no active layout at the moment - DALI_TEST_CHECK(view.GetActiveLayout() == NULL); + DALI_TEST_CHECK(!view.GetActiveLayout()); // Activate the depth layout Vector3 stageSize(application.GetScene().GetSize()); @@ -399,7 +399,7 @@ int UtcDaliItemViewDeactivateCurrentLayout(void) view.AddLayout(*gridLayout); // Check there is no active layout at the moment - DALI_TEST_CHECK(view.GetActiveLayout() == NULL); + DALI_TEST_CHECK(!view.GetActiveLayout()); // Activate the grid layout Vector3 stageSize(application.GetScene().GetSize()); @@ -412,7 +412,7 @@ int UtcDaliItemViewDeactivateCurrentLayout(void) view.DeactivateCurrentLayout(); // Check there is no active layout at the moment - DALI_TEST_CHECK(view.GetActiveLayout() == NULL); + DALI_TEST_CHECK(!view.GetActiveLayout()); END_TEST; } diff --git a/automated-tests/src/dali-toolkit/utc-Dali-SyncImageLoader.cpp b/automated-tests/src/dali-toolkit/utc-Dali-SyncImageLoader.cpp index d37c5fc233..4a694526e4 100644 --- a/automated-tests/src/dali-toolkit/utc-Dali-SyncImageLoader.cpp +++ b/automated-tests/src/dali-toolkit/utc-Dali-SyncImageLoader.cpp @@ -48,7 +48,7 @@ int UtcDaliSyncImageLoaderLoad(void) { PixelData pixelData = Toolkit::SyncImageLoader::Load( gImage_50_RGBA ); - DALI_TEST_EQUALS( pixelData, true, TEST_LOCATION ); + DALI_TEST_CHECK( pixelData ); END_TEST; } @@ -70,5 +70,3 @@ int UtcDaliSyncImageLoaderLoadWithAllOptions(void) END_TEST; } - - diff --git a/automated-tests/src/dali-toolkit/utc-Dali-WebView.cpp b/automated-tests/src/dali-toolkit/utc-Dali-WebView.cpp index aa2fdcc03a..6da9ee0d26 100755 --- a/automated-tests/src/dali-toolkit/utc-Dali-WebView.cpp +++ b/automated-tests/src/dali-toolkit/utc-Dali-WebView.cpp @@ -1676,13 +1676,13 @@ int UtcDaliWebContextHttpRequestInterceptor(void) // load url. context->RegisterRequestInterceptedCallback(&OnRequestIntercepted); DALI_TEST_EQUALS(gRequestInterceptedCallbackCalled, 0, TEST_LOCATION); - DALI_TEST_CHECK(gRequestInterceptorInstance == 0); + DALI_TEST_CHECK(!gRequestInterceptorInstance); Test::EmitGlobalTimerSignal(); DALI_TEST_EQUALS( gRequestInterceptedCallbackCalled, 1, TEST_LOCATION ); // check request interceptor. - DALI_TEST_CHECK(gRequestInterceptorInstance != 0); + DALI_TEST_CHECK(gRequestInterceptorInstance); DALI_TEST_CHECK(gRequestInterceptorInstance->Ignore()); DALI_TEST_CHECK(gRequestInterceptorInstance->SetResponseStatus(400, "error")); DALI_TEST_CHECK(gRequestInterceptorInstance->AddResponseHeader("key1", "value1")); @@ -2352,4 +2352,3 @@ int UtcDaliWebViewGetPlainText(void) END_TEST; } - diff --git a/dali-toolkit/internal/builder/optional-value.h b/dali-toolkit/internal/builder/optional-value.h index 7251ad2532..7a1d9f4be6 100644 --- a/dali-toolkit/internal/builder/optional-value.h +++ b/dali-toolkit/internal/builder/optional-value.h @@ -79,7 +79,6 @@ template class OptionalValue { public: - typedef void (OptionalValue::*bool_type)() const; typedef typename OptionalTypes::ReturnType ReturnType; typedef typename OptionalTypes::ValueType ValueType; @@ -104,18 +103,14 @@ public: return OptionalTypes::Get(mValue); } - // safe bool idiom - operator bool_type() const + explicit operator bool() const { - return mOk == true ? &OptionalValue::this_type_does_not_support_comparisons : 0; + return mOk; } private: bool mOk; ValueType mValue; - void this_type_does_not_support_comparisons() const - { - } }; template diff --git a/dali-toolkit/internal/controls/canvas-view/canvas-view-rasterize-thread.cpp b/dali-toolkit/internal/controls/canvas-view/canvas-view-rasterize-thread.cpp index ba3daae3ae..e3de638118 100644 --- a/dali-toolkit/internal/controls/canvas-view/canvas-view-rasterize-thread.cpp +++ b/dali-toolkit/internal/controls/canvas-view/canvas-view-rasterize-thread.cpp @@ -92,7 +92,7 @@ void CanvasViewRasterizeThread::AddTask(CanvasRendererRasterizingTaskPtr task) // Lock while adding task to the queue ConditionalWait::ScopedLock lock(mConditionalWait); wasEmpty = mRasterizeTasks.empty(); - if(!wasEmpty && task != NULL) + if(!wasEmpty && task) { // Remove the tasks with the same renderer. // Older task which waiting to rasterize and apply the svg to the same renderer is expired. diff --git a/dali-toolkit/internal/controls/magnifier/magnifier-impl.cpp b/dali-toolkit/internal/controls/magnifier/magnifier-impl.cpp index c4e8ce2bcd..6da7915048 100644 --- a/dali-toolkit/internal/controls/magnifier/magnifier-impl.cpp +++ b/dali-toolkit/internal/controls/magnifier/magnifier-impl.cpp @@ -250,7 +250,7 @@ void Magnifier::InitializeRenderTask() bool Magnifier::GetFrameVisibility() const { - return mFrame; + return static_cast(mFrame); } void Magnifier::SetFrameVisibility(bool visible) diff --git a/dali-toolkit/internal/visuals/svg/svg-rasterize-thread.cpp b/dali-toolkit/internal/visuals/svg/svg-rasterize-thread.cpp index 273f08b682..aed445d42a 100644 --- a/dali-toolkit/internal/visuals/svg/svg-rasterize-thread.cpp +++ b/dali-toolkit/internal/visuals/svg/svg-rasterize-thread.cpp @@ -159,7 +159,7 @@ void SvgRasterizeThread::AddTask(RasterizingTaskPtr task) // Lock while adding task to the queue ConditionalWait::ScopedLock lock(mConditionalWait); wasEmpty = mRasterizeTasks.empty(); - if(!wasEmpty && task != NULL) + if(!wasEmpty && task) { // Remove the tasks with the same renderer. // Older task which waiting to rasterize and apply the svg to the same renderer is expired. -- 2.34.1