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>
47 #include <blaze/util/NonCopyable.h>
48 #include <blaze/util/NullType.h>
49 #include <blaze/util/Suffix.h>
50 #include <blaze/util/TrueType.h>
51 #include <blaze/util/TypeList.h>
52 
53 
54 namespace blaze {
55 
56 //=================================================================================================
57 //
58 // ::blaze NAMESPACE FORWARD DECLARATIONS
59 //
60 //=================================================================================================
61 
62 template< typename > class Dependency;
63 template< typename T, typename TL, bool C > struct HasCyclicDependency;
64 
65 
66 
67 
68 //=================================================================================================
69 //
70 // CLASS HASCYCLICDEPENDENCYHELPER
71 //
72 //=================================================================================================
73 
74 //*************************************************************************************************
82 template< typename TL // Type list of checked lifetime dependencies
83  , typename D // Type list of lifetime dependencies to check
84  , size_t N = Length_v<D> > // Length of the dependency type list
85 struct HasCyclicDependencyHelper;
87 //*************************************************************************************************
88 
89 
90 //*************************************************************************************************
98 template< typename TL // Type list of checked lifetime dependencies
99  , size_t N > // Length of the dependency type list
100 struct HasCyclicDependencyHelper<TL,NullType,N>
101 {
102  static constexpr bool value = false;
103 };
105 //*************************************************************************************************
106 
107 
108 //*************************************************************************************************
116 template< typename TL // Type list of checked lifetime dependencies
117  , typename D > // Type list of lifetime dependencies to check
118 struct HasCyclicDependencyHelper<TL,D,1>
119 {
120  using D1 = TypeAt_t<D,0>;
121 
122  static constexpr bool value = HasCyclicDependency<D1,TL,Contains_v<TL,D1>>::value;
123 };
125 //*************************************************************************************************
126 
127 
128 //*************************************************************************************************
136 template< typename TL // Type list of checked lifetime dependencies
137  , typename D > // Type list of lifetime dependencies to check
138 struct HasCyclicDependencyHelper<TL,D,2>
139 {
140  using D1 = TypeAt_t<D,0>;
141  using D2 = TypeAt_t<D,1>;
142 
143  static constexpr bool value = ( HasCyclicDependency<D1,TL,Contains_v<TL,D1>>::value ||
144  HasCyclicDependency<D2,TL,Contains_v<TL,D2>>::value );
145 };
147 //*************************************************************************************************
148 
149 
150 //*************************************************************************************************
158 template< typename TL // Type list of checked lifetime dependencies
159  , typename D > // Type list of lifetime dependencies to check
160 struct HasCyclicDependencyHelper<TL,D,3>
161 {
162  using D1 = TypeAt_t<D,0>;
163  using D2 = TypeAt_t<D,1>;
164  using D3 = TypeAt_t<D,2>;
165 
166  static constexpr bool value = ( HasCyclicDependency<D1,TL,Contains_v<TL,D1>>::value ||
167  HasCyclicDependency<D2,TL,Contains_v<TL,D2>>::value ||
168  HasCyclicDependency<D3,TL,Contains_v<TL,D3>>::value );
169 };
171 //*************************************************************************************************
172 
173 
174 //*************************************************************************************************
182 template< typename TL // Type list of checked lifetime dependencies
183  , typename D > // Type list of lifetime dependencies to check
184 struct HasCyclicDependencyHelper<TL,D,4>
185 {
186  using D1 = TypeAt_t<D,0>;
187  using D2 = TypeAt_t<D,1>;
188  using D3 = TypeAt_t<D,2>;
189  using D4 = TypeAt_t<D,3>;
190 
191  static constexpr bool value = ( HasCyclicDependency<D1,TL,Contains_v<TL,D1>>::value ||
192  HasCyclicDependency<D2,TL,Contains_v<TL,D2>>::value ||
193  HasCyclicDependency<D3,TL,Contains_v<TL,D3>>::value ||
194  HasCyclicDependency<D4,TL,Contains_v<TL,D4>>::value );
195 };
197 //*************************************************************************************************
198 
199 
200 //*************************************************************************************************
208 template< typename TL // Type list of checked lifetime dependencies
209  , typename D > // Type list of lifetime dependencies to check
210 struct HasCyclicDependencyHelper<TL,D,5>
211 {
212  using D1 = TypeAt_t<D,0>;
213  using D2 = TypeAt_t<D,1>;
214  using D3 = TypeAt_t<D,2>;
215  using D4 = TypeAt_t<D,3>;
216  using D5 = TypeAt_t<D,4>;
217 
218  static constexpr bool value = ( HasCyclicDependency<D1,TL,Contains_v<TL,D1>>::value ||
219  HasCyclicDependency<D2,TL,Contains_v<TL,D2>>::value ||
220  HasCyclicDependency<D3,TL,Contains_v<TL,D3>>::value ||
221  HasCyclicDependency<D4,TL,Contains_v<TL,D4>>::value ||
222  HasCyclicDependency<D5,TL,Contains_v<TL,D5>>::value );
223 };
225 //*************************************************************************************************
226 
227 
228 //*************************************************************************************************
236 template< typename TL // Type list of checked lifetime dependencies
237  , typename D > // Type list of lifetime dependencies to check
238 struct HasCyclicDependencyHelper<TL,D,6>
239 {
240  using D1 = TypeAt_t<D,0>;
241  using D2 = TypeAt_t<D,1>;
242  using D3 = TypeAt_t<D,2>;
243  using D4 = TypeAt_t<D,3>;
244  using D5 = TypeAt_t<D,4>;
245  using D6 = TypeAt_t<D,5>;
246 
247  static constexpr bool value = ( HasCyclicDependency<D1,TL,Contains_v<TL,D1>>::value ||
248  HasCyclicDependency<D2,TL,Contains_v<TL,D2>>::value ||
249  HasCyclicDependency<D3,TL,Contains_v<TL,D3>>::value ||
250  HasCyclicDependency<D4,TL,Contains_v<TL,D4>>::value ||
251  HasCyclicDependency<D5,TL,Contains_v<TL,D5>>::value ||
252  HasCyclicDependency<D6,TL,Contains_v<TL,D6>>::value );
253 };
255 //*************************************************************************************************
256 
257 
258 //*************************************************************************************************
266 template< typename TL // Type list of checked lifetime dependencies
267  , typename D > // Type list of lifetime dependencies to check
268 struct HasCyclicDependencyHelper<TL,D,7>
269 {
270  using D1 = TypeAt_t<D,0>;
271  using D2 = TypeAt_t<D,1>;
272  using D3 = TypeAt_t<D,2>;
273  using D4 = TypeAt_t<D,3>;
274  using D5 = TypeAt_t<D,4>;
275  using D6 = TypeAt_t<D,5>;
276  using D7 = TypeAt_t<D,6>;
277 
278  static constexpr bool value = ( HasCyclicDependency<D1,TL,Contains_v<TL,D1>>::value ||
279  HasCyclicDependency<D2,TL,Contains_v<TL,D2>>::value ||
280  HasCyclicDependency<D3,TL,Contains_v<TL,D3>>::value ||
281  HasCyclicDependency<D4,TL,Contains_v<TL,D4>>::value ||
282  HasCyclicDependency<D5,TL,Contains_v<TL,D5>>::value ||
283  HasCyclicDependency<D6,TL,Contains_v<TL,D6>>::value ||
284  HasCyclicDependency<D7,TL,Contains_v<TL,D7>>::value );
285 };
287 //*************************************************************************************************
288 
289 
290 //*************************************************************************************************
298 template< typename TL // Type list of checked lifetime dependencies
299  , typename D > // Type list of lifetime dependencies to check
300 struct HasCyclicDependencyHelper<TL,D,8>
301 {
302  using D1 = TypeAt_t<D,0>;
303  using D2 = TypeAt_t<D,1>;
304  using D3 = TypeAt_t<D,2>;
305  using D4 = TypeAt_t<D,3>;
306  using D5 = TypeAt_t<D,4>;
307  using D6 = TypeAt_t<D,5>;
308  using D7 = TypeAt_t<D,6>;
309  using D8 = TypeAt_t<D,7>;
310 
311  static constexpr bool value = ( HasCyclicDependency<D1,TL,Contains_v<TL,D1>>::value ||
312  HasCyclicDependency<D2,TL,Contains_v<TL,D2>>::value ||
313  HasCyclicDependency<D3,TL,Contains_v<TL,D3>>::value ||
314  HasCyclicDependency<D4,TL,Contains_v<TL,D4>>::value ||
315  HasCyclicDependency<D5,TL,Contains_v<TL,D5>>::value ||
316  HasCyclicDependency<D6,TL,Contains_v<TL,D6>>::value ||
317  HasCyclicDependency<D7,TL,Contains_v<TL,D7>>::value ||
318  HasCyclicDependency<D8,TL,Contains_v<TL,D8>>::value );
319 };
321 //*************************************************************************************************
322 
323 
324 
325 
326 //=================================================================================================
327 //
328 // CLASS HASCYCLICDEPENDENCY
329 //
330 //=================================================================================================
331 
332 //*************************************************************************************************
341 template< typename T // The type to be checked for cyclic lifetime dependencies
342  , typename TL // Type list of checked lifetime dependencies
343  , bool C = Contains_v<TL,T> > // Flag to indicate whether T is contained in TL
344 struct HasCyclicDependency
345  : public BoolConstant< HasCyclicDependencyHelper<Append_t<TL,T>,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  : public TrueType
364 {};
366 //*************************************************************************************************
367 
368 
369 
370 
371 //=================================================================================================
372 //
373 // DETECT_CYCLIC_LIFETIME_DEPENDENCY CONSTRAINT
374 //
375 //=================================================================================================
376 
377 //*************************************************************************************************
383 #define BLAZE_DETECT_CYCLIC_LIFETIME_DEPENDENCY(T) \
384  static_assert( ( !blaze::HasCyclicDependency<T,blaze::NullType>::value ), "Cyclic dependency detected" )
385 //*************************************************************************************************
386 
387 
388 
389 
390 //=================================================================================================
391 //
392 // BEFRIEND_SINGLETON MACRO
393 //
394 //=================================================================================================
395 
396 //*************************************************************************************************
403 #define BLAZE_BEFRIEND_SINGLETON \
404  template< typename, typename, typename, typename, typename, typename, typename, typename, typename > friend class blaze::Singleton; \
405  template< typename, typename, bool > friend struct blaze::HasCyclicDependency; \
406  template< typename > friend class blaze::Dependency;
407 //*************************************************************************************************
408 
409 
410 
411 
412 //=================================================================================================
413 //
414 // CLASS SINGLETON
415 //
416 //=================================================================================================
417 
418 //*************************************************************************************************
552 template< typename T // Type of the singleton (CRTP pattern)
553  , typename D1 = NullType // Type of the first lifetime dependency
554  , typename D2 = NullType // Type of the second lifetime dependency
555  , typename D3 = NullType // Type of the third lifetime dependency
556  , typename D4 = NullType // Type of the fourth lifetime dependency
557  , typename D5 = NullType // Type of the fifth lifetime dependency
558  , typename D6 = NullType // Type of the sixth lifetime dependency
559  , typename D7 = NullType // Type of the seventh lifetime dependency
560  , typename D8 = NullType > // Type of the eighth lifetime dependency
562  : private NonCopyable
563 {
564  public:
565  //**Type definitions****************************************************************************
568 
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  {
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>
671  : private NonCopyable
672 {
673  public:
674  //**Type definitions****************************************************************************
677 
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  {
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>
776  : private NonCopyable
777 {
778  public:
779  //**Type definitions****************************************************************************
781  using SingletonType = Singleton<T,D1,D2,D3,D4,D5,D6,NullType,NullType>;
782 
784  using Dependencies = TypeList< D1, D2, D3, D4, D5, D6 >;
785  //**********************************************************************************************
786 
787  protected:
788  //**Constructor*********************************************************************************
793  explicit Singleton()
794  : dependency1_( D1::instance() ) // Handle to the first lifetime dependency
795  , dependency2_( D2::instance() ) // Handle to the second lifetime dependency
796  , dependency3_( D3::instance() ) // Handle to the third lifetime dependency
797  , dependency4_( D4::instance() ) // Handle to the fourth lifetime dependency
798  , dependency5_( D5::instance() ) // Handle to the fifth lifetime dependency
799  , dependency6_( D6::instance() ) // Handle to the sixth lifetime dependency
800  {
802  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D1, typename D1::SingletonType );
803  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D2, typename D2::SingletonType );
804  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D3, typename D3::SingletonType );
805  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D4, typename D4::SingletonType );
806  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D5, typename D5::SingletonType );
807  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D6, typename D6::SingletonType );
814  }
815  //**********************************************************************************************
816 
817  //**Destructor**********************************************************************************
820  ~Singleton()
821  {}
822  //**********************************************************************************************
823 
824  public:
825  //**Instance function***************************************************************************
828  static std::shared_ptr<T> instance()
829  {
830  static std::shared_ptr<T> object( new T() );
831  return object;
832  }
834  //**********************************************************************************************
835 
836  private:
837  //**Member variables****************************************************************************
840  std::shared_ptr<D1> dependency1_;
841  std::shared_ptr<D2> dependency2_;
842  std::shared_ptr<D3> dependency3_;
843  std::shared_ptr<D4> dependency4_;
844  std::shared_ptr<D5> dependency5_;
845  std::shared_ptr<D6> dependency6_;
846 
847  //**********************************************************************************************
848 };
850 //*************************************************************************************************
851 
852 
853 
854 
855 //=================================================================================================
856 //
857 // SINGLETON SPECIALIZATION (5 LIFETIME DEPENDENCIES)
858 //
859 //=================================================================================================
860 
861 //*************************************************************************************************
869 template< typename T // Type of the singleton (CRTP pattern)
870  , typename D1 // Type of the first lifetime dependency
871  , typename D2 // Type of the second lifetime dependency
872  , typename D3 // Type of the third lifetime dependency
873  , typename D4 // Type of the fourth lifetime dependency
874  , typename D5 > // Type of the fifth lifetime dependency
875 class Singleton<T,D1,D2,D3,D4,D5,NullType,NullType,NullType>
876  : private NonCopyable
877 {
878  public:
879  //**Type definitions****************************************************************************
881  using SingletonType = Singleton<T,D1,D2,D3,D4,D5,NullType,NullType,NullType>;
882 
884  using Dependencies = TypeList< D1, D2, D3, D4, D5 >;
885  //**********************************************************************************************
886 
887  protected:
888  //**Constructor*********************************************************************************
893  explicit Singleton()
894  : dependency1_( D1::instance() ) // Handle to the first lifetime dependency
895  , dependency2_( D2::instance() ) // Handle to the second lifetime dependency
896  , dependency3_( D3::instance() ) // Handle to the third lifetime dependency
897  , dependency4_( D4::instance() ) // Handle to the fourth lifetime dependency
898  , dependency5_( D5::instance() ) // Handle to the fifth lifetime dependency
899  {
901  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D1, typename D1::SingletonType );
902  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D2, typename D2::SingletonType );
903  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D3, typename D3::SingletonType );
904  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D4, typename D4::SingletonType );
905  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D5, typename D5::SingletonType );
911  }
912  //**********************************************************************************************
913 
914  //**Destructor**********************************************************************************
917  ~Singleton()
918  {}
919  //**********************************************************************************************
920 
921  public:
922  //**Instance function***************************************************************************
925  static std::shared_ptr<T> instance()
926  {
927  static std::shared_ptr<T> object( new T() );
928  return object;
929  }
931  //**********************************************************************************************
932 
933  private:
934  //**Member variables****************************************************************************
937  std::shared_ptr<D1> dependency1_;
938  std::shared_ptr<D2> dependency2_;
939  std::shared_ptr<D3> dependency3_;
940  std::shared_ptr<D4> dependency4_;
941  std::shared_ptr<D5> dependency5_;
942 
943  //**********************************************************************************************
944 };
946 //*************************************************************************************************
947 
948 
949 
950 
951 //=================================================================================================
952 //
953 // SINGLETON SPECIALIZATION (4 LIFETIME DEPENDENCIES)
954 //
955 //=================================================================================================
956 
957 //*************************************************************************************************
965 template< typename T // Type of the singleton (CRTP pattern)
966  , typename D1 // Type of the first lifetime dependency
967  , typename D2 // Type of the second lifetime dependency
968  , typename D3 // Type of the third lifetime dependency
969  , typename D4 > // Type of the fourth lifetime dependency
970 class Singleton<T,D1,D2,D3,D4,NullType,NullType,NullType,NullType>
971  : private NonCopyable
972 {
973  public:
974  //**Type definitions****************************************************************************
976  using SingletonType = Singleton<T,D1,D2,D3,D4,NullType,NullType,NullType,NullType>;
977 
979  using Dependencies = TypeList< D1, D2, D3, D4 >;
980  //**********************************************************************************************
981 
982  protected:
983  //**Constructor*********************************************************************************
988  explicit Singleton()
989  : dependency1_( D1::instance() ) // Handle to the first lifetime dependency
990  , dependency2_( D2::instance() ) // Handle to the second lifetime dependency
991  , dependency3_( D3::instance() ) // Handle to the third lifetime dependency
992  , dependency4_( D4::instance() ) // Handle to the fourth lifetime dependency
993  {
995  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D1, typename D1::SingletonType );
996  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D2, typename D2::SingletonType );
997  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D3, typename D3::SingletonType );
998  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D4, typename D4::SingletonType );
1003  }
1004  //**********************************************************************************************
1005 
1006  //**Destructor**********************************************************************************
1009  ~Singleton()
1010  {}
1011  //**********************************************************************************************
1012 
1013  public:
1014  //**Instance function***************************************************************************
1017  static std::shared_ptr<T> instance()
1018  {
1019  static std::shared_ptr<T> object( new T() );
1020  return object;
1021  }
1023  //**********************************************************************************************
1024 
1025  private:
1026  //**Member variables****************************************************************************
1029  std::shared_ptr<D1> dependency1_;
1030  std::shared_ptr<D2> dependency2_;
1031  std::shared_ptr<D3> dependency3_;
1032  std::shared_ptr<D4> dependency4_;
1033 
1034  //**********************************************************************************************
1035 };
1037 //*************************************************************************************************
1038 
1039 
1040 
1041 
1042 //=================================================================================================
1043 //
1044 // SINGLETON SPECIALIZATION (3 LIFETIME DEPENDENCIES)
1045 //
1046 //=================================================================================================
1047 
1048 //*************************************************************************************************
1056 template< typename T // Type of the singleton (CRTP pattern)
1057  , typename D1 // Type of the first lifetime dependency
1058  , typename D2 // Type of the second lifetime dependency
1059  , typename D3 > // Type of the third lifetime dependency
1060 class Singleton<T,D1,D2,D3,NullType,NullType,NullType,NullType,NullType>
1061  : private NonCopyable
1062 {
1063  public:
1064  //**Type definitions****************************************************************************
1066  using SingletonType = Singleton<T,D1,D2,D3,NullType,NullType,NullType,NullType,NullType>;
1067 
1069  using Dependencies = TypeList< D1, D2, D3 >;
1070  //**********************************************************************************************
1071 
1072  protected:
1073  //**Constructor*********************************************************************************
1078  explicit Singleton()
1079  : dependency1_( D1::instance() ) // Handle to the first lifetime dependency
1080  , dependency2_( D2::instance() ) // Handle to the second lifetime dependency
1081  , dependency3_( D3::instance() ) // Handle to the third lifetime dependency
1082  {
1084  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D1, typename D1::SingletonType );
1085  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D2, typename D2::SingletonType );
1086  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D3, typename D3::SingletonType );
1090  }
1091  //**********************************************************************************************
1092 
1093  //**Destructor**********************************************************************************
1096  ~Singleton()
1097  {}
1098  //**********************************************************************************************
1099 
1100  public:
1101  //**Instance function***************************************************************************
1104  static std::shared_ptr<T> instance()
1105  {
1106  static std::shared_ptr<T> object( new T() );
1107  return object;
1108  }
1110  //**********************************************************************************************
1111 
1112  private:
1113  //**Member variables****************************************************************************
1116  std::shared_ptr<D1> dependency1_;
1117  std::shared_ptr<D2> dependency2_;
1118  std::shared_ptr<D3> dependency3_;
1119 
1120  //**********************************************************************************************
1121 };
1123 //*************************************************************************************************
1124 
1125 
1126 
1127 
1128 //=================================================================================================
1129 //
1130 // SINGLETON SPECIALIZATION (2 LIFETIME DEPENDENCIES)
1131 //
1132 //=================================================================================================
1133 
1134 //*************************************************************************************************
1142 template< typename T // Type of the singleton (CRTP pattern)
1143  , typename D1 // Type of the first lifetime dependency
1144  , typename D2 > // Type of the second lifetime dependency
1145 class Singleton<T,D1,D2,NullType,NullType,NullType,NullType,NullType,NullType>
1146  : private NonCopyable
1147 {
1148  public:
1149  //**Type definitions****************************************************************************
1151  using SingletonType = Singleton<T,D1,D2,NullType,NullType,NullType,NullType,NullType,NullType>;
1152 
1154  using Dependencies = TypeList< D1, D2 >;
1155  //**********************************************************************************************
1156 
1157  protected:
1158  //**Constructor*********************************************************************************
1163  explicit Singleton()
1164  : dependency1_( D1::instance() ) // Handle to the first lifetime dependency
1165  , dependency2_( D2::instance() ) // Handle to the second lifetime dependency
1166  {
1168  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D1, typename D1::SingletonType );
1169  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D2, typename D2::SingletonType );
1172  }
1173  //**********************************************************************************************
1174 
1175  //**Destructor**********************************************************************************
1178  ~Singleton()
1179  {}
1180  //**********************************************************************************************
1181 
1182  public:
1183  //**Instance function***************************************************************************
1186  static std::shared_ptr<T> instance()
1187  {
1188  static std::shared_ptr<T> object( new T() );
1189  return object;
1190  }
1192  //**********************************************************************************************
1193 
1194  private:
1195  //**Member variables****************************************************************************
1198  std::shared_ptr<D1> dependency1_;
1199  std::shared_ptr<D2> dependency2_;
1200 
1201  //**********************************************************************************************
1202 };
1204 //*************************************************************************************************
1205 
1206 
1207 
1208 
1209 //=================================================================================================
1210 //
1211 // SINGLETON SPECIALIZATION (1 LIFETIME DEPENDENCY)
1212 //
1213 //=================================================================================================
1214 
1215 //*************************************************************************************************
1223 template< typename T // Type of the singleton (CRTP pattern)
1224  , typename D1 > // Type of the lifetime dependency
1225 class Singleton<T,D1,NullType,NullType,NullType,NullType,NullType,NullType,NullType>
1226  : private NonCopyable
1227 {
1228  public:
1229  //**Type definitions****************************************************************************
1231  using SingletonType = Singleton<T,D1,NullType,NullType,NullType,NullType,NullType,NullType,NullType>;
1232 
1234  using Dependencies = TypeList< D1 >;
1235  //**********************************************************************************************
1236 
1237  protected:
1238  //**Constructor*********************************************************************************
1243  explicit Singleton()
1244  : dependency1_( D1::instance() ) // Handle to the lifetime dependency
1245  {
1247  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D1, typename D1::SingletonType );
1249  }
1250  //**********************************************************************************************
1251 
1252  //**Destructor**********************************************************************************
1255  ~Singleton()
1256  {}
1257  //**********************************************************************************************
1258 
1259  public:
1260  //**Instance function***************************************************************************
1263  static std::shared_ptr<T> instance()
1264  {
1265  static std::shared_ptr<T> object( new T() );
1266  return object;
1267  }
1269  //**********************************************************************************************
1270 
1271  private:
1272  //**Member variables****************************************************************************
1275  std::shared_ptr<D1> dependency1_;
1276 
1277  //**********************************************************************************************
1278 };
1280 //*************************************************************************************************
1281 
1282 
1283 
1284 
1285 //=================================================================================================
1286 //
1287 // SINGLETON SPECIALIZATION (0 LIFETIME DEPENDENCIES)
1288 //
1289 //=================================================================================================
1290 
1291 //*************************************************************************************************
1299 template< typename T > // Type of the singleton (CRTP pattern)
1300 class Singleton<T,NullType,NullType,NullType,NullType,NullType,NullType,NullType,NullType>
1301  : private NonCopyable
1302 {
1303  public:
1304  //**Type definitions****************************************************************************
1306  using SingletonType = Singleton<T,NullType,NullType,NullType,NullType,NullType,NullType,NullType,NullType>;
1307 
1309  using Dependencies = NullType;
1310  //**********************************************************************************************
1311 
1312  protected:
1313  //**Constructor*********************************************************************************
1318  explicit Singleton()
1319  {
1321  }
1322  //**********************************************************************************************
1323 
1324  //**Destructor**********************************************************************************
1327  ~Singleton()
1328  {}
1329  //**********************************************************************************************
1330 
1331  public:
1332  //**Instance function***************************************************************************
1335  static std::shared_ptr<T> instance()
1336  {
1337  static std::shared_ptr<T> object( new T() );
1338  return object;
1339  }
1341  //**********************************************************************************************
1342 };
1344 //*************************************************************************************************
1345 
1346 } // namespace blaze
1347 
1348 #endif
typename TypeAt< TL, Index >::Type TypeAt_t
Auxiliary alias declaration for the TypeAt class template.The TypeAt_t alias declaration provides a c...
Definition: TypeAt.h:140
Utility type for generic codes.
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
BoolConstant< true > TrueType
Type traits base class.The TrueType class is used as base class for type traits and value traits that...
Definition: TrueType.h:61
~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
TypeList< D1, D2, D3, D4, D5, D6, D7, D8 > Dependencies
Type list of all lifetime dependencies.
Definition: Singleton.h:570
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:58
std::shared_ptr< D6 > dependency6_
Handle to the sixth lifetime dependency.
Definition: Singleton.h:637
#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
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.
Implementation of a type list.The TypeList class template represents a list of data types of arbitrar...
Definition: TypeList.h:119
Base class for all lifetime managed singletons.The Singleton class represents the base class for all ...
Definition: Singleton.h:561
Singleton< T, D1, D2, D3, D4, D5, D6, D7, D8 > SingletonType
Type of this Singleton instance.
Definition: Singleton.h:567
Header file for the Unique class template.
Utility type for generic codes.The NullType class represents an invalid or terminating data type for ...
Definition: NullType.h:54
IntegralConstant< bool, B > BoolConstant
Generic wrapper for a compile time constant boolean value.The BoolConstant class template represents ...
Definition: IntegralConstant.h:101
Definition: Singleton.h:63
Header file for the IntegralConstant class template.
#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:383
Singleton()
Constructor for the Singleton class.
Definition: Singleton.h:579
Header file for compile time constraints.
Header file for the TrueType type/value trait base class.