DotNetDupe
4.0.6
C++17/20 Implementation of the .NET Base Class Library (BCL)
Toggle main menu visibility
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
"
6
#include "
System/ArgumentException.h
"
7
#include "
System/Collections/Generic/Dictionary.h
"
8
#include "
System/Threading/CriticalSection.h
"
9
#include "
System/Threading/Lock.h
"
10
11
namespace
DotNetDupe
{
12
namespace
System
{
13
namespace
Collections
{
14
namespace
Concurrent
{
15
23
template
<
typename
TKey,
typename
TValue>
24
class
ConcurrentDictionary
:
public
Object
{
25
private
:
26
mutable
Threading::CriticalSection
m_csLock;
27
Generic::Dictionary<TKey, TValue>
m_dict;
28
29
public
:
31
ConcurrentDictionary
() =
default
;
32
34
~ConcurrentDictionary
()
override
{
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
128
Array<TKey>
GetKeys
()
const
{
129
Threading::CriticalSectionLock
lock(m_csLock);
130
return
m_dict.GetKeys();
131
}
132
133
Array<TValue>
GetValues
()
const
{
134
Threading::CriticalSectionLock
lock(m_csLock);
135
return
m_dict.GetValues();
136
}
137
};
138
139
}
140
}
141
}
142
}
ArgumentException.h
Defines the exception thrown when an invalid argument is provided to a method.
Array.h
Provides methods for creating, manipulating, searching, and sorting arrays.
Common.h
Defines common cross-platform macros, export decorators, and fundamental types.
CriticalSection.h
Provides a re-entrant mutual exclusion primitive for thread synchronization.
Dictionary.h
Lock.h
Provides an RAII-style scoped lock wrapper around synchronization primitives.
Object.h
Base object class for DotNetDupe mirroring .NET System.Object.
DotNetDupe::System::Array
Provides methods for creating, manipulating, searching, and sorting arrays, thereby serving as the ba...
Definition
Array.h:29
DotNetDupe::System::Collections::Concurrent::ConcurrentDictionary::ConcurrentDictionary
ConcurrentDictionary()=default
Initializes a new instance of the ConcurrentDictionary class that is empty.
DotNetDupe::System::Collections::Concurrent::ConcurrentDictionary::operator[]
TValue & operator[](const TKey &key)
Definition
ConcurrentDictionary.h:121
DotNetDupe::System::Collections::Concurrent::ConcurrentDictionary::TryAdd
bool TryAdd(const TKey &key, const TValue &value)
Attempts to add the specified key and value to the ConcurrentDictionary.
Definition
ConcurrentDictionary.h:42
DotNetDupe::System::Collections::Concurrent::ConcurrentDictionary::GetOrAdd
TValue GetOrAdd(const TKey &key, F valueFactory)
Definition
ConcurrentDictionary.h:100
DotNetDupe::System::Collections::Concurrent::ConcurrentDictionary::TryRemove
bool TryRemove(const TKey &key, TValue &value)
Definition
ConcurrentDictionary.h:60
DotNetDupe::System::Collections::Concurrent::ConcurrentDictionary::TryGetValue
bool TryGetValue(const TKey &key, TValue &value) const
Attempts to get the value associated with the specified key from the ConcurrentDictionary.
Definition
ConcurrentDictionary.h:55
DotNetDupe::System::Collections::Concurrent::ConcurrentDictionary::Clear
void Clear()
Definition
ConcurrentDictionary.h:74
DotNetDupe::System::Collections::Concurrent::ConcurrentDictionary::ContainsKey
bool ContainsKey(const TKey &key) const
Definition
ConcurrentDictionary.h:69
DotNetDupe::System::Collections::Concurrent::ConcurrentDictionary::IsEmpty
bool IsEmpty() const
Definition
ConcurrentDictionary.h:84
DotNetDupe::System::Collections::Concurrent::ConcurrentDictionary::GetKeys
Array< TKey > GetKeys() const
Definition
ConcurrentDictionary.h:128
DotNetDupe::System::Collections::Concurrent::ConcurrentDictionary::GetValues
Array< TValue > GetValues() const
Definition
ConcurrentDictionary.h:133
DotNetDupe::System::Collections::Concurrent::ConcurrentDictionary::AddOrUpdate
TValue AddOrUpdate(const TKey &key, const TValue &addValue, const TValue &updateValue)
Definition
ConcurrentDictionary.h:111
DotNetDupe::System::Collections::Concurrent::ConcurrentDictionary::GetOrAdd
TValue GetOrAdd(const TKey &key, const TValue &value)
Definition
ConcurrentDictionary.h:89
DotNetDupe::System::Collections::Concurrent::ConcurrentDictionary::GetCount
int GetCount() const
Definition
ConcurrentDictionary.h:79
DotNetDupe::System::Collections::Concurrent::ConcurrentDictionary::~ConcurrentDictionary
~ConcurrentDictionary() override
Destructor. Clears dictionary contents.
Definition
ConcurrentDictionary.h:34
DotNetDupe::System::Collections::Generic::Dictionary
Represents a collection of keys and values.
Definition
Dictionary.h:66
DotNetDupe::System::Object
Supports all classes in the DotNetDupe class hierarchy.
Definition
Object.h:18
DotNetDupe::System::Threading::CriticalSection
Provides a re-entrant mutual exclusion primitive for thread synchronization.
Definition
CriticalSection.h:19
DotNetDupe::System::Collections::Concurrent
Definition
BlockingCollection.h:16
DotNetDupe::System::Collections
Definition
BlockingCollection.h:15
DotNetDupe::System::Threading::CriticalSectionLock
Lock< CriticalSection > CriticalSectionLock
Convenience alias for Lock<CriticalSection>.
Definition
Lock.h:71
DotNetDupe::System
Definition
Action.h:11
DotNetDupe
Definition
IServiceCollection.h:7
Include
System
Collections
Concurrent
ConcurrentDictionary.h
Generated by
1.18.0