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

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

type_trait.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
34 #ifndef __INCLUDE_MIST_TYPE_TRAIT_H__
35 #define __INCLUDE_MIST_TYPE_TRAIT_H__
36
37 #ifndef __INCLUDE_MIST_CONF_H__
38 #include "mist_conf.h"
39 #endif
40
41
42 // mist名前空間の始まり
43 _MIST_BEGIN
44
45
46 // 型属性を調べる
47
48
55 template< class T > struct is_char { _MIST_CONST( bool, value, false ); };
56 template<> struct is_char< unsigned char >{ _MIST_CONST( bool, value, true ); };
57 template<> struct is_char< signed char > { _MIST_CONST( bool, value, true ); };
58 template<> struct is_char< char > { _MIST_CONST( bool, value, true ); };
59
60
67 template< class T > struct is_float { _MIST_CONST( bool, value, false ); };
68 template<> struct is_float< float > { _MIST_CONST( bool, value, true ); };
69 template<> struct is_float< double > { _MIST_CONST( bool, value, true ); };
70 template<> struct is_float< long double > { _MIST_CONST( bool, value, true ); };
71
72
73
80 template< class T > struct is_integer { _MIST_CONST( bool, value, false ); };
81 template<> struct is_integer< unsigned char > { _MIST_CONST( bool, value, true ); };
82 template<> struct is_integer< unsigned short >{ _MIST_CONST( bool, value, true ); };
83 template<> struct is_integer< unsigned int > { _MIST_CONST( bool, value, true ); };
84 template<> struct is_integer< unsigned long > { _MIST_CONST( bool, value, true ); };
85 template<> struct is_integer< signed char > { _MIST_CONST( bool, value, true ); };
86 template<> struct is_integer< signed short > { _MIST_CONST( bool, value, true ); };
87 template<> struct is_integer< signed int > { _MIST_CONST( bool, value, true ); };
88 template<> struct is_integer< signed long > { _MIST_CONST( bool, value, true ); };
89 template<> struct is_integer< bool > { _MIST_CONST( bool, value, true ); };
90 template<> struct is_integer< char > { _MIST_CONST( bool, value, true ); };
91
92 #if defined( __MIST64__ ) && __MIST64__ != 0 && !( defined( __GNUC__ ) || defined( __APPLE__ ) || defined( __ICC ) )
93 template<> struct is_integer< size_t > { _MIST_CONST( bool, value, true ); };
94 template<> struct is_integer< ptrdiff_t > { _MIST_CONST( bool, value, true ); };
95 #endif
96
103 template< class T > struct is_signed { _MIST_CONST( bool, value, false ); };
104 template<> struct is_signed< signed char > { _MIST_CONST( bool, value, true ); };
105 template<> struct is_signed< signed short > { _MIST_CONST( bool, value, true ); };
106 template<> struct is_signed< signed int > { _MIST_CONST( bool, value, true ); };
107 template<> struct is_signed< signed long > { _MIST_CONST( bool, value, true ); };
108 template<> struct is_signed< char > { _MIST_CONST( bool, value, true ); };
109 template<> struct is_signed< float > { _MIST_CONST( bool, value, true ); };
110 template<> struct is_signed< double > { _MIST_CONST( bool, value, true ); };
111 template<> struct is_signed< long double > { _MIST_CONST( bool, value, true ); };
112
113 #if defined( __MIST64__ ) && __MIST64__ != 0 && !( defined( __GNUC__ ) || defined( __APPLE__ ) || defined( __ICC ) )
114 template<> struct is_signed< ptrdiff_t > { _MIST_CONST( bool, value, true ); };
115 #endif
116
117
118
125 template< class T > struct is_arithmetic { _MIST_CONST( bool, value, false ); };
126 template<> struct is_arithmetic< unsigned char > { _MIST_CONST( bool, value, true ); };
127 template<> struct is_arithmetic< unsigned short >{ _MIST_CONST( bool, value, true ); };
128 template<> struct is_arithmetic< unsigned int > { _MIST_CONST( bool, value, true ); };
129 template<> struct is_arithmetic< unsigned long > { _MIST_CONST( bool, value, true ); };
130 template<> struct is_arithmetic< signed char > { _MIST_CONST( bool, value, true ); };
131 template<> struct is_arithmetic< signed short > { _MIST_CONST( bool, value, true ); };
132 template<> struct is_arithmetic< signed int > { _MIST_CONST( bool, value, true ); };
133 template<> struct is_arithmetic< signed long > { _MIST_CONST( bool, value, true ); };
134 template<> struct is_arithmetic< bool > { _MIST_CONST( bool, value, true ); };
135 template<> struct is_arithmetic< char > { _MIST_CONST( bool, value, true ); };
136 template<> struct is_arithmetic< float > { _MIST_CONST( bool, value, true ); };
137 template<> struct is_arithmetic< double > { _MIST_CONST( bool, value, true ); };
138 template<> struct is_arithmetic< long double > { _MIST_CONST( bool, value, true ); };
139
140 #if defined( __MIST64__ ) && __MIST64__ != 0 && !( defined( __GNUC__ ) || defined( __APPLE__ ) || defined( __ICC ) )
141 template<> struct is_arithmetic< size_t > { _MIST_CONST( bool, value, true ); };
142 template<> struct is_arithmetic< ptrdiff_t > { _MIST_CONST( bool, value, true ); };
143 #endif
144
145
152 template< class T > struct is_builtin { _MIST_CONST( bool, value, false ); };
153 template<> struct is_builtin< unsigned char > { _MIST_CONST( bool, value, true ); };
154 template<> struct is_builtin< unsigned short >{ _MIST_CONST( bool, value, true ); };
155 template<> struct is_builtin< unsigned int > { _MIST_CONST( bool, value, true ); };
156 template<> struct is_builtin< unsigned long > { _MIST_CONST( bool, value, true ); };
157 template<> struct is_builtin< signed char > { _MIST_CONST( bool, value, true ); };
158 template<> struct is_builtin< signed short > { _MIST_CONST( bool, value, true ); };
159 template<> struct is_builtin< signed int > { _MIST_CONST( bool, value, true ); };
160 template<> struct is_builtin< signed long > { _MIST_CONST( bool, value, true ); };
161 template<> struct is_builtin< bool > { _MIST_CONST( bool, value, true ); };
162 template<> struct is_builtin< char > { _MIST_CONST( bool, value, true ); };
163 template<> struct is_builtin< float > { _MIST_CONST( bool, value, true ); };
164 template<> struct is_builtin< double > { _MIST_CONST( bool, value, true ); };
165 template<> struct is_builtin< long double > { _MIST_CONST( bool, value, true ); };
166
167 #if defined( __MIST64__ ) && __MIST64__ != 0 && !( defined( __GNUC__ ) || defined( __APPLE__ ) || defined( __ICC ) )
168 template<> struct is_builtin< size_t > { _MIST_CONST( bool, value, true ); };
169 template<> struct is_builtin< ptrdiff_t > { _MIST_CONST( bool, value, true ); };
170 #endif
171
172
179 template< class T > struct float_type { typedef double value_type; };
180 template<> struct float_type< float > { typedef float value_type; };
181 template<> struct float_type< long double > { typedef long double value_type; };
182
183
184
185
194 template< class T1, class T2 > struct promote_trait { typedef T1 value_type; };
195
196 #define PROMOTE_TRAIT( T1, T2, T3 ) \
197 template< > struct promote_trait< T1, T2 > { typedef T3 value_type; }; \
198 template< > struct promote_trait< T2, T1 > { typedef T3 value_type; };
199
200 PROMOTE_TRAIT( unsigned char, unsigned short, unsigned short )
201 PROMOTE_TRAIT( unsigned char, unsigned int, unsigned int )
202 PROMOTE_TRAIT( unsigned char, unsigned long, unsigned long )
203 PROMOTE_TRAIT( unsigned char, signed char, signed short )
204 PROMOTE_TRAIT( unsigned char, signed short, signed short )
205 PROMOTE_TRAIT( unsigned char, signed int, signed int )
206 PROMOTE_TRAIT( unsigned char, signed long, signed long )
207 PROMOTE_TRAIT( unsigned char, bool, unsigned char )
208 PROMOTE_TRAIT( unsigned char, char, signed char )
209 PROMOTE_TRAIT( unsigned char, float, float )
210 PROMOTE_TRAIT( unsigned char, double, double )
211 PROMOTE_TRAIT( unsigned char, long double, long double )
212 PROMOTE_TRAIT( signed char, unsigned short, signed int )
213 PROMOTE_TRAIT( signed char, unsigned int, signed long )
214 PROMOTE_TRAIT( signed char, unsigned long, signed long )
215 PROMOTE_TRAIT( signed char, signed short, signed short )
216 PROMOTE_TRAIT( signed char, signed int, signed int )
217 PROMOTE_TRAIT( signed char, signed long, signed long )
218 PROMOTE_TRAIT( signed char, bool, signed char )
219 PROMOTE_TRAIT( signed char, char, signed char )
220 PROMOTE_TRAIT( signed char, float, float )
221 PROMOTE_TRAIT( signed char, double, double )
222 PROMOTE_TRAIT( signed char, long double, long double )
223 PROMOTE_TRAIT( char, unsigned short, signed int )
224 PROMOTE_TRAIT( char, unsigned int, signed long )
225 PROMOTE_TRAIT( char, unsigned long, signed long )
226 PROMOTE_TRAIT( char, signed short, signed short )
227 PROMOTE_TRAIT( char, signed int, signed int )
228 PROMOTE_TRAIT( char, signed long, signed long )
229 PROMOTE_TRAIT( char, bool, char )
230 PROMOTE_TRAIT( char, float, float )
231 PROMOTE_TRAIT( char, double, double )
232 PROMOTE_TRAIT( char, long double, long double )
233 PROMOTE_TRAIT( unsigned short, unsigned int, unsigned int )
234 PROMOTE_TRAIT( unsigned short, unsigned long, unsigned long )
235 PROMOTE_TRAIT( unsigned short, signed short, signed int )
236 PROMOTE_TRAIT( unsigned short, signed int, signed int )
237 PROMOTE_TRAIT( unsigned short, signed long, signed long )
238 PROMOTE_TRAIT( unsigned short, bool, unsigned short )
239 PROMOTE_TRAIT( unsigned short, float, float )
240 PROMOTE_TRAIT( unsigned short, double, double )
241 PROMOTE_TRAIT( unsigned short, long double, long double )
242 PROMOTE_TRAIT( signed short, unsigned int, signed int )
243 PROMOTE_TRAIT( signed short, unsigned long, signed long )
244 PROMOTE_TRAIT( signed short, signed int, signed int )
245 PROMOTE_TRAIT( signed short, signed long, signed long )
246 PROMOTE_TRAIT( signed short, bool, signed short )
247 PROMOTE_TRAIT( signed short, float, float )
248 PROMOTE_TRAIT( signed short, double, double )
249 PROMOTE_TRAIT( signed short, long double, long double )
250 PROMOTE_TRAIT( unsigned int, unsigned long, unsigned long )
251 PROMOTE_TRAIT( unsigned int, signed int, signed long )
252 PROMOTE_TRAIT( unsigned int, signed long, signed long )
253 PROMOTE_TRAIT( unsigned int, bool, unsigned int )
254 PROMOTE_TRAIT( unsigned int, float, double )
255 PROMOTE_TRAIT( unsigned int, double, double )
256 PROMOTE_TRAIT( unsigned int, long double, long double )
257 PROMOTE_TRAIT( signed int, unsigned long, signed long )
258 PROMOTE_TRAIT( signed int, signed long, signed long )
259 PROMOTE_TRAIT( signed int, bool, signed int )
260 PROMOTE_TRAIT( signed int, float, double )
261 PROMOTE_TRAIT( signed int, double, double )
262 PROMOTE_TRAIT( signed int, long double, long double )
263 PROMOTE_TRAIT( unsigned long, signed long, signed long )
264 PROMOTE_TRAIT( unsigned long, bool, unsigned long )
265 PROMOTE_TRAIT( unsigned long, float, long double )
266 PROMOTE_TRAIT( unsigned long, double, double )
267 PROMOTE_TRAIT( unsigned long, long double, long double )
268 PROMOTE_TRAIT( signed long, bool, signed int )
269 PROMOTE_TRAIT( signed long, float, double )
270 PROMOTE_TRAIT( signed long, double, double )
271 PROMOTE_TRAIT( signed long, long double, long double )
272 PROMOTE_TRAIT( bool, float, float )
273 PROMOTE_TRAIT( bool, double, double )
274 PROMOTE_TRAIT( bool, long double, long double )
275 PROMOTE_TRAIT( float, double, double )
276 PROMOTE_TRAIT( float, long double, long double )
277 PROMOTE_TRAIT( double, long double, long double )
278
279 #undef PROMOTE_TRAIT
280
281 #if defined( __MIST_MSVC__ ) && __MIST_MSVC__ < 7
282
283 // MSVC6 では，演算子に対応する関数の決定が適切に行われないので，デフォルトで 'double' との演算として定義する．
284
289 #define DEFINE_PROMOTE_BIND_OPERATOR1( TYPE, OPERATOR ) \
290 template < class T1, class T2 > \
291 inline TYPE< promote_trait< T1, T2 >::value_type > \
292 operator OPERATOR( const TYPE< T1 > &t1, const TYPE< T2 > &t2 ) \
293 { \
294 return( TYPE< promote_trait< T1, T2 >::value_type >( t1 ) OPERATOR ## = t2 ); \
295 }
296
301 #define DEFINE_PROMOTE_BIND_OPERATOR2( TYPE, OPERATOR ) \
302 template < class T > \
303 inline TYPE< promote_trait< T, double >::value_type > \
304 operator OPERATOR( const TYPE< T > &t1, const double &t2 ) \
305 { \
306 return( TYPE< promote_trait< T, double >::value_type >( t1 ) OPERATOR ## = t2 ); \
307 }
308
313 #define DEFINE_PROMOTE_BIND_OPERATOR3( TYPE, OPERATOR ) \
314 template < class T > \
315 inline TYPE< promote_trait< T, double >::value_type > \
316 operator OPERATOR( const double &t1, const TYPE< T > &t2 ) \
317 { \
318 return( TYPE< promote_trait< T, double >::value_type >( t2 ) OPERATOR ## = t1 ); \
319 }
320
325 #define DEFINE_PROMOTE_BIND_OPERATOR4( TYPE, OPERATOR ) \
326 template < class T > \
327 inline TYPE< promote_trait< T, double >::value_type > \
328 operator OPERATOR( const double &t1, const TYPE< T > &t2 ) \
329 { \
330 return( TYPE< promote_trait< T, double >::value_type >( t1 ) OPERATOR ## = t2 ); \
331 }
332
333 #else
334
339 #define DEFINE_PROMOTE_BIND_OPERATOR1( TYPE, OPERATOR ) \
340 template < class T1, class T2 > \
341 inline TYPE< typename promote_trait< T1, T2 >::value_type > \
342 operator OPERATOR( const TYPE< T1 > &t1, const TYPE< T2 > &t2 ) \
343 { \
344 return( TYPE< typename promote_trait< T1, T2 >::value_type >( t1 ) OPERATOR ## = t2 ); \
345 }
346
351 #define DEFINE_PROMOTE_BIND_OPERATOR2( TYPE, OPERATOR ) \
352 template < class T1, class T2 > \
353 inline TYPE< typename promote_trait< T1, T2 >::value_type > \
354 operator OPERATOR( const TYPE< T1 > &t1, const T2 &t2 ) \
355 { \
356 return( TYPE< typename promote_trait< T1, T2 >::value_type >( t1 ) OPERATOR ## = t2 ); \
357 }
358
363 #define DEFINE_PROMOTE_BIND_OPERATOR3( TYPE, OPERATOR ) \
364 template < class T1, class T2 > \
365 inline TYPE< typename promote_trait< T1, T2 >::value_type > \
366 operator OPERATOR( const T1 &t1, const TYPE< T2 > &t2 ) \
367 { \
368 return( TYPE< typename promote_trait< T1, T2 >::value_type >( t2 ) OPERATOR ## = t1 ); \
369 }
370
375 #define DEFINE_PROMOTE_BIND_OPERATOR4( TYPE, OPERATOR ) \
376 template < class T1, class T2 > \
377 inline TYPE< typename promote_trait< T1, T2 >::value_type > \
378 operator OPERATOR( const T1 &t1, const TYPE< T2 > &t2 ) \
379 { \
380 return( TYPE< typename promote_trait< T1, T2 >::value_type >( t1 ) OPERATOR ## = t2 ); \
381 }
382
387 #define DEFINE_PROMOTE_BIND_OPERATOR1_( TYPE, OPERATOR ) \
388 template < class T1, class T2, size_t NDIM > \
389 inline TYPE< typename promote_trait< T1, T2 >::value_type, NDIM > \
390 operator OPERATOR( const TYPE< T1, NDIM > &t1, const TYPE< T2, NDIM > &t2 ) \
391 { \
392 return( TYPE< typename promote_trait< T1, T2 >::value_type, NDIM >( t1 ) OPERATOR ## = t2 ); \
393 }
394
399 #define DEFINE_PROMOTE_BIND_OPERATOR2_( TYPE, OPERATOR ) \
400 template < class T1, class T2, size_t NDIM > \
401 inline TYPE< typename promote_trait< T1, T2 >::value_type, NDIM > \
402 operator OPERATOR( const TYPE< T1, NDIM > &t1, const T2 &t2 ) \
403 { \
404 return( TYPE< typename promote_trait< T1, T2 >::value_type, NDIM >( t1 ) OPERATOR ## = t2 ); \
405 }
406
411 #define DEFINE_PROMOTE_BIND_OPERATOR3_( TYPE, OPERATOR ) \
412 template < class T1, class T2, size_t NDIM > \
413 inline TYPE< typename promote_trait< T1, T2 >::value_type, NDIM > \
414 operator OPERATOR( const T1 &t1, const TYPE< T2, NDIM > &t2 ) \
415 { \
416 return( TYPE< typename promote_trait< T1, T2 >::value_type, NDIM >( t2 ) OPERATOR ## = t1 ); \
417 }
418
423 #define DEFINE_PROMOTE_BIND_OPERATOR4_( TYPE, OPERATOR ) \
424 template < class T1, class T2, size_t NDIM > \
425 inline TYPE< typename promote_trait< T1, T2 >::value_type, NDIM > \
426 operator OPERATOR( const T1 &t1, const TYPE< T2, NDIM > &t2 ) \
427 { \
428 return( TYPE< typename promote_trait< T1, T2 >::value_type, NDIM >( t1 ) OPERATOR ## = t2 ); \
429 }
430
431 #endif
432
433
434
439 template< class T >
440 struct type_trait{ typedef T value_type; };
441
442
443 // 値の四捨五入をするクラス
444 template < bool b >
445 struct __half_adjust__
446 {
447 static double convert( const double value )
448 {
449 return( value + 0.5 );
450 }
451 };
452
453
454 // 値の四捨五入をするクラス
455 template < >
456 struct __half_adjust__< false >
457 {
458 static double convert( const double value )
459 {
460 return( value );
461 }
462 };
463
464
469 template < class T >
470 struct half_adjust
471 {
472 typedef T value_type;
473
474 static value_type convert( const double value )
475 {
476 return( static_cast< value_type >( __half_adjust__< is_integer< value_type >::value >::convert( value ) ) );
477 }
478 };
479
480
493 template < bool b1, bool b2, bool b3 = true, bool b4 = true, bool b5 = true, bool b6 = true, bool b7 = true >
494 struct type_and
495 {
496 _MIST_CONST( bool, value, false );
497 };
498
499 template <>
500 struct type_and< true, true, true, true, true, true, true >
501 {
502 _MIST_CONST( bool, value, true );
503 };
504
505
506
519 template < bool b1, bool b2, bool b3 = false, bool b4 = false, bool b5 = false, bool b6 = false, bool b7 = false >
520 struct type_or
521 {
522 _MIST_CONST( bool, value, true );
523 };
524
525 template <>
526 struct type_or< false, false, false, false, false, false, false >
527 {
528 _MIST_CONST( bool, value, false );
529 };
530
531
532
533
540 template < bool b1 >
541 struct type_not
542 {
543 _MIST_CONST( bool, value, true );
544 };
545
546 template <>
547 struct type_not< true >
548 {
549 _MIST_CONST( bool, value, false );
550 };
551
552
553
554
562 template < bool b1, bool b2 >
563 struct type_equal
564 {
565 _MIST_CONST( bool, value, false );
566 };
567
568 template <>
569 struct type_equal< true, true >
570 {
571 _MIST_CONST( bool, value, true );
572 };
573
574 template <>
575 struct type_equal< false, false >
576 {
577 _MIST_CONST( bool, value, true );
578 };
579
580
581
582
583 // mist名前空間の終わり
584 _MIST_END
585
586
587 #endif // __INCLUDE_MIST_TYPE_TRAIT_H__
588