1
0

Singleton.h 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #pragma once
  2. #ifndef NULLSOFT_UTILITY_SINGLETON_H
  3. #define NULLSOFT_UTILITY_SINGLETON_H
  4. #include <api/service/waservicefactoryi.h>
  5. #include <api/service/services.h>
  6. /*
  7. ====== Usage ======
  8. disp_t: your Dispatchable base class
  9. implt_t: your implementation class
  10. SingletonServiceFactory<disp_t, impl_t> myFactory;
  11. impl_t myImplementation;
  12. //....
  13. //during service registration
  14. myFactory.Register(WASABI_API_SVC, &myImplementation);
  15. //during service deregistration
  16. myFactory.Deregister(WASABI_API_SVC, &myImplementation);
  17. ==== Class requirements ====
  18. your base or implementation class requires the following three static methods
  19. static FOURCC getServiceType(); // return your type (e.g. WaSvc::UNIQUE)... might already be defined in the dispatchable base class
  20. static const char *getServiceName(); // return your service name
  21. static GUID getServiceGuid(); // return your service GUID
  22. */
  23. template <class disp_t, class impl_t>
  24. class SingletonServiceFactory : public waServiceFactory
  25. {
  26. public:
  27. SingletonServiceFactory() : impl(0)
  28. {
  29. }
  30. ~SingletonServiceFactory()
  31. {
  32. }
  33. void Register(api_service *serviceManager, impl_t *new_impl)
  34. {
  35. impl=new_impl;
  36. serviceManager->service_register(this);
  37. }
  38. void Deregister(api_service *serviceManager)
  39. {
  40. if (impl)
  41. {
  42. serviceManager->service_deregister(this);
  43. impl=0;
  44. }
  45. }
  46. private:
  47. FOURCC svc_serviceType() { return impl_t::getServiceType(); }
  48. const char *svc_getServiceName() { return impl_t::getServiceName(); }
  49. GUID svc_getGuid() { return impl_t::getServiceGuid(); } // GUID per service factory, can be INVALID_GUID
  50. void *svc_getInterface(int global_lock = TRUE) { return static_cast<disp_t *>(impl); }
  51. int svc_supportNonLockingGetInterface() { return 1; }
  52. int svc_releaseInterface(void *ifc) { return 0; }
  53. const wchar_t *svc_getTestString() { return 0; }
  54. int svc_notify(int msg, int param1 = 0, int param2 = 0) { return 0; }
  55. private:
  56. impl_t *impl;
  57. protected:
  58. #define CBCLASS SingletonServiceFactory<disp_t, impl_t>
  59. START_DISPATCH_INLINE;
  60. CB(WASERVICEFACTORY_GETSERVICETYPE, svc_serviceType);
  61. CB(WASERVICEFACTORY_GETSERVICENAME, svc_getServiceName);
  62. CB(WASERVICEFACTORY_GETGUID, svc_getGuid);
  63. CB(WASERVICEFACTORY_GETINTERFACE, svc_getInterface);
  64. CB(WASERVICEFACTORY_SUPPORTNONLOCKINGGETINTERFACE, svc_supportNonLockingGetInterface);
  65. CB(WASERVICEFACTORY_RELEASEINTERFACE, svc_releaseInterface);
  66. CB(WASERVICEFACTORY_GETTESTSTRING, svc_getTestString);
  67. CB(WASERVICEFACTORY_SERVICENOTIFY, svc_notify);
  68. END_DISPATCH;
  69. #undef CBCLASS
  70. };
  71. #endif