- メインページ
- モジュール
- ネームスペース
- データ構造
- ファイル

- ファイル一覧
- グローバル

limits.h
説明を見る。
1 //
2 // Copyright (c) 2003-2011, MIST Project, Nagoya University
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without modification,
6 // are permitted provided that the following conditions are met:
7 //
8 // 1. Redistributions of source code must retain the above copyright notice,
9 // this list of conditions and the following disclaimer.
10 //
11 // 2. Redistributions in binary form must reproduce the above copyright notice,
12 // this list of conditions and the following disclaimer in the documentation
13 // and/or other materials provided with the distribution.
14 //
15 // 3. Neither the name of the Nagoya University nor the names of its contributors
16 // may be used to endorse or promote products derived from this software
17 // without specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
20 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
21 // FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
22 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25 // IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
26 // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 //
28
33 #ifndef __INCLUDE_MIST_LIMITS__
34 #define __INCLUDE_MIST_LIMITS__
35
36 #include <limits.h>
37 #include <float.h>
38 #include <limits>
39
40
41 #ifndef __INCLUDE_MIST_CONF_H__
42 #include "config/mist_conf.h"
43 #endif
44
45 #ifndef __INCLUDE_MIST_COLOR_H__
46 #include "config/color.h"
47 #endif
48
49 #ifndef __INCLUDE_MIST_TYPE_TRAIT_H__
50 #include "config/type_trait.h"
51 #endif
52
53 // mist名前空間の始まり
54 _MIST_BEGIN
55
56
64
65
66 // numeric_limitsの特殊化用マクロ
67 #define _DEFINE_TYPE_LIMITS( _type_, _integer_, _signed_, _tiny_, _min_, _max_, _zero_ ) \
68 template < >\
69 struct type_limits< _type_ >\
70 {\
71 typedef _type_ value_type;\
72 \
73 enum{ is_integer = _integer_ };\
74 enum{ is_signed = _signed_ };\
75 \
76 static value_type tiny( ) { return( _tiny_ ); }\
77 static value_type minimum( ) { return( _min_ ); }\
78 static value_type maximum( ) { return( _max_ ); }\
79 static value_type zero( ){ return( _zero_ ); }\
80 };\
81
82
102 template < class T >
103 struct type_limits
104 {
105 typedef T value_type;
106
111 _MIST_CONST( bool, is_integer, std::numeric_limits< T >::is_integer );
112
117 _MIST_CONST( bool, is_signed, std::numeric_limits< T >::is_signed );
118
119
121 static value_type tiny( ) { return( static_cast< value_type >( 1 ) ); }
122
124 static value_type minimum( ) { return( (std::numeric_limits< T >::min)( ) ); }
125
127 static value_type maximum( ) { return( (std::numeric_limits< T >::max)( ) ); }
128
130 static value_type zero( ){ return( static_cast< value_type >( 0 ) ); }
131 };
132
133 _DEFINE_TYPE_LIMITS( float, false, true, FLT_MIN, -FLT_MAX, FLT_MAX, 0.0f )
134 _DEFINE_TYPE_LIMITS( double, false, true, DBL_MIN, -DBL_MAX, DBL_MAX, 0 )
135 _DEFINE_TYPE_LIMITS( long double, false, true, LDBL_MIN, -LDBL_MAX, LDBL_MAX, 0 )
136
137
139 // 型に関する情報の取得グループの終わり
140
141
142 #if defined(__MIST_MSVC__) && __MIST_MSVC__ < 7
143
144 #define _DEFINE_COLOR_TYPE_LIMITS( type ) \
145 template < >\
146 struct type_limits< rgb< type > >\
147 {\
148 typedef rgb< type > value_type;\
149 typedef rgb< type >::value_type rgb_value_type;\
150 \
151 enum{ is_integer = type_limits< rgb_value_type >::is_integer };\
152 enum{ is_signed = type_limits< rgb_value_type >::is_signed };\
153 \
154 static value_type minimum( ) { return( rgb< type >( type_limits< rgb_value_type >::minimum( ) ) ); }\
155 static value_type maximum( ) { return( rgb< type >( type_limits< rgb_value_type >::maximum( ) ) ); }\
156 static value_type zero( ) { return( rgb< type >( type_limits< rgb_value_type >::zero( ) ) ); }\
157 };\
158 template < >\
159 struct type_limits< rgba< type > >\
160 {\
161 typedef rgba< type > value_type;\
162 typedef rgba< type >::value_type rgb_value_type;\
163 \
164 enum{ is_integer = type_limits< rgb_value_type >::is_integer };\
165 enum{ is_signed = type_limits< rgb_value_type >::is_signed };\
166 \
167 static value_type minimum( ) { return( rgba< type >( type_limits< rgb_value_type >::minimum( ) ) ); }\
168 static value_type maximum( ) { return( rgba< type >( type_limits< rgb_value_type >::maximum( ) ) ); }\
169 static value_type zero( ) { return( rgba< type >( type_limits< rgb_value_type >::zero( ) ) ); }\
170 };\
171 template < >\
172 struct type_limits< bgr< type > >\
173 {\
174 typedef bgr< type > value_type;\
175 typedef bgr< type >::value_type bgr_value_type;\
176 \
177 enum{ is_integer = type_limits< bgr_value_type >::is_integer };\
178 enum{ is_signed = type_limits< bgr_value_type >::is_signed };\
179 \
180 static value_type minimum( ) { return( bgr< type >( type_limits< bgr_value_type >::minimum( ) ) ); }\
181 static value_type maximum( ) { return( bgr< type >( type_limits< bgr_value_type >::maximum( ) ) ); }\
182 static value_type zero( ) { return( bgr< type >( type_limits< bgr_value_type >::zero( ) ) ); }\
183 };\
184 template < >\
185 struct type_limits< bgra< type > >\
186 {\
187 typedef bgra< type > value_type;\
188 typedef bgra< type >::value_type bgr_value_type;\
189 \
190 enum{ is_integer = type_limits< bgr_value_type >::is_integer };\
191 enum{ is_signed = type_limits< bgr_value_type >::is_signed };\
192 \
193 static value_type minimum( ) { return( bgra< type >( type_limits< bgr_value_type >::minimum( ) ) ); }\
194 static value_type maximum( ) { return( bgra< type >( type_limits< bgr_value_type >::maximum( ) ) ); }\
195 static value_type zero( ) { return( bgra< type >( type_limits< bgr_value_type >::zero( ) ) ); }\
196 };\
197
198 // 各型に対する特殊化
199 _DEFINE_COLOR_TYPE_LIMITS(unsigned char)
200 _DEFINE_COLOR_TYPE_LIMITS(unsigned short)
201 _DEFINE_COLOR_TYPE_LIMITS(unsigned int)
202 _DEFINE_COLOR_TYPE_LIMITS(unsigned long)
203 _DEFINE_COLOR_TYPE_LIMITS(signed char)
204 _DEFINE_COLOR_TYPE_LIMITS(signed short)
205 _DEFINE_COLOR_TYPE_LIMITS(signed int)
206 _DEFINE_COLOR_TYPE_LIMITS(signed long)
207 _DEFINE_COLOR_TYPE_LIMITS(bool)
208 _DEFINE_COLOR_TYPE_LIMITS(char)
209 _DEFINE_COLOR_TYPE_LIMITS(float)
210 _DEFINE_COLOR_TYPE_LIMITS(double)
211 _DEFINE_COLOR_TYPE_LIMITS(long double)
212
213 #undef _DEFINE_COLOR_TYPE_LIMITS
214
215 #else
216
217 template < class T >
218 struct type_limits< rgb< T > >
219 {
220 typedef rgb< T > value_type;
221 typedef typename rgb< T >::value_type rgb_value_type;
222
223 enum{ is_integer = type_limits< rgb_value_type >::is_integer };
224 enum{ is_signed = type_limits< rgb_value_type >::is_signed };
225
226 static value_type minimum( ) { return( rgb< T >( type_limits< rgb_value_type >::minimum( ) ) ); }
227 static value_type maximum( ) { return( rgb< T >( type_limits< rgb_value_type >::maximum( ) ) ); }
228 static value_type zero( ) { return( rgb< T >( type_limits< rgb_value_type >::zero( ) ) ); }
229 };
230
231 template < class T >
232 struct type_limits< rgba< T > >
233 {
234 typedef rgba< T > value_type;
235 typedef typename rgba< T >::value_type rgb_value_type;
236
237 enum{ is_integer = type_limits< rgb_value_type >::is_integer };
238 enum{ is_signed = type_limits< rgb_value_type >::is_signed };
239
240 static value_type minimum( ) { return( rgba< T >( type_limits< rgb_value_type >::minimum( ) ) ); }
241 static value_type maximum( ) { return( rgba< T >( type_limits< rgb_value_type >::maximum( ) ) ); }
242 static value_type zero( ) { return( rgba< T >( type_limits< rgb_value_type >::zero( ) ) ); }
243 };
244
245 template < class T >
246 struct type_limits< bgr< T > >
247 {
248 typedef bgr< T > value_type;
249 typedef typename bgr< T >::value_type bgr_value_type;
250
251 enum{ is_integer = type_limits< bgr_value_type >::is_integer };
252 enum{ is_signed = type_limits< bgr_value_type >::is_signed };
253
254 static value_type minimum( ) { return( bgr< T >( type_limits< bgr_value_type >::minimum( ) ) ); }
255 static value_type maximum( ) { return( bgr< T >( type_limits< bgr_value_type >::maximum( ) ) ); }
256 static value_type zero( ) { return( bgr< T >( type_limits< bgr_value_type >::zero( ) ) ); }
257 };
258
259 template < class T >
260 struct type_limits< bgra< T > >
261 {
262 typedef bgra< T > value_type;
263 typedef typename bgra< T >::value_type bgr_value_type;
264
265 enum{ is_integer = type_limits< bgr_value_type >::is_integer };
266 enum{ is_signed = type_limits< bgr_value_type >::is_signed };
267
268 static value_type minimum( ) { return( bgra< T >( type_limits< bgr_value_type >::minimum( ) ) ); }
269 static value_type maximum( ) { return( bgra< T >( type_limits< bgr_value_type >::maximum( ) ) ); }
270 static value_type zero( ) { return( bgra< T >( type_limits< bgr_value_type >::zero( ) ) ); }
271 };
272
273 #endif
274
275
276 #undef _DEFINE_TYPE_LIMITS
277
278
279 namespace __limits_min_max__
280 {
281 template < class T >
282 inline const T limits_min_max__( const T &v, const T &min, const T &max )
283 {
284 return( v < min ? min : ( v > max ? max : v ) );
285 }
286
287 template < bool _ISCOLOR_ >
288 struct limits_min_max
289 {
290 template < class T >
291 static const T limits( const T &v, const T &min, const T &max )
292 {
293 return( limits_min_max__( v, min, max ) );
294 }
295 };
296
297 template < >
298 struct limits_min_max< true >
299 {
300 template < class T >
301 static const rgb< T > limits( const rgb< T > &v, const rgb< T > &min, const rgb< T > &max )
302 {
303 return( rgb< T >( limits_min_max__( v.r, min.r, max.r ), limits_min_max__( v.g, min.g, max.g ), limits_min_max__( v.b, min.b, max.b ) ) );
304 }
305
306 template < class T >
307 static const rgba< T > limits( const rgba< T > &v, const rgba< T > &min, const rgba< T > &max )
308 {
309 return( rgba< T >( limits_min_max__( v.r, min.r, max.r ), limits_min_max__( v.g, min.g, max.g ), limits_min_max__( v.b, min.b, max.b ), limits_min_max__( v.a, min.a, max.a ) ) );
310 }
311
312 template < class T >
313 static const bgr< T > limits( const bgr< T > &v, const bgr< T > &min, const bgr< T > &max )
314 {
315 return( bgr< T >( limits_min_max__( v.r, min.r, max.r ), limits_min_max__( v.g, min.g, max.g ), limits_min_max__( v.b, min.b, max.b ) ) );
316 }
317
318 template < class T >
319 static const bgra< T > limits( const bgra< T > &v, const bgra< T > &min, const bgra< T > &max )
320 {
321 return( bgra< T >( limits_min_max__( v.r, min.r, max.r ), limits_min_max__( v.g, min.g, max.g ), limits_min_max__( v.b, min.b, max.b ), limits_min_max__( v.a, min.a, max.a ) ) );
322 }
323 };
324 }
325
326
327 namespace __limits_0_255__
328 {
329 template < bool _ISSIGNED_ >
330 struct __limits_0_255__
331 {
332 template < class T >
333 static const T limits( const T &v )
334 {
335 return( v < 0 ? 0 : ( v > 255 ? 255 : v ) );
336 }
337 };
338
339 template < >
340 struct __limits_0_255__< false >
341 {
342 template < class T >
343 static const T limits( const T &v )
344 {
345 return( v > 255 ? 255 : v );
346 }
347 };
348
349 template < bool _ISCHAR_ >
350 struct limits_0_255__
351 {
352 template < class T >
353 static const T limits( const T &v )
354 {
355 return( v );
356 }
357 };
358
359 template < >
360 struct limits_0_255__< false >
361 {
362 template < class T >
363 static const T limits( const T &v )
364 {
365 return( __limits_0_255__< is_signed< T >::value >::limits( v ) );
366 }
367 };
368
369 template < bool _ISCOLOR_ >
370 struct limits_0_255
371 {
372 template < class T >
373 static const T limits( const T &v )
374 {
375 return( limits_0_255__< is_char< T >::value >::limits( v ) );
376 }
377 };
378
379 template < >
380 struct limits_0_255< true >
381 {
382 template < class T >
383 static const rgb< T > limits( const rgb< T > &v )
384 {
385 return( rgb< T >( limits_0_255__< is_char< T >::value >::limits( v.r ), limits_0_255__< is_char< T >::value >::limits( v.g ), limits_0_255__< is_char< T >::value >::limits( v.b ) ) );
386 }
387
388 template < class T >
389 static const rgba< T > limits( const rgba< T > &v )
390 {
391 return( rgba< T >( limits_0_255__< is_char< T >::value >::limits( v.r ), limits_0_255__< is_char< T >::value >::limits( v.g ), limits_0_255__< is_char< T >::value >::limits( v.b ), limits_0_255__< is_char< T >::value >::limits( v.a ) ) );
392 }
393
394 template < class T >
395 static const bgr< T > limits( const bgr< T > &v )
396 {
397 return( bgr< T >( limits_0_255__< is_char< T >::value >::limits( v.r ), limits_0_255__< is_char< T >::value >::limits( v.g ), limits_0_255__< is_char< T >::value >::limits( v.b ) ) );
398 }
399
400 template < class T >
401 static const bgra< T > limits( const bgra< T > &v )
402 {
403 return( bgra< T >( limits_0_255__< is_char< T >::value >::limits( v.r ), limits_0_255__< is_char< T >::value >::limits( v.g ), limits_0_255__< is_char< T >::value >::limits( v.b ), limits_0_255__< is_char< T >::value >::limits( v.a ) ) );
404 }
405 };
406 }
407
408
409
413
414
425 template < class T >
426 inline const T limits_min_max( const T &v, const T &min, const T &max )
427 {
428 return( __limits_min_max__::limits_min_max< is_color< T >::value >::limits( v, min, max ) );
429 }
430
431
432
441 template < class T >
442 inline const T limits_0_255( const T &v )
443 {
444 return( __limits_0_255__::limits_0_255< is_color< T >::value >::limits( v ) );
445 }
446
447
449 // 型に関する情報の取得グループの終わり
450
451
452 // mist名前空間の終わり
453 _MIST_END
454
455 #endif // __INCLUDE_MIST_LIMITS__
456