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 ||
142  HasCyclicDependency<D2,TL,Contains<TL,D2>::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 ||
165  HasCyclicDependency<D2,TL,Contains<TL,D2>::value>::value ||
166  HasCyclicDependency<D3,TL,Contains<TL,D3>::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 ||
190  HasCyclicDependency<D2,TL,Contains<TL,D2>::value>::value ||
191  HasCyclicDependency<D3,TL,Contains<TL,D3>::value>::value ||
192  HasCyclicDependency<D4,TL,Contains<TL,D4>::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 ||
217  HasCyclicDependency<D2,TL,Contains<TL,D2>::value>::value ||
218  HasCyclicDependency<D3,TL,Contains<TL,D3>::value>::value ||
219  HasCyclicDependency<D4,TL,Contains<TL,D4>::value>::value ||
220  HasCyclicDependency<D5,TL,Contains<TL,D5>::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 ||
246  HasCyclicDependency<D2,TL,Contains<TL,D2>::value>::value ||
247  HasCyclicDependency<D3,TL,Contains<TL,D3>::value>::value ||
248  HasCyclicDependency<D4,TL,Contains<TL,D4>::value>::value ||
249  HasCyclicDependency<D5,TL,Contains<TL,D5>::value>::value ||
250  HasCyclicDependency<D6,TL,Contains<TL,D6>::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 ||
277  HasCyclicDependency<D2,TL,Contains<TL,D2>::value>::value ||
278  HasCyclicDependency<D3,TL,Contains<TL,D3>::value>::value ||
279  HasCyclicDependency<D4,TL,Contains<TL,D4>::value>::value ||
280  HasCyclicDependency<D5,TL,Contains<TL,D5>::value>::value ||
281  HasCyclicDependency<D6,TL,Contains<TL,D6>::value>::value ||
282  HasCyclicDependency<D7,TL,Contains<TL,D7>::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 ||
310  HasCyclicDependency<D2,TL,Contains<TL,D2>::value>::value ||
311  HasCyclicDependency<D3,TL,Contains<TL,D3>::value>::value ||
312  HasCyclicDependency<D4,TL,Contains<TL,D4>::value>::value ||
313  HasCyclicDependency<D5,TL,Contains<TL,D5>::value>::value ||
314  HasCyclicDependency<D6,TL,Contains<TL,D6>::value>::value ||
315  HasCyclicDependency<D7,TL,Contains<TL,D7>::value>::value ||
316  HasCyclicDependency<D8,TL,Contains<TL,D8>::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 //*************************************************************************************************
554 template< typename T // Type of the singleton (CRTP pattern)
555  , typename D1 = NullType // Type of the first lifetime dependency
556  , typename D2 = NullType // Type of the second lifetime dependency
557  , typename D3 = NullType // Type of the third lifetime dependency
558  , typename D4 = NullType // Type of the fourth lifetime dependency
559  , typename D5 = NullType // Type of the fifth lifetime dependency
560  , typename D6 = NullType // Type of the sixth lifetime dependency
561  , typename D7 = NullType // Type of the seventh lifetime dependency
562  , typename D8 = NullType > // Type of the eighth lifetime dependency
563 class Singleton : private NonCopyable
564 {
565  public:
566  //**Type definitions****************************************************************************
569 
571  typedef BLAZE_TYPELIST_8( D1, D2, D3, D4, D5, D6, D7, D8 ) Dependencies;
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  {
590  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( T, SingletonType );
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> : private NonCopyable
672 {
673  public:
674  //**Type definitions****************************************************************************
677 
679  typedef BLAZE_TYPELIST_7( D1, D2, D3, D4, D5, D6, D7 ) Dependencies;
680  //**********************************************************************************************
681 
682  protected:
683  //**Constructor*********************************************************************************
688  explicit Singleton()
689  : dependency1_( D1::instance() ) // Handle to the first lifetime dependency
690  , dependency2_( D2::instance() ) // Handle to the second lifetime dependency
691  , dependency3_( D3::instance() ) // Handle to the third lifetime dependency
692  , dependency4_( D4::instance() ) // Handle to the fourth lifetime dependency
693  , dependency5_( D5::instance() ) // Handle to the fifth lifetime dependency
694  , dependency6_( D6::instance() ) // Handle to the sixth lifetime dependency
695  , dependency7_( D7::instance() ) // Handle to the seventh lifetime dependency
696  {
697  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( T, SingletonType );
698  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D1, typename D1::SingletonType );
699  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D2, typename D2::SingletonType );
700  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D3, typename D3::SingletonType );
701  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D4, typename D4::SingletonType );
702  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D5, typename D5::SingletonType );
703  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D6, typename D6::SingletonType );
704  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D7, typename D7::SingletonType );
712  }
713  //**********************************************************************************************
714 
715  //**Destructor**********************************************************************************
718  ~Singleton()
719  {}
720  //**********************************************************************************************
721 
722  public:
723  //**Instance function***************************************************************************
726  static std::shared_ptr<T> instance()
727  {
728  static std::shared_ptr<T> object( new T() );
729  return object;
730  }
732  //**********************************************************************************************
733 
734  private:
735  //**Member variables****************************************************************************
738  std::shared_ptr<D1> dependency1_;
739  std::shared_ptr<D2> dependency2_;
740  std::shared_ptr<D3> dependency3_;
741  std::shared_ptr<D4> dependency4_;
742  std::shared_ptr<D5> dependency5_;
743  std::shared_ptr<D6> dependency6_;
744  std::shared_ptr<D7> dependency7_;
745 
746  //**********************************************************************************************
747 };
749 //*************************************************************************************************
750 
751 
752 
753 
754 //=================================================================================================
755 //
756 // SINGLETON SPECIALIZATION (6 LIFETIME DEPENDENCIES)
757 //
758 //=================================================================================================
759 
760 //*************************************************************************************************
768 template< typename T // Type of the singleton (CRTP pattern)
769  , typename D1 // Type of the first lifetime dependency
770  , typename D2 // Type of the second lifetime dependency
771  , typename D3 // Type of the third lifetime dependency
772  , typename D4 // Type of the fourth lifetime dependency
773  , typename D5 // Type of the fifth lifetime dependency
774  , typename D6 > // Type of the eighth lifetime dependency
775 class Singleton<T,D1,D2,D3,D4,D5,D6,NullType,NullType> : private NonCopyable
776 {
777  public:
778  //**Type definitions****************************************************************************
780  typedef Singleton<T,D1,D2,D3,D4,D5,D6,NullType,NullType> SingletonType;
781 
783  typedef BLAZE_TYPELIST_6( D1, D2, D3, D4, D5, D6 ) Dependencies;
784  //**********************************************************************************************
785 
786  protected:
787  //**Constructor*********************************************************************************
792  explicit Singleton()
793  : dependency1_( D1::instance() ) // Handle to the first lifetime dependency
794  , dependency2_( D2::instance() ) // Handle to the second lifetime dependency
795  , dependency3_( D3::instance() ) // Handle to the third lifetime dependency
796  , dependency4_( D4::instance() ) // Handle to the fourth lifetime dependency
797  , dependency5_( D5::instance() ) // Handle to the fifth lifetime dependency
798  , dependency6_( D6::instance() ) // Handle to the sixth lifetime dependency
799  {
800  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( T, SingletonType );
801  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D1, typename D1::SingletonType );
802  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D2, typename D2::SingletonType );
803  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D3, typename D3::SingletonType );
804  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D4, typename D4::SingletonType );
805  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D5, typename D5::SingletonType );
806  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D6, typename D6::SingletonType );
813  }
814  //**********************************************************************************************
815 
816  //**Destructor**********************************************************************************
819  ~Singleton()
820  {}
821  //**********************************************************************************************
822 
823  public:
824  //**Instance function***************************************************************************
827  static std::shared_ptr<T> instance()
828  {
829  static std::shared_ptr<T> object( new T() );
830  return object;
831  }
833  //**********************************************************************************************
834 
835  private:
836  //**Member variables****************************************************************************
839  std::shared_ptr<D1> dependency1_;
840  std::shared_ptr<D2> dependency2_;
841  std::shared_ptr<D3> dependency3_;
842  std::shared_ptr<D4> dependency4_;
843  std::shared_ptr<D5> dependency5_;
844  std::shared_ptr<D6> dependency6_;
845 
846  //**********************************************************************************************
847 };
849 //*************************************************************************************************
850 
851 
852 
853 
854 //=================================================================================================
855 //
856 // SINGLETON SPECIALIZATION (5 LIFETIME DEPENDENCIES)
857 //
858 //=================================================================================================
859 
860 //*************************************************************************************************
868 template< typename T // Type of the singleton (CRTP pattern)
869  , typename D1 // Type of the first lifetime dependency
870  , typename D2 // Type of the second lifetime dependency
871  , typename D3 // Type of the third lifetime dependency
872  , typename D4 // Type of the fourth lifetime dependency
873  , typename D5 > // Type of the fifth lifetime dependency
874 class Singleton<T,D1,D2,D3,D4,D5,NullType,NullType,NullType> : private NonCopyable
875 {
876  public:
877  //**Type definitions****************************************************************************
879  typedef Singleton<T,D1,D2,D3,D4,D5,NullType,NullType,NullType> SingletonType;
880 
882  typedef BLAZE_TYPELIST_5( D1, D2, D3, D4, D5 ) Dependencies;
883  //**********************************************************************************************
884 
885  protected:
886  //**Constructor*********************************************************************************
891  explicit Singleton()
892  : dependency1_( D1::instance() ) // Handle to the first lifetime dependency
893  , dependency2_( D2::instance() ) // Handle to the second lifetime dependency
894  , dependency3_( D3::instance() ) // Handle to the third lifetime dependency
895  , dependency4_( D4::instance() ) // Handle to the fourth lifetime dependency
896  , dependency5_( D5::instance() ) // Handle to the fifth lifetime dependency
897  {
898  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( T, SingletonType );
899  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D1, typename D1::SingletonType );
900  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D2, typename D2::SingletonType );
901  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D3, typename D3::SingletonType );
902  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D4, typename D4::SingletonType );
903  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D5, typename D5::SingletonType );
909  }
910  //**********************************************************************************************
911 
912  //**Destructor**********************************************************************************
915  ~Singleton()
916  {}
917  //**********************************************************************************************
918 
919  public:
920  //**Instance function***************************************************************************
923  static std::shared_ptr<T> instance()
924  {
925  static std::shared_ptr<T> object( new T() );
926  return object;
927  }
929  //**********************************************************************************************
930 
931  private:
932  //**Member variables****************************************************************************
935  std::shared_ptr<D1> dependency1_;
936  std::shared_ptr<D2> dependency2_;
937  std::shared_ptr<D3> dependency3_;
938  std::shared_ptr<D4> dependency4_;
939  std::shared_ptr<D5> dependency5_;
940 
941  //**********************************************************************************************
942 };
944 //*************************************************************************************************
945 
946 
947 
948 
949 //=================================================================================================
950 //
951 // SINGLETON SPECIALIZATION (4 LIFETIME DEPENDENCIES)
952 //
953 //=================================================================================================
954 
955 //*************************************************************************************************
963 template< typename T // Type of the singleton (CRTP pattern)
964  , typename D1 // Type of the first lifetime dependency
965  , typename D2 // Type of the second lifetime dependency
966  , typename D3 // Type of the third lifetime dependency
967  , typename D4 > // Type of the fourth lifetime dependency
968 class Singleton<T,D1,D2,D3,D4,NullType,NullType,NullType,NullType> : private NonCopyable
969 {
970  public:
971  //**Type definitions****************************************************************************
973  typedef Singleton<T,D1,D2,D3,D4,NullType,NullType,NullType,NullType> SingletonType;
974 
976  typedef BLAZE_TYPELIST_4( D1, D2, D3, D4 ) Dependencies;
977  //**********************************************************************************************
978 
979  protected:
980  //**Constructor*********************************************************************************
985  explicit Singleton()
986  : dependency1_( D1::instance() ) // Handle to the first lifetime dependency
987  , dependency2_( D2::instance() ) // Handle to the second lifetime dependency
988  , dependency3_( D3::instance() ) // Handle to the third lifetime dependency
989  , dependency4_( D4::instance() ) // Handle to the fourth lifetime dependency
990  {
991  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( T, SingletonType );
992  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D1, typename D1::SingletonType );
993  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D2, typename D2::SingletonType );
994  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D3, typename D3::SingletonType );
995  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D4, typename D4::SingletonType );
1000  }
1001  //**********************************************************************************************
1002 
1003  //**Destructor**********************************************************************************
1006  ~Singleton()
1007  {}
1008  //**********************************************************************************************
1009 
1010  public:
1011  //**Instance function***************************************************************************
1014  static std::shared_ptr<T> instance()
1015  {
1016  static std::shared_ptr<T> object( new T() );
1017  return object;
1018  }
1020  //**********************************************************************************************
1021 
1022  private:
1023  //**Member variables****************************************************************************
1026  std::shared_ptr<D1> dependency1_;
1027  std::shared_ptr<D2> dependency2_;
1028  std::shared_ptr<D3> dependency3_;
1029  std::shared_ptr<D4> dependency4_;
1030 
1031  //**********************************************************************************************
1032 };
1034 //*************************************************************************************************
1035 
1036 
1037 
1038 
1039 //=================================================================================================
1040 //
1041 // SINGLETON SPECIALIZATION (3 LIFETIME DEPENDENCIES)
1042 //
1043 //=================================================================================================
1044 
1045 //*************************************************************************************************
1053 template< typename T // Type of the singleton (CRTP pattern)
1054  , typename D1 // Type of the first lifetime dependency
1055  , typename D2 // Type of the second lifetime dependency
1056  , typename D3 > // Type of the third lifetime dependency
1057 class Singleton<T,D1,D2,D3,NullType,NullType,NullType,NullType,NullType> : private NonCopyable
1058 {
1059  public:
1060  //**Type definitions****************************************************************************
1062  typedef Singleton<T,D1,D2,D3,NullType,NullType,NullType,NullType,NullType> SingletonType;
1063 
1065  typedef BLAZE_TYPELIST_3( D1, D2, D3 ) Dependencies;
1066  //**********************************************************************************************
1067 
1068  protected:
1069  //**Constructor*********************************************************************************
1074  explicit Singleton()
1075  : dependency1_( D1::instance() ) // Handle to the first lifetime dependency
1076  , dependency2_( D2::instance() ) // Handle to the second lifetime dependency
1077  , dependency3_( D3::instance() ) // Handle to the third lifetime dependency
1078  {
1079  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( T, SingletonType );
1080  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D1, typename D1::SingletonType );
1081  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D2, typename D2::SingletonType );
1082  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D3, typename D3::SingletonType );
1086  }
1087  //**********************************************************************************************
1088 
1089  //**Destructor**********************************************************************************
1092  ~Singleton()
1093  {}
1094  //**********************************************************************************************
1095 
1096  public:
1097  //**Instance function***************************************************************************
1100  static std::shared_ptr<T> instance()
1101  {
1102  static std::shared_ptr<T> object( new T() );
1103  return object;
1104  }
1106  //**********************************************************************************************
1107 
1108  private:
1109  //**Member variables****************************************************************************
1112  std::shared_ptr<D1> dependency1_;
1113  std::shared_ptr<D2> dependency2_;
1114  std::shared_ptr<D3> dependency3_;
1115 
1116  //**********************************************************************************************
1117 };
1119 //*************************************************************************************************
1120 
1121 
1122 
1123 
1124 //=================================================================================================
1125 //
1126 // SINGLETON SPECIALIZATION (2 LIFETIME DEPENDENCIES)
1127 //
1128 //=================================================================================================
1129 
1130 //*************************************************************************************************
1138 template< typename T // Type of the singleton (CRTP pattern)
1139  , typename D1 // Type of the first lifetime dependency
1140  , typename D2 > // Type of the second lifetime dependency
1141 class Singleton<T,D1,D2,NullType,NullType,NullType,NullType,NullType,NullType> : private NonCopyable
1142 {
1143  public:
1144  //**Type definitions****************************************************************************
1146  typedef Singleton<T,D1,D2,NullType,NullType,NullType,NullType,NullType,NullType> SingletonType;
1147 
1149  typedef BLAZE_TYPELIST_2( D1, D2 ) Dependencies;
1150  //**********************************************************************************************
1151 
1152  protected:
1153  //**Constructor*********************************************************************************
1158  explicit Singleton()
1159  : dependency1_( D1::instance() ) // Handle to the first lifetime dependency
1160  , dependency2_( D2::instance() ) // Handle to the second lifetime dependency
1161  {
1162  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( T, SingletonType );
1163  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D1, typename D1::SingletonType );
1164  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D2, typename D2::SingletonType );
1167  }
1168  //**********************************************************************************************
1169 
1170  //**Destructor**********************************************************************************
1173  ~Singleton()
1174  {}
1175  //**********************************************************************************************
1176 
1177  public:
1178  //**Instance function***************************************************************************
1181  static std::shared_ptr<T> instance()
1182  {
1183  static std::shared_ptr<T> object( new T() );
1184  return object;
1185  }
1187  //**********************************************************************************************
1188 
1189  private:
1190  //**Member variables****************************************************************************
1193  std::shared_ptr<D1> dependency1_;
1194  std::shared_ptr<D2> dependency2_;
1195 
1196  //**********************************************************************************************
1197 };
1199 //*************************************************************************************************
1200 
1201 
1202 
1203 
1204 //=================================================================================================
1205 //
1206 // SINGLETON SPECIALIZATION (1 LIFETIME DEPENDENCY)
1207 //
1208 //=================================================================================================
1209 
1210 //*************************************************************************************************
1218 template< typename T // Type of the singleton (CRTP pattern)
1219  , typename D1 > // Type of the lifetime dependency
1220 class Singleton<T,D1,NullType,NullType,NullType,NullType,NullType,NullType,NullType> : private NonCopyable
1221 {
1222  public:
1223  //**Type definitions****************************************************************************
1225  typedef Singleton<T,D1,NullType,NullType,NullType,NullType,NullType,NullType,NullType> SingletonType;
1226 
1228  typedef BLAZE_TYPELIST_1( D1 ) Dependencies;
1229  //**********************************************************************************************
1230 
1231  protected:
1232  //**Constructor*********************************************************************************
1237  explicit Singleton()
1238  : dependency1_( D1::instance() ) // Handle to the lifetime dependency
1239  {
1240  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( T, SingletonType );
1241  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D1, typename D1::SingletonType );
1243  }
1244  //**********************************************************************************************
1245 
1246  //**Destructor**********************************************************************************
1249  ~Singleton()
1250  {}
1251  //**********************************************************************************************
1252 
1253  public:
1254  //**Instance function***************************************************************************
1257  static std::shared_ptr<T> instance()
1258  {
1259  static std::shared_ptr<T> object( new T() );
1260  return object;
1261  }
1263  //**********************************************************************************************
1264 
1265  private:
1266  //**Member variables****************************************************************************
1269  std::shared_ptr<D1> dependency1_;
1270 
1271  //**********************************************************************************************
1272 };
1274 //*************************************************************************************************
1275 
1276 
1277 
1278 
1279 //=================================================================================================
1280 //
1281 // SINGLETON SPECIALIZATION (0 LIFETIME DEPENDENCIES)
1282 //
1283 //=================================================================================================
1284 
1285 //*************************************************************************************************
1293 template< typename T > // Type of the singleton (CRTP pattern)
1294 class Singleton<T,NullType,NullType,NullType,NullType,NullType,NullType,NullType,NullType> : private NonCopyable
1295 {
1296  public:
1297  //**Type definitions****************************************************************************
1299  typedef Singleton<T,NullType,NullType,NullType,NullType,NullType,NullType,NullType,NullType> SingletonType;
1300 
1302  typedef NullType Dependencies;
1303  //**********************************************************************************************
1304 
1305  protected:
1306  //**Constructor*********************************************************************************
1311  explicit Singleton()
1312  {
1313  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( T, SingletonType );
1314  }
1315  //**********************************************************************************************
1316 
1317  //**Destructor**********************************************************************************
1320  ~Singleton()
1321  {}
1322  //**********************************************************************************************
1323 
1324  public:
1325  //**Instance function***************************************************************************
1328  static std::shared_ptr<T> instance()
1329  {
1330  static std::shared_ptr<T> object( new T() );
1331  return object;
1332  }
1334  //**********************************************************************************************
1335 };
1337 //*************************************************************************************************
1338 
1339 } // namespace blaze
1340 
1341 #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
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
typedef BLAZE_TYPELIST_8(D1, D2, D3, D4, D5, D6, D7, D8) Dependencies
Type list of all lifetime dependencies.
~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
#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:563
#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
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:568
#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
Singleton()
Constructor for the Singleton class.
Definition: Singleton.h:580
Header file for compile time constraints.