DotNetDupe 4.0.6
C++17/20 Implementation of the .NET Base Class Library (BCL)
Loading...
Searching...
No Matches
ConcurrentDictionary.h
Go to the documentation of this file.
1#pragma once
2
3#include "Common.h"
4#include "System/Object.h"
5#include "System/Array.h"
10
11namespace DotNetDupe {
12 namespace System {
13 namespace Collections {
14 namespace Concurrent {
15
23 template <typename TKey, typename TValue>
25 private:
26 mutable Threading::CriticalSection m_csLock;
28
29 public:
32
35 Clear();
36 }
37
42 bool TryAdd(const TKey& key, const TValue& value) {
43 Threading::CriticalSectionLock lock(m_csLock);
44 if (m_dict.ContainsKey(key)) {
45 return false;
46 }
47 m_dict.Add(key, value);
48 return true;
49 }
50
55 bool TryGetValue(const TKey& key, TValue& value) const {
56 Threading::CriticalSectionLock lock(m_csLock);
57 return m_dict.TryGetValue(key, value);
58 }
59
60 bool TryRemove(const TKey& key, TValue& value) {
61 Threading::CriticalSectionLock lock(m_csLock);
62 if (m_dict.TryGetValue(key, value)) {
63 m_dict.Remove(key);
64 return true;
65 }
66 return false;
67 }
68
69 bool ContainsKey(const TKey& key) const {
70 Threading::CriticalSectionLock lock(m_csLock);
71 return m_dict.ContainsKey(key);
72 }
73
74 void Clear() {
75 Threading::CriticalSectionLock lock(m_csLock);
76 m_dict.Clear();
77 }
78
79 int GetCount() const {
80 Threading::CriticalSectionLock lock(m_csLock);
81 return m_dict.GetCount();
82 }
83
84 bool IsEmpty() const {
85 Threading::CriticalSectionLock lock(m_csLock);
86 return m_dict.GetCount() == 0;
87 }
88
89 TValue GetOrAdd(const TKey& key, const TValue& value) {
90 Threading::CriticalSectionLock lock(m_csLock);
91 TValue existingVal;
92 if (m_dict.TryGetValue(key, existingVal)) {
93 return existingVal;
94 }
95 m_dict.Add(key, value);
96 return value;
97 }
98
99 template <typename F>
100 TValue GetOrAdd(const TKey& key, F valueFactory) {
101 Threading::CriticalSectionLock lock(m_csLock);
102 TValue existingVal;
103 if (m_dict.TryGetValue(key, existingVal)) {
104 return existingVal;
105 }
106 TValue val = valueFactory(key);
107 m_dict.Add(key, val);
108 return val;
109 }
110
111 TValue AddOrUpdate(const TKey& key, const TValue& addValue, const TValue& updateValue) {
112 Threading::CriticalSectionLock lock(m_csLock);
113 if (m_dict.ContainsKey(key)) {
114 m_dict[key] = updateValue;
115 return updateValue;
116 }
117 m_dict.Add(key, addValue);
118 return addValue;
119 }
120
121 TValue& operator[](const TKey& key) {
122 Threading::CriticalSectionLock lock(m_csLock);
123 // Be careful returning a reference while lock is released.
124 // The user must synchronize external accesses to this reference.
125 return m_dict[key];
126 }
127
129 Threading::CriticalSectionLock lock(m_csLock);
130 return m_dict.GetKeys();
131 }
132
134 Threading::CriticalSectionLock lock(m_csLock);
135 return m_dict.GetValues();
136 }
137 };
138
139 }
140 }
141 }
142}
Defines the exception thrown when an invalid argument is provided to a method.
Provides methods for creating, manipulating, searching, and sorting arrays.
Defines common cross-platform macros, export decorators, and fundamental types.
Provides a re-entrant mutual exclusion primitive for thread synchronization.
Provides an RAII-style scoped lock wrapper around synchronization primitives.
Base object class for DotNetDupe mirroring .NET System.Object.
Provides methods for creating, manipulating, searching, and sorting arrays, thereby serving as the ba...
Definition Array.h:29
ConcurrentDictionary()=default
Initializes a new instance of the ConcurrentDictionary class that is empty.
bool TryAdd(const TKey &key, const TValue &value)
Attempts to add the specified key and value to the ConcurrentDictionary.
bool TryGetValue(const TKey &key, TValue &value) const
Attempts to get the value associated with the specified key from the ConcurrentDictionary.
TValue AddOrUpdate(const TKey &key, const TValue &addValue, const TValue &updateValue)
~ConcurrentDictionary() override
Destructor. Clears dictionary contents.
Represents a collection of keys and values.
Definition Dictionary.h:66
Supports all classes in the DotNetDupe class hierarchy.
Definition Object.h:18
Provides a re-entrant mutual exclusion primitive for thread synchronization.
Lock< CriticalSection > CriticalSectionLock
Convenience alias for Lock<CriticalSection>.
Definition Lock.h:71