All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Singleton.h
Go to the documentation of this file.
1 //=================================================================================================
20 // //=================================================================================================
21 
22 #ifndef _BLAZE_UTIL_SINGLETON_SINGLETON_H_
23 #define _BLAZE_UTIL_SINGLETON_SINGLETON_H_
24 
25 
26 //*************************************************************************************************
27 // Includes
28 //*************************************************************************************************
29 
30 #include <boost/shared_ptr.hpp>
31 #include <boost/thread/mutex.hpp>
33 #include <blaze/util/NonCopyable.h>
34 #include <blaze/util/NullType.h>
35 #include <blaze/util/Suffix.h>
36 #include <blaze/util/TypeList.h>
37 
38 
39 namespace blaze {
40 
41 //=================================================================================================
42 //
43 // ::blaze NAMESPACE FORWARD DECLARATIONS
44 //
45 //=================================================================================================
46 
47 template< typename > class Dependency;
48 template< typename T, typename TL, bool C > struct HasCyclicDependency;
49 
50 
51 
52 
53 //=================================================================================================
54 //
55 // CLASS HASCYCLICDEPENDENCYHELPER
56 //
57 //=================================================================================================
58 
59 //*************************************************************************************************
67 template< typename TL // Type list of checked lifetime dependencies
68  , typename D // Type list of lifetime dependencies to check
69  , size_t N = Length<D>::value > // Length of the dependency type list
70 struct HasCyclicDependencyHelper;
72 //*************************************************************************************************
73 
74 
75 //*************************************************************************************************
83 template< typename TL // Type list of checked lifetime dependencies
84  , size_t N > // Length of the dependency type list
85 struct HasCyclicDependencyHelper<TL,NullType,N>
86 {
87  enum { value = 0 };
88 };
90 //*************************************************************************************************
91 
92 
93 //*************************************************************************************************
101 template< typename TL // Type list of checked lifetime dependencies
102  , typename D > // Type list of lifetime dependencies to check
103 struct HasCyclicDependencyHelper<TL,D,1>
104 {
105  typedef typename TypeAt<D,0>::Result D1;
106 
107  enum { value = HasCyclicDependency<D1,TL,Contains<TL,D1>::value>::value };
108 };
110 //*************************************************************************************************
111 
112 
113 //*************************************************************************************************
121 template< typename TL // Type list of checked lifetime dependencies
122  , typename D > // Type list of lifetime dependencies to check
123 struct HasCyclicDependencyHelper<TL,D,2>
124 {
125  typedef typename TypeAt<D,0>::Result D1;
126  typedef typename TypeAt<D,1>::Result D2;
127 
128  enum { value = HasCyclicDependency<D1,TL,Contains<TL,D1>::value>::value ||
129  HasCyclicDependency<D2,TL,Contains<TL,D2>::value>::value };
130 };
132 //*************************************************************************************************
133 
134 
135 //*************************************************************************************************
143 template< typename TL // Type list of checked lifetime dependencies
144  , typename D > // Type list of lifetime dependencies to check
145 struct HasCyclicDependencyHelper<TL,D,3>
146 {
147  typedef typename TypeAt<D,0>::Result D1;
148  typedef typename TypeAt<D,1>::Result D2;
149  typedef typename TypeAt<D,2>::Result D3;
150 
151  enum { value = HasCyclicDependency<D1,TL,Contains<TL,D1>::value>::value ||
152  HasCyclicDependency<D2,TL,Contains<TL,D2>::value>::value ||
153  HasCyclicDependency<D3,TL,Contains<TL,D3>::value>::value };
154 };
156 //*************************************************************************************************
157 
158 
159 //*************************************************************************************************
167 template< typename TL // Type list of checked lifetime dependencies
168  , typename D > // Type list of lifetime dependencies to check
169 struct HasCyclicDependencyHelper<TL,D,4>
170 {
171  typedef typename TypeAt<D,0>::Result D1;
172  typedef typename TypeAt<D,1>::Result D2;
173  typedef typename TypeAt<D,2>::Result D3;
174  typedef typename TypeAt<D,3>::Result D4;
175 
176  enum { value = HasCyclicDependency<D1,TL,Contains<TL,D1>::value>::value ||
177  HasCyclicDependency<D2,TL,Contains<TL,D2>::value>::value ||
178  HasCyclicDependency<D3,TL,Contains<TL,D3>::value>::value ||
179  HasCyclicDependency<D4,TL,Contains<TL,D4>::value>::value };
180 };
182 //*************************************************************************************************
183 
184 
185 //*************************************************************************************************
193 template< typename TL // Type list of checked lifetime dependencies
194  , typename D > // Type list of lifetime dependencies to check
195 struct HasCyclicDependencyHelper<TL,D,5>
196 {
197  typedef typename TypeAt<D,0>::Result D1;
198  typedef typename TypeAt<D,1>::Result D2;
199  typedef typename TypeAt<D,2>::Result D3;
200  typedef typename TypeAt<D,3>::Result D4;
201  typedef typename TypeAt<D,4>::Result D5;
202 
203  enum { value = HasCyclicDependency<D1,TL,Contains<TL,D1>::value>::value ||
204  HasCyclicDependency<D2,TL,Contains<TL,D2>::value>::value ||
205  HasCyclicDependency<D3,TL,Contains<TL,D3>::value>::value ||
206  HasCyclicDependency<D4,TL,Contains<TL,D4>::value>::value ||
207  HasCyclicDependency<D5,TL,Contains<TL,D5>::value>::value };
208 };
210 //*************************************************************************************************
211 
212 
213 //*************************************************************************************************
221 template< typename TL // Type list of checked lifetime dependencies
222  , typename D > // Type list of lifetime dependencies to check
223 struct HasCyclicDependencyHelper<TL,D,6>
224 {
225  typedef typename TypeAt<D,0>::Result D1;
226  typedef typename TypeAt<D,1>::Result D2;
227  typedef typename TypeAt<D,2>::Result D3;
228  typedef typename TypeAt<D,3>::Result D4;
229  typedef typename TypeAt<D,4>::Result D5;
230  typedef typename TypeAt<D,5>::Result D6;
231 
232  enum { value = HasCyclicDependency<D1,TL,Contains<TL,D1>::value>::value ||
233  HasCyclicDependency<D2,TL,Contains<TL,D2>::value>::value ||
234  HasCyclicDependency<D3,TL,Contains<TL,D3>::value>::value ||
235  HasCyclicDependency<D4,TL,Contains<TL,D4>::value>::value ||
236  HasCyclicDependency<D5,TL,Contains<TL,D5>::value>::value ||
237  HasCyclicDependency<D6,TL,Contains<TL,D6>::value>::value };
238 };
240 //*************************************************************************************************
241 
242 
243 //*************************************************************************************************
251 template< typename TL // Type list of checked lifetime dependencies
252  , typename D > // Type list of lifetime dependencies to check
253 struct HasCyclicDependencyHelper<TL,D,7>
254 {
255  typedef typename TypeAt<D,0>::Result D1;
256  typedef typename TypeAt<D,1>::Result D2;
257  typedef typename TypeAt<D,2>::Result D3;
258  typedef typename TypeAt<D,3>::Result D4;
259  typedef typename TypeAt<D,4>::Result D5;
260  typedef typename TypeAt<D,5>::Result D6;
261  typedef typename TypeAt<D,6>::Result D7;
262 
263  enum { value = HasCyclicDependency<D1,TL,Contains<TL,D1>::value>::value ||
264  HasCyclicDependency<D2,TL,Contains<TL,D2>::value>::value ||
265  HasCyclicDependency<D3,TL,Contains<TL,D3>::value>::value ||
266  HasCyclicDependency<D4,TL,Contains<TL,D4>::value>::value ||
267  HasCyclicDependency<D5,TL,Contains<TL,D5>::value>::value ||
268  HasCyclicDependency<D6,TL,Contains<TL,D6>::value>::value ||
269  HasCyclicDependency<D7,TL,Contains<TL,D7>::value>::value };
270 };
272 //*************************************************************************************************
273 
274 
275 //*************************************************************************************************
283 template< typename TL // Type list of checked lifetime dependencies
284  , typename D > // Type list of lifetime dependencies to check
285 struct HasCyclicDependencyHelper<TL,D,8>
286 {
287  typedef typename TypeAt<D,0>::Result D1;
288  typedef typename TypeAt<D,1>::Result D2;
289  typedef typename TypeAt<D,2>::Result D3;
290  typedef typename TypeAt<D,3>::Result D4;
291  typedef typename TypeAt<D,4>::Result D5;
292  typedef typename TypeAt<D,5>::Result D6;
293  typedef typename TypeAt<D,6>::Result D7;
294  typedef typename TypeAt<D,7>::Result D8;
295 
296  enum { value = HasCyclicDependency<D1,TL,Contains<TL,D1>::value>::value ||
297  HasCyclicDependency<D2,TL,Contains<TL,D2>::value>::value ||
298  HasCyclicDependency<D3,TL,Contains<TL,D3>::value>::value ||
299  HasCyclicDependency<D4,TL,Contains<TL,D4>::value>::value ||
300  HasCyclicDependency<D5,TL,Contains<TL,D5>::value>::value ||
301  HasCyclicDependency<D6,TL,Contains<TL,D6>::value>::value ||
302  HasCyclicDependency<D7,TL,Contains<TL,D7>::value>::value ||
303  HasCyclicDependency<D8,TL,Contains<TL,D8>::value>::value };
304 };
306 //*************************************************************************************************
307 
308 
309 
310 
311 //=================================================================================================
312 //
313 // CLASS HASCYCLICDEPENDENCY
314 //
315 //=================================================================================================
316 
317 //*************************************************************************************************
326 template< typename T // The type to be checked for cyclic lifetime dependencies
327  , typename TL // Type list of checked lifetime dependencies
328  , bool C=Contains<TL,T>::value > // Flag to indicate whether T is contained in TL
329 struct HasCyclicDependency
330 {
331  typedef typename Append<TL,T>::Result ETL;
332  enum { value = HasCyclicDependencyHelper<ETL,typename T::Dependencies>::value };
333 };
335 //*************************************************************************************************
336 
337 
338 //*************************************************************************************************
347 template< typename T // The type to be checked for cyclic lifetime dependencies
348  , typename TL > // Type list of checked lifetime dependencies
349 struct HasCyclicDependency<T,TL,true>
350 {
351  enum { value = 1 };
352 };
354 //*************************************************************************************************
355 
356 
357 
358 
359 //=================================================================================================
360 //
361 // CLASS CYCLIC_LIFETIME_DEPENDENCY_TEST
362 //
363 //=================================================================================================
364 
365 //*************************************************************************************************
376 template< int > struct CYCLIC_LIFETIME_DEPENDENCY_TEST {};
378 //*************************************************************************************************
379 
380 
381 
382 
383 //=================================================================================================
384 //
385 // DETECT_CYCLIC_LIFETIME_DEPENDENCY CONSTRAINT
386 //
387 //=================================================================================================
388 
389 //*************************************************************************************************
399 template< bool > struct CYCLIC_LIFETIME_DEPENDENCY_DETECTED;
400 template<> struct CYCLIC_LIFETIME_DEPENDENCY_DETECTED<false> { enum { value = 1 }; };
402 //*************************************************************************************************
403 
404 
405 //*************************************************************************************************
411 #define BLAZE_DETECT_CYCLIC_LIFETIME_DEPENDENCY(T) \
412  typedef \
413  blaze::CYCLIC_LIFETIME_DEPENDENCY_TEST< \
414  blaze::CYCLIC_LIFETIME_DEPENDENCY_DETECTED< blaze::HasCyclicDependency<T,blaze::NullType>::value >::value > \
415  BLAZE_JOIN( DETECT_CYCLIC_LIFETIME_DEPENDENCY_TYPEDEF, __LINE__ )
416 //*************************************************************************************************
417 
418 
419 
420 
421 //=================================================================================================
422 //
423 // BEFRIEND_SINGLETON MACRO
424 //
425 //=================================================================================================
426 
427 //*************************************************************************************************
434 #define BLAZE_BEFRIEND_SINGLETON \
435  template< typename, typename, typename, typename, typename, typename, typename, typename, typename > friend class blaze::Singleton; \
436  template< typename, typename, bool > friend struct blaze::HasCyclicDependency; \
437  template< typename > friend class blaze::Dependency;
438 //*************************************************************************************************
439 
440 
441 
442 
443 //=================================================================================================
444 //
445 // CLASS SINGLETON
446 //
447 //=================================================================================================
448 
449 //*************************************************************************************************
584 template< typename T // Type of the singleton (CRTP pattern)
585  , typename D1 = NullType // Type of the first lifetime dependency
586  , typename D2 = NullType // Type of the second lifetime dependency
587  , typename D3 = NullType // Type of the third lifetime dependency
588  , typename D4 = NullType // Type of the fourth lifetime dependency
589  , typename D5 = NullType // Type of the fifth lifetime dependency
590  , typename D6 = NullType // Type of the sixth lifetime dependency
591  , typename D7 = NullType // Type of the seventh lifetime dependency
592  , typename D8 = NullType > // Type of the eighth lifetime dependency
593 class Singleton : private NonCopyable
594 {
595  public:
596  //**Type definitions****************************************************************************
599 
601  typedef BLAZE_TYPELIST_8( D1, D2, D3, D4, D5, D6, D7, D8 ) Dependencies;
602  //**********************************************************************************************
603 
604  protected:
605  //**Constructor*********************************************************************************
610  explicit Singleton()
611  : dependency1_( D1::instance() ) // Handle to the first lifetime dependency
612  , dependency2_( D2::instance() ) // Handle to the second lifetime dependency
613  , dependency3_( D3::instance() ) // Handle to the third lifetime dependency
614  , dependency4_( D4::instance() ) // Handle to the fourth lifetime dependency
615  , dependency5_( D5::instance() ) // Handle to the fifth lifetime dependency
616  , dependency6_( D6::instance() ) // Handle to the sixth lifetime dependency
617  , dependency7_( D7::instance() ) // Handle to the seventh lifetime dependency
618  , dependency8_( D8::instance() ) // Handle to the eighth lifetime dependency
619  {
621  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D1, typename D1::SingletonType );
622  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D2, typename D2::SingletonType );
623  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D3, typename D3::SingletonType );
624  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D4, typename D4::SingletonType );
625  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D5, typename D5::SingletonType );
626  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D6, typename D6::SingletonType );
627  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D7, typename D7::SingletonType );
628  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D8, typename D8::SingletonType );
637  }
638  //**********************************************************************************************
639 
640  //**Destructor**********************************************************************************
644  {}
645  //**********************************************************************************************
646 
647  public:
648  //**Instance function***************************************************************************
651  static boost::shared_ptr<T> instance()
652  {
653  boost::mutex::scoped_lock lock( instanceMutex_ );
654  static boost::shared_ptr<T> object( new T() );
655  return object;
656  }
658  //**********************************************************************************************
659 
660  private:
661  //**Member variables****************************************************************************
664  boost::shared_ptr<D1> dependency1_;
665  boost::shared_ptr<D2> dependency2_;
666  boost::shared_ptr<D3> dependency3_;
667  boost::shared_ptr<D4> dependency4_;
668  boost::shared_ptr<D5> dependency5_;
669  boost::shared_ptr<D6> dependency6_;
670  boost::shared_ptr<D7> dependency7_;
671  boost::shared_ptr<D8> dependency8_;
672 
673  //**********************************************************************************************
674 
675  protected:
676  //**Member variables****************************************************************************
679  static boost::mutex instanceMutex_;
680 
681  //**********************************************************************************************
682 };
683 //*************************************************************************************************
684 
685 
686 
687 
688 //=================================================================================================
689 //
690 // SINGLETON SPECIALIZATION (7 LIFETIME DEPENDENCIES)
691 //
692 //=================================================================================================
693 
694 //*************************************************************************************************
702 template< typename T // Type of the singleton (CRTP pattern)
703  , typename D1 // Type of the first lifetime dependency
704  , typename D2 // Type of the second lifetime dependency
705  , typename D3 // Type of the third lifetime dependency
706  , typename D4 // Type of the fourth lifetime dependency
707  , typename D5 // Type of the fifth lifetime dependency
708  , typename D6 // Type of the sixth lifetime dependency
709  , typename D7 > // Type of the eighth lifetime dependency
710 class Singleton<T,D1,D2,D3,D4,D5,D6,D7,NullType> : private NonCopyable
711 {
712  public:
713  //**Type definitions****************************************************************************
716 
718  typedef BLAZE_TYPELIST_7( D1, D2, D3, D4, D5, D6, D7 ) Dependencies;
719  //**********************************************************************************************
720 
721  protected:
722  //**Constructor*********************************************************************************
727  explicit Singleton()
728  : dependency1_( D1::instance() ) // Handle to the first lifetime dependency
729  , dependency2_( D2::instance() ) // Handle to the second lifetime dependency
730  , dependency3_( D3::instance() ) // Handle to the third lifetime dependency
731  , dependency4_( D4::instance() ) // Handle to the fourth lifetime dependency
732  , dependency5_( D5::instance() ) // Handle to the fifth lifetime dependency
733  , dependency6_( D6::instance() ) // Handle to the sixth lifetime dependency
734  , dependency7_( D7::instance() ) // Handle to the seventh lifetime dependency
735  {
737  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D1, typename D1::SingletonType );
738  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D2, typename D2::SingletonType );
739  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D3, typename D3::SingletonType );
740  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D4, typename D4::SingletonType );
741  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D5, typename D5::SingletonType );
742  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D6, typename D6::SingletonType );
743  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D7, typename D7::SingletonType );
751  }
752  //**********************************************************************************************
753 
754  //**Destructor**********************************************************************************
757  ~Singleton()
758  {}
759  //**********************************************************************************************
760 
761  public:
762  //**Instance function***************************************************************************
765  static boost::shared_ptr<T> instance()
766  {
767  boost::mutex::scoped_lock lock( instanceMutex_ );
768  static boost::shared_ptr<T> object( new T() );
769  return object;
770  }
772  //**********************************************************************************************
773 
774  private:
775  //**Member variables****************************************************************************
778  boost::shared_ptr<D1> dependency1_;
779  boost::shared_ptr<D2> dependency2_;
780  boost::shared_ptr<D3> dependency3_;
781  boost::shared_ptr<D4> dependency4_;
782  boost::shared_ptr<D5> dependency5_;
783  boost::shared_ptr<D6> dependency6_;
784  boost::shared_ptr<D7> dependency7_;
785 
786  //**********************************************************************************************
787 
788  protected:
789  //**Member variables****************************************************************************
792  static boost::mutex instanceMutex_;
793 
794  //**********************************************************************************************
795 };
797 //*************************************************************************************************
798 
799 
800 
801 
802 //=================================================================================================
803 //
804 // SINGLETON SPECIALIZATION (6 LIFETIME DEPENDENCIES)
805 //
806 //=================================================================================================
807 
808 //*************************************************************************************************
816 template< typename T // Type of the singleton (CRTP pattern)
817  , typename D1 // Type of the first lifetime dependency
818  , typename D2 // Type of the second lifetime dependency
819  , typename D3 // Type of the third lifetime dependency
820  , typename D4 // Type of the fourth lifetime dependency
821  , typename D5 // Type of the fifth lifetime dependency
822  , typename D6 > // Type of the eighth lifetime dependency
823 class Singleton<T,D1,D2,D3,D4,D5,D6,NullType,NullType> : private NonCopyable
824 {
825  public:
826  //**Type definitions****************************************************************************
828  typedef Singleton<T,D1,D2,D3,D4,D5,D6,NullType,NullType> SingletonType;
829 
831  typedef BLAZE_TYPELIST_6( D1, D2, D3, D4, D5, D6 ) Dependencies;
832  //**********************************************************************************************
833 
834  protected:
835  //**Constructor*********************************************************************************
840  explicit Singleton()
841  : dependency1_( D1::instance() ) // Handle to the first lifetime dependency
842  , dependency2_( D2::instance() ) // Handle to the second lifetime dependency
843  , dependency3_( D3::instance() ) // Handle to the third lifetime dependency
844  , dependency4_( D4::instance() ) // Handle to the fourth lifetime dependency
845  , dependency5_( D5::instance() ) // Handle to the fifth lifetime dependency
846  , dependency6_( D6::instance() ) // Handle to the sixth lifetime dependency
847  {
849  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D1, typename D1::SingletonType );
850  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D2, typename D2::SingletonType );
851  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D3, typename D3::SingletonType );
852  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D4, typename D4::SingletonType );
853  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D5, typename D5::SingletonType );
854  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D6, typename D6::SingletonType );
861  }
862  //**********************************************************************************************
863 
864  //**Destructor**********************************************************************************
867  ~Singleton()
868  {}
869  //**********************************************************************************************
870 
871  public:
872  //**Instance function***************************************************************************
875  static boost::shared_ptr<T> instance()
876  {
877  boost::mutex::scoped_lock lock( instanceMutex_ );
878  static boost::shared_ptr<T> object( new T() );
879  return object;
880  }
882  //**********************************************************************************************
883 
884  private:
885  //**Member variables****************************************************************************
888  boost::shared_ptr<D1> dependency1_;
889  boost::shared_ptr<D2> dependency2_;
890  boost::shared_ptr<D3> dependency3_;
891  boost::shared_ptr<D4> dependency4_;
892  boost::shared_ptr<D5> dependency5_;
893  boost::shared_ptr<D6> dependency6_;
894 
895  //**********************************************************************************************
896 
897  protected:
898  //**Member variables****************************************************************************
901  static boost::mutex instanceMutex_;
902 
903  //**********************************************************************************************
904 };
906 //*************************************************************************************************
907 
908 
909 
910 
911 //=================================================================================================
912 //
913 // SINGLETON SPECIALIZATION (5 LIFETIME DEPENDENCIES)
914 //
915 //=================================================================================================
916 
917 //*************************************************************************************************
925 template< typename T // Type of the singleton (CRTP pattern)
926  , typename D1 // Type of the first lifetime dependency
927  , typename D2 // Type of the second lifetime dependency
928  , typename D3 // Type of the third lifetime dependency
929  , typename D4 // Type of the fourth lifetime dependency
930  , typename D5 > // Type of the fifth lifetime dependency
931 class Singleton<T,D1,D2,D3,D4,D5,NullType,NullType,NullType> : private NonCopyable
932 {
933  public:
934  //**Type definitions****************************************************************************
936  typedef Singleton<T,D1,D2,D3,D4,D5,NullType,NullType,NullType> SingletonType;
937 
939  typedef BLAZE_TYPELIST_5( D1, D2, D3, D4, D5 ) Dependencies;
940  //**********************************************************************************************
941 
942  protected:
943  //**Constructor*********************************************************************************
948  explicit Singleton()
949  : dependency1_( D1::instance() ) // Handle to the first lifetime dependency
950  , dependency2_( D2::instance() ) // Handle to the second lifetime dependency
951  , dependency3_( D3::instance() ) // Handle to the third lifetime dependency
952  , dependency4_( D4::instance() ) // Handle to the fourth lifetime dependency
953  , dependency5_( D5::instance() ) // Handle to the fifth lifetime dependency
954  {
956  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D1, typename D1::SingletonType );
957  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D2, typename D2::SingletonType );
958  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D3, typename D3::SingletonType );
959  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D4, typename D4::SingletonType );
960  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D5, typename D5::SingletonType );
966  }
967  //**********************************************************************************************
968 
969  //**Destructor**********************************************************************************
972  ~Singleton()
973  {}
974  //**********************************************************************************************
975 
976  public:
977  //**Instance function***************************************************************************
980  static boost::shared_ptr<T> instance()
981  {
982  boost::mutex::scoped_lock lock( instanceMutex_ );
983  static boost::shared_ptr<T> object( new T() );
984  return object;
985  }
987  //**********************************************************************************************
988 
989  private:
990  //**Member variables****************************************************************************
993  boost::shared_ptr<D1> dependency1_;
994  boost::shared_ptr<D2> dependency2_;
995  boost::shared_ptr<D3> dependency3_;
996  boost::shared_ptr<D4> dependency4_;
997  boost::shared_ptr<D5> dependency5_;
998 
999  //**********************************************************************************************
1000 
1001  protected:
1002  //**Member variables****************************************************************************
1005  static boost::mutex instanceMutex_;
1006 
1007  //**********************************************************************************************
1008 };
1010 //*************************************************************************************************
1011 
1012 
1013 
1014 
1015 //=================================================================================================
1016 //
1017 // SINGLETON SPECIALIZATION (4 LIFETIME DEPENDENCIES)
1018 //
1019 //=================================================================================================
1020 
1021 //*************************************************************************************************
1029 template< typename T // Type of the singleton (CRTP pattern)
1030  , typename D1 // Type of the first lifetime dependency
1031  , typename D2 // Type of the second lifetime dependency
1032  , typename D3 // Type of the third lifetime dependency
1033  , typename D4 > // Type of the fourth lifetime dependency
1034 class Singleton<T,D1,D2,D3,D4,NullType,NullType,NullType,NullType> : private NonCopyable
1035 {
1036  public:
1037  //**Type definitions****************************************************************************
1039  typedef Singleton<T,D1,D2,D3,D4,NullType,NullType,NullType,NullType> SingletonType;
1040 
1042  typedef BLAZE_TYPELIST_4( D1, D2, D3, D4 ) Dependencies;
1043  //**********************************************************************************************
1044 
1045  protected:
1046  //**Constructor*********************************************************************************
1051  explicit Singleton()
1052  : dependency1_( D1::instance() ) // Handle to the first lifetime dependency
1053  , dependency2_( D2::instance() ) // Handle to the second lifetime dependency
1054  , dependency3_( D3::instance() ) // Handle to the third lifetime dependency
1055  , dependency4_( D4::instance() ) // Handle to the fourth lifetime dependency
1056  {
1058  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D1, typename D1::SingletonType );
1059  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D2, typename D2::SingletonType );
1060  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D3, typename D3::SingletonType );
1061  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D4, typename D4::SingletonType );
1066  }
1067  //**********************************************************************************************
1068 
1069  //**Destructor**********************************************************************************
1072  ~Singleton()
1073  {}
1074  //**********************************************************************************************
1075 
1076  public:
1077  //**Instance function***************************************************************************
1080  static boost::shared_ptr<T> instance()
1081  {
1082  boost::mutex::scoped_lock lock( instanceMutex_ );
1083  static boost::shared_ptr<T> object( new T() );
1084  return object;
1085  }
1087  //**********************************************************************************************
1088 
1089  private:
1090  //**Member variables****************************************************************************
1093  boost::shared_ptr<D1> dependency1_;
1094  boost::shared_ptr<D2> dependency2_;
1095  boost::shared_ptr<D3> dependency3_;
1096  boost::shared_ptr<D4> dependency4_;
1097 
1098  //**********************************************************************************************
1099 
1100  protected:
1101  //**Member variables****************************************************************************
1104  static boost::mutex instanceMutex_;
1105 
1106  //**********************************************************************************************
1107 };
1109 //*************************************************************************************************
1110 
1111 
1112 
1113 
1114 //=================================================================================================
1115 //
1116 // SINGLETON SPECIALIZATION (3 LIFETIME DEPENDENCIES)
1117 //
1118 //=================================================================================================
1119 
1120 //*************************************************************************************************
1128 template< typename T // Type of the singleton (CRTP pattern)
1129  , typename D1 // Type of the first lifetime dependency
1130  , typename D2 // Type of the second lifetime dependency
1131  , typename D3 > // Type of the third lifetime dependency
1132 class Singleton<T,D1,D2,D3,NullType,NullType,NullType,NullType,NullType> : private NonCopyable
1133 {
1134  public:
1135  //**Type definitions****************************************************************************
1137  typedef Singleton<T,D1,D2,D3,NullType,NullType,NullType,NullType,NullType> SingletonType;
1138 
1140  typedef BLAZE_TYPELIST_3( D1, D2, D3 ) Dependencies;
1141  //**********************************************************************************************
1142 
1143  protected:
1144  //**Constructor*********************************************************************************
1149  explicit Singleton()
1150  : dependency1_( D1::instance() ) // Handle to the first lifetime dependency
1151  , dependency2_( D2::instance() ) // Handle to the second lifetime dependency
1152  , dependency3_( D3::instance() ) // Handle to the third lifetime dependency
1153  {
1155  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D1, typename D1::SingletonType );
1156  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D2, typename D2::SingletonType );
1157  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D3, typename D3::SingletonType );
1161  }
1162  //**********************************************************************************************
1163 
1164  //**Destructor**********************************************************************************
1167  ~Singleton()
1168  {}
1169  //**********************************************************************************************
1170 
1171  public:
1172  //**Instance function***************************************************************************
1175  static boost::shared_ptr<T> instance()
1176  {
1177  boost::mutex::scoped_lock lock( instanceMutex_ );
1178  static boost::shared_ptr<T> object( new T() );
1179  return object;
1180  }
1182  //**********************************************************************************************
1183 
1184  private:
1185  //**Member variables****************************************************************************
1188  boost::shared_ptr<D1> dependency1_;
1189  boost::shared_ptr<D2> dependency2_;
1190  boost::shared_ptr<D3> dependency3_;
1191 
1192  //**********************************************************************************************
1193 
1194  protected:
1195  //**Member variables****************************************************************************
1198  static boost::mutex instanceMutex_;
1199 
1200  //**********************************************************************************************
1201 };
1203 //*************************************************************************************************
1204 
1205 
1206 
1207 
1208 //=================================================================================================
1209 //
1210 // SINGLETON SPECIALIZATION (2 LIFETIME DEPENDENCIES)
1211 //
1212 //=================================================================================================
1213 
1214 //*************************************************************************************************
1222 template< typename T // Type of the singleton (CRTP pattern)
1223  , typename D1 // Type of the first lifetime dependency
1224  , typename D2 > // Type of the second lifetime dependency
1225 class Singleton<T,D1,D2,NullType,NullType,NullType,NullType,NullType,NullType> : private NonCopyable
1226 {
1227  public:
1228  //**Type definitions****************************************************************************
1230  typedef Singleton<T,D1,D2,NullType,NullType,NullType,NullType,NullType,NullType> SingletonType;
1231 
1233  typedef BLAZE_TYPELIST_2( D1, D2 ) Dependencies;
1234  //**********************************************************************************************
1235 
1236  protected:
1237  //**Constructor*********************************************************************************
1242  explicit Singleton()
1243  : dependency1_( D1::instance() ) // Handle to the first lifetime dependency
1244  , dependency2_( D2::instance() ) // Handle to the second lifetime dependency
1245  {
1247  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D1, typename D1::SingletonType );
1248  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D2, typename D2::SingletonType );
1251  }
1252  //**********************************************************************************************
1253 
1254  //**Destructor**********************************************************************************
1257  ~Singleton()
1258  {}
1259  //**********************************************************************************************
1260 
1261  public:
1262  //**Instance function***************************************************************************
1265  static boost::shared_ptr<T> instance()
1266  {
1267  boost::mutex::scoped_lock lock( instanceMutex_ );
1268  static boost::shared_ptr<T> object( new T() );
1269  return object;
1270  }
1272  //**********************************************************************************************
1273 
1274  private:
1275  //**Member variables****************************************************************************
1278  boost::shared_ptr<D1> dependency1_;
1279  boost::shared_ptr<D2> dependency2_;
1280 
1281  //**********************************************************************************************
1282 
1283  protected:
1284  //**Member variables****************************************************************************
1287  static boost::mutex instanceMutex_;
1288 
1289  //**********************************************************************************************
1290 };
1292 //*************************************************************************************************
1293 
1294 
1295 
1296 
1297 //=================================================================================================
1298 //
1299 // SINGLETON SPECIALIZATION (1 LIFETIME DEPENDENCY)
1300 //
1301 //=================================================================================================
1302 
1303 //*************************************************************************************************
1311 template< typename T // Type of the singleton (CRTP pattern)
1312  , typename D1 > // Type of the lifetime dependency
1313 class Singleton<T,D1,NullType,NullType,NullType,NullType,NullType,NullType,NullType> : private NonCopyable
1314 {
1315  public:
1316  //**Type definitions****************************************************************************
1318  typedef Singleton<T,D1,NullType,NullType,NullType,NullType,NullType,NullType,NullType> SingletonType;
1319 
1321  typedef BLAZE_TYPELIST_1( D1 ) Dependencies;
1322  //**********************************************************************************************
1323 
1324  protected:
1325  //**Constructor*********************************************************************************
1330  explicit Singleton()
1331  : dependency1_( D1::instance() ) // Handle to the lifetime dependency
1332  {
1334  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D1, typename D1::SingletonType );
1336  }
1337  //**********************************************************************************************
1338 
1339  //**Destructor**********************************************************************************
1342  ~Singleton()
1343  {}
1344  //**********************************************************************************************
1345 
1346  public:
1347  //**Instance function***************************************************************************
1350  static boost::shared_ptr<T> instance()
1351  {
1352  boost::mutex::scoped_lock lock( instanceMutex_ );
1353  static boost::shared_ptr<T> object( new T() );
1354  return object;
1355  }
1357  //**********************************************************************************************
1358 
1359  private:
1360  //**Member variables****************************************************************************
1363  boost::shared_ptr<D1> dependency1_;
1364 
1365  //**********************************************************************************************
1366 
1367  protected:
1368  //**Member variables****************************************************************************
1371  static boost::mutex instanceMutex_;
1372 
1373  //**********************************************************************************************
1374 };
1376 //*************************************************************************************************
1377 
1378 
1379 
1380 
1381 //=================================================================================================
1382 //
1383 // SINGLETON SPECIALIZATION (0 LIFETIME DEPENDENCIES)
1384 //
1385 //=================================================================================================
1386 
1387 //*************************************************************************************************
1395 template< typename T > // Type of the singleton (CRTP pattern)
1396 class Singleton<T,NullType,NullType,NullType,NullType,NullType,NullType,NullType,NullType> : private NonCopyable
1397 {
1398  public:
1399  //**Type definitions****************************************************************************
1401  typedef Singleton<T,NullType,NullType,NullType,NullType,NullType,NullType,NullType,NullType> SingletonType;
1402 
1404  typedef NullType Dependencies;
1405  //**********************************************************************************************
1406 
1407  protected:
1408  //**Constructor*********************************************************************************
1413  explicit Singleton()
1414  {
1416  }
1417  //**********************************************************************************************
1418 
1419  //**Destructor**********************************************************************************
1422  ~Singleton()
1423  {}
1424  //**********************************************************************************************
1425 
1426  public:
1427  //**Instance function***************************************************************************
1430  static boost::shared_ptr<T> instance()
1431  {
1432  boost::mutex::scoped_lock lock( instanceMutex_ );
1433  static boost::shared_ptr<T> object( new T() );
1434  return object;
1435  }
1437  //**********************************************************************************************
1438 
1439  protected:
1440  //**Member variables****************************************************************************
1443  static boost::mutex instanceMutex_;
1444 
1445  //**********************************************************************************************
1446 };
1448 //*************************************************************************************************
1449 
1450 
1451 
1452 
1453 //=================================================================================================
1454 //
1455 // DEFINITION AND INITIALIZATION OF THE STATIC MEMBER VARIABLES
1456 //
1457 //=================================================================================================
1458 
1459 template< typename T // Type of the singleton (CRTP pattern)
1460  , typename A // Type of the first lifetime dependency
1461  , typename B // Type of the second lifetime dependency
1462  , typename C // Type of the third lifetime dependency
1463  , typename D // Type of the fourth lifetime dependency
1464  , typename E // Type of the fifth lifetime dependency
1465  , typename F // Type of the sixth lifetime dependency
1466  , typename G // Type of the seventh lifetime dependency
1467  , typename H > // Type of the eighth lifetime dependency
1468 boost::mutex Singleton<T,A,B,C,D,E,F,G,H>::instanceMutex_;
1469 
1470 template< typename T // Type of the singleton (CRTP pattern)
1471  , typename A // Type of the first lifetime dependency
1472  , typename B // Type of the second lifetime dependency
1473  , typename C // Type of the third lifetime dependency
1474  , typename D // Type of the fourth lifetime dependency
1475  , typename E // Type of the fifth lifetime dependency
1476  , typename F // Type of the sixth lifetime dependency
1477  , typename G > // Type of the seventh lifetime dependency
1478 boost::mutex Singleton<T,A,B,C,D,E,F,G,NullType>::instanceMutex_;
1479 
1480 template< typename T // Type of the singleton (CRTP pattern)
1481  , typename A // Type of the first lifetime dependency
1482  , typename B // Type of the second lifetime dependency
1483  , typename C // Type of the third lifetime dependency
1484  , typename D // Type of the fourth lifetime dependency
1485  , typename E // Type of the fifth lifetime dependency
1486  , typename F > // Type of the sixth lifetime dependency
1487 boost::mutex Singleton<T,A,B,C,D,E,F,NullType,NullType>::instanceMutex_;
1488 
1489 template< typename T // Type of the singleton (CRTP pattern)
1490  , typename A // Type of the first lifetime dependency
1491  , typename B // Type of the second lifetime dependency
1492  , typename C // Type of the third lifetime dependency
1493  , typename D // Type of the fourth lifetime dependency
1494  , typename E > // Type of the fifth lifetime dependency
1495 boost::mutex Singleton<T,A,B,C,D,E,NullType,NullType,NullType>::instanceMutex_;
1496 
1497 template< typename T // Type of the singleton (CRTP pattern)
1498  , typename A // Type of the first lifetime dependency
1499  , typename B // Type of the second lifetime dependency
1500  , typename C // Type of the third lifetime dependency
1501  , typename D > // Type of the fourth lifetime dependency
1502 boost::mutex Singleton<T,A,B,C,D,NullType,NullType,NullType,NullType>::instanceMutex_;
1503 
1504 template< typename T // Type of the singleton (CRTP pattern)
1505  , typename A // Type of the first lifetime dependency
1506  , typename B // Type of the second lifetime dependency
1507  , typename C > // Type of the third lifetime dependency
1508 boost::mutex Singleton<T,A,B,C,NullType,NullType,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 boost::mutex Singleton<T,A,B,NullType,NullType,NullType,NullType,NullType,NullType>::instanceMutex_;
1514 
1515 template< typename T // Type of the singleton (CRTP pattern)
1516  , typename A > // Type of the first lifetime dependency
1517 boost::mutex Singleton<T,A,NullType,NullType,NullType,NullType,NullType,NullType,NullType>::instanceMutex_;
1518 
1519 template< typename T > // Type of the singleton (CRTP pattern)
1520 boost::mutex Singleton<T,NullType,NullType,NullType,NullType,NullType,NullType,NullType,NullType>::instanceMutex_;
1521 
1522 } // namespace blaze
1523 
1524 #endif