forked from KDE/kdevelop
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract ScopedIncrementor into utility
Move the helper class ScopedIncrementor into kdevplatform/util, because it has other uses than just in the BreakpointModel. Introduce a new class template NonNegative to assert that we don't accidentally decrement the counter below zero. Convert the class ScopedIncrementor into a class template that can operate with an instance of NonNegative or any other integral type. If a future use arises for being able to move a ScopedIncrementor guard or construct it from an already incremented counter, such semantics can be implemented later.
- Loading branch information
Showing
4 changed files
with
69 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
SPDX-FileCopyrightText: 2024 Jarmo Tiitto <[email protected]> | ||
SPDX-License-Identifier: LGPL-2.1-or-later | ||
*/ | ||
|
||
#ifndef KDEVPLATFORM_SCOPEDINCREMENTOR_H | ||
#define KDEVPLATFORM_SCOPEDINCREMENTOR_H | ||
|
||
#include <QtAssert> | ||
#include <QtClassHelperMacros> | ||
|
||
namespace KDevelop { | ||
|
||
template<typename T = int> | ||
class NonNegative | ||
{ | ||
public: | ||
NonNegative& operator++() | ||
{ | ||
++m_value; | ||
return *this; | ||
} | ||
|
||
NonNegative& operator--() | ||
{ | ||
Q_ASSERT(m_value); | ||
--m_value; | ||
return *this; | ||
} | ||
|
||
operator bool() const | ||
{ | ||
return m_value; | ||
} | ||
|
||
private: | ||
T m_value = 0; | ||
}; | ||
|
||
template<typename T = NonNegative<int>> | ||
class ScopedIncrementor | ||
{ | ||
public: | ||
explicit ScopedIncrementor(T& value) | ||
: m_value(value) | ||
{ | ||
++m_value; | ||
} | ||
|
||
~ScopedIncrementor() | ||
{ | ||
--m_value; | ||
} | ||
|
||
Q_DISABLE_COPY_MOVE(ScopedIncrementor) | ||
|
||
private: | ||
T& m_value; | ||
}; | ||
|
||
} | ||
|
||
#endif // KDEVPLATFORM_SCOPEDINCREMENTOR_H |