Singleton.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. #ifndef __WASABI_SINGLETON_H
  2. #define __WASABI_SINGLETON_H
  3. #include "api/service/waservicefactoryi.h"
  4. #include "api/service/services.h"
  5. template <class api_t, class impl_t>
  6. class Singleton : public waServiceFactoryI
  7. {
  8. public:
  9. Singleton() : impl( 0 ), orig( 0 ) {}
  10. Singleton( impl_t *new_impl, bool existing ) : impl( 0 ), orig( 0 )
  11. {
  12. if ( existing )
  13. Register( new_impl );
  14. else
  15. RegisterNew( new_impl );
  16. }
  17. Singleton( impl_t *&new_impl ) : impl( 0 ), orig( 0 ) { RegisterNew( new_impl ); }
  18. ~Singleton() { Deregister(); }
  19. void Register( impl_t *new_impl )
  20. {
  21. impl = new_impl;
  22. WASABI_API_SVC->service_register( this );
  23. }
  24. void RegisterNew( impl_t *&new_impl )
  25. {
  26. if ( !new_impl )
  27. {
  28. orig = &new_impl;
  29. new_impl = new impl_t;
  30. }
  31. impl = new_impl;
  32. WASABI_API_SVC->service_register( this );
  33. }
  34. void Deregister()
  35. {
  36. if ( impl )
  37. {
  38. WASABI_API_SVC->service_deregister( this );
  39. if ( orig )
  40. {
  41. delete impl;
  42. *orig = 0;
  43. }
  44. impl = 0;
  45. }
  46. }
  47. impl_t *impl;
  48. impl_t **orig;
  49. FOURCC svc_serviceType() { return WaSvc::UNIQUE; } // TODO: make this a (defaulted) template parameter
  50. const char *svc_getServiceName() { return impl_t::getServiceName(); }
  51. GUID svc_getGuid() { return impl_t::getServiceGuid(); } // GUID per service factory, can be INVALID_GUID
  52. void *svc_getInterface( int global_lock = TRUE ) { return static_cast<api_t *>( impl ); }
  53. int svc_supportNonLockingGetInterface() { return TRUE; }
  54. int svc_releaseInterface( void *ifc ) { return 0; }
  55. CfgItem *svc_getCfgInterface() { return NULL; }
  56. const wchar_t *svc_getTestString() { return 0; }
  57. int svc_notify( int msg, int param1 = 0, int param2 = 0 ) { return 0; }
  58. };
  59. template <class api_t, class impl_t>
  60. class Singleton2 : public waServiceFactoryI
  61. {
  62. public:
  63. Singleton2() : impl( 0 ), orig( 0 ) {}
  64. Singleton2( impl_t *new_impl, bool existing ) : impl( 0 ), orig( 0 )
  65. {
  66. if ( existing )
  67. Register( new_impl );
  68. else
  69. RegisterNew( new_impl );
  70. }
  71. Singleton2( impl_t *&new_impl ) : impl( 0 ), orig( 0 ) { RegisterNew( new_impl ); }
  72. ~Singleton2() { Deregister(); }
  73. void Register( impl_t *new_impl )
  74. {
  75. impl = new_impl;
  76. WASABI_API_SVC->service_register( this );
  77. }
  78. void RegisterNew( impl_t *&new_impl )
  79. {
  80. if ( !new_impl )
  81. {
  82. orig = &new_impl;
  83. new_impl = new impl_t;
  84. }
  85. impl = new_impl;
  86. WASABI_API_SVC->service_register( this );
  87. }
  88. void Deregister()
  89. {
  90. if ( impl )
  91. {
  92. WASABI_API_SVC->service_deregister( this );
  93. if ( orig )
  94. {
  95. delete impl;
  96. *orig = 0;
  97. }
  98. impl = 0;
  99. }
  100. }
  101. impl_t *impl;
  102. impl_t **orig;
  103. FOURCC svc_serviceType() { return WaSvc::UNIQUE; } // TODO: make this a (defaulted) template parameter
  104. const char *svc_getServiceName() { return impl_t::getServiceName(); }
  105. GUID svc_getGuid() { return impl_t::getServiceGuid(); } // GUID per service factory, can be INVALID_GUID
  106. void *svc_getInterface( int global_lock = TRUE ) { return static_cast<api_t *>( impl ); }
  107. int svc_supportNonLockingGetInterface() { return TRUE; }
  108. int svc_releaseInterface( void *ifc ) { return 0; }
  109. CfgItem *svc_getCfgInterface() { return NULL; }
  110. const wchar_t *svc_getTestString() { return 0; }
  111. int svc_notify( int msg, int param1 = 0, int param2 = 0 ) { return 0; }
  112. };
  113. #endif