idisposable.cpp 581 B

123456789101112131415161718192021222324252627282930313233
  1. #include "idisposable.h"
  2. #include "Debug.h"
  3. idisposable::idisposable()
  4. {
  5. usage = 1;
  6. }
  7. idisposable::~idisposable()
  8. {
  9. // All the work happens in the derived destructor.
  10. }
  11. void idisposable::dereference(idisposable **object_ptr)
  12. {
  13. DEBUG_FUNCTION
  14. DEBUG_LINE
  15. if ((object_ptr == nullptr) || ((*object_ptr) == nullptr)) {
  16. DEBUG_LINE
  17. return; // The object is already deleted.
  18. }
  19. DEBUG_LINE
  20. idisposable *item;
  21. item = *object_ptr;
  22. DEBUG_LINE
  23. item->usage--;
  24. DEBUG_LINE
  25. if (item->usage <= 0) {
  26. DEBUG_LINE
  27. delete item;
  28. }
  29. DEBUG_LINE
  30. (*object_ptr) = nullptr;
  31. DEBUG_LINE
  32. }