WeakReferenceDictionary.h 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. #pragma once
  6. namespace JsUtil
  7. {
  8. interface IWeakReferenceDictionary
  9. {
  10. virtual void Cleanup() = 0;
  11. };
  12. template <
  13. class TKey,
  14. class TValue,
  15. class SizePolicy = PowerOf2SizePolicy,
  16. template <typename ValueOrKey> class Comparer = DefaultComparer
  17. >
  18. class WeakReferenceDictionary: public BaseDictionary<TKey, RecyclerWeakReference<TValue>*, RecyclerNonLeafAllocator, SizePolicy, Comparer, WeakRefValueDictionaryEntry>,
  19. public IWeakReferenceDictionary
  20. {
  21. public:
  22. WeakReferenceDictionary(Recycler* recycler, int capacity = 0):
  23. BaseDictionary(recycler, capacity)
  24. {
  25. Assert(reinterpret_cast<void*>(this) == reinterpret_cast<void*>((IWeakReferenceDictionary*) this));
  26. }
  27. virtual void Cleanup() override
  28. {
  29. this->MapAndRemoveIf([](EntryType& entry)
  30. {
  31. return (EntryType::NeedsCleanup(entry));
  32. });
  33. }
  34. private:
  35. using BaseDictionary::Clone;
  36. using BaseDictionary::Copy;
  37. PREVENT_COPY(WeakReferenceDictionary);
  38. };
  39. };