Blaze 3.9
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>
48#include <blaze/util/NullType.h>
49#include <blaze/util/Suffix.h>
50#include <blaze/util/TypeList.h>
51
52
53namespace blaze {
54
55//=================================================================================================
56//
57// ::blaze NAMESPACE FORWARD DECLARATIONS
58//
59//=================================================================================================
60
61template< typename > class Dependency;
62template< typename T, typename TL, bool C > struct HasCyclicDependency;
63
64
65
66
67//=================================================================================================
68//
69// CLASS HASCYCLICDEPENDENCYHELPER
70//
71//=================================================================================================
72
73//*************************************************************************************************
81template< typename TL // Type list of checked lifetime dependencies
82 , typename D // Type list of lifetime dependencies to check
83 , size_t N = Length_v<D> > // Length of the dependency type list
84struct HasCyclicDependencyHelper;
86//*************************************************************************************************
87
88
89//*************************************************************************************************
97template< typename TL // Type list of checked lifetime dependencies
98 , size_t N > // Length of the dependency type list
99struct HasCyclicDependencyHelper<TL,NullType,N>
100{
101 static constexpr bool value = false;
102};
104//*************************************************************************************************
105
106
107//*************************************************************************************************
115template< typename TL // Type list of checked lifetime dependencies
116 , typename D > // Type list of lifetime dependencies to check
117struct HasCyclicDependencyHelper<TL,D,1>
118{
119 using D1 = TypeAt_t<D,0>;
120
121 static constexpr bool value = HasCyclicDependency<D1,TL,Contains_v<TL,D1>>::value;
122};
124//*************************************************************************************************
125
126
127//*************************************************************************************************
135template< typename TL // Type list of checked lifetime dependencies
136 , typename D > // Type list of lifetime dependencies to check
137struct HasCyclicDependencyHelper<TL,D,2>
138{
139 using D1 = TypeAt_t<D,0>;
140 using D2 = TypeAt_t<D,1>;
141
142 static constexpr bool value = ( HasCyclicDependency<D1,TL,Contains_v<TL,D1>>::value ||
143 HasCyclicDependency<D2,TL,Contains_v<TL,D2>>::value );
144};
146//*************************************************************************************************
147
148
149//*************************************************************************************************
157template< typename TL // Type list of checked lifetime dependencies
158 , typename D > // Type list of lifetime dependencies to check
159struct HasCyclicDependencyHelper<TL,D,3>
160{
161 using D1 = TypeAt_t<D,0>;
162 using D2 = TypeAt_t<D,1>;
163 using D3 = TypeAt_t<D,2>;
164
165 static constexpr bool value = ( HasCyclicDependency<D1,TL,Contains_v<TL,D1>>::value ||
166 HasCyclicDependency<D2,TL,Contains_v<TL,D2>>::value ||
167 HasCyclicDependency<D3,TL,Contains_v<TL,D3>>::value );
168};
170//*************************************************************************************************
171
172
173//*************************************************************************************************
181template< typename TL // Type list of checked lifetime dependencies
182 , typename D > // Type list of lifetime dependencies to check
183struct HasCyclicDependencyHelper<TL,D,4>
184{
185 using D1 = TypeAt_t<D,0>;
186 using D2 = TypeAt_t<D,1>;
187 using D3 = TypeAt_t<D,2>;
188 using D4 = TypeAt_t<D,3>;
189
190 static constexpr bool value = ( HasCyclicDependency<D1,TL,Contains_v<TL,D1>>::value ||
191 HasCyclicDependency<D2,TL,Contains_v<TL,D2>>::value ||
192 HasCyclicDependency<D3,TL,Contains_v<TL,D3>>::value ||
193 HasCyclicDependency<D4,TL,Contains_v<TL,D4>>::value );
194};
196//*************************************************************************************************
197
198
199//*************************************************************************************************
207template< typename TL // Type list of checked lifetime dependencies
208 , typename D > // Type list of lifetime dependencies to check
209struct HasCyclicDependencyHelper<TL,D,5>
210{
211 using D1 = TypeAt_t<D,0>;
212 using D2 = TypeAt_t<D,1>;
213 using D3 = TypeAt_t<D,2>;
214 using D4 = TypeAt_t<D,3>;
215 using D5 = TypeAt_t<D,4>;
216
217 static constexpr bool value = ( HasCyclicDependency<D1,TL,Contains_v<TL,D1>>::value ||
218 HasCyclicDependency<D2,TL,Contains_v<TL,D2>>::value ||
219 HasCyclicDependency<D3,TL,Contains_v<TL,D3>>::value ||
220 HasCyclicDependency<D4,TL,Contains_v<TL,D4>>::value ||
221 HasCyclicDependency<D5,TL,Contains_v<TL,D5>>::value );
222};
224//*************************************************************************************************
225
226
227//*************************************************************************************************
235template< typename TL // Type list of checked lifetime dependencies
236 , typename D > // Type list of lifetime dependencies to check
237struct HasCyclicDependencyHelper<TL,D,6>
238{
239 using D1 = TypeAt_t<D,0>;
240 using D2 = TypeAt_t<D,1>;
241 using D3 = TypeAt_t<D,2>;
242 using D4 = TypeAt_t<D,3>;
243 using D5 = TypeAt_t<D,4>;
244 using D6 = TypeAt_t<D,5>;
245
246 static constexpr bool value = ( HasCyclicDependency<D1,TL,Contains_v<TL,D1>>::value ||
247 HasCyclicDependency<D2,TL,Contains_v<TL,D2>>::value ||
248 HasCyclicDependency<D3,TL,Contains_v<TL,D3>>::value ||
249 HasCyclicDependency<D4,TL,Contains_v<TL,D4>>::value ||
250 HasCyclicDependency<D5,TL,Contains_v<TL,D5>>::value ||
251 HasCyclicDependency<D6,TL,Contains_v<TL,D6>>::value );
252};
254//*************************************************************************************************
255
256
257//*************************************************************************************************
265template< typename TL // Type list of checked lifetime dependencies
266 , typename D > // Type list of lifetime dependencies to check
267struct HasCyclicDependencyHelper<TL,D,7>
268{
269 using D1 = TypeAt_t<D,0>;
270 using D2 = TypeAt_t<D,1>;
271 using D3 = TypeAt_t<D,2>;
272 using D4 = TypeAt_t<D,3>;
273 using D5 = TypeAt_t<D,4>;
274 using D6 = TypeAt_t<D,5>;
275 using D7 = TypeAt_t<D,6>;
276
277 static constexpr bool value = ( HasCyclicDependency<D1,TL,Contains_v<TL,D1>>::value ||
278 HasCyclicDependency<D2,TL,Contains_v<TL,D2>>::value ||
279 HasCyclicDependency<D3,TL,Contains_v<TL,D3>>::value ||
280 HasCyclicDependency<D4,TL,Contains_v<TL,D4>>::value ||
281 HasCyclicDependency<D5,TL,Contains_v<TL,D5>>::value ||
282 HasCyclicDependency<D6,TL,Contains_v<TL,D6>>::value ||
283 HasCyclicDependency<D7,TL,Contains_v<TL,D7>>::value );
284};
286//*************************************************************************************************
287
288
289//*************************************************************************************************
297template< typename TL // Type list of checked lifetime dependencies
298 , typename D > // Type list of lifetime dependencies to check
299struct HasCyclicDependencyHelper<TL,D,8>
300{
301 using D1 = TypeAt_t<D,0>;
302 using D2 = TypeAt_t<D,1>;
303 using D3 = TypeAt_t<D,2>;
304 using D4 = TypeAt_t<D,3>;
305 using D5 = TypeAt_t<D,4>;
306 using D6 = TypeAt_t<D,5>;
307 using D7 = TypeAt_t<D,6>;
308 using D8 = TypeAt_t<D,7>;
309
310 static constexpr bool value = ( HasCyclicDependency<D1,TL,Contains_v<TL,D1>>::value ||
311 HasCyclicDependency<D2,TL,Contains_v<TL,D2>>::value ||
312 HasCyclicDependency<D3,TL,Contains_v<TL,D3>>::value ||
313 HasCyclicDependency<D4,TL,Contains_v<TL,D4>>::value ||
314 HasCyclicDependency<D5,TL,Contains_v<TL,D5>>::value ||
315 HasCyclicDependency<D6,TL,Contains_v<TL,D6>>::value ||
316 HasCyclicDependency<D7,TL,Contains_v<TL,D7>>::value ||
317 HasCyclicDependency<D8,TL,Contains_v<TL,D8>>::value );
318};
320//*************************************************************************************************
321
322
323
324
325//=================================================================================================
326//
327// CLASS HASCYCLICDEPENDENCY
328//
329//=================================================================================================
330
331//*************************************************************************************************
340template< typename T // The type to be checked for cyclic lifetime dependencies
341 , typename TL // Type list of checked lifetime dependencies
342 , bool C = Contains_v<TL,T> > // Flag to indicate whether T is contained in TL
343struct HasCyclicDependency
344 : public BoolConstant< HasCyclicDependencyHelper<Append_t<TL,T>,typename T::Dependencies>::value >
345{};
347//*************************************************************************************************
348
349
350//*************************************************************************************************
359template< typename T // The type to be checked for cyclic lifetime dependencies
360 , typename TL > // Type list of checked lifetime dependencies
361struct HasCyclicDependency<T,TL,true>
362 : public TrueType
363{};
365//*************************************************************************************************
366
367
368
369
370//=================================================================================================
371//
372// DETECT_CYCLIC_LIFETIME_DEPENDENCY CONSTRAINT
373//
374//=================================================================================================
375
376//*************************************************************************************************
382#define BLAZE_DETECT_CYCLIC_LIFETIME_DEPENDENCY(T) \
383 static_assert( ( !blaze::HasCyclicDependency<T,blaze::NullType>::value ), "Cyclic dependency detected" )
384//*************************************************************************************************
385
386
387
388
389//=================================================================================================
390//
391// BEFRIEND_SINGLETON MACRO
392//
393//=================================================================================================
394
395//*************************************************************************************************
402#define BLAZE_BEFRIEND_SINGLETON \
403 template< typename, typename, typename, typename, typename, typename, typename, typename, typename > friend class blaze::Singleton; \
404 template< typename, typename, bool > friend struct blaze::HasCyclicDependency; \
405 template< typename > friend class blaze::Dependency;
406//*************************************************************************************************
407
408
409
410
411//=================================================================================================
412//
413// CLASS SINGLETON
414//
415//=================================================================================================
416
417//*************************************************************************************************
551template< typename T // Type of the singleton (CRTP pattern)
552 , typename D1 = NullType // Type of the first lifetime dependency
553 , typename D2 = NullType // Type of the second lifetime dependency
554 , typename D3 = NullType // Type of the third lifetime dependency
555 , typename D4 = NullType // Type of the fourth lifetime dependency
556 , typename D5 = NullType // Type of the fifth lifetime dependency
557 , typename D6 = NullType // Type of the sixth lifetime dependency
558 , typename D7 = NullType // Type of the seventh lifetime dependency
559 , typename D8 = NullType > // Type of the eighth lifetime dependency
561 : private NonCopyable
562{
563 public:
564 //**Type definitions****************************************************************************
567
570 //**********************************************************************************************
571
572 protected:
573 //**Constructor*********************************************************************************
579 : dependency1_( D1::instance() ) // Handle to the first lifetime dependency
580 , dependency2_( D2::instance() ) // Handle to the second lifetime dependency
581 , dependency3_( D3::instance() ) // Handle to the third lifetime dependency
582 , dependency4_( D4::instance() ) // Handle to the fourth lifetime dependency
583 , dependency5_( D5::instance() ) // Handle to the fifth lifetime dependency
584 , dependency6_( D6::instance() ) // Handle to the sixth lifetime dependency
585 , dependency7_( D7::instance() ) // Handle to the seventh lifetime dependency
586 , dependency8_( D8::instance() ) // Handle to the eighth lifetime dependency
587 {
589 BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D1, typename D1::SingletonType );
590 BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D2, typename D2::SingletonType );
591 BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D3, typename D3::SingletonType );
592 BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D4, typename D4::SingletonType );
593 BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D5, typename D5::SingletonType );
594 BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D6, typename D6::SingletonType );
595 BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D7, typename D7::SingletonType );
596 BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D8, typename D8::SingletonType );
605 }
606 //**********************************************************************************************
607
608 //**Destructor**********************************************************************************
612 {}
613 //**********************************************************************************************
614
615 public:
616 //**Instance function***************************************************************************
619 static std::shared_ptr<T> instance()
620 {
621 static std::shared_ptr<T> object( new T() );
622 return object;
623 }
625 //**********************************************************************************************
626
627 private:
628 //**Member variables****************************************************************************
631 std::shared_ptr<D1> dependency1_;
632 std::shared_ptr<D2> dependency2_;
633 std::shared_ptr<D3> dependency3_;
634 std::shared_ptr<D4> dependency4_;
635 std::shared_ptr<D5> dependency5_;
636 std::shared_ptr<D6> dependency6_;
637 std::shared_ptr<D7> dependency7_;
638 std::shared_ptr<D8> dependency8_;
640 //**********************************************************************************************
641};
642//*************************************************************************************************
643
644
645
646
647//=================================================================================================
648//
649// SINGLETON SPECIALIZATION (7 LIFETIME DEPENDENCIES)
650//
651//=================================================================================================
652
653//*************************************************************************************************
661template< typename T // Type of the singleton (CRTP pattern)
662 , typename D1 // Type of the first lifetime dependency
663 , typename D2 // Type of the second lifetime dependency
664 , typename D3 // Type of the third lifetime dependency
665 , typename D4 // Type of the fourth lifetime dependency
666 , typename D5 // Type of the fifth lifetime dependency
667 , typename D6 // Type of the sixth lifetime dependency
668 , typename D7 > // Type of the eighth lifetime dependency
669class Singleton<T,D1,D2,D3,D4,D5,D6,D7,NullType>
670 : private NonCopyable
671{
672 public:
673 //**Type definitions****************************************************************************
676
679 //**********************************************************************************************
680
681 protected:
682 //**Constructor*********************************************************************************
687 Singleton()
688 : dependency1_( D1::instance() ) // Handle to the first lifetime dependency
689 , dependency2_( D2::instance() ) // Handle to the second lifetime dependency
690 , dependency3_( D3::instance() ) // Handle to the third lifetime dependency
691 , dependency4_( D4::instance() ) // Handle to the fourth lifetime dependency
692 , dependency5_( D5::instance() ) // Handle to the fifth lifetime dependency
693 , dependency6_( D6::instance() ) // Handle to the sixth lifetime dependency
694 , dependency7_( D7::instance() ) // Handle to the seventh lifetime dependency
695 {
697 BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D1, typename D1::SingletonType );
698 BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D2, typename D2::SingletonType );
699 BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D3, typename D3::SingletonType );
700 BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D4, typename D4::SingletonType );
701 BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D5, typename D5::SingletonType );
702 BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D6, typename D6::SingletonType );
703 BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D7, typename D7::SingletonType );
711 }
712 //**********************************************************************************************
713
714 //**Destructor**********************************************************************************
717 ~Singleton()
718 {}
719 //**********************************************************************************************
720
721 public:
722 //**Instance function***************************************************************************
725 static std::shared_ptr<T> instance()
726 {
727 static std::shared_ptr<T> object( new T() );
728 return object;
729 }
731 //**********************************************************************************************
732
733 private:
734 //**Member variables****************************************************************************
737 std::shared_ptr<D1> dependency1_;
738 std::shared_ptr<D2> dependency2_;
739 std::shared_ptr<D3> dependency3_;
740 std::shared_ptr<D4> dependency4_;
741 std::shared_ptr<D5> dependency5_;
742 std::shared_ptr<D6> dependency6_;
743 std::shared_ptr<D7> dependency7_;
745 //**********************************************************************************************
746};
748//*************************************************************************************************
749
750
751
752
753//=================================================================================================
754//
755// SINGLETON SPECIALIZATION (6 LIFETIME DEPENDENCIES)
756//
757//=================================================================================================
758
759//*************************************************************************************************
767template< typename T // Type of the singleton (CRTP pattern)
768 , typename D1 // Type of the first lifetime dependency
769 , typename D2 // Type of the second lifetime dependency
770 , typename D3 // Type of the third lifetime dependency
771 , typename D4 // Type of the fourth lifetime dependency
772 , typename D5 // Type of the fifth lifetime dependency
773 , typename D6 > // Type of the eighth lifetime dependency
774class Singleton<T,D1,D2,D3,D4,D5,D6,NullType,NullType>
775 : private NonCopyable
776{
777 public:
778 //**Type definitions****************************************************************************
780 using SingletonType = Singleton<T,D1,D2,D3,D4,D5,D6,NullType,NullType>;
781
783 using Dependencies = TypeList< D1, D2, D3, D4, D5, D6 >;
784 //**********************************************************************************************
785
786 protected:
787 //**Constructor*********************************************************************************
792 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 {
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_;
846 //**********************************************************************************************
847};
849//*************************************************************************************************
850
851
852
853
854//=================================================================================================
855//
856// SINGLETON SPECIALIZATION (5 LIFETIME DEPENDENCIES)
857//
858//=================================================================================================
859
860//*************************************************************************************************
868template< 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
874class Singleton<T,D1,D2,D3,D4,D5,NullType,NullType,NullType>
875 : private NonCopyable
876{
877 public:
878 //**Type definitions****************************************************************************
880 using SingletonType = Singleton<T,D1,D2,D3,D4,D5,NullType,NullType,NullType>;
881
883 using Dependencies = TypeList< D1, D2, D3, D4, D5 >;
884 //**********************************************************************************************
885
886 protected:
887 //**Constructor*********************************************************************************
892 Singleton()
893 : dependency1_( D1::instance() ) // Handle to the first lifetime dependency
894 , dependency2_( D2::instance() ) // Handle to the second lifetime dependency
895 , dependency3_( D3::instance() ) // Handle to the third lifetime dependency
896 , dependency4_( D4::instance() ) // Handle to the fourth lifetime dependency
897 , dependency5_( D5::instance() ) // Handle to the fifth lifetime dependency
898 {
900 BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D1, typename D1::SingletonType );
901 BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D2, typename D2::SingletonType );
902 BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D3, typename D3::SingletonType );
903 BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D4, typename D4::SingletonType );
904 BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D5, typename D5::SingletonType );
910 }
911 //**********************************************************************************************
912
913 //**Destructor**********************************************************************************
916 ~Singleton()
917 {}
918 //**********************************************************************************************
919
920 public:
921 //**Instance function***************************************************************************
924 static std::shared_ptr<T> instance()
925 {
926 static std::shared_ptr<T> object( new T() );
927 return object;
928 }
930 //**********************************************************************************************
931
932 private:
933 //**Member variables****************************************************************************
936 std::shared_ptr<D1> dependency1_;
937 std::shared_ptr<D2> dependency2_;
938 std::shared_ptr<D3> dependency3_;
939 std::shared_ptr<D4> dependency4_;
940 std::shared_ptr<D5> dependency5_;
942 //**********************************************************************************************
943};
945//*************************************************************************************************
946
947
948
949
950//=================================================================================================
951//
952// SINGLETON SPECIALIZATION (4 LIFETIME DEPENDENCIES)
953//
954//=================================================================================================
955
956//*************************************************************************************************
964template< typename T // Type of the singleton (CRTP pattern)
965 , typename D1 // Type of the first lifetime dependency
966 , typename D2 // Type of the second lifetime dependency
967 , typename D3 // Type of the third lifetime dependency
968 , typename D4 > // Type of the fourth lifetime dependency
969class Singleton<T,D1,D2,D3,D4,NullType,NullType,NullType,NullType>
970 : private NonCopyable
971{
972 public:
973 //**Type definitions****************************************************************************
975 using SingletonType = Singleton<T,D1,D2,D3,D4,NullType,NullType,NullType,NullType>;
976
978 using Dependencies = TypeList< D1, D2, D3, D4 >;
979 //**********************************************************************************************
980
981 protected:
982 //**Constructor*********************************************************************************
987 Singleton()
988 : dependency1_( D1::instance() ) // Handle to the first lifetime dependency
989 , dependency2_( D2::instance() ) // Handle to the second lifetime dependency
990 , dependency3_( D3::instance() ) // Handle to the third lifetime dependency
991 , dependency4_( D4::instance() ) // Handle to the fourth lifetime dependency
992 {
994 BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D1, typename D1::SingletonType );
995 BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D2, typename D2::SingletonType );
996 BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D3, typename D3::SingletonType );
997 BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D4, typename D4::SingletonType );
1002 }
1003 //**********************************************************************************************
1004
1005 //**Destructor**********************************************************************************
1008 ~Singleton()
1009 {}
1010 //**********************************************************************************************
1011
1012 public:
1013 //**Instance function***************************************************************************
1016 static std::shared_ptr<T> instance()
1017 {
1018 static std::shared_ptr<T> object( new T() );
1019 return object;
1020 }
1022 //**********************************************************************************************
1023
1024 private:
1025 //**Member variables****************************************************************************
1028 std::shared_ptr<D1> dependency1_;
1029 std::shared_ptr<D2> dependency2_;
1030 std::shared_ptr<D3> dependency3_;
1031 std::shared_ptr<D4> dependency4_;
1033 //**********************************************************************************************
1034};
1036//*************************************************************************************************
1037
1038
1039
1040
1041//=================================================================================================
1042//
1043// SINGLETON SPECIALIZATION (3 LIFETIME DEPENDENCIES)
1044//
1045//=================================================================================================
1046
1047//*************************************************************************************************
1055template< typename T // Type of the singleton (CRTP pattern)
1056 , typename D1 // Type of the first lifetime dependency
1057 , typename D2 // Type of the second lifetime dependency
1058 , typename D3 > // Type of the third lifetime dependency
1059class Singleton<T,D1,D2,D3,NullType,NullType,NullType,NullType,NullType>
1060 : private NonCopyable
1061{
1062 public:
1063 //**Type definitions****************************************************************************
1065 using SingletonType = Singleton<T,D1,D2,D3,NullType,NullType,NullType,NullType,NullType>;
1066
1068 using Dependencies = TypeList< D1, D2, D3 >;
1069 //**********************************************************************************************
1070
1071 protected:
1072 //**Constructor*********************************************************************************
1077 Singleton()
1078 : dependency1_( D1::instance() ) // Handle to the first lifetime dependency
1079 , dependency2_( D2::instance() ) // Handle to the second lifetime dependency
1080 , dependency3_( D3::instance() ) // Handle to the third lifetime dependency
1081 {
1083 BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D1, typename D1::SingletonType );
1084 BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D2, typename D2::SingletonType );
1085 BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D3, typename D3::SingletonType );
1089 }
1090 //**********************************************************************************************
1091
1092 //**Destructor**********************************************************************************
1095 ~Singleton()
1096 {}
1097 //**********************************************************************************************
1098
1099 public:
1100 //**Instance function***************************************************************************
1103 static std::shared_ptr<T> instance()
1104 {
1105 static std::shared_ptr<T> object( new T() );
1106 return object;
1107 }
1109 //**********************************************************************************************
1110
1111 private:
1112 //**Member variables****************************************************************************
1115 std::shared_ptr<D1> dependency1_;
1116 std::shared_ptr<D2> dependency2_;
1117 std::shared_ptr<D3> dependency3_;
1119 //**********************************************************************************************
1120};
1122//*************************************************************************************************
1123
1124
1125
1126
1127//=================================================================================================
1128//
1129// SINGLETON SPECIALIZATION (2 LIFETIME DEPENDENCIES)
1130//
1131//=================================================================================================
1132
1133//*************************************************************************************************
1141template< typename T // Type of the singleton (CRTP pattern)
1142 , typename D1 // Type of the first lifetime dependency
1143 , typename D2 > // Type of the second lifetime dependency
1144class Singleton<T,D1,D2,NullType,NullType,NullType,NullType,NullType,NullType>
1145 : private NonCopyable
1146{
1147 public:
1148 //**Type definitions****************************************************************************
1150 using SingletonType = Singleton<T,D1,D2,NullType,NullType,NullType,NullType,NullType,NullType>;
1151
1153 using Dependencies = TypeList< D1, D2 >;
1154 //**********************************************************************************************
1155
1156 protected:
1157 //**Constructor*********************************************************************************
1162 Singleton()
1163 : dependency1_( D1::instance() ) // Handle to the first lifetime dependency
1164 , dependency2_( D2::instance() ) // Handle to the second lifetime dependency
1165 {
1167 BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D1, typename D1::SingletonType );
1168 BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D2, typename D2::SingletonType );
1171 }
1172 //**********************************************************************************************
1173
1174 //**Destructor**********************************************************************************
1177 ~Singleton()
1178 {}
1179 //**********************************************************************************************
1180
1181 public:
1182 //**Instance function***************************************************************************
1185 static std::shared_ptr<T> instance()
1186 {
1187 static std::shared_ptr<T> object( new T() );
1188 return object;
1189 }
1191 //**********************************************************************************************
1192
1193 private:
1194 //**Member variables****************************************************************************
1197 std::shared_ptr<D1> dependency1_;
1198 std::shared_ptr<D2> dependency2_;
1200 //**********************************************************************************************
1201};
1203//*************************************************************************************************
1204
1205
1206
1207
1208//=================================================================================================
1209//
1210// SINGLETON SPECIALIZATION (1 LIFETIME DEPENDENCY)
1211//
1212//=================================================================================================
1213
1214//*************************************************************************************************
1222template< typename T // Type of the singleton (CRTP pattern)
1223 , typename D1 > // Type of the lifetime dependency
1224class Singleton<T,D1,NullType,NullType,NullType,NullType,NullType,NullType,NullType>
1225 : private NonCopyable
1226{
1227 public:
1228 //**Type definitions****************************************************************************
1230 using SingletonType = Singleton<T,D1,NullType,NullType,NullType,NullType,NullType,NullType,NullType>;
1231
1233 using Dependencies = TypeList< D1 >;
1234 //**********************************************************************************************
1235
1236 protected:
1237 //**Constructor*********************************************************************************
1242 Singleton()
1243 : dependency1_( D1::instance() ) // Handle to the lifetime dependency
1244 {
1246 BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D1, typename D1::SingletonType );
1248 }
1249 //**********************************************************************************************
1250
1251 //**Destructor**********************************************************************************
1254 ~Singleton()
1255 {}
1256 //**********************************************************************************************
1257
1258 public:
1259 //**Instance function***************************************************************************
1262 static std::shared_ptr<T> instance()
1263 {
1264 static std::shared_ptr<T> object( new T() );
1265 return object;
1266 }
1268 //**********************************************************************************************
1269
1270 private:
1271 //**Member variables****************************************************************************
1274 std::shared_ptr<D1> dependency1_;
1276 //**********************************************************************************************
1277};
1279//*************************************************************************************************
1280
1281
1282
1283
1284//=================================================================================================
1285//
1286// SINGLETON SPECIALIZATION (0 LIFETIME DEPENDENCIES)
1287//
1288//=================================================================================================
1289
1290//*************************************************************************************************
1298template< typename T > // Type of the singleton (CRTP pattern)
1299class Singleton<T,NullType,NullType,NullType,NullType,NullType,NullType,NullType,NullType>
1300 : private NonCopyable
1301{
1302 public:
1303 //**Type definitions****************************************************************************
1305 using SingletonType = Singleton<T,NullType,NullType,NullType,NullType,NullType,NullType,NullType,NullType>;
1306
1308 using Dependencies = NullType;
1309 //**********************************************************************************************
1310
1311 protected:
1312 //**Constructor*********************************************************************************
1317 Singleton()
1318 {
1320 }
1321 //**********************************************************************************************
1322
1323 //**Destructor**********************************************************************************
1326 ~Singleton()
1327 {}
1328 //**********************************************************************************************
1329
1330 public:
1331 //**Instance function***************************************************************************
1334 static std::shared_ptr<T> instance()
1335 {
1336 static std::shared_ptr<T> object( new T() );
1337 return object;
1338 }
1340 //**********************************************************************************************
1341};
1343//*************************************************************************************************
1344
1345} // namespace blaze
1346
1347#endif
Constraint on the inheritance relationship of a data type.
Header file for the IntegralConstant class template.
Base class for non-copyable class instances.
Utility type for generic codes.
Header file for compile time constraints.
Header file for the type list functionality.
Base class for non-copyable class instances.
Definition: NonCopyable.h:64
Utility type for generic codes.
Definition: NullType.h:55
Base class for all lifetime managed singletons.
Definition: Singleton.h:562
Singleton()
Constructor for the Singleton class.
Definition: Singleton.h:578
std::shared_ptr< D6 > dependency6_
Handle to the sixth lifetime dependency.
Definition: Singleton.h:636
std::shared_ptr< D1 > dependency1_
Handle to the first lifetime dependency.
Definition: Singleton.h:631
std::shared_ptr< D7 > dependency7_
Handle to the seventh lifetime dependency.
Definition: Singleton.h:637
std::shared_ptr< D2 > dependency2_
Handle to the second lifetime dependency.
Definition: Singleton.h:632
TypeList< D1, D2, D3, D4, D5, D6, D7, D8 > Dependencies
Type list of all lifetime dependencies.
Definition: Singleton.h:569
Singleton< T, D1, D2, D3, D4, D5, D6, D7, D8 > SingletonType
Type of this Singleton instance.
Definition: Singleton.h:566
std::shared_ptr< D4 > dependency4_
Handle to the fourth lifetime dependency.
Definition: Singleton.h:634
std::shared_ptr< D8 > dependency8_
Handle to the eighth lifetime dependency.
Definition: Singleton.h:638
std::shared_ptr< D5 > dependency5_
Handle to the fifth lifetime dependency.
Definition: Singleton.h:635
std::shared_ptr< D3 > dependency3_
Handle to the third lifetime dependency.
Definition: Singleton.h:633
~Singleton()
Destructor for the Singleton class.
Definition: Singleton.h:611
#define BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM(D, B)
Constraint on the inheritance relationship of a data type.
Definition: DerivedFrom.h:60
#define BLAZE_DETECT_CYCLIC_LIFETIME_DEPENDENCY(T)
Constraint on the data type.
Definition: Singleton.h:382
typename TypeAt< TL, Index >::Type TypeAt_t
Auxiliary alias declaration for the TypeAt class template.
Definition: TypeAt.h:139
BoolConstant< true > TrueType
Type traits base class.
Definition: IntegralConstant.h:132
IntegralConstant< bool, B > BoolConstant
Generic wrapper for a compile time constant boolean value.
Definition: IntegralConstant.h:110
Definition: Singleton.h:62
Implementation of a type list.
Definition: TypeList.h:120