File indexing completed on 2024-04-14 05:45:45
0001 /* 0002 This file is part of the Okteta Core library, made within the KDE community. 0003 0004 SPDX-FileCopyrightText: 2003, 2007, 2009 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 OKTETA_BYTEARRAYMODEL_P_HPP 0010 #define OKTETA_BYTEARRAYMODEL_P_HPP 0011 0012 // lib 0013 #include "abstractbytearraymodel_p.hpp" 0014 #include "bytearraymodel.hpp" 0015 #include "bookmarksconstiterator.hpp" 0016 #include "bookmarklistconstiteratoradapter.hpp" 0017 #include "bookmarklist.hpp" 0018 #include "arraychangemetricslist.hpp" 0019 0020 namespace Okteta { 0021 0022 class ByteArrayModelPrivate : public AbstractByteArrayModelPrivate 0023 { 0024 public: 0025 ByteArrayModelPrivate(ByteArrayModel* parent, Byte* data, int size, int rawSize, bool keepsMemory); 0026 ByteArrayModelPrivate(ByteArrayModel* parent, const Byte* data, int size); 0027 ByteArrayModelPrivate(ByteArrayModel* parent, int size, int maxSize); 0028 0029 ~ByteArrayModelPrivate() override; 0030 0031 public: // AbstractByteArrayModel API 0032 Byte byte(Address offset) const; 0033 Size size() const; 0034 bool isReadOnly() const; 0035 bool isModified() const; 0036 0037 Size insert(Address offset, const Byte* insertData, int insertLength); 0038 Size remove(const AddressRange& removeRange); 0039 Size replace(const AddressRange& removeRange, const Byte* insertData, int insertLength); 0040 bool swap(Address firstStart, const AddressRange& secondRange); 0041 Size fill(Byte fillByte, Address offset = 0, Size fillLength = -1); 0042 void setByte(Address offset, Byte byte); 0043 0044 void setModified(bool modified = true); 0045 0046 public: 0047 void setReadOnly(bool readOnly = true); 0048 void setMaxSize(int maxSize); 0049 /** sets whether the memory given by setData or in the constructor should be kept on resize 0050 */ 0051 void setKeepsMemory(bool keepsMemory = true); 0052 void setAutoDelete(bool autoDelete = true); 0053 void setData(Byte* data, int size, int rawSize = -1, bool keepMemory = true); 0054 0055 public: 0056 Byte* data() const; 0057 int maxSize() const; 0058 /** returns whether the memory of the byte array is kept on resize */ 0059 bool keepsMemory() const; 0060 // TODO 0.10: turn this bool flag into a real flag which also tells how to free the memory if "autodeleted" 0061 // perhaps merge keepsMemory flag into that new flag as well 0062 bool autoDelete() const; 0063 0064 public: // Bookmarkable API 0065 void addBookmarks(const QVector<Bookmark>& bookmarks); 0066 void removeBookmarks(const QVector<Bookmark>& bookmarks); 0067 void removeAllBookmarks(); 0068 void setBookmark(unsigned int index, const Bookmark& bookmark); 0069 0070 BookmarksConstIterator createBookmarksConstIterator() const; 0071 const Bookmark& bookmarkAt(unsigned int index) const; 0072 const Bookmark& bookmarkFor(int offset) const; 0073 bool containsBookmarkFor(int offset) const; 0074 unsigned int bookmarksCount() const; 0075 0076 private: 0077 /** resizes the buffer, if possible, saving the data and splitting the data, if demanded 0078 * @param AddSize additional size the buffer should grow 0079 * @param splitOffset if -1 does not split 0080 * @param saveUpperPart true if upper part should be copied into new buffer 0081 * @return additional size the buffer has grown 0082 */ 0083 int addSize(int AddSize, int splitPosition = -1, bool saveUpperPart = true); 0084 0085 private: 0086 /** */ 0087 Byte* mData; 0088 /** size of the data */ 0089 int mSize; 0090 /** mSize of data array */ 0091 int mRawSize; 0092 /** maximal size of array, unlimited if -1 */ 0093 int mMaxSize = -1; 0094 /** flag whether the initially given memory should be kept */ 0095 bool mKeepsMemory : 1; 0096 /** flag whether the */ 0097 bool mAutoDelete : 1; 0098 /** */ 0099 bool mReadOnly : 1; 0100 /** */ 0101 bool mModified : 1; 0102 /** */ 0103 BookmarkList m_bookmarks; 0104 0105 private: 0106 Q_DECLARE_PUBLIC(ByteArrayModel) 0107 }; 0108 0109 // use delete [], since usually mData should be allocated by calling new Byte[n] 0110 inline ByteArrayModelPrivate::~ByteArrayModelPrivate() 0111 { 0112 if (mAutoDelete) { 0113 delete [] mData; 0114 } 0115 } 0116 0117 inline Byte ByteArrayModelPrivate::byte(Address offset) const { return mData[offset]; } 0118 inline Size ByteArrayModelPrivate::size() const { return mSize; } 0119 0120 inline bool ByteArrayModelPrivate::isReadOnly() const { return mReadOnly; } 0121 inline bool ByteArrayModelPrivate::isModified() const { return mModified; } 0122 0123 inline void ByteArrayModelPrivate::setReadOnly(bool isReadOnly) 0124 { 0125 Q_Q(ByteArrayModel); 0126 0127 if (mReadOnly != isReadOnly) { 0128 mReadOnly = isReadOnly; 0129 Q_EMIT q->readOnlyChanged(isReadOnly); 0130 } 0131 } 0132 inline void ByteArrayModelPrivate::setMaxSize(int maxSize) { mMaxSize = maxSize; } 0133 inline void ByteArrayModelPrivate::setKeepsMemory(bool keepsMemory) { mKeepsMemory = keepsMemory; } 0134 inline void ByteArrayModelPrivate::setAutoDelete(bool autoDelete) { mAutoDelete = autoDelete; } 0135 inline void ByteArrayModelPrivate::setByte(Address offset, Byte byte) 0136 { 0137 Q_Q(ByteArrayModel); 0138 0139 const bool wasModifiedBefore = mModified; 0140 mData[offset] = byte; 0141 mModified = true; 0142 Q_EMIT q->contentsChanged(ArrayChangeMetricsList::oneReplacement(offset, 1, 1)); 0143 if (!wasModifiedBefore) { 0144 Q_EMIT q->modifiedChanged(true); 0145 } 0146 } 0147 inline void ByteArrayModelPrivate::setModified(bool modified) 0148 { 0149 Q_Q(ByteArrayModel); 0150 0151 mModified = modified; 0152 Q_EMIT q->modifiedChanged(mModified); 0153 } 0154 0155 inline Byte* ByteArrayModelPrivate::data() const { return mData; } 0156 inline int ByteArrayModelPrivate::maxSize() const { return mMaxSize; } 0157 inline bool ByteArrayModelPrivate::keepsMemory() const { return mKeepsMemory; } 0158 inline bool ByteArrayModelPrivate::autoDelete() const { return mAutoDelete; } 0159 0160 inline void ByteArrayModelPrivate::addBookmarks(const QVector<Bookmark>& bookmarks) 0161 { 0162 Q_Q(ByteArrayModel); 0163 0164 m_bookmarks.addBookmarks(bookmarks); 0165 Q_EMIT q->bookmarksAdded(bookmarks); 0166 } 0167 inline void ByteArrayModelPrivate::removeBookmarks(const QVector<Bookmark>& bookmarks) 0168 { 0169 Q_Q(ByteArrayModel); 0170 0171 m_bookmarks.removeBookmarks(bookmarks); 0172 Q_EMIT q->bookmarksRemoved(bookmarks); 0173 } 0174 0175 inline void ByteArrayModelPrivate::removeAllBookmarks() 0176 { 0177 Q_Q(ByteArrayModel); 0178 0179 const QVector<Bookmark> bookmarks = m_bookmarks.list(); 0180 m_bookmarks.clear(); 0181 Q_EMIT q->bookmarksRemoved(bookmarks); 0182 } 0183 inline void ByteArrayModelPrivate::setBookmark(unsigned int index, const Bookmark& bookmark) 0184 { 0185 Q_Q(ByteArrayModel); 0186 0187 m_bookmarks.setBookmark(index, bookmark); 0188 0189 const QVector<int> changedBookmarkIndizes { 0190 static_cast<int>(index) 0191 }; 0192 Q_EMIT q->bookmarksModified(changedBookmarkIndizes); 0193 } 0194 0195 inline BookmarksConstIterator ByteArrayModelPrivate::createBookmarksConstIterator() const 0196 { 0197 return BookmarksConstIterator(new BookmarkListConstIteratorAdapter(m_bookmarks)); 0198 } 0199 0200 inline const Bookmark& ByteArrayModelPrivate::bookmarkFor(int offset) const 0201 { 0202 return m_bookmarks.bookmark(offset); 0203 } 0204 inline const Bookmark& ByteArrayModelPrivate::bookmarkAt(unsigned int index) const 0205 { 0206 return m_bookmarks.at(index); 0207 } 0208 inline bool ByteArrayModelPrivate::containsBookmarkFor(int offset) const { return m_bookmarks.contains(offset); } 0209 inline unsigned int ByteArrayModelPrivate::bookmarksCount() const { return m_bookmarks.size(); } 0210 0211 } 0212 0213 #endif