TypeList.h
Go to the documentation of this file.
1 //=================================================================================================
33 //=================================================================================================
34 
35 #ifndef _BLAZE_UTIL_TYPELIST_H_
36 #define _BLAZE_UTIL_TYPELIST_H_
37 
38 
39 //*************************************************************************************************
40 // Includes
41 //*************************************************************************************************
42 
43 #include <blaze/util/NullType.h>
44 #include <blaze/util/Types.h>
45 
46 
47 namespace blaze {
48 
49 //=================================================================================================
50 //
51 // CLASS TYPELIST
52 //
53 //=================================================================================================
54 
55 //*************************************************************************************************
114 template< typename H // Head of the type list
115  , typename T > // Tail of the type list
116 struct TypeList
117 {
118  //**Type definitions****************************************************************************
119  using Head = H;
120  using Tail = T;
121  //**********************************************************************************************
122 };
123 //*************************************************************************************************
124 
125 
126 
127 
128 //=================================================================================================
129 //
130 // TYPE LIST GENERATION MACROS
131 //
132 //=================================================================================================
133 
134 //*************************************************************************************************
149 #define BLAZE_TYPELIST_1( T1 ) \
150  TypeList< T1, NullType >
151 //*************************************************************************************************
152 
153 
154 //*************************************************************************************************
170 #define BLAZE_TYPELIST_2( T1, T2 ) \
171  TypeList< T1, BLAZE_TYPELIST_1( T2 ) >
172 //*************************************************************************************************
173 
174 
175 //*************************************************************************************************
191 #define BLAZE_TYPELIST_3( T1, T2, T3 ) \
192  TypeList< T1, BLAZE_TYPELIST_2( T2, T3 ) >
193 //*************************************************************************************************
194 
195 
196 //*************************************************************************************************
212 #define BLAZE_TYPELIST_4( T1, T2, T3, T4 ) \
213  TypeList< T1, BLAZE_TYPELIST_3( T2, T3, T4 ) >
214 //*************************************************************************************************
215 
216 
217 //*************************************************************************************************
233 #define BLAZE_TYPELIST_5( T1, T2, T3, T4, T5 ) \
234  TypeList< T1, BLAZE_TYPELIST_4( T2, T3, T4, T5 ) >
235 //*************************************************************************************************
236 
237 
238 //*************************************************************************************************
254 #define BLAZE_TYPELIST_6( T1, T2, T3, T4, T5, T6 ) \
255  TypeList< T1, BLAZE_TYPELIST_5( T2, T3, T4, T5, T6 ) >
256 //*************************************************************************************************
257 
258 
259 //*************************************************************************************************
275 #define BLAZE_TYPELIST_7( T1, T2, T3, T4, T5, T6, T7 ) \
276  TypeList< T1, BLAZE_TYPELIST_6( T2, T3, T4, T5, T6, T7 ) >
277 //*************************************************************************************************
278 
279 
280 //*************************************************************************************************
296 #define BLAZE_TYPELIST_8( T1, T2, T3, T4, T5, T6, T7, T8 ) \
297  TypeList< T1, BLAZE_TYPELIST_7( T2, T3, T4, T5, T6, T7, T8 ) >
298 //*************************************************************************************************
299 
300 
301 //*************************************************************************************************
317 #define BLAZE_TYPELIST_9( T1, T2, T3, T4, T5, T6, T7, T8, T9 ) \
318  TypeList< T1, BLAZE_TYPELIST_8( T2, T3, T4, T5, T6, T7, T8, T9 ) >
319 //*************************************************************************************************
320 
321 
322 //*************************************************************************************************
339 #define BLAZE_TYPELIST_10( T1, T2, T3, T4, T5, T6, T7, T8, T9, T10 ) \
340  TypeList< T1, BLAZE_TYPELIST_9( T2, T3, T4, T5, T6, T7, T8, T9, T10 ) >
341 //*************************************************************************************************
342 
343 
344 
345 
346 //=================================================================================================
347 //
348 // LENGTH OF A TYPE LIST
349 //
350 //=================================================================================================
351 
352 //*************************************************************************************************
368 template< typename TList > // Type of the type list
369 struct Length;
370 //*************************************************************************************************
371 
372 
373 //*************************************************************************************************
378 template<>
379 struct Length< NullType >
380 {
381  //**Member enumeration**************************************************************************
382  enum { value = 0 };
383  //**********************************************************************************************
384 };
386 //*************************************************************************************************
387 
388 
389 //*************************************************************************************************
394 template< typename Head // Type of the head of the type list
395  , typename Tail > // Type of the tail of the type list
396 struct Length< TypeList<Head,Tail> >
397 {
398  //**Member enumeration**************************************************************************
399  enum { value = 1 + Length<Tail>::value };
400  //**********************************************************************************************
401 };
403 //*************************************************************************************************
404 
405 
406 
407 
408 //=================================================================================================
409 //
410 // INDEXED ACCESS
411 //
412 //=================================================================================================
413 
414 //*************************************************************************************************
432 template< typename TList // Type of the type list
433  , size_t Index > // Type list access index
434 struct TypeAt;
435 //*************************************************************************************************
436 
437 
438 //*************************************************************************************************
443 template< typename Head // Type of the head of the type list
444  , typename Tail > // Type of the tail of the type list
445 struct TypeAt< TypeList<Head,Tail>, 0 >
446 {
447  //**Member enumeration**************************************************************************
448  using Result = Head;
449  //**********************************************************************************************
450 };
452 //*************************************************************************************************
453 
454 
455 //*************************************************************************************************
460 template< size_t Index > // Type list access index
461 struct TypeAt< NullType, Index >
462 {
463  //**Member enumeration**************************************************************************
464  using Result = NullType;
465  //**********************************************************************************************
466 };
468 //*************************************************************************************************
469 
470 
471 //*************************************************************************************************
476 template< typename Head // Type of the head of the type list
477  , typename Tail // Type of the tail of the type list
478  , size_t Index > // Type list access index
479 struct TypeAt< TypeList<Head,Tail>, Index >
480 {
481  //**Member enumeration**************************************************************************
482  using Result = typename TypeAt< Tail, Index-1 >::Result;
483  //**********************************************************************************************
484 };
486 //*************************************************************************************************
487 
488 
489 
490 
491 //=================================================================================================
492 //
493 // TYPE LIST SEARCH
494 //
495 //=================================================================================================
496 
497 //*************************************************************************************************
518 template< typename TList // Type of the type list
519  , typename Type > // The search type
520 struct Contains;
521 //*************************************************************************************************
522 
523 
524 //*************************************************************************************************
529 template< typename Type > // The search type
530 struct Contains< NullType, Type >
531 {
532  //**Member enumeration**************************************************************************
533  enum { value = 0 };
534  //**********************************************************************************************
535 };
537 //*************************************************************************************************
538 
539 
540 //*************************************************************************************************
545 template< typename Tail // Type of the tail of the type list
546  , typename Type > // The search type
547 struct Contains< TypeList<Type,Tail>, Type >
548 {
549  //**Member enumeration**************************************************************************
550  enum { value = 1 };
551  //**********************************************************************************************
552 };
554 //*************************************************************************************************
555 
556 
557 //*************************************************************************************************
562 template< typename Head // Type of the head of the type list
563  , typename Tail // Type of the tail of the type list
564  , typename Type > // The search type
565 struct Contains< TypeList<Head,Tail>, Type >
566 {
567  //**Member enumeration**************************************************************************
568  enum { value = Contains<Tail,Type>::value };
569  //**********************************************************************************************
570 };
572 //*************************************************************************************************
573 
574 
575 //*************************************************************************************************
607 template< typename TList // Type of the type list
608  , typename Type > // The search type
610 //*************************************************************************************************
611 
612 
613 //*************************************************************************************************
618 template< typename Type > // The search type
619 struct ContainsRelated< NullType, Type >
620 {
621  //**Member enumeration**************************************************************************
622  enum { value = 0 };
623  //**********************************************************************************************
624 };
626 //*************************************************************************************************
627 
628 
629 //*************************************************************************************************
634 template< typename Head // Type of the head of the type list
635  , typename Tail // Type of the tail of the type list
636  , typename Type > // The search type
637 struct ContainsRelated< TypeList<Head,Tail>, Type >
638 {
639  private:
640  //**********************************************************************************************
641  class No {};
642  class Yes { No no[2]; };
643  //**********************************************************************************************
644 
645  //**********************************************************************************************
646  static Yes test( Head );
647  static No test( ... );
648  static Type createType();
649  //**********************************************************************************************
650 
651  //**Member enumeration**************************************************************************
652  enum { tmp = sizeof( test( createType() ) ) == sizeof( Yes ) ? 1 : 0 };
653  //**********************************************************************************************
654 
655  public:
656  //**Member enumeration**************************************************************************
657  enum { value = tmp == 1 ? 1 : ( ContainsRelated<Tail,Type>::value ) };
658  //**********************************************************************************************
659 };
661 //*************************************************************************************************
662 
663 
664 //*************************************************************************************************
683 template< typename TList // Type of the type list
684  , typename Type > // The search type
685 struct IndexOf;
686 //*************************************************************************************************
687 
688 
689 //*************************************************************************************************
694 template< typename Type > // The search type
695 struct IndexOf< NullType, Type >
696 {
697  //**Member enumeration**************************************************************************
698  enum { value = -1 };
699  //**********************************************************************************************
700 };
702 //*************************************************************************************************
703 
704 
705 //*************************************************************************************************
710 template< typename Tail // Type of the tail of the type list
711  , typename Type > // The search type
712 struct IndexOf< TypeList<Type,Tail>, Type >
713 {
714  //**Member enumeration**************************************************************************
715  enum { value = 0 };
716  //**********************************************************************************************
717 };
719 //*************************************************************************************************
720 
721 
722 //*************************************************************************************************
727 template< typename Head // Type of the head of the type list
728  , typename Tail // Type of the tail of the type list
729  , typename Type > // The search type
730 struct IndexOf< TypeList<Head,Tail>, Type >
731 {
732  private:
733  //**Member enumeration**************************************************************************
734  enum { tmp = IndexOf<Tail,Type>::value };
735  //**********************************************************************************************
736 
737  public:
738  //**Member enumeration**************************************************************************
739  enum { value = tmp == -1 ? -1 : 1 + tmp };
740  //**********************************************************************************************
741 };
743 //*************************************************************************************************
744 
745 
746 
747 
748 //=================================================================================================
749 //
750 // APPENDING TO TYPE LISTS
751 //
752 //=================================================================================================
753 
754 //*************************************************************************************************
769 template< typename TList // Type of the type list
770  , typename Type > // The type to be appended to the type list
771 struct Append;
772 //*************************************************************************************************
773 
774 
775 //*************************************************************************************************
780 template<>
781 struct Append< NullType, NullType >
782 {
783  //**Type definitions****************************************************************************
784  using Result = NullType;
785  //**********************************************************************************************
786 };
788 //*************************************************************************************************
789 
790 
791 //*************************************************************************************************
796 template< typename Type > // The type to be appended to the type list
797 struct Append< NullType, Type >
798 {
799  //**Type definitions****************************************************************************
800  using Result = BLAZE_TYPELIST_1( Type );
801  //**********************************************************************************************
802 };
804 //*************************************************************************************************
805 
806 
807 //*************************************************************************************************
812 template< typename Head // Type of the head of the type list
813  , typename Tail > // Type of the tail of the type list
814 struct Append< NullType, TypeList<Head,Tail> >
815 {
816  //**Type definitions****************************************************************************
817  using Result = TypeList<Head,Tail>;
818  //**********************************************************************************************
819 };
821 //*************************************************************************************************
822 
823 
824 //*************************************************************************************************
829 template< typename Head // Type of the head of the type list
830  , typename Tail // Type of the tail of the type list
831  , typename Type > // The type to be appended to the type list
832 struct Append< TypeList<Head,Tail>, Type >
833 {
834  //**Type definitions****************************************************************************
836  //**********************************************************************************************
837 };
839 //*************************************************************************************************
840 
841 
842 
843 
844 //=================================================================================================
845 //
846 // ERASING FROM TYPE LISTS
847 //
848 //=================================================================================================
849 
850 //*************************************************************************************************
868 template< typename TList // Type of the type list
869  , typename Type > // The type to be erased from the type list
870 struct Erase;
871 //*************************************************************************************************
872 
873 
874 //*************************************************************************************************
879 template< typename Type > // The type to be erased from the type list
880 struct Erase< NullType, Type >
881 {
882  //**Type definitions****************************************************************************
883  using Result = NullType;
884  //**********************************************************************************************
885 };
887 //*************************************************************************************************
888 
889 
890 //*************************************************************************************************
895 template< typename Type // The type to be erased from the type list
896  , typename Tail > // Type of the tail of the type list
897 struct Erase< TypeList<Type,Tail>, Type >
898 {
899  //**Type definitions****************************************************************************
900  using Result = Tail;
901  //**********************************************************************************************
902 };
904 //*************************************************************************************************
905 
906 
907 //*************************************************************************************************
912 template< typename Head // Type of the head of the type list
913  , typename Tail // Type of the tail of the type list
914  , typename Type > // The type to be erased from the type list
915 struct Erase< TypeList<Head,Tail>, Type >
916 {
917  //**Type definitions****************************************************************************
919  //**********************************************************************************************
920 };
922 //*************************************************************************************************
923 
924 
925 //*************************************************************************************************
943 template< typename TList // Type of the type list
944  , typename Type > // The type to be erased from the type list
945 struct EraseAll;
946 //*************************************************************************************************
947 
948 
949 //*************************************************************************************************
954 template< typename Type > // The type to be erased from the type list
955 struct EraseAll< NullType, Type >
956 {
957  //**Type definitions****************************************************************************
958  using Result = NullType;
959  //**********************************************************************************************
960 };
962 //*************************************************************************************************
963 
964 
965 //*************************************************************************************************
970 template< typename Type // The type to be erased from the type list
971  , typename Tail > // Type of the tail of the type list
972 struct EraseAll< TypeList<Type,Tail>, Type >
973 {
974  //**Type definitions****************************************************************************
975  using Result = typename EraseAll<Tail,Type>::Result;
976  //**********************************************************************************************
977 };
979 //*************************************************************************************************
980 
981 
982 //*************************************************************************************************
987 template< typename Head // Type of the head of the type list
988  , typename Tail // Type of the tail of the type list
989  , typename Type > // The type to be erased from the type list
990 struct EraseAll< TypeList<Head,Tail>, Type >
991 {
992  //**Type definitions****************************************************************************
994  //**********************************************************************************************
995 };
997 //*************************************************************************************************
998 
999 
1000 
1001 
1002 //=================================================================================================
1003 //
1004 // REMOVING DUPLICATES FROM TYPE LISTS
1005 //
1006 //=================================================================================================
1007 
1008 //*************************************************************************************************
1025 template< typename TList > // Type of the type list
1026 struct Unique;
1027 //*************************************************************************************************
1028 
1029 
1030 //*************************************************************************************************
1035 template<>
1036 struct Unique< NullType >
1037 {
1038  //**Type definitions****************************************************************************
1039  using Result = NullType;
1040  //**********************************************************************************************
1041 };
1043 //*************************************************************************************************
1044 
1045 
1046 //*************************************************************************************************
1051 template< typename Head // Type of the head of the type list
1052  , typename Tail > // Type of the tail of the type list
1053 struct Unique< TypeList<Head,Tail> >
1054 {
1055  private:
1056  //**Type definitions****************************************************************************
1057  using TL1 = typename Unique<Tail>::Result;
1058  using TL2 = typename Erase<TL1,Head>::Result;
1059  //**********************************************************************************************
1060 
1061  public:
1062  //**Type definitions****************************************************************************
1063  using Result = TypeList<Head,TL2>;
1064  //**********************************************************************************************
1065 };
1067 //*************************************************************************************************
1068 
1069 } // namespace blaze
1070 
1071 #endif
Header file for basic type definitions.
Utility type for generic codes.
Appending a type to a type list.The Append class can be used to append the data type Type to a type l...
Definition: TypeList.h:771
Erasing the first occurrence of a type from a type list.The Erase class can be used to erase the firs...
Definition: TypeList.h:870
T Tail
Type of the tail of the type list.
Definition: TypeList.h:120
Namespace of the Blaze C++ math library.
Definition: Blaze.h:57
#define BLAZE_TYPELIST_1(T1)
Type list generation macro.This macro creates a type list only consisting of the type T1...
Definition: TypeList.h:149
Implementation of a type list.The TypeList class is an implementation of a type list according to the...
Definition: TypeList.h:116
Calculating the length of a type list.The Length class can be used to obtain the length of a type lis...
Definition: TypeList.h:369
Searching a type list.The ContainsRelated class can be used to search the type list for a type relate...
Definition: TypeList.h:609
Indexing a type list.The TypeAt class can be used to access a type list at a specified position to qu...
Definition: TypeList.h:434
Erasing all occurrences of a type from a type list.The EraseAll class can be used to erase all occurr...
Definition: TypeList.h:945
Utility type for generic codes.The NullType class represents an invalid or terminating data type for ...
Definition: NullType.h:54
Erasing all duplicates from a type list.The Unique class can be used to erase all duplicates from a t...
Definition: TypeList.h:1026
H Head
Type of the head of the type list.
Definition: TypeList.h:119
Searching a type list.The Contains class can be used to search the type list for a particular type Ty...
Definition: TypeList.h:520
Searching a type list.The IndexOf class can be used to search the type list for a particular type Typ...
Definition: TypeList.h:685