factoryt.h 2.4 KB

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