[3.0] Update doxygen tags
[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) 2015 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 // EXTERNAL INCLUDES
22 #include <stdint.h>
23
24 // INTERNAL INCLUDES
25 #include <dali/public-api/common/dali-common.h>
26 #include <dali/public-api/common/type-traits.h>
27
28 namespace Dali
29 {
30 /**
31  * @addtogroup dali_core_math
32  * @{
33  */
34
35 /**
36  * @brief Simple class for passing around pairs of small unsigned integers.
37  *
38  * Use this for integer dimensions and points with limited range such as image
39  * sizes and pixel coordinates where a pair of floating point numbers is
40  * inefficient and illogical (i.e. the data is inherently integer).
41  * One of these can be passed in a single 32 bit integer register on
42  * common architectures.
43  * @SINCE_1_0.0
44  */
45 class Uint16Pair
46 {
47 public:
48   /**
49    * @brief Default constructor for the (0, 0) tuple.
50    * @SINCE_1_0.0
51    */
52   Uint16Pair() : mData(0) {}
53
54   /**
55    * @brief Constructor taking separate x and y (width and height) parameters.
56    * @SINCE_1_0.0
57    * @param[in] width The width or X dimension of the tuple. Make sure it is less than 65536,
58    * @param[in] height The height or Y dimension of the tuple. Make sure it is less than 65536,
59    */
60   Uint16Pair( uint32_t width, uint32_t height )
61   {
62     DALI_ASSERT_DEBUG( width < ( 1u << 16 ) && "Width parameter not representable." );
63     DALI_ASSERT_DEBUG( height < ( 1u << 16 ) && "Height parameter not representable." );
64
65     /* Do equivalent of the code below with one aligned memory access:
66      * mComponents[0] = width;
67      * mComponents[1] = height;
68      * Unit tests make sure this is equivalent.
69      **/
70     mData = (height << 16u) + width;
71   }
72
73   /**
74    * @brief Copy constructor.
75    * @SINCE_1_0.0
76    * @param[in] rhs A reference to assign
77    */
78   Uint16Pair( const Uint16Pair& rhs )
79   {
80     mData = rhs.mData;
81   }
82
83   /**
84    * @brief Sets the width.
85    * @SINCE_1_1.13
86    * @param[in] width The x dimension to be stored in this 2-tuple.
87    */
88   void SetWidth( uint16_t width )
89   {
90     mComponents[0] = width;
91   }
92
93   /**
94    * @brief Get the width.
95    * @SINCE_1_0.0
96    * @return the x dimension stored in this 2-tuple
97    */
98   uint16_t GetWidth() const
99   {
100     return mComponents[0];
101   }
102
103   /**
104    * @brief Sets the height.
105    * @SINCE_1_1.13
106    * @param[in] height The y dimension to be stored in this 2-tuple.
107    */
108   void SetHeight( uint16_t height )
109   {
110     mComponents[1] = height;
111   }
112
113   /**
114    * @brief Returns the y dimension stored in this 2-tuple.
115    * @SINCE_1_0.0
116    * @return Height
117    */
118   uint16_t GetHeight() const
119   {
120     return mComponents[1];
121   }
122
123   /**
124    * @brief Sets the x dimension (same as width).
125    * @SINCE_1_1.14
126    * @param[in] x The x dimension to be stored in this 2-tuple.
127    */
128   void SetX( uint16_t x )
129   {
130     mComponents[0] = x;
131   }
132
133   /**
134    * @brief Returns the x dimension stored in this 2-tuple.
135    * @SINCE_1_0.0
136    * @return X
137    */
138   uint16_t GetX()  const
139   {
140     return mComponents[0];
141   }
142
143   /**
144    * @brief Sets the y dimension (same as height).
145    * @SINCE_1_1.14
146    * @param[in] y The y dimension to be stored in this 2-tuple.
147    */
148   void SetY( uint16_t y )
149   {
150     mComponents[1] = y;
151   }
152
153   /**
154    * @brief Returns the y dimension stored in this 2-tuple.
155    * @SINCE_1_0.0
156    * @return Y
157    */
158   uint16_t GetY() const
159   {
160     return mComponents[1];
161   }
162
163   /**
164    * @brief Assignment operator.
165    * @SINCE_1_0.0
166    * @param[in] rhs Handle to an object
167    * @return A reference to this
168    */
169   Uint16Pair& operator=( const Uint16Pair& rhs )
170   {
171     if( rhs != *this )
172     {
173       mData = rhs.mData;
174     }
175     return *this;
176   }
177
178   /**
179    * @brief Equality operator.
180    * @SINCE_1_0.0
181    * @param[in] rhs A reference for comparison
182    * @return True if same
183    */
184   bool operator==( const Uint16Pair& rhs ) const
185   {
186     return mData == rhs.mData;
187   }
188
189   /**
190    * @brief Inequality operator.
191    * @SINCE_1_0.0
192    * @param[in] rhs A reference for comparison
193    * @return True if different
194    */
195   bool operator!=( const Uint16Pair& rhs ) const
196   {
197     return mData != rhs.mData;
198   }
199
200   /**
201    * @brief Less than comparison operator for storing in collections (not geometrically
202    * meaningful).
203    * @SINCE_1_0.0
204    * @param[in] rhs A reference for comparison
205    * @return True if less
206    */
207   bool operator<( const Uint16Pair& rhs ) const
208   {
209     return mData < rhs.mData;
210   }
211
212   /**
213    * @brief Greater than comparison operator for storing in collections (not
214    * geometrically meaningful).
215    * @SINCE_1_0.0
216    * @param[in] rhs A reference for comparison
217    * @return True if greater
218    */
219   bool operator>( const Uint16Pair& rhs ) const
220   {
221     return mData > rhs.mData;
222   }
223
224   /**
225    * @brief Create an instance by rounding a floating point vector to closest
226    * integers.
227    *
228    * Uses a template for loose coupling, to save a header include, and allow any
229    * vector type with .x and .y members to be converted.
230    * @SINCE_1_0.0
231    * @param[in] from Floating point vector2
232    * @return Closest integer value.
233    */
234   template<typename FLOAT_VECTOR_N_TYPE>
235   static Uint16Pair FromFloatVec2( const FLOAT_VECTOR_N_TYPE& from )
236   {
237     DALI_ASSERT_DEBUG( from.x + 0.5f < 65536.0f );
238     DALI_ASSERT_DEBUG( from.y + 0.5f < 65536.0f );
239     return Uint16Pair( from.x + 0.5f, from.y + 0.5f );
240   }
241
242   /**
243    * @brief Create an instance by rounding a floating point array to closest
244    * integers.
245    *
246    * Uses a template to allow any vector type with operator [] to be converted
247    * in addition to plain arrays.
248    * @SINCE_1_0.0
249    * @param[in] from Floating point array
250    * @return Closest integer value.
251    */
252   template<typename FLOAT_ARRAY>
253   static Uint16Pair FromFloatArray( const FLOAT_ARRAY& from )
254   {
255     DALI_ASSERT_DEBUG( from[0] + 0.5f < 65536.0f );
256     DALI_ASSERT_DEBUG( from[1] + 0.5f < 65536.0f );
257     return Uint16Pair( from[0] + 0.5f, from[1] + 0.5f );
258   }
259
260 private:
261   union
262   {
263     // Addressable view of X and Y:
264     uint16_t mComponents[2];
265     // Packed view of X and Y to force alignment and allow a faster copy:
266     uint32_t mData;
267   };
268 };
269
270 // Allow Uint16Pair to be treated as a POD type
271 template <> struct TypeTraits< Uint16Pair > : public BasicTypes< Uint16Pair > { enum { IS_TRIVIAL_TYPE = true }; };
272
273 /**
274  * @}
275  */
276 } // namespace Dali
277
278 #endif // __DALI_UINT_16_PAIR_H__