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  using D1 = typename TypeAt<D,0>::Result;
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  using D1 = typename TypeAt<D,0>::Result;
139  using D2 = typename TypeAt<D,1>::Result;
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  using D1 = typename TypeAt<D,0>::Result;
161  using D2 = typename TypeAt<D,1>::Result;
162  using D3 = typename TypeAt<D,2>::Result;
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  using D1 = typename TypeAt<D,0>::Result;
185  using D2 = typename TypeAt<D,1>::Result;
186  using D3 = typename TypeAt<D,2>::Result;
187  using D4 = typename TypeAt<D,3>::Result;
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  using D1 = typename TypeAt<D,0>::Result;
211  using D2 = typename TypeAt<D,1>::Result;
212  using D3 = typename TypeAt<D,2>::Result;
213  using D4 = typename TypeAt<D,3>::Result;
214  using D5 = typename TypeAt<D,4>::Result;
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  using D1 = typename TypeAt<D,0>::Result;
239  using D2 = typename TypeAt<D,1>::Result;
240  using D3 = typename TypeAt<D,2>::Result;
241  using D4 = typename TypeAt<D,3>::Result;
242  using D5 = typename TypeAt<D,4>::Result;
243  using D6 = typename TypeAt<D,5>::Result;
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  using D1 = typename TypeAt<D,0>::Result;
269  using D2 = typename TypeAt<D,1>::Result;
270  using D3 = typename TypeAt<D,2>::Result;
271  using D4 = typename TypeAt<D,3>::Result;
272  using D5 = typename TypeAt<D,4>::Result;
273  using D6 = typename TypeAt<D,5>::Result;
274  using D7 = typename TypeAt<D,6>::Result;
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  using D1 = typename TypeAt<D,0>::Result;
301  using D2 = typename TypeAt<D,1>::Result;
302  using D3 = typename TypeAt<D,2>::Result;
303  using D4 = typename TypeAt<D,3>::Result;
304  using D5 = typename TypeAt<D,4>::Result;
305  using D6 = typename TypeAt<D,5>::Result;
306  using D7 = typename TypeAt<D,6>::Result;
307  using D8 = typename TypeAt<D,7>::Result;
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  using ETL = typename Append<TL,T>::Result;
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
563  : private NonCopyable
564 {
565  public:
566  //**Type definitions****************************************************************************
569 
571  using Dependencies = BLAZE_TYPELIST_8( D1, D2, D3, D4, D5, D6, D7, D8 );
572  //**********************************************************************************************
573 
574  protected:
575  //**Constructor*********************************************************************************
580  explicit Singleton()
581  : dependency1_( D1::instance() ) // Handle to the first lifetime dependency
582  , dependency2_( D2::instance() ) // Handle to the second lifetime dependency
583  , dependency3_( D3::instance() ) // Handle to the third lifetime dependency
584  , dependency4_( D4::instance() ) // Handle to the fourth lifetime dependency
585  , dependency5_( D5::instance() ) // Handle to the fifth lifetime dependency
586  , dependency6_( D6::instance() ) // Handle to the sixth lifetime dependency
587  , dependency7_( D7::instance() ) // Handle to the seventh lifetime dependency
588  , dependency8_( D8::instance() ) // Handle to the eighth lifetime dependency
589  {
591  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D1, typename D1::SingletonType );
592  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D2, typename D2::SingletonType );
593  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D3, typename D3::SingletonType );
594  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D4, typename D4::SingletonType );
595  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D5, typename D5::SingletonType );
596  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D6, typename D6::SingletonType );
597  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D7, typename D7::SingletonType );
598  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D8, typename D8::SingletonType );
607  }
608  //**********************************************************************************************
609 
610  //**Destructor**********************************************************************************
614  {}
615  //**********************************************************************************************
616 
617  public:
618  //**Instance function***************************************************************************
621  static std::shared_ptr<T> instance()
622  {
623  static std::shared_ptr<T> object( new T() );
624  return object;
625  }
627  //**********************************************************************************************
628 
629  private:
630  //**Member variables****************************************************************************
633  std::shared_ptr<D1> dependency1_;
634  std::shared_ptr<D2> dependency2_;
635  std::shared_ptr<D3> dependency3_;
636  std::shared_ptr<D4> dependency4_;
637  std::shared_ptr<D5> dependency5_;
638  std::shared_ptr<D6> dependency6_;
639  std::shared_ptr<D7> dependency7_;
640  std::shared_ptr<D8> dependency8_;
641 
642  //**********************************************************************************************
643 };
644 //*************************************************************************************************
645 
646 
647 
648 
649 //=================================================================================================
650 //
651 // SINGLETON SPECIALIZATION (7 LIFETIME DEPENDENCIES)
652 //
653 //=================================================================================================
654 
655 //*************************************************************************************************
663 template< typename T // Type of the singleton (CRTP pattern)
664  , typename D1 // Type of the first lifetime dependency
665  , typename D2 // Type of the second lifetime dependency
666  , typename D3 // Type of the third lifetime dependency
667  , typename D4 // Type of the fourth lifetime dependency
668  , typename D5 // Type of the fifth lifetime dependency
669  , typename D6 // Type of the sixth lifetime dependency
670  , typename D7 > // Type of the eighth lifetime dependency
671 class Singleton<T,D1,D2,D3,D4,D5,D6,D7,NullType>
672  : private NonCopyable
673 {
674  public:
675  //**Type definitions****************************************************************************
678 
680  using Dependencies = BLAZE_TYPELIST_7( D1, D2, D3, D4, D5, D6, D7 );
681  //**********************************************************************************************
682 
683  protected:
684  //**Constructor*********************************************************************************
689  explicit Singleton()
690  : dependency1_( D1::instance() ) // Handle to the first lifetime dependency
691  , dependency2_( D2::instance() ) // Handle to the second lifetime dependency
692  , dependency3_( D3::instance() ) // Handle to the third lifetime dependency
693  , dependency4_( D4::instance() ) // Handle to the fourth lifetime dependency
694  , dependency5_( D5::instance() ) // Handle to the fifth lifetime dependency
695  , dependency6_( D6::instance() ) // Handle to the sixth lifetime dependency
696  , dependency7_( D7::instance() ) // Handle to the seventh lifetime dependency
697  {
699  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D1, typename D1::SingletonType );
700  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D2, typename D2::SingletonType );
701  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D3, typename D3::SingletonType );
702  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D4, typename D4::SingletonType );
703  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D5, typename D5::SingletonType );
704  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D6, typename D6::SingletonType );
705  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D7, typename D7::SingletonType );
713  }
714  //**********************************************************************************************
715 
716  //**Destructor**********************************************************************************
719  ~Singleton()
720  {}
721  //**********************************************************************************************
722 
723  public:
724  //**Instance function***************************************************************************
727  static std::shared_ptr<T> instance()
728  {
729  static std::shared_ptr<T> object( new T() );
730  return object;
731  }
733  //**********************************************************************************************
734 
735  private:
736  //**Member variables****************************************************************************
739  std::shared_ptr<D1> dependency1_;
740  std::shared_ptr<D2> dependency2_;
741  std::shared_ptr<D3> dependency3_;
742  std::shared_ptr<D4> dependency4_;
743  std::shared_ptr<D5> dependency5_;
744  std::shared_ptr<D6> dependency6_;
745  std::shared_ptr<D7> dependency7_;
746 
747  //**********************************************************************************************
748 };
750 //*************************************************************************************************
751 
752 
753 
754 
755 //=================================================================================================
756 //
757 // SINGLETON SPECIALIZATION (6 LIFETIME DEPENDENCIES)
758 //
759 //=================================================================================================
760 
761 //*************************************************************************************************
769 template< typename T // Type of the singleton (CRTP pattern)
770  , typename D1 // Type of the first lifetime dependency
771  , typename D2 // Type of the second lifetime dependency
772  , typename D3 // Type of the third lifetime dependency
773  , typename D4 // Type of the fourth lifetime dependency
774  , typename D5 // Type of the fifth lifetime dependency
775  , typename D6 > // Type of the eighth lifetime dependency
776 class Singleton<T,D1,D2,D3,D4,D5,D6,NullType,NullType>
777  : private NonCopyable
778 {
779  public:
780  //**Type definitions****************************************************************************
783 
785  using Dependencies = BLAZE_TYPELIST_6( D1, D2, D3, D4, D5, D6 );
786  //**********************************************************************************************
787 
788  protected:
789  //**Constructor*********************************************************************************
794  explicit Singleton()
795  : dependency1_( D1::instance() ) // Handle to the first lifetime dependency
796  , dependency2_( D2::instance() ) // Handle to the second lifetime dependency
797  , dependency3_( D3::instance() ) // Handle to the third lifetime dependency
798  , dependency4_( D4::instance() ) // Handle to the fourth lifetime dependency
799  , dependency5_( D5::instance() ) // Handle to the fifth lifetime dependency
800  , dependency6_( D6::instance() ) // Handle to the sixth lifetime dependency
801  {
803  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D1, typename D1::SingletonType );
804  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D2, typename D2::SingletonType );
805  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D3, typename D3::SingletonType );
806  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D4, typename D4::SingletonType );
807  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D5, typename D5::SingletonType );
808  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D6, typename D6::SingletonType );
815  }
816  //**********************************************************************************************
817 
818  //**Destructor**********************************************************************************
821  ~Singleton()
822  {}
823  //**********************************************************************************************
824 
825  public:
826  //**Instance function***************************************************************************
829  static std::shared_ptr<T> instance()
830  {
831  static std::shared_ptr<T> object( new T() );
832  return object;
833  }
835  //**********************************************************************************************
836 
837  private:
838  //**Member variables****************************************************************************
841  std::shared_ptr<D1> dependency1_;
842  std::shared_ptr<D2> dependency2_;
843  std::shared_ptr<D3> dependency3_;
844  std::shared_ptr<D4> dependency4_;
845  std::shared_ptr<D5> dependency5_;
846  std::shared_ptr<D6> dependency6_;
847 
848  //**********************************************************************************************
849 };
851 //*************************************************************************************************
852 
853 
854 
855 
856 //=================================================================================================
857 //
858 // SINGLETON SPECIALIZATION (5 LIFETIME DEPENDENCIES)
859 //
860 //=================================================================================================
861 
862 //*************************************************************************************************
870 template< typename T // Type of the singleton (CRTP pattern)
871  , typename D1 // Type of the first lifetime dependency
872  , typename D2 // Type of the second lifetime dependency
873  , typename D3 // Type of the third lifetime dependency
874  , typename D4 // Type of the fourth lifetime dependency
875  , typename D5 > // Type of the fifth lifetime dependency
876 class Singleton<T,D1,D2,D3,D4,D5,NullType,NullType,NullType>
877  : private NonCopyable
878 {
879  public:
880  //**Type definitions****************************************************************************
883 
885  using Dependencies = BLAZE_TYPELIST_5( D1, D2, D3, D4, D5 );
886  //**********************************************************************************************
887 
888  protected:
889  //**Constructor*********************************************************************************
894  explicit Singleton()
895  : dependency1_( D1::instance() ) // Handle to the first lifetime dependency
896  , dependency2_( D2::instance() ) // Handle to the second lifetime dependency
897  , dependency3_( D3::instance() ) // Handle to the third lifetime dependency
898  , dependency4_( D4::instance() ) // Handle to the fourth lifetime dependency
899  , dependency5_( D5::instance() ) // Handle to the fifth lifetime dependency
900  {
902  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D1, typename D1::SingletonType );
903  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D2, typename D2::SingletonType );
904  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D3, typename D3::SingletonType );
905  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D4, typename D4::SingletonType );
906  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D5, typename D5::SingletonType );
912  }
913  //**********************************************************************************************
914 
915  //**Destructor**********************************************************************************
918  ~Singleton()
919  {}
920  //**********************************************************************************************
921 
922  public:
923  //**Instance function***************************************************************************
926  static std::shared_ptr<T> instance()
927  {
928  static std::shared_ptr<T> object( new T() );
929  return object;
930  }
932  //**********************************************************************************************
933 
934  private:
935  //**Member variables****************************************************************************
938  std::shared_ptr<D1> dependency1_;
939  std::shared_ptr<D2> dependency2_;
940  std::shared_ptr<D3> dependency3_;
941  std::shared_ptr<D4> dependency4_;
942  std::shared_ptr<D5> dependency5_;
943 
944  //**********************************************************************************************
945 };
947 //*************************************************************************************************
948 
949 
950 
951 
952 //=================================================================================================
953 //
954 // SINGLETON SPECIALIZATION (4 LIFETIME DEPENDENCIES)
955 //
956 //=================================================================================================
957 
958 //*************************************************************************************************
966 template< typename T // Type of the singleton (CRTP pattern)
967  , typename D1 // Type of the first lifetime dependency
968  , typename D2 // Type of the second lifetime dependency
969  , typename D3 // Type of the third lifetime dependency
970  , typename D4 > // Type of the fourth lifetime dependency
971 class Singleton<T,D1,D2,D3,D4,NullType,NullType,NullType,NullType>
972  : private NonCopyable
973 {
974  public:
975  //**Type definitions****************************************************************************
978 
980  using Dependencies = BLAZE_TYPELIST_4( D1, D2, D3, D4 );
981  //**********************************************************************************************
982 
983  protected:
984  //**Constructor*********************************************************************************
989  explicit Singleton()
990  : dependency1_( D1::instance() ) // Handle to the first lifetime dependency
991  , dependency2_( D2::instance() ) // Handle to the second lifetime dependency
992  , dependency3_( D3::instance() ) // Handle to the third lifetime dependency
993  , dependency4_( D4::instance() ) // Handle to the fourth lifetime dependency
994  {
996  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D1, typename D1::SingletonType );
997  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D2, typename D2::SingletonType );
998  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D3, typename D3::SingletonType );
999  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D4, typename D4::SingletonType );
1004  }
1005  //**********************************************************************************************
1006 
1007  //**Destructor**********************************************************************************
1010  ~Singleton()
1011  {}
1012  //**********************************************************************************************
1013 
1014  public:
1015  //**Instance function***************************************************************************
1018  static std::shared_ptr<T> instance()
1019  {
1020  static std::shared_ptr<T> object( new T() );
1021  return object;
1022  }
1024  //**********************************************************************************************
1025 
1026  private:
1027  //**Member variables****************************************************************************
1030  std::shared_ptr<D1> dependency1_;
1031  std::shared_ptr<D2> dependency2_;
1032  std::shared_ptr<D3> dependency3_;
1033  std::shared_ptr<D4> dependency4_;
1034 
1035  //**********************************************************************************************
1036 };
1038 //*************************************************************************************************
1039 
1040 
1041 
1042 
1043 //=================================================================================================
1044 //
1045 // SINGLETON SPECIALIZATION (3 LIFETIME DEPENDENCIES)
1046 //
1047 //=================================================================================================
1048 
1049 //*************************************************************************************************
1057 template< typename T // Type of the singleton (CRTP pattern)
1058  , typename D1 // Type of the first lifetime dependency
1059  , typename D2 // Type of the second lifetime dependency
1060  , typename D3 > // Type of the third lifetime dependency
1061 class Singleton<T,D1,D2,D3,NullType,NullType,NullType,NullType,NullType>
1062  : private NonCopyable
1063 {
1064  public:
1065  //**Type definitions****************************************************************************
1068 
1070  using Dependencies = BLAZE_TYPELIST_3( D1, D2, D3 );
1071  //**********************************************************************************************
1072 
1073  protected:
1074  //**Constructor*********************************************************************************
1079  explicit Singleton()
1080  : dependency1_( D1::instance() ) // Handle to the first lifetime dependency
1081  , dependency2_( D2::instance() ) // Handle to the second lifetime dependency
1082  , dependency3_( D3::instance() ) // Handle to the third lifetime dependency
1083  {
1085  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D1, typename D1::SingletonType );
1086  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D2, typename D2::SingletonType );
1087  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D3, typename D3::SingletonType );
1091  }
1092  //**********************************************************************************************
1093 
1094  //**Destructor**********************************************************************************
1097  ~Singleton()
1098  {}
1099  //**********************************************************************************************
1100 
1101  public:
1102  //**Instance function***************************************************************************
1105  static std::shared_ptr<T> instance()
1106  {
1107  static std::shared_ptr<T> object( new T() );
1108  return object;
1109  }
1111  //**********************************************************************************************
1112 
1113  private:
1114  //**Member variables****************************************************************************
1117  std::shared_ptr<D1> dependency1_;
1118  std::shared_ptr<D2> dependency2_;
1119  std::shared_ptr<D3> dependency3_;
1120 
1121  //**********************************************************************************************
1122 };
1124 //*************************************************************************************************
1125 
1126 
1127 
1128 
1129 //=================================================================================================
1130 //
1131 // SINGLETON SPECIALIZATION (2 LIFETIME DEPENDENCIES)
1132 //
1133 //=================================================================================================
1134 
1135 //*************************************************************************************************
1143 template< typename T // Type of the singleton (CRTP pattern)
1144  , typename D1 // Type of the first lifetime dependency
1145  , typename D2 > // Type of the second lifetime dependency
1146 class Singleton<T,D1,D2,NullType,NullType,NullType,NullType,NullType,NullType>
1147  : private NonCopyable
1148 {
1149  public:
1150  //**Type definitions****************************************************************************
1153 
1155  using Dependencies = BLAZE_TYPELIST_2( D1, D2 );
1156  //**********************************************************************************************
1157 
1158  protected:
1159  //**Constructor*********************************************************************************
1164  explicit Singleton()
1165  : dependency1_( D1::instance() ) // Handle to the first lifetime dependency
1166  , dependency2_( D2::instance() ) // Handle to the second lifetime dependency
1167  {
1169  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D1, typename D1::SingletonType );
1170  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D2, typename D2::SingletonType );
1173  }
1174  //**********************************************************************************************
1175 
1176  //**Destructor**********************************************************************************
1179  ~Singleton()
1180  {}
1181  //**********************************************************************************************
1182 
1183  public:
1184  //**Instance function***************************************************************************
1187  static std::shared_ptr<T> instance()
1188  {
1189  static std::shared_ptr<T> object( new T() );
1190  return object;
1191  }
1193  //**********************************************************************************************
1194 
1195  private:
1196  //**Member variables****************************************************************************
1199  std::shared_ptr<D1> dependency1_;
1200  std::shared_ptr<D2> dependency2_;
1201 
1202  //**********************************************************************************************
1203 };
1205 //*************************************************************************************************
1206 
1207 
1208 
1209 
1210 //=================================================================================================
1211 //
1212 // SINGLETON SPECIALIZATION (1 LIFETIME DEPENDENCY)
1213 //
1214 //=================================================================================================
1215 
1216 //*************************************************************************************************
1224 template< typename T // Type of the singleton (CRTP pattern)
1225  , typename D1 > // Type of the lifetime dependency
1226 class Singleton<T,D1,NullType,NullType,NullType,NullType,NullType,NullType,NullType>
1227  : private NonCopyable
1228 {
1229  public:
1230  //**Type definitions****************************************************************************
1233 
1235  using Dependencies = BLAZE_TYPELIST_1( D1 );
1236  //**********************************************************************************************
1237 
1238  protected:
1239  //**Constructor*********************************************************************************
1244  explicit Singleton()
1245  : dependency1_( D1::instance() ) // Handle to the lifetime dependency
1246  {
1248  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D1, typename D1::SingletonType );
1250  }
1251  //**********************************************************************************************
1252 
1253  //**Destructor**********************************************************************************
1256  ~Singleton()
1257  {}
1258  //**********************************************************************************************
1259 
1260  public:
1261  //**Instance function***************************************************************************
1264  static std::shared_ptr<T> instance()
1265  {
1266  static std::shared_ptr<T> object( new T() );
1267  return object;
1268  }
1270  //**********************************************************************************************
1271 
1272  private:
1273  //**Member variables****************************************************************************
1276  std::shared_ptr<D1> dependency1_;
1277 
1278  //**********************************************************************************************
1279 };
1281 //*************************************************************************************************
1282 
1283 
1284 
1285 
1286 //=================================================================================================
1287 //
1288 // SINGLETON SPECIALIZATION (0 LIFETIME DEPENDENCIES)
1289 //
1290 //=================================================================================================
1291 
1292 //*************************************************************************************************
1300 template< typename T > // Type of the singleton (CRTP pattern)
1301 class Singleton<T,NullType,NullType,NullType,NullType,NullType,NullType,NullType,NullType>
1302  : private NonCopyable
1303 {
1304  public:
1305  //**Type definitions****************************************************************************
1308 
1310  using Dependencies = NullType;
1311  //**********************************************************************************************
1312 
1313  protected:
1314  //**Constructor*********************************************************************************
1319  explicit Singleton()
1320  {
1322  }
1323  //**********************************************************************************************
1324 
1325  //**Destructor**********************************************************************************
1328  ~Singleton()
1329  {}
1330  //**********************************************************************************************
1331 
1332  public:
1333  //**Instance function***************************************************************************
1336  static std::shared_ptr<T> instance()
1337  {
1338  static std::shared_ptr<T> object( new T() );
1339  return object;
1340  }
1342  //**********************************************************************************************
1343 };
1345 //*************************************************************************************************
1346 
1347 } // namespace blaze
1348 
1349 #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:633
Base class for non-copyable class instances.
std::shared_ptr< D2 > dependency2_
Handle to the second lifetime dependency.
Definition: Singleton.h:634
std::shared_ptr< D7 > dependency7_
Handle to the seventh lifetime dependency.
Definition: Singleton.h:639
std::shared_ptr< D4 > dependency4_
Handle to the fourth lifetime dependency.
Definition: Singleton.h:636
~Singleton()
Destructor for the Singleton class.
Definition: Singleton.h:613
std::shared_ptr< D8 > dependency8_
Handle to the eighth lifetime dependency.
Definition: Singleton.h:640
std::shared_ptr< D5 > dependency5_
Handle to the fifth lifetime dependency.
Definition: Singleton.h:637
std::shared_ptr< D3 > dependency3_
Handle to the third lifetime dependency.
Definition: Singleton.h:635
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:638
#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
BLAZE_TYPELIST_8(D1, D2, D3, D4, D5, D6, D7, D8) Dependencies
Type list of all lifetime dependencies.
Definition: Singleton.h:571
#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
#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:580
Header file for compile time constraints.