Replace obsolete safe bool idiom with explicit operator bool 02/269402/2
authorArtur Świgoń <a.swigon@samsung.com>
Thu, 13 Jan 2022 14:29:55 +0000 (15:29 +0100)
committerArtur Świgoń <a.swigon@samsung.com>
Mon, 17 Jan 2022 09:27:19 +0000 (10:27 +0100)
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

automated-tests/src/dali-toolkit/dali-toolkit-test-utils/dali-test-suite-utils.cpp
automated-tests/src/dali-toolkit/dali-toolkit-test-utils/dali-test-suite-utils.h
automated-tests/src/dali-toolkit/dali-toolkit-test-utils/test-graphics-controller.cpp
automated-tests/src/dali-toolkit/utc-Dali-ItemView.cpp
automated-tests/src/dali-toolkit/utc-Dali-SyncImageLoader.cpp
automated-tests/src/dali-toolkit/utc-Dali-WebView.cpp
dali-toolkit/internal/builder/optional-value.h
dali-toolkit/internal/controls/canvas-view/canvas-view-rasterize-thread.cpp
dali-toolkit/internal/controls/magnifier/magnifier-impl.cpp
dali-toolkit/internal/visuals/svg/svg-rasterize-thread.cpp

index 523bd8c..299a0e1 100644 (file)
@@ -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<void*>(handle.GetObjectPtr());
+}
+
 void DALI_TEST_EQUALS(const BaseHandle& baseHandle1, const BaseHandle& baseHandle2, const char* location)
 {
   DALI_TEST_EQUALS<const BaseHandle&>(baseHandle1, baseHandle2, location);
index c6e63e7..a070298 100644 (file)
@@ -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.
index d2d8b16..fb2afda 100644 (file)
@@ -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;
 }
 
index da5ce83..e127395 100644 (file)
@@ -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;
 }
 
index d37c5fc..4a69452 100644 (file)
@@ -48,7 +48,7 @@ int UtcDaliSyncImageLoaderLoad(void)
 {
   PixelData pixelData = Toolkit::SyncImageLoader::Load( gImage_50_RGBA );
 
-  DALI_TEST_EQUALS<bool>( pixelData, true, TEST_LOCATION );
+  DALI_TEST_CHECK( pixelData );
 
   END_TEST;
 }
@@ -70,5 +70,3 @@ int UtcDaliSyncImageLoaderLoadWithAllOptions(void)
 
   END_TEST;
 }
-
-
index aa2fdcc..6da9ee0 100755 (executable)
@@ -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;
 }
-
index 7251ad2..7a1d9f4 100644 (file)
@@ -79,7 +79,6 @@ template<typename T>
 class OptionalValue
 {
 public:
-  typedef void (OptionalValue::*bool_type)() const;
   typedef typename OptionalTypes<T>::ReturnType ReturnType;
   typedef typename OptionalTypes<T>::ValueType  ValueType;
 
@@ -104,18 +103,14 @@ public:
     return OptionalTypes<T>::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<typename T, typename U>
index ba3daae..e3de638 100644 (file)
@@ -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.
index c4e8ce2..6da7915 100644 (file)
@@ -250,7 +250,7 @@ void Magnifier::InitializeRenderTask()
 
 bool Magnifier::GetFrameVisibility() const
 {
-  return mFrame;
+  return static_cast<bool>(mFrame);
 }
 
 void Magnifier::SetFrameVisibility(bool visible)
index 273f08b..aed445d 100644 (file)
@@ -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.