6 #include "Threading/Mutex/Mutex.h"
7 #include "Core/Exceptions.h"
13 #define MutexVectorLockGuard(x) RecursiveLockGuard(x.mutex())
19 #define MutexVectorLock(x) RecursiveLockGuard(x.mutex().lock())
24 #define MutexVectorUnlock(x) RecursiveLockGuard(x.mutex().unlock())
66 #define InClassLock() RecursiveLockGuard(this->m_oMutex)
96 typedef std::function<bool(
const T &)> FnMatch;
102 bool fWasSuccessful =
false;
105 for (
size_t i = 0, nSize = this->
size(); i < nSize;) {
106 if (i_fnMatch(this->m_atVector[i])) {
109 fWasSuccessful =
true;
115 return fWasSuccessful;
123 for (
size_t i = 0, nSize = this->
size(); i < nSize; i++) {
124 if (i_fnMatch(this->m_atVector[i])) {
144 T
find(FnMatch i_fnMatch)
const {
146 for (
size_t i = 0, nSize = this->
size(); i < nSize; i++) {
147 if (i_fnMatch(this->m_atVector[i])) {
148 return this->m_atVector[i];
152 throw NoEntryFoundException;
159 T
get(FnMatch i_fnMatch)
const {
160 return this->
find(i_fnMatch);
166 std::vector<T>
findAll(FnMatch i_fnMatch)
const {
167 std::vector<T> atData;
170 for (
size_t i = 0, nSize = this->
size(); i < nSize; i++) {
171 if (i_fnMatch(this->m_atVector[i])) {
172 atData.push_back(this->m_atVector[i]);
182 std::vector<T>
getAll(FnMatch i_fnMatch)
const {
183 return this->
findAll(i_fnMatch);
192 for (
size_t i = 0, nSize = this->
size(); i < nSize; i++) {
193 if (i_fnMatch(this->m_atVector[i])) {
213 std::vector<size_t> anIndexes;
216 for (
size_t i = 0, nSize = this->
size(); i < nSize; i++) {
217 if (i_fnMatch(this->m_atVector[i])) {
218 anIndexes.push_back(i);
236 T
take(FnMatch i_fnMatch)
const {
238 for (
size_t i = 0, nSize = this->
size(); i < nSize; i++) {
239 if (i_fnMatch(this->m_atVector[i])) {
240 T tData = this->m_atVector[i];
246 throw NoEntryFoundException;
252 std::vector<T>
takeAll(FnMatch i_fnMatch)
const {
253 std::vector<T> atData;
256 for (
size_t i = 0, nSize = this->
size(); i < nSize;) {
257 if (i_fnMatch(this->m_atVector[i])) {
258 atData.push_back(this->m_atVector[i]);
273 return this->m_oMutex;
279 return this->m_atVector;
285 return this->m_atVector.begin();
291 return this->m_atVector.end();
297 return this->m_atVector.cbegin();
303 return this->m_atVector.cend();
309 return this->m_atVector.size();
315 return this->m_atVector.empty();
321 return *this->m_atVector.begin();
327 return *(this->
end() - 1);
333 T &
at(
const size_t i_nIndex) {
340 return this->m_atVector;
348 for (
const T &tValue: this->m_atVector) {
349 if (tValue == i_oValue) {
362 return this->m_atVector.resize(i_nSize);
369 return this->m_atVector.reserve(i_nSize);
376 return this->m_atVector.push_back(i_oValue);
383 return this->m_atVector.push_back(std::move(i_oValue));
389 for (
const T &oItem: i_atValue) {
397 for (
const T &oItem: i_atValue) {
405 MutexVectorLockGuard(i_atValue);
406 return this->
push_back(i_atValue.rawVector());
412 return this->
push_back(std::move(i_atValue.rawVector()));
421 if (i_nIndex > this->
size()) {
425 this->m_atVector.erase(this->
begin() + i_nIndex);
435 for (
size_t i = 0, nSize = this->
size(); i < nSize; i++) {
436 if (this->m_atVector[i] == i_oValue) {
447 this->m_atVector.clear();
456 this->m_atVector = i_tOther;
465 this->m_atVector = std::move(i_tOther);
474 MutexVectorLockGuard(i_tOther);
485 MutexVectorLockGuard(i_tOther);
486 this->
operator=(std::move(i_tOther.rawVector()));
494 if (i_nIndex >= this->
size()) {
495 throw IndexOutOfRangeException;
498 return this->m_atVector[i_nIndex];
505 if (i_nIndex >= this->
size()) {
506 throw IndexOutOfRangeException;
509 return this->m_atVector[i_nIndex];
519 std::vector<T> m_atVector;
A class for thread-safe handling of vectors.
Definition: MutexVector.h:65
void clear()
Removes all elements from the vector.
Definition: MutexVector.h:445
auto begin() const
Returns an iterator to the beginning of the vector.
Definition: MutexVector.h:284
std::vector< size_t > findIndexAll(FnMatch i_fnMatch) const
Finds all indexes of the elements in the vector that match the given condition.
Definition: MutexVector.h:212
CMutexVector(const CMutexVector< T > &i_oVector)
Copy constructor that takes a const reference to another CMutexVector.
Definition: MutexVector.h:82
void push_back(std::vector< T > &&i_atValue)
Moves a vector of elements to the end of the vector.
Definition: MutexVector.h:396
CMutexVector & operator=(std::vector< T > &&i_tOther)
Assignment operator that moves a std::vector<T> to the CMutexVector.
Definition: MutexVector.h:463
T & at(const size_t i_nIndex)
Returns a reference to the element at the specified index.
Definition: MutexVector.h:333
T find(FnMatch i_fnMatch) const
Finds the first element in the vector that matches the given condition.
Definition: MutexVector.h:144
bool contains(const T &i_oValue) const
Checks if the vector contains the specified value.
Definition: MutexVector.h:346
bool removeOne(FnMatch i_fnMatch)
Removes the first element from the vector that matches the given condition.
Definition: MutexVector.h:121
bool remove(const T &i_oValue)
Removes the first occurrence of the specified value from the vector.
Definition: MutexVector.h:433
CMutexVector()=default
Default constructor for CMutexVector.
void push_back(CMutexVector< T > &&i_atValue)
Moves another CMutexVector to the end of the vector.
Definition: MutexVector.h:411
T get(FnMatch i_fnMatch) const
Alias for find(FnMatch).
Definition: MutexVector.h:159
T & first()
Returns a reference to the first element in the vector.
Definition: MutexVector.h:320
std::vector< size_t > indexOfAll(FnMatch i_fnMatch) const
Alias for findIndexAll(FnMatch).
Definition: MutexVector.h:228
virtual ~CMutexVector()=default
Destructor for CMutexVector.
T & last()
Returns a reference to the last element in the vector.
Definition: MutexVector.h:326
void push_back(const CMutexVector< T > &i_atValue)
Appends another CMutexVector to the end of the vector.
Definition: MutexVector.h:404
auto cend() const
Returns a const iterator to the end of the vector.
Definition: MutexVector.h:302
auto cbegin() const
Returns a const iterator to the beginning of the vector.
Definition: MutexVector.h:296
bool removeAll(FnMatch i_fnMatch)
Removes all elements from the vector that match the given condition.
Definition: MutexVector.h:101
size_t findIndex(FnMatch i_fnMatch) const
Finds the index of the first element in the vector that matches the given condition.
Definition: MutexVector.h:190
CMutexVector & operator=(const std::vector< T > &i_tOther)
Assignment operator that copies a std::vector<T> to the CMutexVector.
Definition: MutexVector.h:454
bool isEmpty() const
Checks if the vector is empty.
Definition: MutexVector.h:314
CMutexVector(std::vector< T > &&i_oVector)
Move constructor that takes an rvalue reference to a vector.
Definition: MutexVector.h:77
virtual CMutexVector & operator=(CMutexVector< T > &&i_tOther) noexcept
Assignment operator that moves another CMutexVector to the CMutexVector.
Definition: MutexVector.h:482
void push_back(T &&i_oValue)
Moves a new element to the end of the vector.
Definition: MutexVector.h:381
bool remove(FnMatch i_fnMatch)
Removes the first element from the vector that matches the given condition.
Definition: MutexVector.h:136
auto end() const
Returns an iterator to the end of the vector.
Definition: MutexVector.h:290
void reserve(const size_t i_nSize)
Reserves memory for the specified number of elements.
Definition: MutexVector.h:367
void push_back(const T &i_oValue)
Appends a new element to the end of the vector.
Definition: MutexVector.h:374
size_t size() const
Returns the size of the vector.
Definition: MutexVector.h:308
void resize(const size_t i_nSize)
Resizes the vector to the specified size.
Definition: MutexVector.h:360
std::vector< T > findAll(FnMatch i_fnMatch) const
Finds all elements in the vector that match the given condition.
Definition: MutexVector.h:166
void push_back(const std::vector< T > &i_atValue)
Appends a vector of elements to the end of the vector.
Definition: MutexVector.h:388
std::vector< T > toStdVector() const
Converts the CMutexVector to a standard vector.
Definition: MutexVector.h:339
const T & operator[](const size_t i_nIndex) const
Const access operator that returns a const reference to the element at the specified index.
Definition: MutexVector.h:504
CMutexVector(std::vector< T > &i_oVector)
Copy constructor that takes a non-const reference to a vector.
Definition: MutexVector.h:72
std::vector< T > takeAll(FnMatch i_fnMatch) const
Takes all elements from the vector that match the given condition and removes them from the vector.
Definition: MutexVector.h:252
std::vector< T > getAll(FnMatch i_fnMatch) const
Alias for findAll(FnMatch).
Definition: MutexVector.h:182
T take(FnMatch i_fnMatch) const
Takes the first element from the vector that matches the given condition and removes it from the vect...
Definition: MutexVector.h:236
virtual CMutexVector & operator=(const CMutexVector< T > &i_tOther)
Assignment operator that copies another CMutexVector to the CMutexVector.
Definition: MutexVector.h:472
bool removeAt(const size_t i_nIndex)
Removes the element at the specified index from the vector.
Definition: MutexVector.h:419
T & operator[](const size_t i_nIndex)
Access operator that returns a reference to the element at the specified index.
Definition: MutexVector.h:493
const CMutex & mutex() const
Returns a reference to the underlying mutex of the CMutexVector.
Definition: MutexVector.h:272
const std::vector< T > & rawVector() const
Returns a reference to the underlying vector.
Definition: MutexVector.h:278
size_t indexOf(FnMatch i_fnMatch) const
Alias for findIndex(FnMatch).
Definition: MutexVector.h:205
A class for handling recursive mutexes.
Definition: Mutex.h:47
The namespace encapsulating threading related classes and functions in the Devel framework.