Allow to compare IntrusivePtr comparision 60/315460/1
authorEunki, Hong <eunkiki.hong@samsung.com>
Thu, 1 Aug 2024 07:00:16 +0000 (16:00 +0900)
committerEunki, Hong <eunkiki.hong@samsung.com>
Thu, 1 Aug 2024 07:27:06 +0000 (16:27 +0900)
Let we allow to compare IntrusivePtr operator<.

After this patch, we can use std::set<IntrusivePtr<T>> or map, without
custom comparisoin functor.

Change-Id: Ifce5ad1fe951837d21766daba4cda1546c263262
Signed-off-by: Eunki, Hong <eunkiki.hong@samsung.com>
automated-tests/src/dali/utc-Dali-IntrusivePtr.cpp
dali/public-api/common/intrusive-ptr.h

index 9e838a7..ccd5f8d 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2022 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2024 Samsung Electronics Co., Ltd.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -632,3 +632,78 @@ int UtcDaliIntrusivePtrOperatorEqualWithNullptr(void)
 
   END_TEST;
 }
+
+/** Comparision of two different types*/
+int UtcDaliIntrusivePtrOperatorLessTU(void)
+{
+  tet_infoline("Test for Dali::IntrusivePtr::operator <(T, U)");
+
+  IntrusivePtr<Counted>         counted1(new Counted);
+  IntrusivePtr<CountedSubclass> countedSubclass1(new CountedSubclass);
+  IntrusivePtr<CountedSubclass> countedSubclass2(new CountedSubclass);
+  IntrusivePtr<Counted>         counted2(countedSubclass2);
+
+  const bool expectResult = counted1.Get() < countedSubclass1.Get();
+
+  DALI_TEST_EQUALS(operator<(counted1, countedSubclass1), expectResult, TEST_LOCATION);
+  DALI_TEST_EQUALS(operator<(countedSubclass1, counted1), !expectResult, TEST_LOCATION);
+  DALI_TEST_EQUALS(operator<(counted2, countedSubclass2), false, TEST_LOCATION);
+  DALI_TEST_EQUALS(operator<(countedSubclass2, counted2), false, TEST_LOCATION);
+  END_TEST;
+}
+
+int UtcDaliIntrusivePtrOperatorLessRightPointerTU(void)
+{
+  tet_infoline("Test for Dali::IntrusivePtr::operator <(T, U*)");
+
+  IntrusivePtr<Counted>         counted1(new Counted);
+  IntrusivePtr<CountedSubclass> countedSubclass1(new CountedSubclass);
+  IntrusivePtr<CountedSubclass> countedSubclass2(new CountedSubclass);
+  IntrusivePtr<Counted>         counted2(countedSubclass2);
+
+  const bool expectResult = counted1.Get() < countedSubclass1.Get();
+
+  DALI_TEST_EQUALS(operator<(counted1, countedSubclass1.Get()), expectResult, TEST_LOCATION);
+  DALI_TEST_EQUALS(operator<(countedSubclass1, counted1.Get()), !expectResult, TEST_LOCATION);
+  DALI_TEST_EQUALS(operator<(counted2, countedSubclass2.Get()), false, TEST_LOCATION);
+  DALI_TEST_EQUALS(operator<(countedSubclass2, counted2.Get()), false, TEST_LOCATION);
+  END_TEST;
+}
+
+int UtcDaliIntrusivePtrOperatorLessLeftPointerTU(void)
+{
+  tet_infoline("Test for Dali::IntrusivePtr::operator <(T*, U)");
+
+  IntrusivePtr<Counted>         counted1(new Counted);
+  IntrusivePtr<CountedSubclass> countedSubclass1(new CountedSubclass);
+  IntrusivePtr<CountedSubclass> countedSubclass2(new CountedSubclass);
+  IntrusivePtr<Counted>         counted2(countedSubclass2);
+
+  const bool expectResult = counted1.Get() < countedSubclass1.Get();
+
+  DALI_TEST_EQUALS(operator<(counted1.Get(), countedSubclass1), expectResult, TEST_LOCATION);
+  DALI_TEST_EQUALS(operator<(countedSubclass1.Get(), counted1), !expectResult, TEST_LOCATION);
+  DALI_TEST_EQUALS(operator<(counted2.Get(), countedSubclass2), false, TEST_LOCATION);
+  DALI_TEST_EQUALS(operator<(countedSubclass2.Get(), counted2), false, TEST_LOCATION);
+  END_TEST;
+}
+
+/** Comparision with nullptr */
+int UtcDaliIntrusivePtrOperatorLessWithNullptr(void)
+{
+  tet_infoline("Test for Dali::IntrusivePtr::operator < nullptr");
+
+  IntrusivePtr<Counted> counted1(new Counted);
+
+  // counted1 is not nullptr.
+  DALI_TEST_CHECK(!(counted1 < nullptr));
+  DALI_TEST_CHECK(nullptr < counted1);
+
+  counted1 = nullptr;
+
+  // counted1 is nullptr.
+  DALI_TEST_CHECK(!(counted1 < nullptr));
+  DALI_TEST_CHECK(!(nullptr < counted1));
+
+  END_TEST;
+}
\ No newline at end of file
index af391f8..6a43d80 100644 (file)
@@ -2,7 +2,7 @@
 #define DALI_INTRUSIVE_PTR_H
 
 /*
- * Copyright (c) 2022 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2024 Samsung Electronics Co., Ltd.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -437,6 +437,76 @@ inline bool operator!=(std::nullptr_t lhs, IntrusivePtr<T> const& rhs)
 }
 
 /**
+ * @brief Comparison overrides of objects wrapped by intrusive pointers.
+ *
+ * @SINCE_2_3.35
+ * @param[in] lhs Intrusive pointer to compare with
+ * @param[in] rhs Intrusive pointer to compare against
+ * @return True if the pointers point of rhs is bigger than lhs
+ */
+template<typename T, typename U>
+inline bool operator<(IntrusivePtr<T> const& lhs, IntrusivePtr<U> const& rhs)
+{
+  return lhs.Get() < rhs.Get();
+}
+
+/**
+ * @brief Comparison overrides of objects wrapped by intrusive pointers.
+ *
+ * @SINCE_2_3.35
+ * @param[in] lhs Object to compare with
+ * @param[in] rhs Intrusive pointer to compare against
+ * @return True if the pointers point of rhs is bigger than lhs
+ */
+template<typename T, typename U>
+inline bool operator<(T* lhs, IntrusivePtr<U> const& rhs)
+{
+  return lhs < rhs.Get();
+}
+
+/**
+ * @brief Comparison overrides of objects wrapped by intrusive pointers.
+ *
+ * @SINCE_2_3.35
+ * @param[in] lhs Intrusive pointer to compare with
+ * @param[in] rhs Object to compare against
+ * @return True if the pointers point of rhs is bigger than lhs
+ */
+template<typename T, typename U>
+inline bool operator<(IntrusivePtr<T> const& lhs, U* rhs)
+{
+  return lhs.Get() < rhs;
+}
+
+/**
+ * @brief Comparison overrides of objects wrapped by intrusive pointers.
+ *
+ * @SINCE_2_3.35
+ * @param[in] lhs nullptr
+ * @param[in] rhs Intrusive pointer to compare against
+ * @return True if the pointers point of rhs is bigger than lhs
+ */
+template<typename T>
+inline bool operator<(std::nullptr_t lhs, IntrusivePtr<T> const& rhs)
+{
+  return nullptr < rhs.Get();
+}
+
+/**
+ * @brief Comparison overrides of objects wrapped by intrusive pointers.
+ *
+ * @SINCE_2_3.35
+ * @param[in] lhs Intrusive pointer to compare with
+ * @param[in] rhs nullptr
+ * @return True if the pointers point of rhs is bigger than lhs
+ */
+template<typename T>
+inline bool operator<(IntrusivePtr<T> const& lhs, std::nullptr_t rhs)
+{
+  return lhs.Get() < nullptr;
+}
+
+/**
  * @}
  */
 } // namespace Dali