889f37182a8deb0654c1666f17d46db4567a3b50
[platform/core/location/maps-plugin-here.git] / inc / engine / graphic / Grp_UtilType.h
1 //
2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
3 //
4 // Licensed under the Apache License, Version 2.0 (the License);
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //     http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16
17 #ifndef HERE_GRAPHIC_UTILTYPE_H
18 #define HERE_GRAPHIC_UTILTYPE_H
19
20 #include "common/HereMaps_global.h"
21
22 TIZEN_MAPS_BEGIN_NAMESPACE
23
24 namespace _Util
25 {
26
27 template <unsigned int preFixed, unsigned int postFixed>
28 struct FixedPoint
29 {
30         FixedPoint(void)
31                 : __fixedPoint(0)
32         {
33         }
34
35         FixedPoint(const FixedPoint& rhs)
36                 : __fixedPoint(rhs.__fixedPoint)
37         {
38         }
39
40         explicit FixedPoint(int value)
41                 : __fixedPoint(value << SHIFT)
42         {
43         }
44
45         explicit FixedPoint(long value)
46                 : __fixedPoint(value << SHIFT)
47         {
48         }
49
50         explicit FixedPoint(float value)
51                 : __fixedPoint(value * ((1 << SHIFT) * 1.0f))
52         {
53         }
54
55         explicit FixedPoint(double value)
56                 : __fixedPoint(value * ((1 << SHIFT) * 1.0))
57         {
58         }
59
60         inline int
61         ToInt() const
62         {
63                 return (__fixedPoint >> SHIFT);
64         }
65
66         inline float
67         ToFloat() const
68         {
69                 return (__fixedPoint / ((1 << SHIFT) * 1.0f));
70         }
71
72         inline double
73         ToDouble() const
74         {
75                 return (__fixedPoint / ((1 << SHIFT) * 1.0));
76         }
77
78         inline void
79         Reset(int fixedPointValue)
80         {
81                 __fixedPoint = fixedPointValue;
82         }
83
84         inline operator const int(void)
85         {
86                 return __fixedPoint;
87         }
88
89 // operator +
90         inline FixedPoint
91         operator +(const FixedPoint& rhs) const
92         {
93                 FixedPoint<preFixed, SHIFT> tempFixed(0);
94
95                 tempFixed.__fixedPoint = __fixedPoint + rhs.__fixedPoint;
96
97                 return tempFixed;
98         }
99
100 // operator -
101         inline FixedPoint
102         operator -(const FixedPoint& rhs) const
103         {
104                 FixedPoint<preFixed, SHIFT> tempFixed(0);
105
106                 tempFixed.__fixedPoint = __fixedPoint - rhs.__fixedPoint;
107
108                 return tempFixed;
109         }
110
111 // operator ==
112         template <typename T>
113         inline bool
114         operator ==(const T& rhs) const
115         {
116                 return (ToFloat() == rhs);
117         }
118
119         inline bool
120         operator ==(const FixedPoint& rhs) const
121         {
122                 return (ToFloat() == rhs.ToFloat);
123         }
124
125 // operator !=
126         template <typename T>
127         inline bool
128         operator !=(const T& rhs) const
129         {
130                 return (ToFloat() != rhs);
131         }
132
133         inline bool
134         operator !=(const FixedPoint& rhs) const
135         {
136                 return (ToFloat() != rhs.ToFloat());
137         }
138
139 // operator <=
140         template <typename T>
141         inline bool
142         operator <=(const T& rhs) const
143         {
144                 return (ToFloat() <= rhs);
145         }
146
147         inline bool
148         operator <=(const FixedPoint& rhs) const
149         {
150                 return (ToFloat() <= rhs.ToFloat());
151         }
152
153 // operator >=
154         template <typename T>
155         inline bool
156         operator >=(const T& rhs) const
157         {
158                 return (ToFloat() >= rhs);
159         }
160
161         inline bool
162         operator >=(const FixedPoint& rhs) const
163         {
164                 return (ToFloat() >= rhs.ToFloat());
165         }
166
167 // operator >
168         template <typename T>
169         inline bool
170         operator >(const T& rhs) const
171         {
172                 return (ToFloat() > rhs);
173         }
174
175         inline bool
176         operator >(const FixedPoint& rhs) const
177         {
178                 return (ToFloat() > rhs.ToFloat());
179         }
180
181 // operator <
182         template <typename T>
183         inline bool
184         operator <(const T& rhs) const
185         {
186                 return (ToFloat() < rhs);
187         }
188
189         inline bool
190         operator <(const FixedPoint& rhs) const
191         {
192                 return (ToFloat() < rhs.ToFloat());
193         }
194
195         int __fixedPoint;
196         enum { SHIFT = postFixed };
197 };
198
199 typedef FixedPoint<26, 6> FixedPoint26_6;
200 typedef FixedPoint<22, 10> FixedPoint22_10;
201
202
203 template <typename T>
204 struct Point
205 {
206         T x;
207         T y;
208 };
209
210 template <typename T>
211 struct Dimension
212 {
213         T w;
214         T h;
215 };
216
217 template <typename T>
218 struct Rectangle
219 {
220         T x;
221         T y;
222         T w;
223         T h;
224 };
225
226 template <typename T>
227 inline bool
228 operator ==(const Rectangle<T>& lhs, const Rectangle<T>& rhs)
229 {
230         return (lhs.x == rhs.x) && (lhs.y == rhs.y) && (lhs.w == rhs.w) && (lhs.h == rhs.h);
231 }
232
233 template <typename T>
234 inline bool
235 operator !=(const Rectangle<T>& lhs, const Rectangle<T>& rhs)
236 {
237         return !(operator ==(lhs, rhs));
238 }
239
240 template <typename T>
241 struct Bounds
242 {
243         T x1;
244         T y1;
245         T x2;
246         T y2;
247
248         bool IsInside(T x, T y)
249         {
250                 return ((x >= x1) && (x < x2) && (y >= y1) && (y < y2));
251         }
252
253         bool IsInsideX(T x)
254         {
255                 return ((x >= x1) && (x < x2));
256         }
257 };
258
259 ////////////////////////////////////////////////////////////////////////////////
260 // String
261
262 struct String
263 {
264         const wchar_t* pStart;
265         int length;
266
267         String(void)
268                 : pStart(L"")
269                 , length(0)
270         {
271         }
272
273         String(const wchar_t* pInputString, int inputStringLength)
274                 : pStart(pInputString)
275                 , length(inputStringLength)
276         {
277         }
278
279         String(const wchar_t* pInputString, int inputStringLength, int offset, int clippedLength)
280         {
281                 const wchar_t* pInputStart = pInputString;
282                 const wchar_t* pInputEnd = pInputStart + inputStringLength;
283
284                 const wchar_t* pRevisedStart = pInputString + offset;
285                 const wchar_t* pRevisedEnd = pRevisedStart + clippedLength;
286
287                 const wchar_t* pClippedStart = (pInputStart > pRevisedStart) ? pInputStart : pRevisedStart;
288                 const wchar_t* pClippedEnd = (pInputEnd < pRevisedEnd) ? pInputEnd : pRevisedEnd;
289
290                 if (pClippedEnd - pClippedStart > 0)
291                 {
292                         this->pStart = pClippedStart;
293                         this->length = pClippedEnd - pClippedStart;
294                 }
295                 else
296                 {
297                         this->pStart = (pClippedStart == pInputString) ? pClippedStart : L"";
298                         this->length = 0;
299                 }
300         }
301
302         operator const wchar_t*(void)
303         {
304                 return pStart;
305         }
306 };
307
308 typedef std::wstring WString;
309
310 } // _Util
311
312 TIZEN_MAPS_END_NAMESPACE
313
314 #endif /* HERE_GRAPHIC_UTILTYPE_H */