Singleton.h
Go to the documentation of this file.
1 //=================================================================================================
33 // //=================================================================================================
34 
35 #ifndef _BLAZE_UTIL_SINGLETON_SINGLETON_H_
36 #define _BLAZE_UTIL_SINGLETON_SINGLETON_H_
37 
38 
39 //*************************************************************************************************
40 // Includes
41 //*************************************************************************************************
42 
43 #include <memory>
44 #include <mutex>
46 #include <blaze/util/NonCopyable.h>
47 #include <blaze/util/NullType.h>
48 #include <blaze/util/Suffix.h>
49 #include <blaze/util/TypeList.h>
50 
51 
52 namespace blaze {
53 
54 //=================================================================================================
55 //
56 // ::blaze NAMESPACE FORWARD DECLARATIONS
57 //
58 //=================================================================================================
59 
60 template< typename > class Dependency;
61 template< typename T, typename TL, bool C > struct HasCyclicDependency;
62 
63 
64 
65 
66 //=================================================================================================
67 //
68 // CLASS HASCYCLICDEPENDENCYHELPER
69 //
70 //=================================================================================================
71 
72 //*************************************************************************************************
80 template< typename TL // Type list of checked lifetime dependencies
81  , typename D // Type list of lifetime dependencies to check
82  , size_t N = Length<D>::value > // Length of the dependency type list
83 struct HasCyclicDependencyHelper;
85 //*************************************************************************************************
86 
87 
88 //*************************************************************************************************
96 template< typename TL // Type list of checked lifetime dependencies
97  , size_t N > // Length of the dependency type list
98 struct HasCyclicDependencyHelper<TL,NullType,N>
99 {
100  enum : bool { value = 0 };
101 };
103 //*************************************************************************************************
104 
105 
106 //*************************************************************************************************
114 template< typename TL // Type list of checked lifetime dependencies
115  , typename D > // Type list of lifetime dependencies to check
116 struct HasCyclicDependencyHelper<TL,D,1>
117 {
118  typedef typename TypeAt<D,0>::Result D1;
119 
120  enum : bool { value = HasCyclicDependency<D1,TL,Contains<TL,D1>::value>::value };
121 };
123 //*************************************************************************************************
124 
125 
126 //*************************************************************************************************
134 template< typename TL // Type list of checked lifetime dependencies
135  , typename D > // Type list of lifetime dependencies to check
136 struct HasCyclicDependencyHelper<TL,D,2>
137 {
138  typedef typename TypeAt<D,0>::Result D1;
139  typedef typename TypeAt<D,1>::Result D2;
140 
141  enum : bool { value = HasCyclicDependency<D1,TL,Contains<TL,D1>::value>::value ||
143 };
145 //*************************************************************************************************
146 
147 
148 //*************************************************************************************************
156 template< typename TL // Type list of checked lifetime dependencies
157  , typename D > // Type list of lifetime dependencies to check
158 struct HasCyclicDependencyHelper<TL,D,3>
159 {
160  typedef typename TypeAt<D,0>::Result D1;
161  typedef typename TypeAt<D,1>::Result D2;
162  typedef typename TypeAt<D,2>::Result D3;
163 
164  enum : bool { value = HasCyclicDependency<D1,TL,Contains<TL,D1>::value>::value ||
167 };
169 //*************************************************************************************************
170 
171 
172 //*************************************************************************************************
180 template< typename TL // Type list of checked lifetime dependencies
181  , typename D > // Type list of lifetime dependencies to check
182 struct HasCyclicDependencyHelper<TL,D,4>
183 {
184  typedef typename TypeAt<D,0>::Result D1;
185  typedef typename TypeAt<D,1>::Result D2;
186  typedef typename TypeAt<D,2>::Result D3;
187  typedef typename TypeAt<D,3>::Result D4;
188 
189  enum : bool { value = HasCyclicDependency<D1,TL,Contains<TL,D1>::value>::value ||
193 };
195 //*************************************************************************************************
196 
197 
198 //*************************************************************************************************
206 template< typename TL // Type list of checked lifetime dependencies
207  , typename D > // Type list of lifetime dependencies to check
208 struct HasCyclicDependencyHelper<TL,D,5>
209 {
210  typedef typename TypeAt<D,0>::Result D1;
211  typedef typename TypeAt<D,1>::Result D2;
212  typedef typename TypeAt<D,2>::Result D3;
213  typedef typename TypeAt<D,3>::Result D4;
214  typedef typename TypeAt<D,4>::Result D5;
215 
216  enum : bool { value = HasCyclicDependency<D1,TL,Contains<TL,D1>::value>::value ||
221 };
223 //*************************************************************************************************
224 
225 
226 //*************************************************************************************************
234 template< typename TL // Type list of checked lifetime dependencies
235  , typename D > // Type list of lifetime dependencies to check
236 struct HasCyclicDependencyHelper<TL,D,6>
237 {
238  typedef typename TypeAt<D,0>::Result D1;
239  typedef typename TypeAt<D,1>::Result D2;
240  typedef typename TypeAt<D,2>::Result D3;
241  typedef typename TypeAt<D,3>::Result D4;
242  typedef typename TypeAt<D,4>::Result D5;
243  typedef typename TypeAt<D,5>::Result D6;
244 
245  enum : bool { value = HasCyclicDependency<D1,TL,Contains<TL,D1>::value>::value ||
251 };
253 //*************************************************************************************************
254 
255 
256 //*************************************************************************************************
264 template< typename TL // Type list of checked lifetime dependencies
265  , typename D > // Type list of lifetime dependencies to check
266 struct HasCyclicDependencyHelper<TL,D,7>
267 {
268  typedef typename TypeAt<D,0>::Result D1;
269  typedef typename TypeAt<D,1>::Result D2;
270  typedef typename TypeAt<D,2>::Result D3;
271  typedef typename TypeAt<D,3>::Result D4;
272  typedef typename TypeAt<D,4>::Result D5;
273  typedef typename TypeAt<D,5>::Result D6;
274  typedef typename TypeAt<D,6>::Result D7;
275 
276  enum : bool { value = HasCyclicDependency<D1,TL,Contains<TL,D1>::value>::value ||
283 };
285 //*************************************************************************************************
286 
287 
288 //*************************************************************************************************
296 template< typename TL // Type list of checked lifetime dependencies
297  , typename D > // Type list of lifetime dependencies to check
298 struct HasCyclicDependencyHelper<TL,D,8>
299 {
300  typedef typename TypeAt<D,0>::Result D1;
301  typedef typename TypeAt<D,1>::Result D2;
302  typedef typename TypeAt<D,2>::Result D3;
303  typedef typename TypeAt<D,3>::Result D4;
304  typedef typename TypeAt<D,4>::Result D5;
305  typedef typename TypeAt<D,5>::Result D6;
306  typedef typename TypeAt<D,6>::Result D7;
307  typedef typename TypeAt<D,7>::Result D8;
308 
309  enum : bool { value = HasCyclicDependency<D1,TL,Contains<TL,D1>::value>::value ||
317 };
319 //*************************************************************************************************
320 
321 
322 
323 
324 //=================================================================================================
325 //
326 // CLASS HASCYCLICDEPENDENCY
327 //
328 //=================================================================================================
329 
330 //*************************************************************************************************
339 template< typename T // The type to be checked for cyclic lifetime dependencies
340  , typename TL // Type list of checked lifetime dependencies
341  , bool C=Contains<TL,T>::value > // Flag to indicate whether T is contained in TL
342 struct HasCyclicDependency
343 {
344  typedef typename Append<TL,T>::Result ETL;
345  enum : bool { value = HasCyclicDependencyHelper<ETL,typename T::Dependencies>::value };
346 };
348 //*************************************************************************************************
349 
350 
351 //*************************************************************************************************
360 template< typename T // The type to be checked for cyclic lifetime dependencies
361  , typename TL > // Type list of checked lifetime dependencies
362 struct HasCyclicDependency<T,TL,true>
363 {
364  enum : bool { value = 1 };
365 };
367 //*************************************************************************************************
368 
369 
370 
371 
372 //=================================================================================================
373 //
374 // DETECT_CYCLIC_LIFETIME_DEPENDENCY CONSTRAINT
375 //
376 //=================================================================================================
377 
378 //*************************************************************************************************
384 #define BLAZE_DETECT_CYCLIC_LIFETIME_DEPENDENCY(T) \
385  static_assert( ( !blaze::HasCyclicDependency<T,blaze::NullType>::value ), "Cyclic dependency detected" )
386 //*************************************************************************************************
387 
388 
389 
390 
391 //=================================================================================================
392 //
393 // BEFRIEND_SINGLETON MACRO
394 //
395 //=================================================================================================
396 
397 //*************************************************************************************************
404 #define BLAZE_BEFRIEND_SINGLETON \
405  template< typename, typename, typename, typename, typename, typename, typename, typename, typename > friend class blaze::Singleton; \
406  template< typename, typename, bool > friend struct blaze::HasCyclicDependency; \
407  template< typename > friend class blaze::Dependency;
408 //*************************************************************************************************
409 
410 
411 
412 
413 //=================================================================================================
414 //
415 // CLASS SINGLETON
416 //
417 //=================================================================================================
418 
419 //*************************************************************************************************
553 template< typename T // Type of the singleton (CRTP pattern)
554  , typename D1 = NullType // Type of the first lifetime dependency
555  , typename D2 = NullType // Type of the second lifetime dependency
556  , typename D3 = NullType // Type of the third lifetime dependency
557  , typename D4 = NullType // Type of the fourth lifetime dependency
558  , typename D5 = NullType // Type of the fifth lifetime dependency
559  , typename D6 = NullType // Type of the sixth lifetime dependency
560  , typename D7 = NullType // Type of the seventh lifetime dependency
561  , typename D8 = NullType > // Type of the eighth lifetime dependency
562 class Singleton : private NonCopyable
563 {
564  public:
565  //**Type definitions****************************************************************************
568 
570  typedef BLAZE_TYPELIST_8( D1, D2, D3, D4, D5, D6, D7, D8 ) Dependencies;
571  //**********************************************************************************************
572 
573  protected:
574  //**Constructor*********************************************************************************
579  explicit Singleton()
580  : dependency1_( D1::instance() ) // Handle to the first lifetime dependency
581  , dependency2_( D2::instance() ) // Handle to the second lifetime dependency
582  , dependency3_( D3::instance() ) // Handle to the third lifetime dependency
583  , dependency4_( D4::instance() ) // Handle to the fourth lifetime dependency
584  , dependency5_( D5::instance() ) // Handle to the fifth lifetime dependency
585  , dependency6_( D6::instance() ) // Handle to the sixth lifetime dependency
586  , dependency7_( D7::instance() ) // Handle to the seventh lifetime dependency
587  , dependency8_( D8::instance() ) // Handle to the eighth lifetime dependency
588  {
589  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( T, SingletonType );
590  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D1, typename D1::SingletonType );
591  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D2, typename D2::SingletonType );
592  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D3, typename D3::SingletonType );
593  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D4, typename D4::SingletonType );
594  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D5, typename D5::SingletonType );
595  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D6, typename D6::SingletonType );
596  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D7, typename D7::SingletonType );
597  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D8, typename D8::SingletonType );
606  }
607  //**********************************************************************************************
608 
609  //**Destructor**********************************************************************************
613  {}
614  //**********************************************************************************************
615 
616  public:
617  //**Instance function***************************************************************************
620  static std::shared_ptr<T> instance()
621  {
622  static std::shared_ptr<T> object( new T() );
623  return object;
624  }
626  //**********************************************************************************************
627 
628  private:
629  //**Member variables****************************************************************************
632  std::shared_ptr<D1> dependency1_;
633  std::shared_ptr<D2> dependency2_;
634  std::shared_ptr<D3> dependency3_;
635  std::shared_ptr<D4> dependency4_;
636  std::shared_ptr<D5> dependency5_;
637  std::shared_ptr<D6> dependency6_;
638  std::shared_ptr<D7> dependency7_;
639  std::shared_ptr<D8> dependency8_;
640 
641  //**********************************************************************************************
642 };
643 //*************************************************************************************************
644 
645 
646 
647 
648 //=================================================================================================
649 //
650 // SINGLETON SPECIALIZATION (7 LIFETIME DEPENDENCIES)
651 //
652 //=================================================================================================
653 
654 //*************************************************************************************************
662 template< typename T // Type of the singleton (CRTP pattern)
663  , typename D1 // Type of the first lifetime dependency
664  , typename D2 // Type of the second lifetime dependency
665  , typename D3 // Type of the third lifetime dependency
666  , typename D4 // Type of the fourth lifetime dependency
667  , typename D5 // Type of the fifth lifetime dependency
668  , typename D6 // Type of the sixth lifetime dependency
669  , typename D7 > // Type of the eighth lifetime dependency
670 class Singleton<T,D1,D2,D3,D4,D5,D6,D7,NullType> : private NonCopyable
671 {
672  public:
673  //**Type definitions****************************************************************************
676 
678  typedef BLAZE_TYPELIST_7( D1, D2, D3, D4, D5, D6, D7 ) Dependencies;
679  //**********************************************************************************************
680 
681  protected:
682  //**Constructor*********************************************************************************
687  explicit Singleton()
688  : dependency1_( D1::instance() ) // Handle to the first lifetime dependency
689  , dependency2_( D2::instance() ) // Handle to the second lifetime dependency
690  , dependency3_( D3::instance() ) // Handle to the third lifetime dependency
691  , dependency4_( D4::instance() ) // Handle to the fourth lifetime dependency
692  , dependency5_( D5::instance() ) // Handle to the fifth lifetime dependency
693  , dependency6_( D6::instance() ) // Handle to the sixth lifetime dependency
694  , dependency7_( D7::instance() ) // Handle to the seventh lifetime dependency
695  {
696  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( T, SingletonType );
697  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D1, typename D1::SingletonType );
698  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D2, typename D2::SingletonType );
699  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D3, typename D3::SingletonType );
700  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D4, typename D4::SingletonType );
701  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D5, typename D5::SingletonType );
702  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D6, typename D6::SingletonType );
703  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D7, typename D7::SingletonType );
711  }
712  //**********************************************************************************************
713 
714  //**Destructor**********************************************************************************
717  ~Singleton()
718  {}
719  //**********************************************************************************************
720 
721  public:
722  //**Instance function***************************************************************************
725  static std::shared_ptr<T> instance()
726  {
727  static std::shared_ptr<T> object( new T() );
728  return object;
729  }
731  //**********************************************************************************************
732 
733  private:
734  //**Member variables****************************************************************************
737  std::shared_ptr<D1> dependency1_;
738  std::shared_ptr<D2> dependency2_;
739  std::shared_ptr<D3> dependency3_;
740  std::shared_ptr<D4> dependency4_;
741  std::shared_ptr<D5> dependency5_;
742  std::shared_ptr<D6> dependency6_;
743  std::shared_ptr<D7> dependency7_;
744 
745  //**********************************************************************************************
746 };
748 //*************************************************************************************************
749 
750 
751 
752 
753 //=================================================================================================
754 //
755 // SINGLETON SPECIALIZATION (6 LIFETIME DEPENDENCIES)
756 //
757 //=================================================================================================
758 
759 //*************************************************************************************************
767 template< typename T // Type of the singleton (CRTP pattern)
768  , typename D1 // Type of the first lifetime dependency
769  , typename D2 // Type of the second lifetime dependency
770  , typename D3 // Type of the third lifetime dependency
771  , typename D4 // Type of the fourth lifetime dependency
772  , typename D5 // Type of the fifth lifetime dependency
773  , typename D6 > // Type of the eighth lifetime dependency
774 class Singleton<T,D1,D2,D3,D4,D5,D6,NullType,NullType> : private NonCopyable
775 {
776  public:
777  //**Type definitions****************************************************************************
780 
782  typedef BLAZE_TYPELIST_6( D1, D2, D3, D4, D5, D6 ) Dependencies;
783  //**********************************************************************************************
784 
785  protected:
786  //**Constructor*********************************************************************************
791  explicit Singleton()
792  : dependency1_( D1::instance() ) // Handle to the first lifetime dependency
793  , dependency2_( D2::instance() ) // Handle to the second lifetime dependency
794  , dependency3_( D3::instance() ) // Handle to the third lifetime dependency
795  , dependency4_( D4::instance() ) // Handle to the fourth lifetime dependency
796  , dependency5_( D5::instance() ) // Handle to the fifth lifetime dependency
797  , dependency6_( D6::instance() ) // Handle to the sixth lifetime dependency
798  {
799  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( T, SingletonType );
800  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D1, typename D1::SingletonType );
801  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D2, typename D2::SingletonType );
802  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D3, typename D3::SingletonType );
803  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D4, typename D4::SingletonType );
804  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D5, typename D5::SingletonType );
805  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D6, typename D6::SingletonType );
812  }
813  //**********************************************************************************************
814 
815  //**Destructor**********************************************************************************
818  ~Singleton()
819  {}
820  //**********************************************************************************************
821 
822  public:
823  //**Instance function***************************************************************************
826  static std::shared_ptr<T> instance()
827  {
828  static std::shared_ptr<T> object( new T() );
829  return object;
830  }
832  //**********************************************************************************************
833 
834  private:
835  //**Member variables****************************************************************************
838  std::shared_ptr<D1> dependency1_;
839  std::shared_ptr<D2> dependency2_;
840  std::shared_ptr<D3> dependency3_;
841  std::shared_ptr<D4> dependency4_;
842  std::shared_ptr<D5> dependency5_;
843  std::shared_ptr<D6> dependency6_;
844 
845  //**********************************************************************************************
846 };
848 //*************************************************************************************************
849 
850 
851 
852 
853 //=================================================================================================
854 //
855 // SINGLETON SPECIALIZATION (5 LIFETIME DEPENDENCIES)
856 //
857 //=================================================================================================
858 
859 //*************************************************************************************************
867 template< typename T // Type of the singleton (CRTP pattern)
868  , typename D1 // Type of the first lifetime dependency
869  , typename D2 // Type of the second lifetime dependency
870  , typename D3 // Type of the third lifetime dependency
871  , typename D4 // Type of the fourth lifetime dependency
872  , typename D5 > // Type of the fifth lifetime dependency
873 class Singleton<T,D1,D2,D3,D4,D5,NullType,NullType,NullType> : private NonCopyable
874 {
875  public:
876  //**Type definitions****************************************************************************
879 
881  typedef BLAZE_TYPELIST_5( D1, D2, D3, D4, D5 ) Dependencies;
882  //**********************************************************************************************
883 
884  protected:
885  //**Constructor*********************************************************************************
890  explicit Singleton()
891  : dependency1_( D1::instance() ) // Handle to the first lifetime dependency
892  , dependency2_( D2::instance() ) // Handle to the second lifetime dependency
893  , dependency3_( D3::instance() ) // Handle to the third lifetime dependency
894  , dependency4_( D4::instance() ) // Handle to the fourth lifetime dependency
895  , dependency5_( D5::instance() ) // Handle to the fifth lifetime dependency
896  {
897  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( T, SingletonType );
898  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D1, typename D1::SingletonType );
899  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D2, typename D2::SingletonType );
900  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D3, typename D3::SingletonType );
901  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D4, typename D4::SingletonType );
902  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D5, typename D5::SingletonType );
908  }
909  //**********************************************************************************************
910 
911  //**Destructor**********************************************************************************
914  ~Singleton()
915  {}
916  //**********************************************************************************************
917 
918  public:
919  //**Instance function***************************************************************************
922  static std::shared_ptr<T> instance()
923  {
924  static std::shared_ptr<T> object( new T() );
925  return object;
926  }
928  //**********************************************************************************************
929 
930  private:
931  //**Member variables****************************************************************************
934  std::shared_ptr<D1> dependency1_;
935  std::shared_ptr<D2> dependency2_;
936  std::shared_ptr<D3> dependency3_;
937  std::shared_ptr<D4> dependency4_;
938  std::shared_ptr<D5> dependency5_;
939 
940  //**********************************************************************************************
941 };
943 //*************************************************************************************************
944 
945 
946 
947 
948 //=================================================================================================
949 //
950 // SINGLETON SPECIALIZATION (4 LIFETIME DEPENDENCIES)
951 //
952 //=================================================================================================
953 
954 //*************************************************************************************************
962 template< typename T // Type of the singleton (CRTP pattern)
963  , typename D1 // Type of the first lifetime dependency
964  , typename D2 // Type of the second lifetime dependency
965  , typename D3 // Type of the third lifetime dependency
966  , typename D4 > // Type of the fourth lifetime dependency
967 class Singleton<T,D1,D2,D3,D4,NullType,NullType,NullType,NullType> : private NonCopyable
968 {
969  public:
970  //**Type definitions****************************************************************************
973 
975  typedef BLAZE_TYPELIST_4( D1, D2, D3, D4 ) Dependencies;
976  //**********************************************************************************************
977 
978  protected:
979  //**Constructor*********************************************************************************
984  explicit Singleton()
985  : dependency1_( D1::instance() ) // Handle to the first lifetime dependency
986  , dependency2_( D2::instance() ) // Handle to the second lifetime dependency
987  , dependency3_( D3::instance() ) // Handle to the third lifetime dependency
988  , dependency4_( D4::instance() ) // Handle to the fourth lifetime dependency
989  {
990  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( T, SingletonType );
991  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D1, typename D1::SingletonType );
992  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D2, typename D2::SingletonType );
993  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D3, typename D3::SingletonType );
994  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D4, typename D4::SingletonType );
999  }
1000  //**********************************************************************************************
1001 
1002  //**Destructor**********************************************************************************
1005  ~Singleton()
1006  {}
1007  //**********************************************************************************************
1008 
1009  public:
1010  //**Instance function***************************************************************************
1013  static std::shared_ptr<T> instance()
1014  {
1015  static std::shared_ptr<T> object( new T() );
1016  return object;
1017  }
1019  //**********************************************************************************************
1020 
1021  private:
1022  //**Member variables****************************************************************************
1025  std::shared_ptr<D1> dependency1_;
1026  std::shared_ptr<D2> dependency2_;
1027  std::shared_ptr<D3> dependency3_;
1028  std::shared_ptr<D4> dependency4_;
1029 
1030  //**********************************************************************************************
1031 };
1033 //*************************************************************************************************
1034 
1035 
1036 
1037 
1038 //=================================================================================================
1039 //
1040 // SINGLETON SPECIALIZATION (3 LIFETIME DEPENDENCIES)
1041 //
1042 //=================================================================================================
1043 
1044 //*************************************************************************************************
1052 template< typename T // Type of the singleton (CRTP pattern)
1053  , typename D1 // Type of the first lifetime dependency
1054  , typename D2 // Type of the second lifetime dependency
1055  , typename D3 > // Type of the third lifetime dependency
1056 class Singleton<T,D1,D2,D3,NullType,NullType,NullType,NullType,NullType> : private NonCopyable
1057 {
1058  public:
1059  //**Type definitions****************************************************************************
1062 
1064  typedef BLAZE_TYPELIST_3( D1, D2, D3 ) Dependencies;
1065  //**********************************************************************************************
1066 
1067  protected:
1068  //**Constructor*********************************************************************************
1073  explicit Singleton()
1074  : dependency1_( D1::instance() ) // Handle to the first lifetime dependency
1075  , dependency2_( D2::instance() ) // Handle to the second lifetime dependency
1076  , dependency3_( D3::instance() ) // Handle to the third lifetime dependency
1077  {
1078  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( T, SingletonType );
1079  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D1, typename D1::SingletonType );
1080  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D2, typename D2::SingletonType );
1081  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D3, typename D3::SingletonType );
1085  }
1086  //**********************************************************************************************
1087 
1088  //**Destructor**********************************************************************************
1091  ~Singleton()
1092  {}
1093  //**********************************************************************************************
1094 
1095  public:
1096  //**Instance function***************************************************************************
1099  static std::shared_ptr<T> instance()
1100  {
1101  static std::shared_ptr<T> object( new T() );
1102  return object;
1103  }
1105  //**********************************************************************************************
1106 
1107  private:
1108  //**Member variables****************************************************************************
1111  std::shared_ptr<D1> dependency1_;
1112  std::shared_ptr<D2> dependency2_;
1113  std::shared_ptr<D3> dependency3_;
1114 
1115  //**********************************************************************************************
1116 };
1118 //*************************************************************************************************
1119 
1120 
1121 
1122 
1123 //=================================================================================================
1124 //
1125 // SINGLETON SPECIALIZATION (2 LIFETIME DEPENDENCIES)
1126 //
1127 //=================================================================================================
1128 
1129 //*************************************************************************************************
1137 template< typename T // Type of the singleton (CRTP pattern)
1138  , typename D1 // Type of the first lifetime dependency
1139  , typename D2 > // Type of the second lifetime dependency
1140 class Singleton<T,D1,D2,NullType,NullType,NullType,NullType,NullType,NullType> : private NonCopyable
1141 {
1142  public:
1143  //**Type definitions****************************************************************************
1146 
1148  typedef BLAZE_TYPELIST_2( D1, D2 ) Dependencies;
1149  //**********************************************************************************************
1150 
1151  protected:
1152  //**Constructor*********************************************************************************
1157  explicit Singleton()
1158  : dependency1_( D1::instance() ) // Handle to the first lifetime dependency
1159  , dependency2_( D2::instance() ) // Handle to the second lifetime dependency
1160  {
1161  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( T, SingletonType );
1162  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D1, typename D1::SingletonType );
1163  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D2, typename D2::SingletonType );
1166  }
1167  //**********************************************************************************************
1168 
1169  //**Destructor**********************************************************************************
1172  ~Singleton()
1173  {}
1174  //**********************************************************************************************
1175 
1176  public:
1177  //**Instance function***************************************************************************
1180  static std::shared_ptr<T> instance()
1181  {
1182  static std::shared_ptr<T> object( new T() );
1183  return object;
1184  }
1186  //**********************************************************************************************
1187 
1188  private:
1189  //**Member variables****************************************************************************
1192  std::shared_ptr<D1> dependency1_;
1193  std::shared_ptr<D2> dependency2_;
1194 
1195  //**********************************************************************************************
1196 };
1198 //*************************************************************************************************
1199 
1200 
1201 
1202 
1203 //=================================================================================================
1204 //
1205 // SINGLETON SPECIALIZATION (1 LIFETIME DEPENDENCY)
1206 //
1207 //=================================================================================================
1208 
1209 //*************************************************************************************************
1217 template< typename T // Type of the singleton (CRTP pattern)
1218  , typename D1 > // Type of the lifetime dependency
1219 class Singleton<T,D1,NullType,NullType,NullType,NullType,NullType,NullType,NullType> : private NonCopyable
1220 {
1221  public:
1222  //**Type definitions****************************************************************************
1225 
1227  typedef BLAZE_TYPELIST_1( D1 ) Dependencies;
1228  //**********************************************************************************************
1229 
1230  protected:
1231  //**Constructor*********************************************************************************
1236  explicit Singleton()
1237  : dependency1_( D1::instance() ) // Handle to the lifetime dependency
1238  {
1239  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( T, SingletonType );
1240  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D1, typename D1::SingletonType );
1242  }
1243  //**********************************************************************************************
1244 
1245  //**Destructor**********************************************************************************
1248  ~Singleton()
1249  {}
1250  //**********************************************************************************************
1251 
1252  public:
1253  //**Instance function***************************************************************************
1256  static std::shared_ptr<T> instance()
1257  {
1258  static std::shared_ptr<T> object( new T() );
1259  return object;
1260  }
1262  //**********************************************************************************************
1263 
1264  private:
1265  //**Member variables****************************************************************************
1268  std::shared_ptr<D1> dependency1_;
1269 
1270  //**********************************************************************************************
1271 };
1273 //*************************************************************************************************
1274 
1275 
1276 
1277 
1278 //=================================================================================================
1279 //
1280 // SINGLETON SPECIALIZATION (0 LIFETIME DEPENDENCIES)
1281 //
1282 //=================================================================================================
1283 
1284 //*************************************************************************************************
1292 template< typename T > // Type of the singleton (CRTP pattern)
1293 class Singleton<T,NullType,NullType,NullType,NullType,NullType,NullType,NullType,NullType> : private NonCopyable
1294 {
1295  public:
1296  //**Type definitions****************************************************************************
1299 
1301  typedef NullType Dependencies;
1302  //**********************************************************************************************
1303 
1304  protected:
1305  //**Constructor*********************************************************************************
1310  explicit Singleton()
1311  {
1312  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( T, SingletonType );
1313  }
1314  //**********************************************************************************************
1315 
1316  //**Destructor**********************************************************************************
1319  ~Singleton()
1320  {}
1321  //**********************************************************************************************
1322 
1323  public:
1324  //**Instance function***************************************************************************
1327  static std::shared_ptr<T> instance()
1328  {
1329  static std::shared_ptr<T> object( new T() );
1330  return object;
1331  }
1333  //**********************************************************************************************
1334 };
1336 //*************************************************************************************************
1337 
1338 } // namespace blaze
1339 
1340 #endif
#define BLAZE_TYPELIST_3(T1, T2, T3)
Type list generation macro.This macro creates a type list consisting of the three types T1...
Definition: TypeList.h:191
Utility type for generic codes.
#define BLAZE_TYPELIST_6(T1, T2, T3, T4, T5, T6)
Type list generation macro.This macro creates a type list consisting of the six types T1...
Definition: TypeList.h:254
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
#define BLAZE_TYPELIST_8(T1, T2, T3, T4, T5, T6, T7, T8)
Type list generation macro.This macro creates a type list consisting of the eight types T1...
Definition: TypeList.h:296
std::shared_ptr< D1 > dependency1_
Handle to the first lifetime dependency.
Definition: Singleton.h:632
Base class for non-copyable class instances.
std::shared_ptr< D2 > dependency2_
Handle to the second lifetime dependency.
Definition: Singleton.h:633
std::shared_ptr< D7 > dependency7_
Handle to the seventh lifetime dependency.
Definition: Singleton.h:638
std::shared_ptr< D4 > dependency4_
Handle to the fourth lifetime dependency.
Definition: Singleton.h:635
~Singleton()
Destructor for the Singleton class.
Definition: Singleton.h:612
std::shared_ptr< D8 > dependency8_
Handle to the eighth lifetime dependency.
Definition: Singleton.h:639
std::shared_ptr< D5 > dependency5_
Handle to the fifth lifetime dependency.
Definition: Singleton.h:636
std::shared_ptr< D3 > dependency3_
Handle to the third lifetime dependency.
Definition: Singleton.h:634
Namespace of the Blaze C++ math library.
Definition: Blaze.h:57
std::shared_ptr< D6 > dependency6_
Handle to the sixth lifetime dependency.
Definition: Singleton.h:637
#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
#define BLAZE_TYPELIST_7(T1, T2, T3, T4, T5, T6, T7)
Type list generation macro.This macro creates a type list consisting of the seven types T1...
Definition: TypeList.h:275
#define BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM(D, B)
Constraint on the inheritance relationship of a data type.In case D is not derived from B...
Definition: DerivedFrom.h:60
#define BLAZE_TYPELIST_2(T1, T2)
Type list generation macro.This macro creates a type list consisting of the two types T1 and T2...
Definition: TypeList.h:170
Base class for non-copyable class instances.The NonCopyable class is intended to work as a base class...
Definition: NonCopyable.h:63
Constraint on the inheritance relationship of a data type.
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
Base class for all lifetime managed singletons.The Singleton class represents the base class for all ...
Definition: Singleton.h:562
#define BLAZE_TYPELIST_5(T1, T2, T3, T4, T5)
Type list generation macro.This macro creates a type list consisting of the five types T1...
Definition: TypeList.h:233
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
Header file for a type list implementation.
Utility type for generic codes.The NullType class represents an invalid or terminating data type for ...
Definition: NullType.h:54
Singleton< T, D1, D2, D3, D4, D5, D6, D7, D8 > SingletonType
Type of this Singleton instance.
Definition: Singleton.h:567
#define BLAZE_TYPELIST_4(T1, T2, T3, T4)
Type list generation macro.This macro creates a type list consisting of the four types T1...
Definition: TypeList.h:212
Definition: Singleton.h:61
#define BLAZE_DETECT_CYCLIC_LIFETIME_DEPENDENCY(T)
Constraint on the data type.In case the given data type T is not an integral data type...
Definition: Singleton.h:384
Searching a type list.The Contains class can be used to search the type list for a particular type Ty...
Definition: TypeList.h:520
Singleton()
Constructor for the Singleton class.
Definition: Singleton.h:579
Header file for compile time constraints.