Skip to content

Commit

Permalink
Extract ScopedIncrementor into utility
Browse files Browse the repository at this point in the history
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
JATothrim authored and vedgy committed Nov 14, 2024
1 parent d8a08c5 commit 299bfaf
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 20 deletions.
4 changes: 2 additions & 2 deletions kdevplatform/debugger/breakpoint/breakpointmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class KDevelop::BreakpointModelPrivate
ReloadState reloadState = ReloadState::Idle;
/// Non-zero while KDevelop code is adding or removing a document mark.
/// This allows to react to user-driven mark changes without getting confused by our own code changes.
int inhibitMarkChange = 0;
NonNegative<> inhibitMarkChange;
QList<Breakpoint*> breakpoints;
/// FIXME: this is just an ugly workaround to not leak deleted breakpoints
/// a real fix would make sure that we actually delete breakpoints
Expand Down Expand Up @@ -688,7 +688,7 @@ void BreakpointModel::reportChange(Breakpoint* breakpoint, Breakpoint::Column co
scheduleSave();
}

ScopedIncrementor BreakpointModel::markChangeGuard()
ScopedIncrementor<> BreakpointModel::markChangeGuard()
{
Q_D(BreakpointModel);

Expand Down
20 changes: 2 additions & 18 deletions kdevplatform/debugger/breakpoint/breakpointmodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "breakpoint.h"

#include <util/namespacedoperatorbitwiseorworkaroundqtbug.h>
#include <util/scopedincrementor.h>

#include <KTextEditor/Document>

Expand All @@ -31,23 +32,6 @@ class IDocument;
class Breakpoint;
class BreakpointModelPrivate;

class ScopedIncrementor
{
int& m_value;

public:
explicit ScopedIncrementor(int& value)
: m_value(value)
{
++m_value;
}
~ScopedIncrementor()
{
--m_value;
}
Q_DISABLE_COPY_MOVE(ScopedIncrementor)
};

class KDEVPLATFORMDEBUGGER_EXPORT BreakpointModel : public QAbstractTableModel
{
Q_OBJECT
Expand Down Expand Up @@ -197,7 +181,7 @@ private Q_SLOTS:
/**
* Call this function and keep the returned guard object alive while adding or removing document marks.
*/
ScopedIncrementor markChangeGuard();
ScopedIncrementor<> markChangeGuard();

/**
* Remove all breakpoint marks from @p document.
Expand Down
1 change: 1 addition & 0 deletions kdevplatform/util/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ install( FILES
projecttestjob.h
widgetcolorizer.h
path.h
scopedincrementor.h
stack.h
stringviewhelpers.h
texteditorhelpers.h
Expand Down
64 changes: 64 additions & 0 deletions kdevplatform/util/scopedincrementor.h
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

0 comments on commit 299bfaf

Please sign in to comment.