servicei.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. #ifndef _SERVICEI_H
  2. #define _SERVICEI_H
  3. // here is where we define some helper implementations of the service factory
  4. // interface
  5. #include "service.h"
  6. #include "waservicefactoryi.h"
  7. #include "waservicefactorybase.h"
  8. #include "waservicefactoryt.h"
  9. #include "waservicefactorytsingle.h"
  10. #include <bfc/ptrlist.h>
  11. #include <bfc/foreach.h>
  12. // DEPRECATED
  13. #define waService waServiceFactory
  14. #define waServiceT waServiceFactoryT
  15. #define waServiceTSingle waServiceFactoryTSingle
  16. // Declaring services via macros
  17. // ie:
  18. // BEGIN_SERVICES(YourClass_Svc)
  19. // DECLARE_SERVICE(SomeSvcCreator<YourServiceClass1>);
  20. // DECLARE_SERVICE(SomeSvcCreator<YourServiceClass2>);
  21. // DECLARE_SERVICE(SomeSvcCreator<YourServiceClass3>);
  22. // DECLARE_SERVICETMULTI(svc_serviceclass, YourMultiInstanceServiceClass1>);
  23. // DECLARE_SERVICETMULTI(svc_serviceclass, YourMultiInstanceServiceClass2>);
  24. // DECLARE_SERVICETMULTI(svc_serviceclass, YourMultiInstanceServiceClass3>);
  25. // DECLARE_SERVICETSINGLE(svc_serviceclass, YourSingleInstanceServiceClass1>);
  26. // DECLARE_SERVICETSINGLE(svc_serviceclass, YourSingleInstanceServiceClass2>);
  27. // DECLARE_SERVICETSINGLE(svc_serviceclass, YourSingleInstanceServiceClass3>);
  28. // END_SERVICES(YourClass_Svc, _YourClass_Svc);
  29. // The macro DECLARE_MODULE_SVCMGR should be defined in your main cpp file if you are compiling
  30. // a standalone exe without using the Application class. In this case you should also add
  31. // MODULE_SVCMGR_ONINIT(); in your app startup and MODULE_SVCMGR_ONSHUTDOWN() in your app shutdown
  32. // (class Application does this already)
  33. // You can use BEGIN_SYS_SERVICES to declare a service that is initialized in priority and
  34. // shutdown late (normal version inits late, shutdowns early)
  35. class StaticServices {
  36. public:
  37. virtual void registerServices()=0;
  38. virtual void unregisterServices()=0;
  39. };
  40. class AutoServiceList : public PtrList<StaticServices> {
  41. public:
  42. AutoServiceList()
  43. {
  44. #ifdef _WIN32 // PORT ME
  45. OutputDebugStringA(">\n");
  46. #endif
  47. }
  48. };
  49. class StaticServiceMgr {
  50. public:
  51. AutoServiceList __m_modules;
  52. AutoServiceList __m_modules2;
  53. };
  54. #define DECLARE_MODULE_SVCMGR StaticServiceMgr *staticServiceMgr = NULL;
  55. extern StaticServiceMgr *staticServiceMgr;
  56. #define MODULE_SVCMGR_ONINIT() { \
  57. foreach(staticServiceMgr->__m_modules) \
  58. staticServiceMgr->__m_modules.getfor()->registerServices(); \
  59. endfor; \
  60. }
  61. #define MODULE_SVCMGR_ONINIT2() { \
  62. foreach(staticServiceMgr->__m_modules2) \
  63. staticServiceMgr->__m_modules2.getfor()->registerServices(); \
  64. endfor; \
  65. }
  66. #define MODULE_SVCMGR_ONSHUTDOWN() { \
  67. foreach(staticServiceMgr->__m_modules) \
  68. staticServiceMgr->__m_modules.getfor()->unregisterServices(); \
  69. endfor; \
  70. }
  71. #define MODULE_SVCMGR_ONSHUTDOWN2() { \
  72. foreach(staticServiceMgr->__m_modules2) \
  73. staticServiceMgr->__m_modules2.getfor()->unregisterServices(); \
  74. endfor; \
  75. }
  76. #define INIT_MODULE_SVCMGR() \
  77. if (!staticServiceMgr) staticServiceMgr = new StaticServiceMgr;
  78. #define BEGIN_SERVICES(CLASSNAME) \
  79. class CLASSNAME : public StaticServices { \
  80. public: \
  81. CLASSNAME() { \
  82. INIT_MODULE_SVCMGR() \
  83. staticServiceMgr->__m_modules.addItem(this); \
  84. } \
  85. virtual ~CLASSNAME() {} \
  86. virtual void registerServices() { \
  87. ASSERT(staticServiceMgr);
  88. #define BEGIN_SERVICES_DEBUG(CLASSNAME, TEXT) \
  89. class CLASSNAME : public StaticServices { \
  90. public: \
  91. CLASSNAME() { \
  92. INIT_MODULE_SVCMGR() \
  93. OutputDebugString("Registering "); \
  94. OutputDebugString(TEXT); \
  95. OutputDebugString("\n"); \
  96. staticServiceMgr->__m_modules.addItem(this); \
  97. } \
  98. virtual ~CLASSNAME() {} \
  99. virtual void registerServices() { \
  100. ASSERT(staticServiceMgr);
  101. #define BEGIN_SYS_SERVICES(CLASSNAME) \
  102. class CLASSNAME : public StaticServices { \
  103. public: \
  104. CLASSNAME() { \
  105. INIT_MODULE_SVCMGR() \
  106. staticServiceMgr->__m_modules2.addItem(this); \
  107. } \
  108. virtual ~CLASSNAME() {} \
  109. virtual void registerServices() {
  110. #define DECLARE_SERVICE(INSTANTIATIONCODE) \
  111. registerService(new INSTANTIATIONCODE);
  112. #define DECLARE_SERVICE_DEBUG(INSTANTIATIONCODE, TEXT) \
  113. OutputDebugString("Starting "); \
  114. OutputDebugString(TEXT); \
  115. OutputDebugString("\n"); \
  116. registerService(new INSTANTIATIONCODE);
  117. #define DECLARE_SERVICETMULTI(SVCTYPE, SVCCLASS) \
  118. registerService(new waServiceFactoryT<SVCTYPE, SVCCLASS>);
  119. #define DECLARE_SERVICETMULTI_DEBUG(SVCTYPE, SVCCLASS, TEXT) \
  120. OutputDebugString("Starting "); \
  121. OutputDebugString(TEXT); \
  122. OutputDebugString("\n"); \
  123. registerService(new waServiceFactoryT<SVCTYPE, SVCCLASS>);
  124. #define DECLARE_SERVICETSINGLE(SVCTYPE, SVCCLASS) \
  125. registerService(new waServiceFactoryTSingle<SVCTYPE, SVCCLASS>);
  126. #define DECLARE_SERVICETSINGLE_DEBUG(SVCTYPE, SVCCLASS, TEXT) \
  127. OutputDebugString("Starting "); \
  128. OutputDebugString(TEXT); \
  129. OutputDebugString("\n"); \
  130. registerService(new waServiceFactoryTSingle<SVCTYPE, SVCCLASS>, TEXT);
  131. #define END_SERVICES(CLASSNAME, INSTANCENAME) \
  132. } \
  133. virtual void unregisterServices() { \
  134. foreach(services) \
  135. waServiceFactoryI *svc = services.getfor(); \
  136. WASABI_API_SVC->service_deregister(svc); \
  137. delete svc; \
  138. endfor; \
  139. services.removeAll();\
  140. } \
  141. private: \
  142. void registerService(waServiceFactoryI *svc) { \
  143. if (svc != NULL) { \
  144. services.addItem(svc); \
  145. WASABI_API_SVC->service_register(svc); \
  146. } \
  147. } \
  148. PtrList<waServiceFactoryI> services; \
  149. }; \
  150. CLASSNAME INSTANCENAME;
  151. #endif