[dali_2.3.35] Merge branch 'devel/master'
[platform/core/uifw/dali-core.git] / dali / public-api / math / uint-16-pair.h
1 #ifndef DALI_UINT_16_PAIR_H
2 #define DALI_UINT_16_PAIR_H
3
4 /*
5  * Copyright (c) 2022 Samsung Electronics Co., Ltd.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  */
20
21 // INTERNAL INCLUDES
22 #include <dali/public-api/math/int-pair.h>
23
24 namespace Dali
25 {
26 /**
27  * @addtogroup dali_core_math
28  * @{
29  */
30
31 /**
32  * @brief Simple class for passing around pairs of small unsigned integers.
33  *
34  * Use this for integer dimensions and points with limited range such as image
35  * sizes and pixel coordinates where a pair of floating point numbers is
36  * inefficient and illogical (i.e. the data is inherently integer).
37  * One of these can be passed in a single 32 bit integer register on
38  * common architectures.
39  * @SINCE_1_0.0
40  */
41 class Uint16Pair : public IntPair<16, false>
42 {
43 public:
44   /**
45    * @brief Default constructor for the (0, 0) tuple.
46    * @SINCE_1_0.0
47    */
48   constexpr Uint16Pair() = default;
49
50   /**
51    * @brief Constructor taking separate x and y (width and height) parameters.
52    * @SINCE_1_0.0
53    * @param[in] width The width or X dimension of the tuple. Make sure it is less than 65536.
54    * @param[in] height The height or Y dimension of the tuple. Make sure it is less than 65536.
55    */
56   constexpr Uint16Pair(uint32_t width, uint32_t height)
57   : IntPair(static_cast<uint16_t>(width), static_cast<uint16_t>(height))
58   {
59     DALI_ASSERT_DEBUG(width < (1u << 16) && "Width parameter not representable.");
60     DALI_ASSERT_DEBUG(height < (1u << 16) && "Height parameter not representable.");
61   }
62
63   /**
64    * @brief Creates an instance by rounding a floating point vector to closest
65    * integers.
66    *
67    * Uses a template for loose coupling, to save a header include, and allow any
68    * vector type with .x and .y members to be converted.
69    * @SINCE_1_0.0
70    * @param[in] from Floating point vector2
71    * @return Closest integer value
72    */
73   template<typename FLOAT_VECTOR_N_TYPE>
74   static Uint16Pair FromFloatVec2(const FLOAT_VECTOR_N_TYPE& from)
75   {
76     DALI_ASSERT_DEBUG(from.x + 0.5f < 65536.0f);
77     DALI_ASSERT_DEBUG(from.y + 0.5f < 65536.0f);
78     return Uint16Pair(from.x + 0.5f, from.y + 0.5f);
79   }
80
81   /**
82    * @brief Creates an instance by rounding a floating point array to closest
83    * integers.
84    *
85    * Uses a template to allow any vector type with operator [] to be converted
86    * in addition to plain arrays.
87    * @SINCE_1_0.0
88    * @param[in] from Floating point array
89    * @return Closest integer value
90    */
91   template<typename FLOAT_ARRAY>
92   static Uint16Pair FromFloatArray(const FLOAT_ARRAY& from)
93   {
94     DALI_ASSERT_DEBUG(from[0] + 0.5f < 65536.0f);
95     DALI_ASSERT_DEBUG(from[1] + 0.5f < 65536.0f);
96     return Uint16Pair(from[0] + 0.5f, from[1] + 0.5f);
97   }
98
99 public:
100   Uint16Pair(const Uint16Pair&)     = default;            ///< Default copy constructor
101   Uint16Pair(Uint16Pair&&) noexcept = default;            ///< Default move constructor
102   Uint16Pair& operator=(const Uint16Pair&) = default;     ///< Default copy assignment operator
103   Uint16Pair& operator=(Uint16Pair&&) noexcept = default; ///< Default move assignment operator
104 };
105
106 // Allow Uint16Pair to be treated as a POD type
107 template<>
108 struct TypeTraits<Uint16Pair> : public BasicTypes<Uint16Pair>
109 {
110   enum
111   {
112     IS_TRIVIAL_TYPE = true
113   };
114 };
115
116 /**
117  * @}
118  */
119 } // namespace Dali
120
121 #endif // DALI_UINT_16_PAIR_H