File indexing completed on 2024-04-21 05:52:55

0001 /*
0002     This file is part of the Okteta Core library, made within the KDE community.
0003 
0004     SPDX-FileCopyrightText: 2008 Friedrich W. H. Kossebau <kossebau@kde.org>
0005 
0006     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0007 */
0008 
0009 #ifndef KPIECETABLE_PIECETABLECHANGEHISTORY_HPP
0010 #define KPIECETABLE_PIECETABLECHANGEHISTORY_HPP
0011 
0012 // lib
0013 #include "abstractpiecetablechange.hpp"
0014 // Qt
0015 #include <QStack>
0016 #include <QString>
0017 
0018 namespace Okteta {
0019 class AddressRangeList;
0020 class ArrayChangeMetricsList;
0021 }
0022 
0023 namespace KPieceTable {
0024 
0025 using AddressRangeList = Okteta::AddressRangeList;
0026 using ArrayChangeMetricsList = Okteta::ArrayChangeMetricsList;
0027 class GroupPieceTableChange;
0028 
0029 class PieceTableChangeHistory
0030 {
0031 public:
0032     PieceTableChangeHistory();
0033     PieceTableChangeHistory(const PieceTableChangeHistory&) = delete;
0034 
0035     ~PieceTableChangeHistory();
0036 
0037     PieceTableChangeHistory& operator=(const PieceTableChangeHistory&) = delete;
0038 
0039 public:
0040     void clear();
0041     /// returns true, if a new change is appended, false if merged
0042     bool appendChange(AbstractPieceTableChange* change);
0043 
0044     /**
0045      * @param pieceTable
0046      * @param changeId
0047      * @param changedRanges
0048      * @param changeList
0049      * @return true if there were changes to revert, false otherwise
0050      */
0051     bool revertBeforeChange(PieceTable* pieceTable, int changeId,
0052                             AddressRangeList* changedRanges, ArrayChangeMetricsList* changeList);
0053 
0054     ///
0055     void openGroupedChange(const QString& description);   // TODO: hand over description? user change id?
0056     void closeGroupedChange(const QString& description);
0057     /// closes the current change, so any following operation will not be tried to merge
0058     void finishChange();
0059 
0060     void setBeforeCurrentChangeAsBase(bool hide);
0061 
0062 public:
0063     /// @return number of changes in the history
0064     int count() const;
0065     /// @return number of changes currently applied
0066     int appliedChangesCount() const;
0067     /// @return description of the change with the id changeId
0068     QString changeDescription(int changeId) const;
0069     /// @return description of the change at the head, empty if there is none
0070     QString headChangeDescription() const;
0071     /// @return true if the current change is the base
0072     bool isAtBase() const;
0073     /// @return size of the data used by the applied changes
0074     Size appliedChangesDataSize() const;
0075 
0076     void getChangeData(ArrayChangeMetrics* metrics, Address* storageOffset, int versionIndex) const;
0077 
0078 private:
0079     /// if true, try to merge changes
0080     bool mTryToMergeAppendedChange = false;
0081     ///
0082     int mAppliedChangesCount = 0;
0083     ///
0084     int mBaseBeforeChangeIndex = 0;
0085     ///
0086     QStack<AbstractPieceTableChange*> mChangeStack;
0087     ///
0088     Size mAppliedChangesDataSize = 0;
0089 
0090     /// if 0, there is no
0091     GroupPieceTableChange* mActiveGroupChange = nullptr;
0092 };
0093 
0094 inline PieceTableChangeHistory::PieceTableChangeHistory() = default;
0095 
0096 inline PieceTableChangeHistory::~PieceTableChangeHistory() { clear(); }
0097 
0098 inline int PieceTableChangeHistory::count()                     const { return mChangeStack.size(); }
0099 inline int PieceTableChangeHistory::appliedChangesCount()       const { return mAppliedChangesCount; }
0100 inline QString PieceTableChangeHistory::headChangeDescription() const { return changeDescription(count() - 1); }
0101 inline bool PieceTableChangeHistory::isAtBase()                 const
0102 {
0103     return (mBaseBeforeChangeIndex == mAppliedChangesCount);
0104 }
0105 inline Size PieceTableChangeHistory::appliedChangesDataSize()    const { return mAppliedChangesDataSize; }
0106 
0107 inline QString PieceTableChangeHistory::changeDescription(int changeId) const
0108 {
0109     const AbstractPieceTableChange* change = mChangeStack.value(changeId);
0110 
0111     return change ? change->description() : QString();
0112 }
0113 
0114 }
0115 
0116 #endif