Make template class Dali::IntPair 75/282675/13
authorEunki, Hong <eunkiki.hong@samsung.com>
Sat, 8 Oct 2022 07:32:02 +0000 (16:32 +0900)
committerEunki, Hong <eunkiki.hong@samsung.com>
Fri, 14 Oct 2022 05:44:09 +0000 (14:44 +0900)
Make integer pair case so we can use
integer based pair struct.

Dali::Uint16Pair already used for a long time.
But now, we need Dali::Int32Pair to use signed case.

uint-16-pair.h is used on third party app, we cannot
remove this file yet.
So, Let we define Uint16Pair in that class.

Change-Id: I24055b8f70c3948a51a0147786725f192ade8990
Signed-off-by: Eunki, Hong <eunkiki.hong@samsung.com>
automated-tests/src/dali/CMakeLists.txt
automated-tests/src/dali/utc-Dali-IntPair.cpp [moved from automated-tests/src/dali/utc-Dali-Uint16Pair.cpp with 66% similarity]
dali/public-api/file.list
dali/public-api/math/int-pair.h [new file with mode: 0644]
dali/public-api/math/uint-16-pair.h

index 7c0ff3f..0baf3ec 100644 (file)
@@ -38,6 +38,7 @@ SET(TC_SOURCES
         utc-Dali-Hash.cpp
         utc-Dali-HitTestAlgorithm.cpp
         utc-Dali-HoverProcessing.cpp
+        utc-Dali-IntPair.cpp
         utc-Dali-IntrusivePtr.cpp
         utc-Dali-KeyEvent.cpp
         utc-Dali-Layer.cpp
@@ -94,7 +95,6 @@ SET(TC_SOURCES
         utc-Dali-TouchProcessing.cpp
         utc-Dali-TypeRegistry.cpp
         utc-Dali-CSharp-TypeRegistry.cpp
-        utc-Dali-Uint16Pair.cpp
         utc-Dali-Vector.cpp
         utc-Dali-Vector2.cpp
         utc-Dali-Vector3.cpp
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2020 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2022 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.
@@ -32,6 +32,8 @@ void DALI_TEST_EQUALS(uint16_t value1, unsigned int value2, const char* location
 }
 } // unnamed namespace
 
+// class Uint16Pair
+
 int UtcDaliUint16PairConstructor01P(void)
 {
   Uint16Pair v;
@@ -252,3 +254,135 @@ int UtcDaliUint16PairFromFloatArrayP(void)
 
   END_TEST;
 }
+
+// class Int32Pair
+
+int UtcDaliInt32PairConstructor01P(void)
+{
+  Int32Pair v;
+
+  DALI_TEST_EQUALS(v.GetX(), 0, TEST_LOCATION);
+  DALI_TEST_EQUALS(v.GetY(), 0, TEST_LOCATION);
+
+  END_TEST;
+}
+
+int UtcDaliInt32PairConstructor02P(void)
+{
+  Int32Pair v(-10, 20);
+
+  DALI_TEST_EQUALS(v.GetX(), -10, TEST_LOCATION);
+  DALI_TEST_EQUALS(v.GetY(), 20, TEST_LOCATION);
+
+  END_TEST;
+}
+
+int UtcDaliInt32PairCopyConstructor(void)
+{
+  Int32Pair u(-5, -10);
+  Int32Pair v(u);
+  DALI_TEST_EQUALS(v.GetX(), -5, TEST_LOCATION);
+  DALI_TEST_EQUALS(v.GetY(), -10, TEST_LOCATION);
+
+  END_TEST;
+}
+
+int UtcDaliInt32PairMoveConstructor(void)
+{
+  Int32Pair u(5, -10);
+  Int32Pair v = std::move(u);
+  DALI_TEST_EQUALS(v.GetX(), 5, TEST_LOCATION);
+  DALI_TEST_EQUALS(v.GetY(), -10, TEST_LOCATION);
+
+  END_TEST;
+}
+
+int UtcDaliInt32PairCopyAssignment(void)
+{
+  Int32Pair u(5, 10);
+  Int32Pair v;
+  v = u;
+  DALI_TEST_EQUALS(v.GetX(), 5, TEST_LOCATION);
+  DALI_TEST_EQUALS(v.GetY(), 10, TEST_LOCATION);
+
+  END_TEST;
+}
+
+int UtcDaliInt32PairMoveAssignment(void)
+{
+  Int32Pair u(5, 10);
+  Int32Pair v;
+  v = std::move(u);
+  DALI_TEST_EQUALS(v.GetX(), 5, TEST_LOCATION);
+  DALI_TEST_EQUALS(v.GetY(), 10, TEST_LOCATION);
+
+  END_TEST;
+}
+
+int UtcDaliInt32PairGetXP(void)
+{
+  Int32Pair v(5, 10);
+  DALI_TEST_EQUALS(v.GetX(), 5, TEST_LOCATION);
+  DALI_TEST_EQUALS(v.GetWidth(), 5, TEST_LOCATION);
+
+  END_TEST;
+}
+
+int UtcDaliInt32PairGetYP(void)
+{
+  Int32Pair v(5, 10);
+  DALI_TEST_EQUALS(v.GetY(), 10, TEST_LOCATION);
+  DALI_TEST_EQUALS(v.GetHeight(), 10, TEST_LOCATION);
+
+  END_TEST;
+}
+
+int UtcDaliInt32PairSetXP(void)
+{
+  Int32Pair v(5, 10);
+  DALI_TEST_EQUALS(v.GetX(), 5, TEST_LOCATION);
+  v.SetX(10);
+  DALI_TEST_EQUALS(v.GetX(), 10, TEST_LOCATION);
+  v.SetWidth(65539);
+  DALI_TEST_EQUALS(v.GetWidth(), 65539, TEST_LOCATION);
+
+  END_TEST;
+}
+
+int UtcDaliInt32PairSetYP(void)
+{
+  Int32Pair v(5, 10);
+  DALI_TEST_EQUALS(v.GetY(), 10, TEST_LOCATION);
+  v.SetY(-5);
+  DALI_TEST_EQUALS(v.GetY(), -5, TEST_LOCATION);
+  v.SetHeight(65537);
+  DALI_TEST_EQUALS(v.GetHeight(), 65537, TEST_LOCATION);
+
+  END_TEST;
+}
+
+int UtcDaliInt32PairEqualsP(void)
+{
+  Int32Pair v(5, 5);
+  Int32Pair u(5, 5);
+  DALI_TEST_EQUALS(v == u, true, TEST_LOCATION);
+
+  v = Int32Pair(5, 4);
+  u = Int32Pair(5, 5);
+  DALI_TEST_EQUALS(v == u, false, TEST_LOCATION);
+
+  END_TEST;
+}
+
+int UtcDaliInt32PairNotEqualsP(void)
+{
+  Int32Pair v(5, 5);
+  Int32Pair u(5, 5);
+  DALI_TEST_EQUALS(v != u, false, TEST_LOCATION);
+
+  v = Int32Pair(5, 4);
+  u = Int32Pair(5, 5);
+  DALI_TEST_EQUALS(v != u, true, TEST_LOCATION);
+
+  END_TEST;
+}
index 33c8153..08b1cf5 100644 (file)
@@ -169,6 +169,7 @@ SET( public_api_core_math_header_files
   ${public_api_src_dir}/math/angle-axis.h
   ${public_api_src_dir}/math/compile-time-math.h
   ${public_api_src_dir}/math/degree.h
+  ${public_api_src_dir}/math/int-pair.h
   ${public_api_src_dir}/math/math-utils.h
   ${public_api_src_dir}/math/matrix.h
   ${public_api_src_dir}/math/matrix3.h
diff --git a/dali/public-api/math/int-pair.h b/dali/public-api/math/int-pair.h
new file mode 100644 (file)
index 0000000..bc87651
--- /dev/null
@@ -0,0 +1,296 @@
+#ifndef DALI_INT_PAIR_H
+#define DALI_INT_PAIR_H
+
+/*
+ * Copyright (c) 2022 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.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+// EXTERNAL INCLUDES
+#include <cstdint>
+#include <type_traits>
+
+// INTERNAL INCLUDES
+#include <dali/public-api/common/dali-common.h>
+#include <dali/public-api/common/type-traits.h>
+
+namespace Dali
+{
+/**
+ * @addtogroup dali_core_math
+ * @{
+ */
+
+/**
+ * @brief Simple class for passing around pairs of small integers.
+ *
+ * Use this for integer dimensions and points with limited range such as image
+ * sizes and pixel coordinates where a pair of floating point numbers is
+ * inefficient and illogical (i.e. the data is inherently integer).
+ *
+ * For example in IntPair<uint16_t>
+ * one of these can be passed in a single 32 bit integer register on
+ * common architectures.
+ *
+ * @SINCE_2_1.45
+ * @tparam Bits The number of bits. It should be 8, 16, or 32.
+ * @tparam IsSigned Determine whether we use signed integer or not.
+ */
+template<unsigned Bits, bool IsSigned, typename = std::enable_if_t<(Bits == 8 || Bits == 16 || Bits == 32)>>
+class IntPair
+{
+public:
+  // Type definition.
+
+  /**
+   * @brief Integer type for get/set access.
+   * @SINCE_2_1.45
+   */
+  using IntType = typename std::conditional<Bits == 8, typename std::conditional<IsSigned, int8_t, uint8_t>::type, typename std::conditional<Bits == 16, typename std::conditional<IsSigned, int16_t, uint16_t>::type, typename std::conditional<IsSigned, int32_t, uint32_t>::type>::type>::type;
+
+  /**
+   * @brief Unsigned integer type that will be stored.
+   * Signed integer bit shift is implementation-defined behaviour.
+   * To more safty works, we need to cast from IntType to UintType before store data.
+   * @SINCE_2_1.45
+   */
+  using UintType = typename std::conditional<Bits == 8, uint8_t, typename std::conditional<Bits == 16, uint16_t, uint32_t>::type>::type;
+
+  /**
+   * @brief 2-tuple stored integer type.
+   * @SINCE_2_1.45
+   */
+  using DataType = typename std::conditional<Bits == 8, uint16_t, typename std::conditional<Bits == 16, uint32_t, uint64_t>::type>::type;
+
+public:
+  /**
+   * @brief Default constructor for the (0, 0) tuple.
+   * @SINCE_2_1.45
+   */
+  constexpr IntPair()
+  : mData(0)
+  {
+  }
+
+  /**
+   * @brief Constructor taking separate x and y (width and height) parameters.
+   * @SINCE_2_1.45
+   * @param[in] width The width or X dimension of the tuple.
+   * @param[in] height The height or Y dimension of the tuple.
+   */
+  constexpr IntPair(IntType width, IntType height)
+  {
+    /* Do equivalent of the code below with one aligned memory access:
+     * mComponents[0] = width;
+     * mComponents[1] = height;
+     * Unit tests make sure this is equivalent.
+     **/
+    mData = (static_cast<DataType>(static_cast<UintType>(height)) << Bits) | static_cast<DataType>(static_cast<UintType>(width));
+  }
+
+  /**
+   * @brief Sets the width.
+   * @SINCE_2_1.45
+   * @param[in] width The x dimension to be stored in this 2-tuple
+   */
+  void SetWidth(IntType width)
+  {
+    mComponents[0] = width;
+  }
+
+  /**
+   * @brief Get the width.
+   * @SINCE_2_1.45
+   * @return the x dimension stored in this 2-tuple
+   */
+  IntType GetWidth() const
+  {
+    return mComponents[0];
+  }
+
+  /**
+   * @brief Sets the height.
+   * @SINCE_2_1.45
+   * @param[in] height The y dimension to be stored in this 2-tuple
+   */
+  void SetHeight(IntType height)
+  {
+    mComponents[1] = height;
+  }
+
+  /**
+   * @brief Returns the y dimension stored in this 2-tuple.
+   * @SINCE_2_1.45
+   * @return Height
+   */
+  IntType GetHeight() const
+  {
+    return mComponents[1];
+  }
+
+  /**
+   * @brief Sets the x dimension (same as width).
+   * @SINCE_2_1.45
+   * @param[in] x The x dimension to be stored in this 2-tuple
+   */
+  void SetX(IntType x)
+  {
+    mComponents[0] = x;
+  }
+
+  /**
+   * @brief Returns the x dimension stored in this 2-tuple.
+   * @SINCE_2_1.45
+   * @return X
+   */
+  IntType GetX() const
+  {
+    return mComponents[0];
+  }
+
+  /**
+   * @brief Sets the y dimension (same as height).
+   * @SINCE_2_1.45
+   * @param[in] y The y dimension to be stored in this 2-tuple
+   */
+  void SetY(IntType y)
+  {
+    mComponents[1] = y;
+  }
+
+  /**
+   * @brief Returns the y dimension stored in this 2-tuple.
+   * @SINCE_2_1.45
+   * @return Y
+   */
+  IntType GetY() const
+  {
+    return mComponents[1];
+  }
+
+  /**
+   * @brief Equality operator.
+   * @SINCE_2_1.45
+   * @param[in] rhs A reference for comparison
+   * @return True if same
+   */
+  bool operator==(const IntPair& rhs) const
+  {
+    return mData == rhs.mData;
+  }
+
+  /**
+   * @brief Inequality operator.
+   * @SINCE_2_1.45
+   * @param[in] rhs A reference for comparison
+   * @return True if different
+   */
+  bool operator!=(const IntPair& rhs) const
+  {
+    return mData != rhs.mData;
+  }
+
+  /**
+   * @brief Less than comparison operator for storing in collections (not geometrically
+   * meaningful).
+   * @SINCE_2_1.45
+   * @param[in] rhs A reference for comparison
+   * @return True if less
+   */
+  bool operator<(const IntPair& rhs) const
+  {
+    return mData < rhs.mData;
+  }
+
+  /**
+   * @brief Greater than comparison operator for storing in collections (not
+   * geometrically meaningful).
+   * @SINCE_2_1.45
+   * @param[in] rhs A reference for comparison
+   * @return True if greater
+   */
+  bool operator>(const IntPair& rhs) const
+  {
+    return mData > rhs.mData;
+  }
+
+public:
+  IntPair(const IntPair&)     = default;            ///< Default copy constructor
+  IntPair(IntPair&&) noexcept = default;            ///< Default move constructor
+  IntPair& operator=(const IntPair&) = default;     ///< Default copy assignment operator
+  IntPair& operator=(IntPair&&) noexcept = default; ///< Default move assignment operator
+
+private:
+  union
+  {
+    // Addressable view of X and Y:
+    IntType mComponents[2];
+    // Packed view of X and Y to force alignment and allow a faster copy:
+    DataType mData;
+  };
+};
+
+/**
+ * @brief Simple class for passing around pairs of signed integers.
+ *
+ * Use this for integer dimensions and points with limited range such as window
+ * position and screen coordinates where a pair of floating point numbers is
+ * inefficient and illogical (i.e. the data is inherently integer).
+ * One of these can be passed in a single 64 bit integer.
+ * @SINCE_2_1.45
+ */
+class Int32Pair : public IntPair<32, true>
+{
+public:
+  /**
+   * @brief Default constructor for the (0, 0) tuple.
+   * @SINCE_2_1.45
+   */
+  constexpr Int32Pair() = default;
+
+  /**
+   * @brief Constructor taking separate x and y (width and height) parameters.
+   * @SINCE_2_1.45
+   * @param[in] x The width or X dimension of the tuple.
+   * @param[in] y The height or Y dimension of the tuple.
+   */
+  constexpr Int32Pair(uint32_t x, uint32_t y)
+  : IntPair(x, y)
+  {
+  }
+
+public:
+  Int32Pair(const Int32Pair&)     = default;            ///< Default copy constructor
+  Int32Pair(Int32Pair&&) noexcept = default;            ///< Default move constructor
+  Int32Pair& operator=(const Int32Pair&) = default;     ///< Default copy assignment operator
+  Int32Pair& operator=(Int32Pair&&) noexcept = default; ///< Default move assignment operator
+};
+
+// Allow Int32Pair to be treated as a POD type
+template<>
+struct TypeTraits<Int32Pair> : public BasicTypes<Int32Pair>
+{
+  enum
+  {
+    IS_TRIVIAL_TYPE = true
+  };
+};
+
+/**
+ * @}
+ */
+} // namespace Dali
+
+#endif // DALI_INT_PAIR_H
index e3d9f49..4b93c3d 100644 (file)
@@ -2,7 +2,7 @@
 #define DALI_UINT_16_PAIR_H
 
 /*
- * Copyright (c) 2020 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2022 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.
  *
  */
 
-// EXTERNAL INCLUDES
-#include <stdint.h>
-
 // INTERNAL INCLUDES
-#include <dali/public-api/common/dali-common.h>
-#include <dali/public-api/common/type-traits.h>
+#include <dali/public-api/math/int-pair.h>
 
 namespace Dali
 {
@@ -42,161 +38,26 @@ namespace Dali
  * common architectures.
  * @SINCE_1_0.0
  */
-class Uint16Pair
+class Uint16Pair : public IntPair<16, false>
 {
 public:
   /**
    * @brief Default constructor for the (0, 0) tuple.
    * @SINCE_1_0.0
    */
-  Uint16Pair()
-  : mData(0)
-  {
-  }
+  constexpr Uint16Pair() = default;
 
   /**
    * @brief Constructor taking separate x and y (width and height) parameters.
    * @SINCE_1_0.0
-   * @param[in] width The width or X dimension of the tuple. Make sure it is less than 65536
-   * @param[in] height The height or Y dimension of the tuple. Make sure it is less than 65536
+   * @param[in] width The width or X dimension of the tuple. Make sure it is less than 65536.
+   * @param[in] height The height or Y dimension of the tuple. Make sure it is less than 65536.
    */
-  Uint16Pair(uint32_t width, uint32_t height)
+  constexpr Uint16Pair(uint32_t width, uint32_t height)
+  : IntPair(static_cast<uint16_t>(width), static_cast<uint16_t>(height))
   {
     DALI_ASSERT_DEBUG(width < (1u << 16) && "Width parameter not representable.");
     DALI_ASSERT_DEBUG(height < (1u << 16) && "Height parameter not representable.");
-
-    /* Do equivalent of the code below with one aligned memory access:
-     * mComponents[0] = width;
-     * mComponents[1] = height;
-     * Unit tests make sure this is equivalent.
-     **/
-    mData = (height << 16u) + width;
-  }
-
-  /**
-   * @brief Sets the width.
-   * @SINCE_1_1.13
-   * @param[in] width The x dimension to be stored in this 2-tuple
-   */
-  void SetWidth(uint16_t width)
-  {
-    mComponents[0] = width;
-  }
-
-  /**
-   * @brief Get the width.
-   * @SINCE_1_0.0
-   * @return the x dimension stored in this 2-tuple
-   */
-  uint16_t GetWidth() const
-  {
-    return mComponents[0];
-  }
-
-  /**
-   * @brief Sets the height.
-   * @SINCE_1_1.13
-   * @param[in] height The y dimension to be stored in this 2-tuple
-   */
-  void SetHeight(uint16_t height)
-  {
-    mComponents[1] = height;
-  }
-
-  /**
-   * @brief Returns the y dimension stored in this 2-tuple.
-   * @SINCE_1_0.0
-   * @return Height
-   */
-  uint16_t GetHeight() const
-  {
-    return mComponents[1];
-  }
-
-  /**
-   * @brief Sets the x dimension (same as width).
-   * @SINCE_1_1.14
-   * @param[in] x The x dimension to be stored in this 2-tuple
-   */
-  void SetX(uint16_t x)
-  {
-    mComponents[0] = x;
-  }
-
-  /**
-   * @brief Returns the x dimension stored in this 2-tuple.
-   * @SINCE_1_0.0
-   * @return X
-   */
-  uint16_t GetX() const
-  {
-    return mComponents[0];
-  }
-
-  /**
-   * @brief Sets the y dimension (same as height).
-   * @SINCE_1_1.14
-   * @param[in] y The y dimension to be stored in this 2-tuple
-   */
-  void SetY(uint16_t y)
-  {
-    mComponents[1] = y;
-  }
-
-  /**
-   * @brief Returns the y dimension stored in this 2-tuple.
-   * @SINCE_1_0.0
-   * @return Y
-   */
-  uint16_t GetY() const
-  {
-    return mComponents[1];
-  }
-
-  /**
-   * @brief Equality operator.
-   * @SINCE_1_0.0
-   * @param[in] rhs A reference for comparison
-   * @return True if same
-   */
-  bool operator==(const Uint16Pair& rhs) const
-  {
-    return mData == rhs.mData;
-  }
-
-  /**
-   * @brief Inequality operator.
-   * @SINCE_1_0.0
-   * @param[in] rhs A reference for comparison
-   * @return True if different
-   */
-  bool operator!=(const Uint16Pair& rhs) const
-  {
-    return mData != rhs.mData;
-  }
-
-  /**
-   * @brief Less than comparison operator for storing in collections (not geometrically
-   * meaningful).
-   * @SINCE_1_0.0
-   * @param[in] rhs A reference for comparison
-   * @return True if less
-   */
-  bool operator<(const Uint16Pair& rhs) const
-  {
-    return mData < rhs.mData;
-  }
-
-  /**
-   * @brief Greater than comparison operator for storing in collections (not
-   * geometrically meaningful).
-   * @SINCE_1_0.0
-   * @param[in] rhs A reference for comparison
-   * @return True if greater
-   */
-  bool operator>(const Uint16Pair& rhs) const
-  {
-    return mData > rhs.mData;
   }
 
   /**
@@ -236,19 +97,10 @@ public:
   }
 
 public:
-  Uint16Pair(const Uint16Pair&) = default;            ///< Default copy constructor
-  Uint16Pair(Uint16Pair&&)      = default;            ///< Default move constructor
-  Uint16Pair& operator=(const Uint16Pair&) = default; ///< Default copy assignment operator
-  Uint16Pair& operator=(Uint16Pair&&) = default;      ///< Default move assignment operator
-
-private:
-  union
-  {
-    // Addressable view of X and Y:
-    uint16_t mComponents[2];
-    // Packed view of X and Y to force alignment and allow a faster copy:
-    uint32_t mData;
-  };
+  Uint16Pair(const Uint16Pair&)     = default;            ///< Default copy constructor
+  Uint16Pair(Uint16Pair&&) noexcept = default;            ///< Default move constructor
+  Uint16Pair& operator=(const Uint16Pair&) = default;     ///< Default copy assignment operator
+  Uint16Pair& operator=(Uint16Pair&&) noexcept = default; ///< Default move assignment operator
 };
 
 // Allow Uint16Pair to be treated as a POD type