All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 <boost/shared_ptr.hpp>
44 #include <boost/thread/mutex.hpp>
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 { 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 { 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 { 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 { 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 { 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 { 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 { 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 { 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 { 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 { 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 { value = 1 };
365 };
367 //*************************************************************************************************
368 
369 
370 
371 
372 //=================================================================================================
373 //
374 // CLASS CYCLIC_LIFETIME_DEPENDENCY_TEST
375 //
376 //=================================================================================================
377 
378 //*************************************************************************************************
389 template< int > struct CYCLIC_LIFETIME_DEPENDENCY_TEST {};
391 //*************************************************************************************************
392 
393 
394 
395 
396 //=================================================================================================
397 //
398 // DETECT_CYCLIC_LIFETIME_DEPENDENCY CONSTRAINT
399 //
400 //=================================================================================================
401 
402 //*************************************************************************************************
412 template< bool > struct CYCLIC_LIFETIME_DEPENDENCY_DETECTED;
413 template<> struct CYCLIC_LIFETIME_DEPENDENCY_DETECTED<false> { enum { value = 1 }; };
415 //*************************************************************************************************
416 
417 
418 //*************************************************************************************************
424 #define BLAZE_DETECT_CYCLIC_LIFETIME_DEPENDENCY(T) \
425  typedef \
426  blaze::CYCLIC_LIFETIME_DEPENDENCY_TEST< \
427  blaze::CYCLIC_LIFETIME_DEPENDENCY_DETECTED< blaze::HasCyclicDependency<T,blaze::NullType>::value >::value > \
428  BLAZE_JOIN( DETECT_CYCLIC_LIFETIME_DEPENDENCY_TYPEDEF, __LINE__ )
429 //*************************************************************************************************
430 
431 
432 
433 
434 //=================================================================================================
435 //
436 // BEFRIEND_SINGLETON MACRO
437 //
438 //=================================================================================================
439 
440 //*************************************************************************************************
447 #define BLAZE_BEFRIEND_SINGLETON \
448  template< typename, typename, typename, typename, typename, typename, typename, typename, typename > friend class blaze::Singleton; \
449  template< typename, typename, bool > friend struct blaze::HasCyclicDependency; \
450  template< typename > friend class blaze::Dependency;
451 //*************************************************************************************************
452 
453 
454 
455 
456 //=================================================================================================
457 //
458 // CLASS SINGLETON
459 //
460 //=================================================================================================
461 
462 //*************************************************************************************************
597 template< typename T // Type of the singleton (CRTP pattern)
598  , typename D1 = NullType // Type of the first lifetime dependency
599  , typename D2 = NullType // Type of the second lifetime dependency
600  , typename D3 = NullType // Type of the third lifetime dependency
601  , typename D4 = NullType // Type of the fourth lifetime dependency
602  , typename D5 = NullType // Type of the fifth lifetime dependency
603  , typename D6 = NullType // Type of the sixth lifetime dependency
604  , typename D7 = NullType // Type of the seventh lifetime dependency
605  , typename D8 = NullType > // Type of the eighth lifetime dependency
606 class Singleton : private NonCopyable
607 {
608  public:
609  //**Type definitions****************************************************************************
612 
614  typedef BLAZE_TYPELIST_8( D1, D2, D3, D4, D5, D6, D7, D8 ) Dependencies;
615  //**********************************************************************************************
616 
617  protected:
618  //**Constructor*********************************************************************************
623  explicit Singleton()
624  : dependency1_( D1::instance() ) // Handle to the first lifetime dependency
625  , dependency2_( D2::instance() ) // Handle to the second lifetime dependency
626  , dependency3_( D3::instance() ) // Handle to the third lifetime dependency
627  , dependency4_( D4::instance() ) // Handle to the fourth lifetime dependency
628  , dependency5_( D5::instance() ) // Handle to the fifth lifetime dependency
629  , dependency6_( D6::instance() ) // Handle to the sixth lifetime dependency
630  , dependency7_( D7::instance() ) // Handle to the seventh lifetime dependency
631  , dependency8_( D8::instance() ) // Handle to the eighth lifetime dependency
632  {
634  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D1, typename D1::SingletonType );
635  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D2, typename D2::SingletonType );
636  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D3, typename D3::SingletonType );
637  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D4, typename D4::SingletonType );
638  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D5, typename D5::SingletonType );
639  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D6, typename D6::SingletonType );
640  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D7, typename D7::SingletonType );
641  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D8, typename D8::SingletonType );
650  }
651  //**********************************************************************************************
652 
653  //**Destructor**********************************************************************************
657  {}
658  //**********************************************************************************************
659 
660  public:
661  //**Instance function***************************************************************************
664  static boost::shared_ptr<T> instance()
665  {
666  boost::mutex::scoped_lock lock( instanceMutex_ );
667  static boost::shared_ptr<T> object( new T() );
668  return object;
669  }
671  //**********************************************************************************************
672 
673  private:
674  //**Member variables****************************************************************************
677  boost::shared_ptr<D1> dependency1_;
678  boost::shared_ptr<D2> dependency2_;
679  boost::shared_ptr<D3> dependency3_;
680  boost::shared_ptr<D4> dependency4_;
681  boost::shared_ptr<D5> dependency5_;
682  boost::shared_ptr<D6> dependency6_;
683  boost::shared_ptr<D7> dependency7_;
684  boost::shared_ptr<D8> dependency8_;
685 
686  //**********************************************************************************************
687 
688  protected:
689  //**Member variables****************************************************************************
692  static boost::mutex instanceMutex_;
693 
694  //**********************************************************************************************
695 };
696 //*************************************************************************************************
697 
698 
699 
700 
701 //=================================================================================================
702 //
703 // SINGLETON SPECIALIZATION (7 LIFETIME DEPENDENCIES)
704 //
705 //=================================================================================================
706 
707 //*************************************************************************************************
715 template< typename T // Type of the singleton (CRTP pattern)
716  , typename D1 // Type of the first lifetime dependency
717  , typename D2 // Type of the second lifetime dependency
718  , typename D3 // Type of the third lifetime dependency
719  , typename D4 // Type of the fourth lifetime dependency
720  , typename D5 // Type of the fifth lifetime dependency
721  , typename D6 // Type of the sixth lifetime dependency
722  , typename D7 > // Type of the eighth lifetime dependency
723 class Singleton<T,D1,D2,D3,D4,D5,D6,D7,NullType> : private NonCopyable
724 {
725  public:
726  //**Type definitions****************************************************************************
729 
731  typedef BLAZE_TYPELIST_7( D1, D2, D3, D4, D5, D6, D7 ) Dependencies;
732  //**********************************************************************************************
733 
734  protected:
735  //**Constructor*********************************************************************************
740  explicit Singleton()
741  : dependency1_( D1::instance() ) // Handle to the first lifetime dependency
742  , dependency2_( D2::instance() ) // Handle to the second lifetime dependency
743  , dependency3_( D3::instance() ) // Handle to the third lifetime dependency
744  , dependency4_( D4::instance() ) // Handle to the fourth lifetime dependency
745  , dependency5_( D5::instance() ) // Handle to the fifth lifetime dependency
746  , dependency6_( D6::instance() ) // Handle to the sixth lifetime dependency
747  , dependency7_( D7::instance() ) // Handle to the seventh lifetime dependency
748  {
750  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D1, typename D1::SingletonType );
751  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D2, typename D2::SingletonType );
752  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D3, typename D3::SingletonType );
753  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D4, typename D4::SingletonType );
754  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D5, typename D5::SingletonType );
755  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D6, typename D6::SingletonType );
756  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D7, typename D7::SingletonType );
764  }
765  //**********************************************************************************************
766 
767  //**Destructor**********************************************************************************
770  ~Singleton()
771  {}
772  //**********************************************************************************************
773 
774  public:
775  //**Instance function***************************************************************************
778  static boost::shared_ptr<T> instance()
779  {
780  boost::mutex::scoped_lock lock( instanceMutex_ );
781  static boost::shared_ptr<T> object( new T() );
782  return object;
783  }
785  //**********************************************************************************************
786 
787  private:
788  //**Member variables****************************************************************************
791  boost::shared_ptr<D1> dependency1_;
792  boost::shared_ptr<D2> dependency2_;
793  boost::shared_ptr<D3> dependency3_;
794  boost::shared_ptr<D4> dependency4_;
795  boost::shared_ptr<D5> dependency5_;
796  boost::shared_ptr<D6> dependency6_;
797  boost::shared_ptr<D7> dependency7_;
798 
799  //**********************************************************************************************
800 
801  protected:
802  //**Member variables****************************************************************************
805  static boost::mutex instanceMutex_;
806 
807  //**********************************************************************************************
808 };
810 //*************************************************************************************************
811 
812 
813 
814 
815 //=================================================================================================
816 //
817 // SINGLETON SPECIALIZATION (6 LIFETIME DEPENDENCIES)
818 //
819 //=================================================================================================
820 
821 //*************************************************************************************************
829 template< typename T // Type of the singleton (CRTP pattern)
830  , typename D1 // Type of the first lifetime dependency
831  , typename D2 // Type of the second lifetime dependency
832  , typename D3 // Type of the third lifetime dependency
833  , typename D4 // Type of the fourth lifetime dependency
834  , typename D5 // Type of the fifth lifetime dependency
835  , typename D6 > // Type of the eighth lifetime dependency
836 class Singleton<T,D1,D2,D3,D4,D5,D6,NullType,NullType> : private NonCopyable
837 {
838  public:
839  //**Type definitions****************************************************************************
841  typedef Singleton<T,D1,D2,D3,D4,D5,D6,NullType,NullType> SingletonType;
842 
844  typedef BLAZE_TYPELIST_6( D1, D2, D3, D4, D5, D6 ) Dependencies;
845  //**********************************************************************************************
846 
847  protected:
848  //**Constructor*********************************************************************************
853  explicit Singleton()
854  : dependency1_( D1::instance() ) // Handle to the first lifetime dependency
855  , dependency2_( D2::instance() ) // Handle to the second lifetime dependency
856  , dependency3_( D3::instance() ) // Handle to the third lifetime dependency
857  , dependency4_( D4::instance() ) // Handle to the fourth lifetime dependency
858  , dependency5_( D5::instance() ) // Handle to the fifth lifetime dependency
859  , dependency6_( D6::instance() ) // Handle to the sixth lifetime dependency
860  {
862  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D1, typename D1::SingletonType );
863  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D2, typename D2::SingletonType );
864  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D3, typename D3::SingletonType );
865  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D4, typename D4::SingletonType );
866  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D5, typename D5::SingletonType );
867  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D6, typename D6::SingletonType );
874  }
875  //**********************************************************************************************
876 
877  //**Destructor**********************************************************************************
880  ~Singleton()
881  {}
882  //**********************************************************************************************
883 
884  public:
885  //**Instance function***************************************************************************
888  static boost::shared_ptr<T> instance()
889  {
890  boost::mutex::scoped_lock lock( instanceMutex_ );
891  static boost::shared_ptr<T> object( new T() );
892  return object;
893  }
895  //**********************************************************************************************
896 
897  private:
898  //**Member variables****************************************************************************
901  boost::shared_ptr<D1> dependency1_;
902  boost::shared_ptr<D2> dependency2_;
903  boost::shared_ptr<D3> dependency3_;
904  boost::shared_ptr<D4> dependency4_;
905  boost::shared_ptr<D5> dependency5_;
906  boost::shared_ptr<D6> dependency6_;
907 
908  //**********************************************************************************************
909 
910  protected:
911  //**Member variables****************************************************************************
914  static boost::mutex instanceMutex_;
915 
916  //**********************************************************************************************
917 };
919 //*************************************************************************************************
920 
921 
922 
923 
924 //=================================================================================================
925 //
926 // SINGLETON SPECIALIZATION (5 LIFETIME DEPENDENCIES)
927 //
928 //=================================================================================================
929 
930 //*************************************************************************************************
938 template< typename T // Type of the singleton (CRTP pattern)
939  , typename D1 // Type of the first lifetime dependency
940  , typename D2 // Type of the second lifetime dependency
941  , typename D3 // Type of the third lifetime dependency
942  , typename D4 // Type of the fourth lifetime dependency
943  , typename D5 > // Type of the fifth lifetime dependency
944 class Singleton<T,D1,D2,D3,D4,D5,NullType,NullType,NullType> : private NonCopyable
945 {
946  public:
947  //**Type definitions****************************************************************************
949  typedef Singleton<T,D1,D2,D3,D4,D5,NullType,NullType,NullType> SingletonType;
950 
952  typedef BLAZE_TYPELIST_5( D1, D2, D3, D4, D5 ) Dependencies;
953  //**********************************************************************************************
954 
955  protected:
956  //**Constructor*********************************************************************************
961  explicit Singleton()
962  : dependency1_( D1::instance() ) // Handle to the first lifetime dependency
963  , dependency2_( D2::instance() ) // Handle to the second lifetime dependency
964  , dependency3_( D3::instance() ) // Handle to the third lifetime dependency
965  , dependency4_( D4::instance() ) // Handle to the fourth lifetime dependency
966  , dependency5_( D5::instance() ) // Handle to the fifth lifetime dependency
967  {
969  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D1, typename D1::SingletonType );
970  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D2, typename D2::SingletonType );
971  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D3, typename D3::SingletonType );
972  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D4, typename D4::SingletonType );
973  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D5, typename D5::SingletonType );
979  }
980  //**********************************************************************************************
981 
982  //**Destructor**********************************************************************************
985  ~Singleton()
986  {}
987  //**********************************************************************************************
988 
989  public:
990  //**Instance function***************************************************************************
993  static boost::shared_ptr<T> instance()
994  {
995  boost::mutex::scoped_lock lock( instanceMutex_ );
996  static boost::shared_ptr<T> object( new T() );
997  return object;
998  }
1000  //**********************************************************************************************
1001 
1002  private:
1003  //**Member variables****************************************************************************
1006  boost::shared_ptr<D1> dependency1_;
1007  boost::shared_ptr<D2> dependency2_;
1008  boost::shared_ptr<D3> dependency3_;
1009  boost::shared_ptr<D4> dependency4_;
1010  boost::shared_ptr<D5> dependency5_;
1011 
1012  //**********************************************************************************************
1013 
1014  protected:
1015  //**Member variables****************************************************************************
1018  static boost::mutex instanceMutex_;
1019 
1020  //**********************************************************************************************
1021 };
1023 //*************************************************************************************************
1024 
1025 
1026 
1027 
1028 //=================================================================================================
1029 //
1030 // SINGLETON SPECIALIZATION (4 LIFETIME DEPENDENCIES)
1031 //
1032 //=================================================================================================
1033 
1034 //*************************************************************************************************
1042 template< typename T // Type of the singleton (CRTP pattern)
1043  , typename D1 // Type of the first lifetime dependency
1044  , typename D2 // Type of the second lifetime dependency
1045  , typename D3 // Type of the third lifetime dependency
1046  , typename D4 > // Type of the fourth lifetime dependency
1047 class Singleton<T,D1,D2,D3,D4,NullType,NullType,NullType,NullType> : private NonCopyable
1048 {
1049  public:
1050  //**Type definitions****************************************************************************
1052  typedef Singleton<T,D1,D2,D3,D4,NullType,NullType,NullType,NullType> SingletonType;
1053 
1055  typedef BLAZE_TYPELIST_4( D1, D2, D3, D4 ) Dependencies;
1056  //**********************************************************************************************
1057 
1058  protected:
1059  //**Constructor*********************************************************************************
1064  explicit Singleton()
1065  : dependency1_( D1::instance() ) // Handle to the first lifetime dependency
1066  , dependency2_( D2::instance() ) // Handle to the second lifetime dependency
1067  , dependency3_( D3::instance() ) // Handle to the third lifetime dependency
1068  , dependency4_( D4::instance() ) // Handle to the fourth lifetime dependency
1069  {
1071  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D1, typename D1::SingletonType );
1072  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D2, typename D2::SingletonType );
1073  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D3, typename D3::SingletonType );
1074  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D4, typename D4::SingletonType );
1079  }
1080  //**********************************************************************************************
1081 
1082  //**Destructor**********************************************************************************
1085  ~Singleton()
1086  {}
1087  //**********************************************************************************************
1088 
1089  public:
1090  //**Instance function***************************************************************************
1093  static boost::shared_ptr<T> instance()
1094  {
1095  boost::mutex::scoped_lock lock( instanceMutex_ );
1096  static boost::shared_ptr<T> object( new T() );
1097  return object;
1098  }
1100  //**********************************************************************************************
1101 
1102  private:
1103  //**Member variables****************************************************************************
1106  boost::shared_ptr<D1> dependency1_;
1107  boost::shared_ptr<D2> dependency2_;
1108  boost::shared_ptr<D3> dependency3_;
1109  boost::shared_ptr<D4> dependency4_;
1110 
1111  //**********************************************************************************************
1112 
1113  protected:
1114  //**Member variables****************************************************************************
1117  static boost::mutex instanceMutex_;
1118 
1119  //**********************************************************************************************
1120 };
1122 //*************************************************************************************************
1123 
1124 
1125 
1126 
1127 //=================================================================================================
1128 //
1129 // SINGLETON SPECIALIZATION (3 LIFETIME DEPENDENCIES)
1130 //
1131 //=================================================================================================
1132 
1133 //*************************************************************************************************
1141 template< 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
1144  , typename D3 > // Type of the third lifetime dependency
1145 class Singleton<T,D1,D2,D3,NullType,NullType,NullType,NullType,NullType> : private NonCopyable
1146 {
1147  public:
1148  //**Type definitions****************************************************************************
1150  typedef Singleton<T,D1,D2,D3,NullType,NullType,NullType,NullType,NullType> SingletonType;
1151 
1153  typedef BLAZE_TYPELIST_3( D1, D2, D3 ) Dependencies;
1154  //**********************************************************************************************
1155 
1156  protected:
1157  //**Constructor*********************************************************************************
1162  explicit Singleton()
1163  : dependency1_( D1::instance() ) // Handle to the first lifetime dependency
1164  , dependency2_( D2::instance() ) // Handle to the second lifetime dependency
1165  , dependency3_( D3::instance() ) // Handle to the third 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 );
1170  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D3, typename D3::SingletonType );
1174  }
1175  //**********************************************************************************************
1176 
1177  //**Destructor**********************************************************************************
1180  ~Singleton()
1181  {}
1182  //**********************************************************************************************
1183 
1184  public:
1185  //**Instance function***************************************************************************
1188  static boost::shared_ptr<T> instance()
1189  {
1190  boost::mutex::scoped_lock lock( instanceMutex_ );
1191  static boost::shared_ptr<T> object( new T() );
1192  return object;
1193  }
1195  //**********************************************************************************************
1196 
1197  private:
1198  //**Member variables****************************************************************************
1201  boost::shared_ptr<D1> dependency1_;
1202  boost::shared_ptr<D2> dependency2_;
1203  boost::shared_ptr<D3> dependency3_;
1204 
1205  //**********************************************************************************************
1206 
1207  protected:
1208  //**Member variables****************************************************************************
1211  static boost::mutex instanceMutex_;
1212 
1213  //**********************************************************************************************
1214 };
1216 //*************************************************************************************************
1217 
1218 
1219 
1220 
1221 //=================================================================================================
1222 //
1223 // SINGLETON SPECIALIZATION (2 LIFETIME DEPENDENCIES)
1224 //
1225 //=================================================================================================
1226 
1227 //*************************************************************************************************
1235 template< typename T // Type of the singleton (CRTP pattern)
1236  , typename D1 // Type of the first lifetime dependency
1237  , typename D2 > // Type of the second lifetime dependency
1238 class Singleton<T,D1,D2,NullType,NullType,NullType,NullType,NullType,NullType> : private NonCopyable
1239 {
1240  public:
1241  //**Type definitions****************************************************************************
1243  typedef Singleton<T,D1,D2,NullType,NullType,NullType,NullType,NullType,NullType> SingletonType;
1244 
1246  typedef BLAZE_TYPELIST_2( D1, D2 ) Dependencies;
1247  //**********************************************************************************************
1248 
1249  protected:
1250  //**Constructor*********************************************************************************
1255  explicit Singleton()
1256  : dependency1_( D1::instance() ) // Handle to the first lifetime dependency
1257  , dependency2_( D2::instance() ) // Handle to the second lifetime dependency
1258  {
1260  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D1, typename D1::SingletonType );
1261  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D2, typename D2::SingletonType );
1264  }
1265  //**********************************************************************************************
1266 
1267  //**Destructor**********************************************************************************
1270  ~Singleton()
1271  {}
1272  //**********************************************************************************************
1273 
1274  public:
1275  //**Instance function***************************************************************************
1278  static boost::shared_ptr<T> instance()
1279  {
1280  boost::mutex::scoped_lock lock( instanceMutex_ );
1281  static boost::shared_ptr<T> object( new T() );
1282  return object;
1283  }
1285  //**********************************************************************************************
1286 
1287  private:
1288  //**Member variables****************************************************************************
1291  boost::shared_ptr<D1> dependency1_;
1292  boost::shared_ptr<D2> dependency2_;
1293 
1294  //**********************************************************************************************
1295 
1296  protected:
1297  //**Member variables****************************************************************************
1300  static boost::mutex instanceMutex_;
1301 
1302  //**********************************************************************************************
1303 };
1305 //*************************************************************************************************
1306 
1307 
1308 
1309 
1310 //=================================================================================================
1311 //
1312 // SINGLETON SPECIALIZATION (1 LIFETIME DEPENDENCY)
1313 //
1314 //=================================================================================================
1315 
1316 //*************************************************************************************************
1324 template< typename T // Type of the singleton (CRTP pattern)
1325  , typename D1 > // Type of the lifetime dependency
1326 class Singleton<T,D1,NullType,NullType,NullType,NullType,NullType,NullType,NullType> : private NonCopyable
1327 {
1328  public:
1329  //**Type definitions****************************************************************************
1331  typedef Singleton<T,D1,NullType,NullType,NullType,NullType,NullType,NullType,NullType> SingletonType;
1332 
1334  typedef BLAZE_TYPELIST_1( D1 ) Dependencies;
1335  //**********************************************************************************************
1336 
1337  protected:
1338  //**Constructor*********************************************************************************
1343  explicit Singleton()
1344  : dependency1_( D1::instance() ) // Handle to the lifetime dependency
1345  {
1347  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D1, typename D1::SingletonType );
1349  }
1350  //**********************************************************************************************
1351 
1352  //**Destructor**********************************************************************************
1355  ~Singleton()
1356  {}
1357  //**********************************************************************************************
1358 
1359  public:
1360  //**Instance function***************************************************************************
1363  static boost::shared_ptr<T> instance()
1364  {
1365  boost::mutex::scoped_lock lock( instanceMutex_ );
1366  static boost::shared_ptr<T> object( new T() );
1367  return object;
1368  }
1370  //**********************************************************************************************
1371 
1372  private:
1373  //**Member variables****************************************************************************
1376  boost::shared_ptr<D1> dependency1_;
1377 
1378  //**********************************************************************************************
1379 
1380  protected:
1381  //**Member variables****************************************************************************
1384  static boost::mutex instanceMutex_;
1385 
1386  //**********************************************************************************************
1387 };
1389 //*************************************************************************************************
1390 
1391 
1392 
1393 
1394 //=================================================================================================
1395 //
1396 // SINGLETON SPECIALIZATION (0 LIFETIME DEPENDENCIES)
1397 //
1398 //=================================================================================================
1399 
1400 //*************************************************************************************************
1408 template< typename T > // Type of the singleton (CRTP pattern)
1409 class Singleton<T,NullType,NullType,NullType,NullType,NullType,NullType,NullType,NullType> : private NonCopyable
1410 {
1411  public:
1412  //**Type definitions****************************************************************************
1414  typedef Singleton<T,NullType,NullType,NullType,NullType,NullType,NullType,NullType,NullType> SingletonType;
1415 
1417  typedef NullType Dependencies;
1418  //**********************************************************************************************
1419 
1420  protected:
1421  //**Constructor*********************************************************************************
1426  explicit Singleton()
1427  {
1429  }
1430  //**********************************************************************************************
1431 
1432  //**Destructor**********************************************************************************
1435  ~Singleton()
1436  {}
1437  //**********************************************************************************************
1438 
1439  public:
1440  //**Instance function***************************************************************************
1443  static boost::shared_ptr<T> instance()
1444  {
1445  boost::mutex::scoped_lock lock( instanceMutex_ );
1446  static boost::shared_ptr<T> object( new T() );
1447  return object;
1448  }
1450  //**********************************************************************************************
1451 
1452  protected:
1453  //**Member variables****************************************************************************
1456  static boost::mutex instanceMutex_;
1457 
1458  //**********************************************************************************************
1459 };
1461 //*************************************************************************************************
1462 
1463 
1464 
1465 
1466 //=================================================================================================
1467 //
1468 // DEFINITION AND INITIALIZATION OF THE STATIC MEMBER VARIABLES
1469 //
1470 //=================================================================================================
1471 
1472 template< typename T // Type of the singleton (CRTP pattern)
1473  , typename A // Type of the first lifetime dependency
1474  , typename B // Type of the second lifetime dependency
1475  , typename C // Type of the third lifetime dependency
1476  , typename D // Type of the fourth lifetime dependency
1477  , typename E // Type of the fifth lifetime dependency
1478  , typename F // Type of the sixth lifetime dependency
1479  , typename G // Type of the seventh lifetime dependency
1480  , typename H > // Type of the eighth lifetime dependency
1481 boost::mutex Singleton<T,A,B,C,D,E,F,G,H>::instanceMutex_;
1482 
1483 template< typename T // Type of the singleton (CRTP pattern)
1484  , typename A // Type of the first lifetime dependency
1485  , typename B // Type of the second lifetime dependency
1486  , typename C // Type of the third lifetime dependency
1487  , typename D // Type of the fourth lifetime dependency
1488  , typename E // Type of the fifth lifetime dependency
1489  , typename F // Type of the sixth lifetime dependency
1490  , typename G > // Type of the seventh lifetime dependency
1491 boost::mutex Singleton<T,A,B,C,D,E,F,G,NullType>::instanceMutex_;
1492 
1493 template< typename T // Type of the singleton (CRTP pattern)
1494  , typename A // Type of the first lifetime dependency
1495  , typename B // Type of the second lifetime dependency
1496  , typename C // Type of the third lifetime dependency
1497  , typename D // Type of the fourth lifetime dependency
1498  , typename E // Type of the fifth lifetime dependency
1499  , typename F > // Type of the sixth lifetime dependency
1500 boost::mutex Singleton<T,A,B,C,D,E,F,NullType,NullType>::instanceMutex_;
1501 
1502 template< typename T // Type of the singleton (CRTP pattern)
1503  , typename A // Type of the first lifetime dependency
1504  , typename B // Type of the second lifetime dependency
1505  , typename C // Type of the third lifetime dependency
1506  , typename D // Type of the fourth lifetime dependency
1507  , typename E > // Type of the fifth lifetime dependency
1508 boost::mutex Singleton<T,A,B,C,D,E,NullType,NullType,NullType>::instanceMutex_;
1509 
1510 template< typename T // Type of the singleton (CRTP pattern)
1511  , typename A // Type of the first lifetime dependency
1512  , typename B // Type of the second lifetime dependency
1513  , typename C // Type of the third lifetime dependency
1514  , typename D > // Type of the fourth lifetime dependency
1515 boost::mutex Singleton<T,A,B,C,D,NullType,NullType,NullType,NullType>::instanceMutex_;
1516 
1517 template< typename T // Type of the singleton (CRTP pattern)
1518  , typename A // Type of the first lifetime dependency
1519  , typename B // Type of the second lifetime dependency
1520  , typename C > // Type of the third lifetime dependency
1521 boost::mutex Singleton<T,A,B,C,NullType,NullType,NullType,NullType,NullType>::instanceMutex_;
1522 
1523 template< typename T // Type of the singleton (CRTP pattern)
1524  , typename A // Type of the first lifetime dependency
1525  , typename B > // Type of the second lifetime dependency
1526 boost::mutex Singleton<T,A,B,NullType,NullType,NullType,NullType,NullType,NullType>::instanceMutex_;
1527 
1528 template< typename T // Type of the singleton (CRTP pattern)
1529  , typename A > // Type of the first lifetime dependency
1530 boost::mutex Singleton<T,A,NullType,NullType,NullType,NullType,NullType,NullType,NullType>::instanceMutex_;
1531 
1532 template< typename T > // Type of the singleton (CRTP pattern)
1533 boost::mutex Singleton<T,NullType,NullType,NullType,NullType,NullType,NullType,NullType,NullType>::instanceMutex_;
1534 
1535 } // namespace blaze
1536 
1537 #endif
boost::shared_ptr< D1 > dependency1_
Handle to the first lifetime dependency.
Definition: Singleton.h:677
#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:185
boost::shared_ptr< D3 > dependency3_
Handle to the third lifetime dependency.
Definition: Singleton.h:679
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:248
boost::shared_ptr< D2 > dependency2_
Handle to the second lifetime dependency.
Definition: Singleton.h:678
Base class for non-copyable class instances.
static boost::mutex instanceMutex_
Synchronization mutex for access to the singleton.
Definition: Singleton.h:692
typedef BLAZE_TYPELIST_8(D1, D2, D3, D4, D5, D6, D7, D8) Dependencies
Type list of all lifetime dependencies.
boost::shared_ptr< D4 > dependency4_
Handle to the fourth lifetime dependency.
Definition: Singleton.h:680
~Singleton()
Destructor for the Singleton class.
Definition: Singleton.h:656
#define BLAZE_TYPELIST_1(T1)
Type list generation macro.This macro creates a type list only consisting of the type T1...
Definition: TypeList.h:143
#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:269
#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:78
#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:164
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.
boost::shared_ptr< D5 > dependency5_
Handle to the fifth lifetime dependency.
Definition: Singleton.h:681
Calculating the length of a type list.The Length class can be used to obtain the length of a type lis...
Definition: TypeList.h:363
boost::shared_ptr< D7 > dependency7_
Handle to the seventh lifetime dependency.
Definition: Singleton.h:683
Base class for all lifetime managed singletons.The Singleton class represents the base class for all ...
Definition: Singleton.h:606
boost::shared_ptr< D6 > dependency6_
Handle to the sixth lifetime dependency.
Definition: Singleton.h:682
#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:227
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:611
boost::shared_ptr< D8 > dependency8_
Handle to the eighth lifetime dependency.
Definition: Singleton.h:684
#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:206
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:424
Singleton()
Constructor for the Singleton class.
Definition: Singleton.h:623
Header file for compile time constraints.