WeakReferenceDictionary.h 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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. typedef BaseDictionary<TKey, RecyclerWeakReference<TValue>*, RecyclerNonLeafAllocator, SizePolicy, Comparer, WeakRefValueDictionaryEntry> Base;
  22. typedef typename Base::EntryType EntryType;
  23. public:
  24. WeakReferenceDictionary(Recycler* recycler, int capacity = 0):
  25. Base(recycler, capacity)
  26. {
  27. Assert(reinterpret_cast<void*>(this) == reinterpret_cast<void*>((IWeakReferenceDictionary*) this));
  28. }
  29. virtual void Cleanup() override
  30. {
  31. this->MapAndRemoveIf([](typename Base::EntryType &entry)
  32. {
  33. return (Base::EntryType::NeedsCleanup(entry));
  34. });
  35. }
  36. private:
  37. using Base::Clone;
  38. using Base::Copy;
  39. PREVENT_COPY(WeakReferenceDictionary);
  40. };
  41. };