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_PIECE_HPP
0010 #define KPIECETABLE_PIECE_HPP
0011 
0012 // lib
0013 #include <addressrange.hpp>
0014 
0015 namespace KPieceTable {
0016 
0017 using Size = Okteta::Size;
0018 using Address = Okteta::Address;
0019 using AddressRange = Okteta::AddressRange;
0020 
0021 class Piece : public AddressRange
0022 {
0023 public:
0024     enum {
0025         OriginalStorage,
0026         ChangeStorage
0027     };
0028 
0029 public:
0030     Piece(Address storageOffset, Size size, int storageId);
0031     Piece(const AddressRange& storageRange, int storageId);
0032     Piece();
0033     Piece(const Piece&);
0034 
0035     ~Piece();
0036 
0037     Piece& operator=(const Piece&) = default;
0038 
0039 public:
0040     int storageId() const;
0041 
0042 public:
0043     void setStorageId(int storageId);
0044     Piece splitAt(Address storageOffset);
0045     Piece splitAtLocal(Address localStorageOffset);
0046     Piece remove(const AddressRange& removeStorageRange);
0047     Piece removeLocal(const AddressRange& localRemoveStorageRange);
0048     Piece removeStartBeforeLocal(Address storageOffset);
0049     Piece removeEndBehindLocal(Address storageOffset);
0050     bool prepend(const Piece& other);
0051     bool append(const Piece& other);
0052 
0053 public:
0054     Piece subPiece(const AddressRange& local) const;
0055 
0056 private:
0057     int mStorageId = OriginalStorage;
0058 };
0059 
0060 inline Piece::Piece(Address storageOffset, Size size, int storageId)
0061     : AddressRange(AddressRange::fromWidth(storageOffset, size))
0062     , mStorageId(storageId)
0063 {}
0064 inline Piece::Piece(const AddressRange& storageRange, int storageId)
0065     : AddressRange(storageRange)
0066     , mStorageId(storageId)
0067 {}
0068 inline Piece::Piece() = default;
0069 inline Piece::Piece(const Piece&) = default;
0070 inline Piece::~Piece() = default;
0071 
0072 inline int Piece::storageId() const { return mStorageId; }
0073 
0074 inline void Piece::setStorageId(int storageId) { mStorageId = storageId; }
0075 
0076 inline Piece Piece::splitAt(Address storageOffset)
0077 {
0078     return Piece(AddressRange::splitAt(storageOffset), mStorageId);
0079 }
0080 inline Piece Piece::splitAtLocal(Address localStorageOffset)
0081 {
0082     return Piece(AddressRange::splitAtLocal(localStorageOffset), mStorageId);
0083 }
0084 inline Piece Piece::remove(const AddressRange& removeStorageRange)
0085 {
0086     return Piece(AddressRange::remove(removeStorageRange), mStorageId);
0087 }
0088 inline Piece Piece::removeLocal(const AddressRange& localRemoveStorageRange)
0089 {
0090     return Piece(AddressRange::removeLocal(localRemoveStorageRange), mStorageId);
0091 }
0092 inline Piece Piece::removeStartBeforeLocal(Address storageOffset)
0093 {
0094     const Address oldStart = start();
0095     moveStartBy(storageOffset);
0096     return Piece(AddressRange(oldStart, nextBeforeStart()), mStorageId);
0097 }
0098 inline Piece Piece::removeEndBehindLocal(Address storageOffset)
0099 {
0100     const Address oldEnd = end();
0101     setEndByWidth(storageOffset + 1);
0102     return Piece(AddressRange(nextBehindEnd(), oldEnd), mStorageId);
0103 }
0104 
0105 inline Piece Piece::subPiece(const AddressRange& local) const
0106 {
0107     return Piece(AddressRange::subRange(local), mStorageId);
0108 }
0109 
0110 inline bool Piece::prepend(const Piece& other)
0111 {
0112     const bool result = (mStorageId == other.mStorageId && AddressRange::prepend(other));
0113     return result;
0114 }
0115 inline bool Piece::append(const Piece& other)
0116 {
0117     const bool result = (mStorageId == other.mStorageId && AddressRange::append(other));
0118     return result;
0119 }
0120 
0121 }
0122 
0123 Q_DECLARE_TYPEINFO(KPieceTable::Piece, Q_MOVABLE_TYPE);
0124 
0125 #endif