1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 #ifndef INCLUDED_SW_INC_DOCARY_HXX
20 #define INCLUDED_SW_INC_DOCARY_HXX
23 #include <type_traits>
24 #include <o3tl/sorted_vector.hxx>
28 #include "section.hxx"
30 #include "numrule.hxx"
40 enum class RedlineType
: sal_uInt16
;
42 /** provides some methods for generic operations on lists that contain SwFormat* subclasses. */
46 virtual size_t GetFormatCount() const = 0;
47 virtual SwFormat
* GetFormat(size_t idx
) const = 0;
48 virtual ~SwFormatsBase() {};
50 SwFormatsBase() = default;
51 SwFormatsBase(SwFormatsBase
const &) = default;
52 SwFormatsBase(SwFormatsBase
&&) = default;
53 SwFormatsBase
& operator =(SwFormatsBase
const &) = default;
54 SwFormatsBase
& operator =(SwFormatsBase
&&) = default;
57 template<typename Value
>
58 class SwVectorModifyBase
61 typedef typename
std::vector
<Value
>::iterator iterator
;
62 typedef typename
std::vector
<Value
>::const_iterator const_iterator
;
63 typedef typename
std::vector
<Value
>::size_type size_type
;
64 typedef typename
std::vector
<Value
>::value_type value_type
;
67 enum class DestructorPolicy
{
73 typename
std::vector
<Value
> mvVals
;
74 const DestructorPolicy mPolicy
;
77 // default destructor deletes all contained elements
78 SwVectorModifyBase(DestructorPolicy policy
= DestructorPolicy::FreeElements
)
82 bool empty() const { return mvVals
.empty(); }
83 Value
const& front() const { return mvVals
.front(); }
84 size_t size() const { return mvVals
.size(); }
85 iterator
begin() { return mvVals
.begin(); }
86 const_iterator
begin() const { return mvVals
.begin(); }
87 iterator
end() { return mvVals
.end(); }
88 const_iterator
end() const { return mvVals
.end(); }
89 void clear() { mvVals
.clear(); }
90 iterator
erase(iterator aIt
) { return mvVals
.erase(aIt
); }
91 iterator
erase(iterator aFirst
, iterator aLast
) { return mvVals
.erase(aFirst
, aLast
); }
92 iterator
insert(iterator aIt
, Value
const& rVal
) { return mvVals
.insert(aIt
, rVal
); }
93 template<typename TInputIterator
>
94 void insert(iterator aIt
, TInputIterator aFirst
, TInputIterator aLast
)
96 mvVals
.insert(aIt
, aFirst
, aLast
);
98 void push_back(Value
const& rVal
) { mvVals
.push_back(rVal
); }
99 void reserve(size_type nSize
) { mvVals
.reserve(nSize
); }
100 Value
const& at(size_type nPos
) const { return mvVals
.at(nPos
); }
101 Value
const& operator[](size_type nPos
) const { return mvVals
[nPos
]; }
102 Value
& operator[](size_type nPos
) { return mvVals
[nPos
]; }
104 // free any remaining child objects based on mPolicy
105 virtual ~SwVectorModifyBase()
107 if (mPolicy
== DestructorPolicy::FreeElements
)
108 for(const_iterator it
= begin(); it
!= end(); ++it
)
112 //TODO: These functions are apparently brittle (but the copy functions are actually used by the
113 // code; the move functions will be implicitly-defined as deleted anyway) and should probably
114 // only be used with DestructorPolicy::KeepELements:
115 SwVectorModifyBase(SwVectorModifyBase
const &) = default;
116 SwVectorModifyBase(SwVectorModifyBase
&&) = default;
117 SwVectorModifyBase
& operator =(SwVectorModifyBase
const &) = default;
118 SwVectorModifyBase
& operator =(SwVectorModifyBase
&&) = default;
120 void DeleteAndDestroy(int aStartIdx
, int aEndIdx
)
122 if (aEndIdx
< aStartIdx
)
124 for (const_iterator it
= begin() + aStartIdx
;
125 it
!= begin() + aEndIdx
; ++it
)
127 erase( begin() + aStartIdx
, begin() + aEndIdx
);
130 size_t GetPos(Value
const& p
) const
132 const_iterator
const it
= std::find(begin(), end(), p
);
133 return it
== end() ? SIZE_MAX
: it
- begin();
136 /// check that given format is still alive (i.e. contained here)
137 bool IsAlive(typename
std::remove_pointer
<Value
>::type
const*const p
) const
138 { return std::find(begin(), end(), p
) != end(); }
140 static void dumpAsXml(xmlTextWriterPtr
/*pWriter*/) {};
143 template<typename Value
>
144 class SwFormatsModifyBase
: public SwVectorModifyBase
<Value
>, public SwFormatsBase
147 SwFormatsModifyBase(typename SwVectorModifyBase
<Value
>::DestructorPolicy
148 policy
= SwVectorModifyBase
<Value
>::DestructorPolicy::FreeElements
)
149 : SwVectorModifyBase
<Value
>(policy
) {}
152 virtual size_t GetFormatCount() const override
153 { return SwVectorModifyBase
<Value
>::size(); }
155 virtual Value
GetFormat(size_t idx
) const override
156 { return SwVectorModifyBase
<Value
>::operator[](idx
); }
158 size_t GetPos(const SwFormat
*p
) const
159 { return SwVectorModifyBase
<Value
>::GetPos( static_cast<Value
>( const_cast<SwFormat
*>( p
) ) ); }
161 /// check if given format is contained here
162 /// @precond pFormat must not have been deleted
163 bool ContainsFormat(SwFormat
const*const pFormat
) const {
164 Value p
= dynamic_cast<Value
>(const_cast<SwFormat
*>(pFormat
));
165 return p
!= nullptr && SwVectorModifyBase
<Value
>::IsAlive(p
);
169 class SwGrfFormatColls final
: public SwFormatsModifyBase
<SwGrfFormatColl
*>
172 SwGrfFormatColls() : SwFormatsModifyBase( DestructorPolicy::KeepElements
) {}
176 /// Unsorted, undeleting SwFrameFormat vector
177 class SwFrameFormatsV final
: public SwFormatsModifyBase
<SwFrameFormat
*>
180 SwFrameFormatsV() : SwFormatsModifyBase( DestructorPolicy::KeepElements
) {}
183 class SwCharFormats final
: public SwFormatsModifyBase
<SwCharFormat
*>
186 void dumpAsXml(xmlTextWriterPtr pWriter
) const;
189 class SwTextFormatColls final
: public SwFormatsModifyBase
<SwTextFormatColl
*>
192 SwTextFormatColls() : SwFormatsModifyBase( DestructorPolicy::KeepElements
) {}
193 void dumpAsXml(xmlTextWriterPtr pWriter
) const;
196 /// Array of Undo-history.
197 class SwSectionFormats final
: public SwFormatsModifyBase
<SwSectionFormat
*>
200 void dumpAsXml(xmlTextWriterPtr pWriter
) const;
203 class SwFieldTypes
: public std::vector
<std::unique_ptr
<SwFieldType
>> {
205 void dumpAsXml(xmlTextWriterPtr pWriter
) const;
208 class SwTOXTypes
: public std::vector
<std::unique_ptr
<SwTOXType
>> {};
210 class SwNumRuleTable final
: public SwVectorModifyBase
<SwNumRule
*> {
212 void dumpAsXml(xmlTextWriterPtr pWriter
) const;
215 struct CompareSwRedlineTable
217 bool operator()(SwRangeRedline
* const &lhs
, SwRangeRedline
* const &rhs
) const;
220 // Notification type for notifying about redlines to LOK clients
221 enum class RedlineNotification
{ Add
, Remove
, Modify
};
226 typedef o3tl::sorted_vector
<SwRangeRedline
*, CompareSwRedlineTable
,
227 o3tl::find_partialorder_ptrequals
> vector_type
;
228 typedef vector_type::size_type size_type
;
229 static constexpr size_type npos
= SAL_MAX_INT32
;
231 vector_type maVector
;
234 bool Contains(const SwRangeRedline
* p
) const { return maVector
.find(const_cast<SwRangeRedline
*>(p
)) != maVector
.end(); }
235 size_type
GetPos(const SwRangeRedline
* p
) const;
237 bool Insert(SwRangeRedline
*& p
);
238 bool Insert(SwRangeRedline
*& p
, size_type
& rInsPos
);
239 bool InsertWithValidRanges(SwRangeRedline
*& p
, size_type
* pInsPos
= nullptr);
241 void Remove( size_type nPos
);
242 void Remove( const SwRangeRedline
* p
);
243 void DeleteAndDestroy(size_type nPos
);
244 void DeleteAndDestroyAll();
246 void dumpAsXml(xmlTextWriterPtr pWriter
) const;
248 size_type
FindNextOfSeqNo( size_type nSttPos
) const;
249 size_type
FindPrevOfSeqNo( size_type nSttPos
) const;
250 /** Search next or previous Redline with the same Seq. No.
251 Search can be restricted via Lookahead.
252 Using 0 makes search the whole array. */
253 size_type
FindNextSeqNo( sal_uInt16 nSeqNo
, size_type nSttPos
) const;
254 size_type
FindPrevSeqNo( sal_uInt16 nSeqNo
, size_type nSttPos
) const;
257 Find the redline at the given position.
259 @param tableIndex position in SwRedlineTable to start searching at, will be updated with the index of the returned
260 redline (or the next redline after the given position if not found)
261 @param next true: redline starts at position and ends after, false: redline starts before position and ends at or after
263 const SwRangeRedline
* FindAtPosition( const SwPosition
& startPosition
, size_type
& tableIndex
, bool next
= true ) const;
265 bool empty() const { return maVector
.empty(); }
266 size_type
size() const { return maVector
.size(); }
267 SwRangeRedline
* operator[]( size_type idx
) const { return maVector
[idx
]; }
268 vector_type::const_iterator
begin() const { return maVector
.begin(); }
269 vector_type::const_iterator
end() const { return maVector
.end(); }
270 void Resort() { maVector
.Resort(); }
272 // Notifies all LOK clients when redlines are added/modified/removed
273 static void LOKRedlineNotification(RedlineNotification eType
, SwRangeRedline
* pRedline
);
276 /// Table that holds 'extra' redlines, such as 'table row insert/delete', 'paragraph moves' etc...
277 class SwExtraRedlineTable
280 std::vector
<SwExtraRedline
*> m_aExtraRedlines
;
283 ~SwExtraRedlineTable();
285 void Insert( SwExtraRedline
* p
);
287 void DeleteAndDestroy( sal_uInt16 nPos
);
288 void DeleteAndDestroyAll();
290 void dumpAsXml(xmlTextWriterPtr pWriter
) const;
292 sal_uInt16
GetSize() const { return m_aExtraRedlines
.size(); }
293 SwExtraRedline
* GetRedline( sal_uInt16 uIndex
) const { return m_aExtraRedlines
.operator[]( uIndex
); }
295 SW_DLLPUBLIC
bool DeleteAllTableRedlines( SwDoc
& rDoc
, const SwTable
& rTable
, bool bSaveInUndo
, RedlineType nRedlineTypeToDelete
);
296 bool DeleteTableRowRedline ( SwDoc
* pDoc
, const SwTableLine
& rTableLine
, bool bSaveInUndo
, RedlineType nRedlineTypeToDelete
);
297 bool DeleteTableCellRedline( SwDoc
* pDoc
, const SwTableBox
& rTableBox
, bool bSaveInUndo
, RedlineType nRedlineTypeToDelete
);
300 typedef std::vector
<SwOLENode
*> SwOLENodes
;
302 #endif // INCLUDED_SW_INC_DOCARY_HXX
304 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */