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 //*************************************************************************************************
565 template< typename T // Type of the singleton (CRTP pattern)
566  , typename D1 = NullType // Type of the first lifetime dependency
567  , typename D2 = NullType // Type of the second lifetime dependency
568  , typename D3 = NullType // Type of the third lifetime dependency
569  , typename D4 = NullType // Type of the fourth lifetime dependency
570  , typename D5 = NullType // Type of the fifth lifetime dependency
571  , typename D6 = NullType // Type of the sixth lifetime dependency
572  , typename D7 = NullType // Type of the seventh lifetime dependency
573  , typename D8 = NullType > // Type of the eighth lifetime dependency
574 class Singleton : private NonCopyable
575 {
576  public:
577  //**Type definitions****************************************************************************
580 
582  typedef BLAZE_TYPELIST_8( D1, D2, D3, D4, D5, D6, D7, D8 ) Dependencies;
583  //**********************************************************************************************
584 
585  protected:
586  //**Constructor*********************************************************************************
591  explicit Singleton()
592  : dependency1_( D1::instance() ) // Handle to the first lifetime dependency
593  , dependency2_( D2::instance() ) // Handle to the second lifetime dependency
594  , dependency3_( D3::instance() ) // Handle to the third lifetime dependency
595  , dependency4_( D4::instance() ) // Handle to the fourth lifetime dependency
596  , dependency5_( D5::instance() ) // Handle to the fifth lifetime dependency
597  , dependency6_( D6::instance() ) // Handle to the sixth lifetime dependency
598  , dependency7_( D7::instance() ) // Handle to the seventh lifetime dependency
599  , dependency8_( D8::instance() ) // Handle to the eighth lifetime dependency
600  {
602  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D1, typename D1::SingletonType );
603  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D2, typename D2::SingletonType );
604  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D3, typename D3::SingletonType );
605  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D4, typename D4::SingletonType );
606  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D5, typename D5::SingletonType );
607  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D6, typename D6::SingletonType );
608  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D7, typename D7::SingletonType );
609  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D8, typename D8::SingletonType );
618  }
619  //**********************************************************************************************
620 
621  //**Destructor**********************************************************************************
625  {}
626  //**********************************************************************************************
627 
628  public:
629  //**Instance function***************************************************************************
632  static boost::shared_ptr<T> instance()
633  {
634  boost::mutex::scoped_lock lock( instanceMutex_ );
635  static boost::shared_ptr<T> object( new T() );
636  return object;
637  }
639  //**********************************************************************************************
640 
641  private:
642  //**Member variables****************************************************************************
645  boost::shared_ptr<D1> dependency1_;
646  boost::shared_ptr<D2> dependency2_;
647  boost::shared_ptr<D3> dependency3_;
648  boost::shared_ptr<D4> dependency4_;
649  boost::shared_ptr<D5> dependency5_;
650  boost::shared_ptr<D6> dependency6_;
651  boost::shared_ptr<D7> dependency7_;
652  boost::shared_ptr<D8> dependency8_;
653 
654  //**********************************************************************************************
655 
656  protected:
657  //**Member variables****************************************************************************
660  static boost::mutex instanceMutex_;
661 
662  //**********************************************************************************************
663 };
664 //*************************************************************************************************
665 
666 
667 
668 
669 //=================================================================================================
670 //
671 // SINGLETON SPECIALIZATION (7 LIFETIME DEPENDENCIES)
672 //
673 //=================================================================================================
674 
675 //*************************************************************************************************
683 template< typename T // Type of the singleton (CRTP pattern)
684  , typename D1 // Type of the first lifetime dependency
685  , typename D2 // Type of the second lifetime dependency
686  , typename D3 // Type of the third lifetime dependency
687  , typename D4 // Type of the fourth lifetime dependency
688  , typename D5 // Type of the fifth lifetime dependency
689  , typename D6 // Type of the sixth lifetime dependency
690  , typename D7 > // Type of the eighth lifetime dependency
691 class Singleton<T,D1,D2,D3,D4,D5,D6,D7,NullType> : private NonCopyable
692 {
693  public:
694  //**Type definitions****************************************************************************
697 
699  typedef BLAZE_TYPELIST_7( D1, D2, D3, D4, D5, D6, D7 ) Dependencies;
700  //**********************************************************************************************
701 
702  protected:
703  //**Constructor*********************************************************************************
708  explicit Singleton()
709  : dependency1_( D1::instance() ) // Handle to the first lifetime dependency
710  , dependency2_( D2::instance() ) // Handle to the second lifetime dependency
711  , dependency3_( D3::instance() ) // Handle to the third lifetime dependency
712  , dependency4_( D4::instance() ) // Handle to the fourth lifetime dependency
713  , dependency5_( D5::instance() ) // Handle to the fifth lifetime dependency
714  , dependency6_( D6::instance() ) // Handle to the sixth lifetime dependency
715  , dependency7_( D7::instance() ) // Handle to the seventh lifetime dependency
716  {
718  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D1, typename D1::SingletonType );
719  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D2, typename D2::SingletonType );
720  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D3, typename D3::SingletonType );
721  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D4, typename D4::SingletonType );
722  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D5, typename D5::SingletonType );
723  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D6, typename D6::SingletonType );
724  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D7, typename D7::SingletonType );
732  }
733  //**********************************************************************************************
734 
735  //**Destructor**********************************************************************************
738  ~Singleton()
739  {}
740  //**********************************************************************************************
741 
742  public:
743  //**Instance function***************************************************************************
746  static boost::shared_ptr<T> instance()
747  {
748  boost::mutex::scoped_lock lock( instanceMutex_ );
749  static boost::shared_ptr<T> object( new T() );
750  return object;
751  }
753  //**********************************************************************************************
754 
755  private:
756  //**Member variables****************************************************************************
759  boost::shared_ptr<D1> dependency1_;
760  boost::shared_ptr<D2> dependency2_;
761  boost::shared_ptr<D3> dependency3_;
762  boost::shared_ptr<D4> dependency4_;
763  boost::shared_ptr<D5> dependency5_;
764  boost::shared_ptr<D6> dependency6_;
765  boost::shared_ptr<D7> dependency7_;
766 
767  //**********************************************************************************************
768 
769  protected:
770  //**Member variables****************************************************************************
773  static boost::mutex instanceMutex_;
774 
775  //**********************************************************************************************
776 };
778 //*************************************************************************************************
779 
780 
781 
782 
783 //=================================================================================================
784 //
785 // SINGLETON SPECIALIZATION (6 LIFETIME DEPENDENCIES)
786 //
787 //=================================================================================================
788 
789 //*************************************************************************************************
797 template< typename T // Type of the singleton (CRTP pattern)
798  , typename D1 // Type of the first lifetime dependency
799  , typename D2 // Type of the second lifetime dependency
800  , typename D3 // Type of the third lifetime dependency
801  , typename D4 // Type of the fourth lifetime dependency
802  , typename D5 // Type of the fifth lifetime dependency
803  , typename D6 > // Type of the eighth lifetime dependency
804 class Singleton<T,D1,D2,D3,D4,D5,D6,NullType,NullType> : private NonCopyable
805 {
806  public:
807  //**Type definitions****************************************************************************
809  typedef Singleton<T,D1,D2,D3,D4,D5,D6,NullType,NullType> SingletonType;
810 
812  typedef BLAZE_TYPELIST_6( D1, D2, D3, D4, D5, D6 ) Dependencies;
813  //**********************************************************************************************
814 
815  protected:
816  //**Constructor*********************************************************************************
821  explicit Singleton()
822  : dependency1_( D1::instance() ) // Handle to the first lifetime dependency
823  , dependency2_( D2::instance() ) // Handle to the second lifetime dependency
824  , dependency3_( D3::instance() ) // Handle to the third lifetime dependency
825  , dependency4_( D4::instance() ) // Handle to the fourth lifetime dependency
826  , dependency5_( D5::instance() ) // Handle to the fifth lifetime dependency
827  , dependency6_( D6::instance() ) // Handle to the sixth lifetime dependency
828  {
830  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D1, typename D1::SingletonType );
831  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D2, typename D2::SingletonType );
832  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D3, typename D3::SingletonType );
833  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D4, typename D4::SingletonType );
834  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D5, typename D5::SingletonType );
835  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D6, typename D6::SingletonType );
842  }
843  //**********************************************************************************************
844 
845  //**Destructor**********************************************************************************
848  ~Singleton()
849  {}
850  //**********************************************************************************************
851 
852  public:
853  //**Instance function***************************************************************************
856  static boost::shared_ptr<T> instance()
857  {
858  boost::mutex::scoped_lock lock( instanceMutex_ );
859  static boost::shared_ptr<T> object( new T() );
860  return object;
861  }
863  //**********************************************************************************************
864 
865  private:
866  //**Member variables****************************************************************************
869  boost::shared_ptr<D1> dependency1_;
870  boost::shared_ptr<D2> dependency2_;
871  boost::shared_ptr<D3> dependency3_;
872  boost::shared_ptr<D4> dependency4_;
873  boost::shared_ptr<D5> dependency5_;
874  boost::shared_ptr<D6> dependency6_;
875 
876  //**********************************************************************************************
877 
878  protected:
879  //**Member variables****************************************************************************
882  static boost::mutex instanceMutex_;
883 
884  //**********************************************************************************************
885 };
887 //*************************************************************************************************
888 
889 
890 
891 
892 //=================================================================================================
893 //
894 // SINGLETON SPECIALIZATION (5 LIFETIME DEPENDENCIES)
895 //
896 //=================================================================================================
897 
898 //*************************************************************************************************
906 template< typename T // Type of the singleton (CRTP pattern)
907  , typename D1 // Type of the first lifetime dependency
908  , typename D2 // Type of the second lifetime dependency
909  , typename D3 // Type of the third lifetime dependency
910  , typename D4 // Type of the fourth lifetime dependency
911  , typename D5 > // Type of the fifth lifetime dependency
912 class Singleton<T,D1,D2,D3,D4,D5,NullType,NullType,NullType> : private NonCopyable
913 {
914  public:
915  //**Type definitions****************************************************************************
917  typedef Singleton<T,D1,D2,D3,D4,D5,NullType,NullType,NullType> SingletonType;
918 
920  typedef BLAZE_TYPELIST_5( D1, D2, D3, D4, D5 ) Dependencies;
921  //**********************************************************************************************
922 
923  protected:
924  //**Constructor*********************************************************************************
929  explicit Singleton()
930  : dependency1_( D1::instance() ) // Handle to the first lifetime dependency
931  , dependency2_( D2::instance() ) // Handle to the second lifetime dependency
932  , dependency3_( D3::instance() ) // Handle to the third lifetime dependency
933  , dependency4_( D4::instance() ) // Handle to the fourth lifetime dependency
934  , dependency5_( D5::instance() ) // Handle to the fifth lifetime dependency
935  {
937  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D1, typename D1::SingletonType );
938  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D2, typename D2::SingletonType );
939  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D3, typename D3::SingletonType );
940  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D4, typename D4::SingletonType );
941  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D5, typename D5::SingletonType );
947  }
948  //**********************************************************************************************
949 
950  //**Destructor**********************************************************************************
953  ~Singleton()
954  {}
955  //**********************************************************************************************
956 
957  public:
958  //**Instance function***************************************************************************
961  static boost::shared_ptr<T> instance()
962  {
963  boost::mutex::scoped_lock lock( instanceMutex_ );
964  static boost::shared_ptr<T> object( new T() );
965  return object;
966  }
968  //**********************************************************************************************
969 
970  private:
971  //**Member variables****************************************************************************
974  boost::shared_ptr<D1> dependency1_;
975  boost::shared_ptr<D2> dependency2_;
976  boost::shared_ptr<D3> dependency3_;
977  boost::shared_ptr<D4> dependency4_;
978  boost::shared_ptr<D5> dependency5_;
979 
980  //**********************************************************************************************
981 
982  protected:
983  //**Member variables****************************************************************************
986  static boost::mutex instanceMutex_;
987 
988  //**********************************************************************************************
989 };
991 //*************************************************************************************************
992 
993 
994 
995 
996 //=================================================================================================
997 //
998 // SINGLETON SPECIALIZATION (4 LIFETIME DEPENDENCIES)
999 //
1000 //=================================================================================================
1001 
1002 //*************************************************************************************************
1010 template< typename T // Type of the singleton (CRTP pattern)
1011  , typename D1 // Type of the first lifetime dependency
1012  , typename D2 // Type of the second lifetime dependency
1013  , typename D3 // Type of the third lifetime dependency
1014  , typename D4 > // Type of the fourth lifetime dependency
1015 class Singleton<T,D1,D2,D3,D4,NullType,NullType,NullType,NullType> : private NonCopyable
1016 {
1017  public:
1018  //**Type definitions****************************************************************************
1020  typedef Singleton<T,D1,D2,D3,D4,NullType,NullType,NullType,NullType> SingletonType;
1021 
1023  typedef BLAZE_TYPELIST_4( D1, D2, D3, D4 ) Dependencies;
1024  //**********************************************************************************************
1025 
1026  protected:
1027  //**Constructor*********************************************************************************
1032  explicit Singleton()
1033  : dependency1_( D1::instance() ) // Handle to the first lifetime dependency
1034  , dependency2_( D2::instance() ) // Handle to the second lifetime dependency
1035  , dependency3_( D3::instance() ) // Handle to the third lifetime dependency
1036  , dependency4_( D4::instance() ) // Handle to the fourth lifetime dependency
1037  {
1039  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D1, typename D1::SingletonType );
1040  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D2, typename D2::SingletonType );
1041  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D3, typename D3::SingletonType );
1042  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D4, typename D4::SingletonType );
1047  }
1048  //**********************************************************************************************
1049 
1050  //**Destructor**********************************************************************************
1053  ~Singleton()
1054  {}
1055  //**********************************************************************************************
1056 
1057  public:
1058  //**Instance function***************************************************************************
1061  static boost::shared_ptr<T> instance()
1062  {
1063  boost::mutex::scoped_lock lock( instanceMutex_ );
1064  static boost::shared_ptr<T> object( new T() );
1065  return object;
1066  }
1068  //**********************************************************************************************
1069 
1070  private:
1071  //**Member variables****************************************************************************
1074  boost::shared_ptr<D1> dependency1_;
1075  boost::shared_ptr<D2> dependency2_;
1076  boost::shared_ptr<D3> dependency3_;
1077  boost::shared_ptr<D4> dependency4_;
1078 
1079  //**********************************************************************************************
1080 
1081  protected:
1082  //**Member variables****************************************************************************
1085  static boost::mutex instanceMutex_;
1086 
1087  //**********************************************************************************************
1088 };
1090 //*************************************************************************************************
1091 
1092 
1093 
1094 
1095 //=================================================================================================
1096 //
1097 // SINGLETON SPECIALIZATION (3 LIFETIME DEPENDENCIES)
1098 //
1099 //=================================================================================================
1100 
1101 //*************************************************************************************************
1109 template< typename T // Type of the singleton (CRTP pattern)
1110  , typename D1 // Type of the first lifetime dependency
1111  , typename D2 // Type of the second lifetime dependency
1112  , typename D3 > // Type of the third lifetime dependency
1113 class Singleton<T,D1,D2,D3,NullType,NullType,NullType,NullType,NullType> : private NonCopyable
1114 {
1115  public:
1116  //**Type definitions****************************************************************************
1118  typedef Singleton<T,D1,D2,D3,NullType,NullType,NullType,NullType,NullType> SingletonType;
1119 
1121  typedef BLAZE_TYPELIST_3( D1, D2, D3 ) Dependencies;
1122  //**********************************************************************************************
1123 
1124  protected:
1125  //**Constructor*********************************************************************************
1130  explicit Singleton()
1131  : dependency1_( D1::instance() ) // Handle to the first lifetime dependency
1132  , dependency2_( D2::instance() ) // Handle to the second lifetime dependency
1133  , dependency3_( D3::instance() ) // Handle to the third lifetime dependency
1134  {
1136  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D1, typename D1::SingletonType );
1137  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D2, typename D2::SingletonType );
1138  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D3, typename D3::SingletonType );
1142  }
1143  //**********************************************************************************************
1144 
1145  //**Destructor**********************************************************************************
1148  ~Singleton()
1149  {}
1150  //**********************************************************************************************
1151 
1152  public:
1153  //**Instance function***************************************************************************
1156  static boost::shared_ptr<T> instance()
1157  {
1158  boost::mutex::scoped_lock lock( instanceMutex_ );
1159  static boost::shared_ptr<T> object( new T() );
1160  return object;
1161  }
1163  //**********************************************************************************************
1164 
1165  private:
1166  //**Member variables****************************************************************************
1169  boost::shared_ptr<D1> dependency1_;
1170  boost::shared_ptr<D2> dependency2_;
1171  boost::shared_ptr<D3> dependency3_;
1172 
1173  //**********************************************************************************************
1174 
1175  protected:
1176  //**Member variables****************************************************************************
1179  static boost::mutex instanceMutex_;
1180 
1181  //**********************************************************************************************
1182 };
1184 //*************************************************************************************************
1185 
1186 
1187 
1188 
1189 //=================================================================================================
1190 //
1191 // SINGLETON SPECIALIZATION (2 LIFETIME DEPENDENCIES)
1192 //
1193 //=================================================================================================
1194 
1195 //*************************************************************************************************
1203 template< typename T // Type of the singleton (CRTP pattern)
1204  , typename D1 // Type of the first lifetime dependency
1205  , typename D2 > // Type of the second lifetime dependency
1206 class Singleton<T,D1,D2,NullType,NullType,NullType,NullType,NullType,NullType> : private NonCopyable
1207 {
1208  public:
1209  //**Type definitions****************************************************************************
1211  typedef Singleton<T,D1,D2,NullType,NullType,NullType,NullType,NullType,NullType> SingletonType;
1212 
1214  typedef BLAZE_TYPELIST_2( D1, D2 ) Dependencies;
1215  //**********************************************************************************************
1216 
1217  protected:
1218  //**Constructor*********************************************************************************
1223  explicit Singleton()
1224  : dependency1_( D1::instance() ) // Handle to the first lifetime dependency
1225  , dependency2_( D2::instance() ) // Handle to the second lifetime dependency
1226  {
1228  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D1, typename D1::SingletonType );
1229  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D2, typename D2::SingletonType );
1232  }
1233  //**********************************************************************************************
1234 
1235  //**Destructor**********************************************************************************
1238  ~Singleton()
1239  {}
1240  //**********************************************************************************************
1241 
1242  public:
1243  //**Instance function***************************************************************************
1246  static boost::shared_ptr<T> instance()
1247  {
1248  boost::mutex::scoped_lock lock( instanceMutex_ );
1249  static boost::shared_ptr<T> object( new T() );
1250  return object;
1251  }
1253  //**********************************************************************************************
1254 
1255  private:
1256  //**Member variables****************************************************************************
1259  boost::shared_ptr<D1> dependency1_;
1260  boost::shared_ptr<D2> dependency2_;
1261 
1262  //**********************************************************************************************
1263 
1264  protected:
1265  //**Member variables****************************************************************************
1268  static boost::mutex instanceMutex_;
1269 
1270  //**********************************************************************************************
1271 };
1273 //*************************************************************************************************
1274 
1275 
1276 
1277 
1278 //=================================================================================================
1279 //
1280 // SINGLETON SPECIALIZATION (1 LIFETIME DEPENDENCY)
1281 //
1282 //=================================================================================================
1283 
1284 //*************************************************************************************************
1292 template< typename T // Type of the singleton (CRTP pattern)
1293  , typename D1 > // Type of the lifetime dependency
1294 class Singleton<T,D1,NullType,NullType,NullType,NullType,NullType,NullType,NullType> : private NonCopyable
1295 {
1296  public:
1297  //**Type definitions****************************************************************************
1299  typedef Singleton<T,D1,NullType,NullType,NullType,NullType,NullType,NullType,NullType> SingletonType;
1300 
1302  typedef BLAZE_TYPELIST_1( D1 ) Dependencies;
1303  //**********************************************************************************************
1304 
1305  protected:
1306  //**Constructor*********************************************************************************
1311  explicit Singleton()
1312  : dependency1_( D1::instance() ) // Handle to the lifetime dependency
1313  {
1315  BLAZE_CONSTRAINT_MUST_BE_DERIVED_FROM( D1, typename D1::SingletonType );
1317  }
1318  //**********************************************************************************************
1319 
1320  //**Destructor**********************************************************************************
1323  ~Singleton()
1324  {}
1325  //**********************************************************************************************
1326 
1327  public:
1328  //**Instance function***************************************************************************
1331  static boost::shared_ptr<T> instance()
1332  {
1333  boost::mutex::scoped_lock lock( instanceMutex_ );
1334  static boost::shared_ptr<T> object( new T() );
1335  return object;
1336  }
1338  //**********************************************************************************************
1339 
1340  private:
1341  //**Member variables****************************************************************************
1344  boost::shared_ptr<D1> dependency1_;
1345 
1346  //**********************************************************************************************
1347 
1348  protected:
1349  //**Member variables****************************************************************************
1352  static boost::mutex instanceMutex_;
1353 
1354  //**********************************************************************************************
1355 };
1357 //*************************************************************************************************
1358 
1359 
1360 
1361 
1362 //=================================================================================================
1363 //
1364 // SINGLETON SPECIALIZATION (0 LIFETIME DEPENDENCIES)
1365 //
1366 //=================================================================================================
1367 
1368 //*************************************************************************************************
1376 template< typename T > // Type of the singleton (CRTP pattern)
1377 class Singleton<T,NullType,NullType,NullType,NullType,NullType,NullType,NullType,NullType> : private NonCopyable
1378 {
1379  public:
1380  //**Type definitions****************************************************************************
1382  typedef Singleton<T,NullType,NullType,NullType,NullType,NullType,NullType,NullType,NullType> SingletonType;
1383 
1385  typedef NullType Dependencies;
1386  //**********************************************************************************************
1387 
1388  protected:
1389  //**Constructor*********************************************************************************
1394  explicit Singleton()
1395  {
1397  }
1398  //**********************************************************************************************
1399 
1400  //**Destructor**********************************************************************************
1403  ~Singleton()
1404  {}
1405  //**********************************************************************************************
1406 
1407  public:
1408  //**Instance function***************************************************************************
1411  static boost::shared_ptr<T> instance()
1412  {
1413  boost::mutex::scoped_lock lock( instanceMutex_ );
1414  static boost::shared_ptr<T> object( new T() );
1415  return object;
1416  }
1418  //**********************************************************************************************
1419 
1420  protected:
1421  //**Member variables****************************************************************************
1424  static boost::mutex instanceMutex_;
1425 
1426  //**********************************************************************************************
1427 };
1429 //*************************************************************************************************
1430 
1431 
1432 
1433 
1434 //=================================================================================================
1435 //
1436 // DEFINITION AND INITIALIZATION OF THE STATIC MEMBER VARIABLES
1437 //
1438 //=================================================================================================
1439 
1440 template< typename T // Type of the singleton (CRTP pattern)
1441  , typename A // Type of the first lifetime dependency
1442  , typename B // Type of the second lifetime dependency
1443  , typename C // Type of the third lifetime dependency
1444  , typename D // Type of the fourth lifetime dependency
1445  , typename E // Type of the fifth lifetime dependency
1446  , typename F // Type of the sixth lifetime dependency
1447  , typename G // Type of the seventh lifetime dependency
1448  , typename H > // Type of the eighth lifetime dependency
1449 boost::mutex Singleton<T,A,B,C,D,E,F,G,H>::instanceMutex_;
1450 
1451 template< typename T // Type of the singleton (CRTP pattern)
1452  , typename A // Type of the first lifetime dependency
1453  , typename B // Type of the second lifetime dependency
1454  , typename C // Type of the third lifetime dependency
1455  , typename D // Type of the fourth lifetime dependency
1456  , typename E // Type of the fifth lifetime dependency
1457  , typename F // Type of the sixth lifetime dependency
1458  , typename G > // Type of the seventh lifetime dependency
1459 boost::mutex Singleton<T,A,B,C,D,E,F,G,NullType>::instanceMutex_;
1460 
1461 template< typename T // Type of the singleton (CRTP pattern)
1462  , typename A // Type of the first lifetime dependency
1463  , typename B // Type of the second lifetime dependency
1464  , typename C // Type of the third lifetime dependency
1465  , typename D // Type of the fourth lifetime dependency
1466  , typename E // Type of the fifth lifetime dependency
1467  , typename F > // Type of the sixth lifetime dependency
1468 boost::mutex Singleton<T,A,B,C,D,E,F,NullType,NullType>::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 boost::mutex Singleton<T,A,B,C,D,E,NullType,NullType,NullType>::instanceMutex_;
1477 
1478 template< typename T // Type of the singleton (CRTP pattern)
1479  , typename A // Type of the first lifetime dependency
1480  , typename B // Type of the second lifetime dependency
1481  , typename C // Type of the third lifetime dependency
1482  , typename D > // Type of the fourth lifetime dependency
1483 boost::mutex Singleton<T,A,B,C,D,NullType,NullType,NullType,NullType>::instanceMutex_;
1484 
1485 template< typename T // Type of the singleton (CRTP pattern)
1486  , typename A // Type of the first lifetime dependency
1487  , typename B // Type of the second lifetime dependency
1488  , typename C > // Type of the third lifetime dependency
1489 boost::mutex Singleton<T,A,B,C,NullType,NullType,NullType,NullType,NullType>::instanceMutex_;
1490 
1491 template< typename T // Type of the singleton (CRTP pattern)
1492  , typename A // Type of the first lifetime dependency
1493  , typename B > // Type of the second lifetime dependency
1494 boost::mutex Singleton<T,A,B,NullType,NullType,NullType,NullType,NullType,NullType>::instanceMutex_;
1495 
1496 template< typename T // Type of the singleton (CRTP pattern)
1497  , typename A > // Type of the first lifetime dependency
1498 boost::mutex Singleton<T,A,NullType,NullType,NullType,NullType,NullType,NullType,NullType>::instanceMutex_;
1499 
1500 template< typename T > // Type of the singleton (CRTP pattern)
1501 boost::mutex Singleton<T,NullType,NullType,NullType,NullType,NullType,NullType,NullType,NullType>::instanceMutex_;
1502 
1503 } // namespace blaze
1504 
1505 #endif