1
0

singleton.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #pragma once
  2. #include "service/ifc_servicefactory.h"
  3. /*
  4. ====== Usage ======
  5. disp_t: your Dispatchable base class
  6. implt_t: your implementation class
  7. SingletonServiceFactory<disp_t, impl_t> myFactory;
  8. impl_t myImplementation;
  9. //....
  10. //during service registration
  11. myFactory.Register(WASABI2_API_SVC, &myImplementation);
  12. //during service deregistration
  13. myFactory.Deregister(WASABI2_API_SVC, &myImplementation);
  14. ==== Class requirements ====
  15. your base or implementation class requires the following three static methods
  16. static FOURCC getServiceType(); // return your type (e.g. WaSvc::UNIQUE)... might already be defined in the dispatchable base class
  17. static const char *getServiceName(); // return your service name
  18. static GUID getServiceGuid(); // return your service GUID
  19. */
  20. class WasabiServiceFactory
  21. {
  22. public:
  23. virtual ~WasabiServiceFactory() {}
  24. virtual void Register(api_service *serviceManager)=0;
  25. virtual void Deregister(api_service *serviceManager)=0;
  26. };
  27. template <class impl_t, class disp_t>
  28. class SingletonServiceFactory : public ifc_serviceFactory
  29. {
  30. public:
  31. SingletonServiceFactory() : impl(0)
  32. {
  33. }
  34. ~SingletonServiceFactory()
  35. {
  36. }
  37. void Register(api_service *serviceManager, impl_t *new_impl)
  38. {
  39. impl=new_impl;
  40. serviceManager->Register(this);
  41. }
  42. void Deregister(api_service *serviceManager)
  43. {
  44. if (impl)
  45. {
  46. serviceManager->Unregister(this);
  47. impl=0;
  48. }
  49. }
  50. private:
  51. GUID WASABICALL ServiceFactory_GetServiceType() { return impl_t::GetServiceType(); }
  52. nx_string_t WASABICALL ServiceFactory_GetServiceName() { return impl_t::GetServiceName(); }
  53. GUID WASABICALL ServiceFactory_GetGUID() { return impl_t::GetServiceGUID(); } // GUID per service factory, can be INVALID_GUID
  54. void *WASABICALL ServiceFactory_GetInterface() { return static_cast<disp_t *>(impl); }
  55. int WASABICALL ServiceFactory_ServiceNotify(int msg, intptr_t param1 = 0, intptr_t param2 = 0) { return 0; }
  56. private:
  57. impl_t *impl;
  58. };
  59. /* same as above, but this one also constructs the implementation object itself
  60. useful when:
  61. 1) you don't need to ever access the implementation object yourself
  62. 2) the implementation class requires no constructor parameters
  63. TODO: might want to change this to "new" the object during Register and "delete" during Deregister,
  64. in case destruction during program termination isn't safe.
  65. */
  66. template <class impl_t, class disp_t>
  67. class SingletonService : public ifc_serviceFactory, public WasabiServiceFactory
  68. {
  69. public:
  70. SingletonService()
  71. {
  72. }
  73. ~SingletonService()
  74. {
  75. }
  76. void Register(api_service *serviceManager)
  77. {
  78. serviceManager->Register(this);
  79. }
  80. void Deregister(api_service *serviceManager)
  81. {
  82. serviceManager->Unregister(this);
  83. }
  84. private:
  85. GUID WASABICALL ServiceFactory_GetServiceType() { return impl_t::GetServiceType(); }
  86. nx_string_t WASABICALL ServiceFactory_GetServiceName() { return impl_t::GetServiceName(); }
  87. GUID WASABICALL ServiceFactory_GetGUID() { return impl_t::GetServiceGUID(); } // GUID per service factory, can be INVALID_GUID
  88. void *WASABICALL ServiceFactory_GetInterface() { return static_cast<disp_t *>(&impl); }
  89. int WASABICALL ServiceFactory_ServiceNotify(int msg, intptr_t param1 = 0, intptr_t param2 = 0) { return 0; }
  90. private:
  91. impl_t impl;
  92. };