refcount.h 656 B

123456789101112131415161718192021
  1. #pragma once
  2. #define WIN32_LEAN_AND_MEAN
  3. #include <windows.h>
  4. template <class ifc_t>
  5. class Countable : public ifc_t
  6. {
  7. public:
  8. Countable()
  9. {
  10. ref_count=1;
  11. }
  12. // this needs to be done like this otherwise the destructor doesn't get called properly (we don't want virtual destructor for various reasons)
  13. #define REFERENCE_COUNT_IMPLEMENTATION size_t AddRef() { return InterlockedIncrement((LONG*)&ref_count); }\
  14. size_t Release() { if (!ref_count) return ref_count; LONG r = InterlockedDecrement((LONG*)&ref_count); if (!r) delete(this); return r; }
  15. protected:
  16. size_t ref_count;
  17. };
  18. #define REFERENCE_COUNTED CB(ADDREF, AddRef); CB(RELEASE, Release);