Tizen 2.4.0 rev3 SDK Public Release
[framework/graphics/dali.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
27 namespace Dali
28 {
29 /**
30  * @addtogroup dali_core_math
31  * @{
32  */
33
34 /**
35  * @brief Simple class for passing around pairs of small unsigned integers.
36  *
37  * Use this for integer dimensions and points with limited range such as image
38  * sizes and pixel coordinates where a pair of floating point numbers is
39  * inefficient and illogical (i.e. the data is inherently integer).
40  * These are immutable. If you want to change a value, make a whole new object.
41  * One of these can be passed in a single 32 bit integer register on
42  * common architectures.
43  * @since_tizen 2.4
44  */
45 class Uint16Pair
46 {
47 public:
48   /**
49    * @brief Default constructor for the (0, 0) vector.
50    * @since_tizen 2.4
51    */
52   Uint16Pair() : mData(0) {}
53
54   /**
55    * @brief Constructor taking separate x and y (width and height) parameters.
56    * @since_tizen 2.4
57    * @param[in] width The width or X dimension of the vector. Make sure it is less than 65536,
58    * @param[in] height The height or Y dimension of the vector. 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_tizen 2.4
76    * @param[in] rhs A reference to the copied Uint16Pair
77    */
78   Uint16Pair( const Uint16Pair& rhs )
79   {
80     mData = rhs.mData;
81   }
82
83   /**
84    * @brief @returns the x dimension stored in this 2-tuple.
85    * @since_tizen 2.4
86    */
87   uint16_t GetWidth() const
88   {
89     return mComponents[0];
90   }
91
92   /**
93    * @brief Returns the y dimension stored in this 2-tuple.
94    * @since_tizen 2.4
95    * @return Height
96    */
97   uint16_t GetHeight() const
98   {
99     return mComponents[1];
100   }
101
102   /**
103    * @brief Returns the x dimension stored in this 2-tuple.
104    * @since_tizen 2.4
105    * @return X
106    */
107   uint16_t GetX()  const
108   {
109     return mComponents[0];
110   }
111
112   /**
113    * @brief Returns the y dimension stored in this 2-tuple.
114    * @since_tizen 2.4
115    * @return Y
116    */
117   uint16_t GetY() const
118   {
119     return mComponents[1];
120   }
121
122   /**
123    * @brief Equality operator.
124    * @since_tizen 2.4
125    * @param[i] rhs A reference for comparison
126    * @return True if same
127    */
128   bool operator==( const Uint16Pair& rhs ) const
129   {
130     return mData == rhs.mData;
131   }
132
133   /**
134    * @brief Inequality operator.
135    * @since_tizen 2.4
136    * @param[i] rhs A reference for comparison
137    * @return True if different
138    */
139   bool operator!=( const Uint16Pair& rhs ) const
140   {
141     return mData != rhs.mData;
142   }
143
144   /**
145    * @brief Less than comparison operator for storing in collections (not geometrically
146    * meaningful).
147    * @since_tizen 2.4
148    * @param[i] rhs A reference for comparison
149    * @return True if less
150    */
151   bool operator<( const Uint16Pair& rhs ) const
152   {
153     return mData < rhs.mData;
154   }
155
156   /**
157    * @brief Greater than comparison operator for storing in collections (not
158    * geometrically meaningful).
159    * @since_tizen 2.4
160    * @param[i] rhs A reference for comparison
161    * @return True if greater
162    */
163   bool operator>( const Uint16Pair& rhs ) const
164   {
165     return mData > rhs.mData;
166   }
167
168   /**
169    * @brief Create an instance by rounding a floating point vector to closest
170    * integers.
171    *
172    * Uses a template for loose coupling, to save a header include, and allow any
173    * vector type with .x and .y members to be converted.
174    * @since_tizen 2.4
175    * @param[in] from Floating point vector2
176    * @return Closest integer value.
177    */
178   template<typename FLOAT_VECTOR_N_TYPE>
179   static Uint16Pair FromFloatVec2( const FLOAT_VECTOR_N_TYPE& from )
180   {
181     DALI_ASSERT_DEBUG( from.x + 0.5f < 65536.0f );
182     DALI_ASSERT_DEBUG( from.y + 0.5f < 65536.0f );
183     return Uint16Pair( from.x + 0.5f, from.y + 0.5f );
184   }
185
186   /**
187    * @brief Create an instance by rounding a floating point array to closest
188    * integers.
189    *
190    * Uses a template to allow any vector type with operator [] to be converted
191    * in addition to plain arrays.
192    * @since_tizen 2.4
193    * @param[in] from Floating point array
194    * @return Closest integer value.
195    */
196   template<typename FLOAT_ARRAY>
197   static Uint16Pair FromFloatArray( const FLOAT_ARRAY& from )
198   {
199     DALI_ASSERT_DEBUG( from[0] + 0.5f < 65536.0f );
200     DALI_ASSERT_DEBUG( from[1] + 0.5f < 65536.0f );
201     return Uint16Pair( from[0] + 0.5f, from[1] + 0.5f );
202   }
203
204 private:
205   union
206   {
207     // Addressable view of X and Y:
208     uint16_t mComponents[2];
209     // Packed view of X and Y to force alignment and allow a faster copy:
210     uint32_t mData;
211   };
212 };
213
214 /**
215  * @}
216  */
217 } // namespace Dali
218
219 #endif // __DALI_UINT_16_PAIR_H__