preCICE v3.1.2
Loading...
Searching...
No Matches
MultiLock.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <algorithm>
4#include <exception>
5#include <map>
6
7namespace precice {
8namespace utils {
9
11public:
13 : runtime_error("MultiLock") {}
14};
15
17public:
19 const char *what() const noexcept override
20 {
21 return "The multilock does not contain the requested lock!";
22 }
23};
24
26template <typename Key>
27class MultiLock {
28public:
30 using key_type = Key;
31
34
42 void add(Key name, bool state)
43 {
44 _locks.emplace(std::move(name), state);
45 }
46
51 template <typename K>
52 void lock(const K &name)
53 {
54 auto iter = _locks.find(name);
55 if (iter == _locks.end()) {
57 } else {
58 iter->second = true;
59 }
60 }
61
63 void lockAll() noexcept
64 {
65 for (auto &kl : _locks) {
66 kl.second = true;
67 }
68 }
69
74 template <typename K>
75 void unlock(const K &name)
76 {
77 auto iter = _locks.find(name);
78 if (iter == _locks.end()) {
80 } else {
81 iter->second = false;
82 }
83 }
84
86 void unlockAll() noexcept
87 {
88 for (auto &kl : _locks) {
89 kl.second = false;
90 }
91 }
92
94 void clear() noexcept
95 {
96 _locks.clear();
97 }
98
105 template <typename K>
106 bool check(const K &name) const
107 {
108 auto iter = _locks.find(name);
109 if (iter == _locks.end()) {
110 throw LockNotFoundException{};
111 } else {
112 return iter->second;
113 }
114 }
115
117 bool checkAll() const noexcept
118 {
119 using KL = typename decltype(_locks)::value_type;
120 return std::all_of(_locks.begin(), _locks.end(), [](const KL &kl) {
121 return kl.second;
122 });
123 }
124
131 template <typename K>
132 bool contains(const K &name) const noexcept
133 {
134 return _locks.find(name) != _locks.end();
135 }
136
138 size_type size() const noexcept
139 {
140 return _locks.size();
141 }
142
144 size_t countLocked() const
145 {
146 return std::count_if(_locks.begin(), _locks.end(), [](typename map_type::value_type const &kv) { return kv.second; });
147 }
148
150 size_t countUnlocked() const
151 {
152 return std::count_if(_locks.begin(), _locks.end(), [](typename map_type::value_type const &kv) { return not kv.second; });
153 }
154
155private:
157
160};
161} // namespace utils
162} // namespace precice
std::string name
T all_of(T... args)
const char * what() const noexcept override
Definition MultiLock.hpp:19
Class handling multiple locks allowing global lock and unlock operations.
Definition MultiLock.hpp:27
size_t countLocked() const
Returns the count of locked locks.
bool check(const K &name) const
Checks the status of a lock.
Key key_type
The type of the key.
Definition MultiLock.hpp:30
size_t countUnlocked() const
Returns the count of unlocked locks.
void add(Key name, bool state)
Adds a lock with a given state.
Definition MultiLock.hpp:42
void lock(const K &name)
Locks a given lock.
Definition MultiLock.hpp:52
void clear() noexcept
Removes all known locks.
Definition MultiLock.hpp:94
void lockAll() noexcept
Locks all known locks.
Definition MultiLock.hpp:63
void unlockAll() noexcept
Unlocks all known locks.
Definition MultiLock.hpp:86
bool checkAll() const noexcept
Checks whether all locks are locked.
typename std::map< Key, bool, std::less<> > map_type
size_type size() const noexcept
Returns the total count of locks.
map_type _locks
The map that keeps track of the locks and their state.
bool contains(const K &name) const noexcept
Checks whether a lock is known.
void unlock(const K &name)
Unlocks a given lock.
Definition MultiLock.hpp:75
T count_if(T... args)
Main namespace of the precice library.