File indexing completed on 2024-04-28 05:52:30

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_GROUPPIECETABLECHANGE_HPP
0010 #define KPIECETABLE_GROUPPIECETABLECHANGE_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 
0028 /** class
0029  * @author Friedrich W. H. Kossebau
0030  */
0031 class GroupPieceTableChange : public AbstractPieceTableChange
0032 {
0033 public:
0034     GroupPieceTableChange(GroupPieceTableChange* parent, const QString& description);
0035     GroupPieceTableChange(const GroupPieceTableChange&) = delete;
0036 
0037     ~GroupPieceTableChange() override;
0038 
0039     GroupPieceTableChange& operator=(const GroupPieceTableChange&) = delete;
0040 
0041 public: // AbstractPieceTableChange API
0042     int type() const override;
0043     QString description() const override;
0044     bool merge(const AbstractPieceTableChange* other) override;
0045     AddressRange apply(PieceTable* pieceTable) const override;
0046     AddressRange revert(PieceTable* pieceTable) const override;
0047     ArrayChangeMetrics metrics() const override;
0048     Size dataSize() const override;
0049 
0050 public:
0051     void setDescription(const QString& description);
0052 
0053 // TODO: think about a function to compress a group, that is not going to be handled in detail anymore
0054 // e.g. several replaces of the same byte or an insert and replace of the inserted data
0055 
0056 public:
0057     AddressRangeList applyGroup(PieceTable* pieceTable) const;
0058     AddressRangeList revertGroup(PieceTable* pieceTable) const;
0059     ArrayChangeMetricsList groupMetrics(bool reverted = false) const;
0060     GroupPieceTableChange* parent() const;
0061 
0062 public: // TODO: this interface part is shared with PieceTableChangeHistory, try to use this fact
0063     bool appendChange(AbstractPieceTableChange* change);
0064     void finishChange();
0065 
0066 public:
0067     /// @return number of changes in the history
0068     int count() const;
0069     /// @return number of changes currently applied
0070     int appliedChangesCount() const;
0071     /// @return description of the change with the id changeId
0072     QString changeDescription(int changeId) const;
0073     /// @return description of the change at the head, empty if there is none
0074     QString headChangeDescription() const;
0075     /// @return true if the current change is the base
0076 //     bool isAtBase() const;
0077     /// @return size of the data used by the applied changes
0078     int appliedChangesDataSize() const;
0079 
0080 private:
0081     QStack<AbstractPieceTableChange*> mChangeStack;
0082     GroupPieceTableChange* mParent;
0083 
0084     QString mDescription;
0085     ///
0086     int mAppliedChangesCount = 0;
0087     ///
0088     Size mAppliedChangesDataSize = 0;
0089     /// if true, try to merge changes
0090     bool mTryToMergeAppendedChange = true;
0091 };
0092 
0093 inline GroupPieceTableChange::GroupPieceTableChange(GroupPieceTableChange* parent, const QString& description)
0094     : mParent(parent)
0095     , mDescription(description)
0096 {}
0097 
0098 inline void GroupPieceTableChange::setDescription(const QString& description) { mDescription = description; }
0099 inline GroupPieceTableChange* GroupPieceTableChange::parent() const { return mParent; }
0100 inline void GroupPieceTableChange::finishChange() { mTryToMergeAppendedChange = false; }
0101 inline int GroupPieceTableChange::count()                     const { return mChangeStack.size(); }
0102 inline int GroupPieceTableChange::appliedChangesCount()       const { return mAppliedChangesCount; }
0103 inline QString GroupPieceTableChange::headChangeDescription() const { return changeDescription(count() - 1); }
0104 inline QString GroupPieceTableChange::changeDescription(int changeId) const
0105 {
0106     const AbstractPieceTableChange* change = mChangeStack.value(changeId);
0107 
0108     return change ? change->description() : QString();
0109 }
0110 
0111 }
0112 
0113 #endif