preCICE v3.2.0
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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::utils {
8
10public:
12 : runtime_error("MultiLock") {}
13};
14
16public:
18 const char *what() const noexcept override
19 {
20 return "The multilock does not contain the requested lock!";
21 }
22};
23
25template <typename Key>
26class MultiLock {
27public:
29 using key_type = Key;
30
33
41 void add(Key name, bool state)
42 {
43 _locks.emplace(std::move(name), state);
44 }
45
50 template <typename K>
51 void lock(const K &name)
52 {
53 auto iter = _locks.find(name);
54 if (iter == _locks.end()) {
56 } else {
57 iter->second = true;
58 }
59 }
60
62 void lockAll() noexcept
63 {
64 for (auto &kl : _locks) {
65 kl.second = true;
66 }
67 }
68
73 template <typename K>
74 void unlock(const K &name)
75 {
76 auto iter = _locks.find(name);
77 if (iter == _locks.end()) {
79 } else {
80 iter->second = false;
81 }
82 }
83
85 void unlockAll() noexcept
86 {
87 for (auto &kl : _locks) {
88 kl.second = false;
89 }
90 }
91
93 void clear() noexcept
94 {
95 _locks.clear();
96 }
97
104 template <typename K>
105 bool check(const K &name) const
106 {
107 auto iter = _locks.find(name);
108 if (iter == _locks.end()) {
109 throw LockNotFoundException{};
110 } else {
111 return iter->second;
112 }
113 }
114
116 bool checkAll() const noexcept
117 {
118 using KL = typename decltype(_locks)::value_type;
119 return std::all_of(_locks.begin(), _locks.end(), [](const KL &kl) {
120 return kl.second;
121 });
122 }
123
130 template <typename K>
131 bool contains(const K &name) const noexcept
132 {
133 return _locks.find(name) != _locks.end();
134 }
135
137 size_type size() const noexcept
138 {
139 return _locks.size();
140 }
141
143 size_t countLocked() const
144 {
145 return std::count_if(_locks.begin(), _locks.end(), [](typename map_type::value_type const &kv) { return kv.second; });
146 }
147
149 size_t countUnlocked() const
150 {
151 return std::count_if(_locks.begin(), _locks.end(), [](typename map_type::value_type const &kv) { return not kv.second; });
152 }
153
154private:
156
159};
160} // namespace precice::utils
std::string name
T all_of(T... args)
const char * what() const noexcept override
Definition MultiLock.hpp:18
Class handling multiple locks allowing global lock and unlock operations.
Definition MultiLock.hpp:26
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:29
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:41
void lock(const K &name)
Locks a given lock.
Definition MultiLock.hpp:51
void clear() noexcept
Removes all known locks.
Definition MultiLock.hpp:93
void lockAll() noexcept
Locks all known locks.
Definition MultiLock.hpp:62
void unlockAll() noexcept
Unlocks all known locks.
Definition MultiLock.hpp:85
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:74
T count_if(T... args)
contains precice-related utilities.