1
0

waservicefactorytsingle.h 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. #ifndef __WASERVICEFACTORYTSINGLE_IMPL_H
  2. #define __WASERVICEFACTORYTSINGLE_IMPL_H
  3. /*<?<autoheader/>*/
  4. /*?>*/
  5. #include "waservicefactorybase.h"
  6. #include <bfc/bfc_assert.h>
  7. // this is a service factory template that holds one copy of a class
  8. // and reissues the pointer as needed, with reference counting
  9. template <class SERVICETYPE, class SERVICE>
  10. class waServiceFactoryTSingle : public waServiceFactoryBase<SERVICETYPE, SERVICE> {
  11. public:
  12. waServiceFactoryTSingle(GUID myGuid = INVALID_GUID) :
  13. refcount(0), waServiceFactoryBase<SERVICETYPE, SERVICE>(myGuid) {
  14. singleService = new SERVICE;
  15. }
  16. waServiceFactoryTSingle(SERVICE *svc, GUID myGuid = INVALID_GUID) :
  17. singleService(svc), refcount(0),
  18. waServiceFactoryBase<SERVICETYPE, SERVICE>(myGuid) { }
  19. ~waServiceFactoryTSingle() {
  20. delete singleService;
  21. }
  22. virtual SERVICETYPE *newService() {
  23. ASSERT(singleService != NULL);
  24. refcount++;
  25. return singleService;
  26. }
  27. virtual int delService(SERVICETYPE *service) {
  28. ASSERT(static_cast<SERVICE*>(service) == singleService);
  29. refcount--;
  30. ASSERT(refcount >= 0);
  31. return 1;
  32. }
  33. SERVICE *getSingleService() { return singleService; }
  34. private:
  35. SERVICE * singleService;
  36. int refcount;
  37. };
  38. #endif // __WASERVICEFACTORYTSINGLE_IMPL_H