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 523bd8ce2226d65b73e98c27ee37c5994dfaa69c..299a0e1a0bf79dd08ba2486e887795c39cc10236 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 c6e63e7e47116861c3c9f1664c1e64201590530c..a070298379f9dda36146c5259818e9fe588118b7 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 d2d8b161b9a14bafb49ee24ed5bd6b667bd26824..fb2afdaa63ac29f92cc09a3a5db97dbbc63f1611 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 da5ce83797ba1f559585c25e05794b7d8f0769f9..e1273952bf039c5f02226729522ab3e48b4d76e3 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 d37c5fc2337f579f7cf668875be43f499309aba2..4a694526e4857ceb59c2656333a5837f3d2fff9e 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 aa2fdcc03a204f626ad805923b7c3b59db5974c1..6da9ee0d26c773a7f83d577f190d7e4e9b2ffa38 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 7251ad253293afe0300f1a898f2d0dd61a95d31c..7a1d9f4be67b78fc24b62be5c51a4053d76eda8f 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 ba3daae3ae65dcde5eedb2e2af929529cfbe1e44..e3de6381180614eb9f8f11afcb9f372fd4b1d589 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 c4e8ce2bcd99c7c7b9192b315d61e2c1c596230a..6da7915048d68fae9aa9014279b1fb8ec545a632 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 273f08b682d11c14440524f51de3c4ef89cf8874..aed445d42a8dc112a57eab5bb7b112df82266138 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.