ListedRefCounted.h 650 B

123456789101112131415161718192021222324252627282930
  1. /*
  2. * Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/RefCounted.h>
  8. namespace Kernel {
  9. template<typename T>
  10. class ListedRefCounted : public RefCountedBase {
  11. public:
  12. bool unref() const
  13. {
  14. bool did_hit_zero = T::all_instances().with([&](auto& list) {
  15. if (deref_base())
  16. return false;
  17. list.remove(const_cast<T&>(static_cast<T const&>(*this)));
  18. return true;
  19. });
  20. if (did_hit_zero)
  21. delete const_cast<T*>(static_cast<T const*>(this));
  22. return did_hit_zero;
  23. }
  24. };
  25. }