LibreOffice Module sw (master)  1
docary.hxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
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/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
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 .
18  */
19 #ifndef INCLUDED_SW_INC_DOCARY_HXX
20 #define INCLUDED_SW_INC_DOCARY_HXX
21 
22 #include <map>
23 #include <vector>
24 #include <type_traits>
25 #include <o3tl/sorted_vector.hxx>
26 
27 #include "fmtcol.hxx"
28 #include "frmfmt.hxx"
29 #include "section.hxx"
30 #include "tox.hxx"
31 #include "numrule.hxx"
32 #include "fldbas.hxx"
33 
34 class SwRangeRedline;
35 class SwExtraRedline;
36 class SwOLENode;
37 class SwTable;
38 class SwTableLine;
39 class SwTableBox;
40 struct SwPosition;
41 enum class RedlineType : sal_uInt16;
42 
45 {
46 public:
47  virtual size_t GetFormatCount() const = 0;
48  virtual SwFormat* GetFormat(size_t idx) const = 0;
49  virtual ~SwFormatsBase();
50 
51  // default linear search implementation, some subclasses will override with a more efficient search
52  virtual SwFormat* FindFormatByName(const OUString& rName) const;
53 
54  SwFormatsBase() = default;
55  SwFormatsBase(SwFormatsBase const &) = default;
56  SwFormatsBase(SwFormatsBase &&) = default;
57  SwFormatsBase & operator =(SwFormatsBase const &) = default;
58  SwFormatsBase & operator =(SwFormatsBase &&) = default;
59 };
60 
61 template<typename Value>
63 {
64 public:
65  typedef typename std::vector<Value>::iterator iterator;
66  typedef typename std::vector<Value>::const_iterator const_iterator;
67  typedef typename std::vector<Value>::size_type size_type;
68  typedef typename std::vector<Value>::value_type value_type;
69 
70 protected:
71  enum class DestructorPolicy {
74  };
75 
76 private:
77  typename std::vector<Value> mvVals;
79 
80 protected:
81  // default destructor deletes all contained elements
83  : mPolicy(policy) {}
84 
85 public:
86  bool empty() const { return mvVals.empty(); }
87  Value const& front() const { return mvVals.front(); }
88  size_t size() const { return mvVals.size(); }
89  iterator begin() { return mvVals.begin(); }
90  const_iterator begin() const { return mvVals.begin(); }
91  iterator end() { return mvVals.end(); }
92  const_iterator end() const { return mvVals.end(); }
93  void clear() { mvVals.clear(); }
94  iterator erase(iterator aIt) { return mvVals.erase(aIt); }
95  iterator erase(iterator aFirst, iterator aLast) { return mvVals.erase(aFirst, aLast); }
96  iterator insert(iterator aIt, Value const& rVal) { return mvVals.insert(aIt, rVal); }
97  template<typename TInputIterator>
98  void insert(iterator aIt, TInputIterator aFirst, TInputIterator aLast)
99  {
100  mvVals.insert(aIt, aFirst, aLast);
101  }
102  void push_back(Value const& rVal) { mvVals.push_back(rVal); }
103  void reserve(size_type nSize) { mvVals.reserve(nSize); }
104  Value const& at(size_type nPos) const { return mvVals.at(nPos); }
105  Value const& operator[](size_type nPos) const { return mvVals[nPos]; }
106  Value& operator[](size_type nPos) { return mvVals[nPos]; }
107 
108  // free any remaining child objects based on mPolicy
110  {
111  if (mPolicy == DestructorPolicy::FreeElements)
112  for(const_iterator it = begin(); it != end(); ++it)
113  delete *it;
114  }
115 
116  //TODO: These functions are apparently brittle (but the copy functions are actually used by the
117  // code; the move functions will be implicitly-defined as deleted anyway) and should probably
118  // only be used with DestructorPolicy::KeepELements:
119  SwVectorModifyBase(SwVectorModifyBase const &) = default;
121  SwVectorModifyBase & operator =(SwVectorModifyBase const &) = default;
123 
124  void DeleteAndDestroy(int aStartIdx, int aEndIdx)
125  {
126  if (aEndIdx < aStartIdx)
127  return;
128  for (const_iterator it = begin() + aStartIdx;
129  it != begin() + aEndIdx; ++it)
130  delete *it;
131  erase( begin() + aStartIdx, begin() + aEndIdx);
132  }
133 
134  size_t GetPos(Value const& p) const
135  {
136  const_iterator const it = std::find(begin(), end(), p);
137  return it == end() ? SIZE_MAX : it - begin();
138  }
139 
141  bool IsAlive(typename std::remove_pointer<Value>::type const*const p) const
142  { return std::find(begin(), end(), p) != end(); }
143 
144  static void dumpAsXml(xmlTextWriterPtr /*pWriter*/) {};
145 };
146 
147 template<typename Value>
149 {
150 protected:
153  : SwVectorModifyBase<Value>(policy) {}
154 
155 public:
156  virtual size_t GetFormatCount() const override
157  { return SwVectorModifyBase<Value>::size(); }
158 
159  virtual Value GetFormat(size_t idx) const override
161 
162  size_t GetPos(const SwFormat *p) const
163  { return SwVectorModifyBase<Value>::GetPos( static_cast<Value>( const_cast<SwFormat*>( p ) ) ); }
164 
165  // Override return type to reduce casting
166  virtual Value FindFormatByName(const OUString& rName) const override
167  { return static_cast<Value>(SwFormatsBase::FindFormatByName(rName)); }
168 };
169 
170 class SwGrfFormatColls final : public SwFormatsModifyBase<SwGrfFormatColl*>
171 {
172 public:
174 };
175 
176 
178 class SwFrameFormatsV final : public SwFormatsModifyBase<SwFrameFormat*>
179 {
180 public:
182 };
183 
184 class SwTextFormatColls final : public SwFormatsModifyBase<SwTextFormatColl*>
185 {
186 public:
188  void dumpAsXml(xmlTextWriterPtr pWriter) const;
189 };
190 
192 class SwSectionFormats final : public SwFormatsModifyBase<SwSectionFormat*>
193 {
194 public:
195  void dumpAsXml(xmlTextWriterPtr pWriter) const;
196 };
197 
198 class SwFieldTypes : public std::vector<std::unique_ptr<SwFieldType>> {
199 public:
200  void dumpAsXml(xmlTextWriterPtr pWriter) const;
201 };
202 
203 class SwTOXTypes : public std::vector<std::unique_ptr<SwTOXType>> {};
204 
205 class SwNumRuleTable final : public SwVectorModifyBase<SwNumRule*> {
206 public:
207  void dumpAsXml(xmlTextWriterPtr pWriter) const;
208 };
209 
211 {
212  bool operator()(SwRangeRedline* const &lhs, SwRangeRedline* const &rhs) const;
213 };
214 
215 // Notification type for notifying about redlines to LOK clients
217 
219 {
220 public:
224  static constexpr size_type npos = SAL_MAX_INT32;
225 private:
226  vector_type maVector;
230 public:
231  ~SwRedlineTable();
232  bool Contains(const SwRangeRedline* p) const { return maVector.find(const_cast<SwRangeRedline*>(p)) != maVector.end(); }
233  size_type GetPos(const SwRangeRedline* p) const;
234 
235  bool Insert(SwRangeRedline*& p);
236  bool Insert(SwRangeRedline*& p, size_type& rInsPos);
237  bool InsertWithValidRanges(SwRangeRedline*& p, size_type* pInsPos = nullptr);
239 
240  void Remove( size_type nPos );
241  void Remove( const SwRangeRedline* p );
242  void DeleteAndDestroy(size_type nPos);
243  void DeleteAndDestroyAll();
244 
245  void dumpAsXml(xmlTextWriterPtr pWriter) const;
246 
247  size_type FindNextOfSeqNo( size_type nSttPos ) const;
248  size_type FindPrevOfSeqNo( size_type nSttPos ) const;
252  size_type FindNextSeqNo( sal_uInt16 nSeqNo, size_type nSttPos ) const;
253  size_type FindPrevSeqNo( sal_uInt16 nSeqNo, size_type nSttPos ) const;
254 
262  const SwRangeRedline* FindAtPosition( const SwPosition& startPosition, size_type& tableIndex, bool next = true ) const;
263  // is there a redline with the same text content from the same author (near the redline),
264  // but with the opposite type (Insert or Delete). It's used to recognize tracked text moving.
265  bool isMoved(size_type tableIndex) const;
266 
267  bool empty() const { return maVector.empty(); }
268  size_type size() const { return maVector.size(); }
269  SwRangeRedline* operator[]( size_type idx ) const { return maVector[idx]; }
270  vector_type::const_iterator begin() const { return maVector.begin(); }
271  vector_type::const_iterator end() const { return maVector.end(); }
272  void Resort() { maVector.Resort(); }
273 
274  // Notifies all LOK clients when redlines are added/modified/removed
275  static void LOKRedlineNotification(RedlineNotification eType, SwRangeRedline* pRedline);
276 
277 private:
279 };
280 
283 {
284 private:
285  std::vector<SwExtraRedline*> m_aExtraRedlines;
286 
287 public:
289 
290  void Insert( SwExtraRedline* p );
291 
292  void DeleteAndDestroy( sal_uInt16 nPos);
293  void DeleteAndDestroyAll();
294 
295  void dumpAsXml(xmlTextWriterPtr pWriter) const;
296 
297  sal_uInt16 GetSize() const { return m_aExtraRedlines.size(); }
298  SwExtraRedline* GetRedline( sal_uInt16 uIndex ) const { return m_aExtraRedlines.operator[]( uIndex ); }
299 
300  SW_DLLPUBLIC bool DeleteAllTableRedlines( SwDoc& rDoc, const SwTable& rTable, bool bSaveInUndo, RedlineType nRedlineTypeToDelete );
301  bool DeleteTableRowRedline ( SwDoc* pDoc, const SwTableLine& rTableLine, bool bSaveInUndo, RedlineType nRedlineTypeToDelete );
302  bool DeleteTableCellRedline( SwDoc* pDoc, const SwTableBox& rTableBox, bool bSaveInUndo, RedlineType nRedlineTypeToDelete );
303 };
304 
305 typedef std::vector<SwOLENode*> SwOLENodes;
306 
307 #endif // INCLUDED_SW_INC_DOCARY_HXX
308 
309 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
void DeleteAndDestroy(int aStartIdx, int aEndIdx)
Definition: docary.hxx:124
void Resort()
Definition: docary.hxx:272
void insert(iterator aIt, TInputIterator aFirst, TInputIterator aLast)
Definition: docary.hxx:98
virtual Value FindFormatByName(const OUString &rName) const override
Definition: docary.hxx:166
bool InsertWithValidRanges(SwRangeRedline *&p, size_type *pInsPos=nullptr)
Definition: docredln.cxx:597
void Insert(SwExtraRedline *p)
Definition: docredln.cxx:2007
static void dumpAsXml(xmlTextWriterPtr)
Definition: docary.hxx:144
Marks a position in the document model.
Definition: pam.hxx:36
void reserve(size_type nSize)
Definition: docary.hxx:103
vector_type::const_iterator end() const
Definition: docary.hxx:271
bool Contains(const SwRangeRedline *p) const
Definition: docary.hxx:232
size_type FindPrevOfSeqNo(size_type nSttPos) const
Definition: docredln.cxx:690
iterator erase(iterator aFirst, iterator aLast)
Definition: docary.hxx:95
virtual SwFormat * FindFormatByName(const OUString &rName) const
Definition: format.cxx:772
void Remove(size_type nPos)
Definition: docredln.cxx:646
Definition: doc.hxx:188
size_t GetPos(Value const &p) const
Definition: docary.hxx:134
const_iterator find(const Value &x) const
virtual size_t GetFormatCount() const override
Definition: docary.hxx:156
SwTableLine is one table row in the document model.
Definition: swtable.hxx:358
virtual ~SwFormatsBase()
Definition: format.cxx:769
SwExtraRedline * GetRedline(sal_uInt16 uIndex) const
Definition: docary.hxx:298
std::vector< Value >::const_iterator const_iterator
Definition: docary.hxx:66
Value const & at(size_type nPos) const
Definition: docary.hxx:104
std::vector< Value > mvVals
Definition: docary.hxx:77
void dumpAsXml(xmlTextWriterPtr pWriter) const
Definition: number.cxx:1524
bool empty() const
Definition: docary.hxx:86
size_t GetPos(const SwFormat *p) const
Definition: docary.hxx:162
Array of Undo-history.
Definition: docary.hxx:192
size_type size() const
Definition: docary.hxx:268
Value
provides some methods for generic operations on lists that contain SwFormat* subclasses.
Definition: docary.hxx:44
const_iterator end() const
Definition: docary.hxx:92
bool empty() const
Definition: docary.hxx:267
Unsorted, undeleting SwFrameFormat vector.
Definition: docary.hxx:178
vector_type::const_iterator begin() const
Definition: docary.hxx:270
SwFormatsBase()=default
static void LOKRedlineNotification(RedlineNotification eType, SwRangeRedline *pRedline)
Emits LOK notification about one addition / removal of a redline item.
Definition: docredln.cxx:357
bool HasOverlappingElements() const
Definition: docary.hxx:238
void DeleteAndDestroy(sal_uInt16 nPos)
Definition: docredln.cxx:2013
void dumpAsXml(xmlTextWriterPtr pWriter) const
Definition: fmtcol.cxx:496
size_type size() const
bool IsAlive(typename std::remove_pointer< Value >::type const *const p) const
check that given format is still alive (i.e. contained here)
Definition: docary.hxx:141
Base class for various Writer styles.
Definition: format.hxx:46
vector_type maVector
Definition: docary.hxx:226
sal_uInt16 GetSize() const
Definition: docary.hxx:297
Table that holds 'extra' redlines, such as 'table row insert/delete', 'paragraph moves' etc...
Definition: docary.hxx:282
size_type FindPrevSeqNo(sal_uInt16 nSeqNo, size_type nSttPos) const
Definition: docredln.cxx:721
struct _xmlTextWriter * xmlTextWriterPtr
size_type GetPos(const SwRangeRedline *p) const
Definition: docredln.cxx:630
Value const & front() const
Definition: docary.hxx:87
#define SAL_MAX_INT32
std::vector< SwOLENode * > SwOLENodes
Definition: docary.hxx:305
std::vector< Value >::value_type value_type
Definition: docary.hxx:68
void DeleteAndDestroy(size_type nPos)
Definition: docredln.cxx:675
iterator begin()
Definition: docary.hxx:89
std::vector< Value >::size_type size_type
Definition: docary.hxx:67
void dumpAsXml(xmlTextWriterPtr pWriter) const
Definition: section.cxx:1029
void DeleteAndDestroyAll()
Definition: docredln.cxx:663
virtual size_t GetFormatCount() const =0
vector_type::size_type size_type
Definition: docary.hxx:223
SwFormatsBase & operator=(SwFormatsBase const &)=default
bool DeleteTableRowRedline(SwDoc *pDoc, const SwTableLine &rTableLine, bool bSaveInUndo, RedlineType nRedlineTypeToDelete)
Definition: docredln.cxx:209
size_t size() const
Definition: docary.hxx:88
void dumpAsXml(xmlTextWriterPtr pWriter) const
Definition: fldbas.cxx:213
void dumpAsXml(xmlTextWriterPtr pWriter) const
Definition: docredln.cxx:820
std::vector< Value >::iterator iterator
Definition: docary.hxx:65
const SwRangeRedline * FindAtPosition(const SwPosition &startPosition, size_type &tableIndex, bool next=true) const
Find the redline at the given position.
Definition: docredln.cxx:742
iterator erase(iterator aIt)
Definition: docary.hxx:94
Value const & operator[](size_type nPos) const
Definition: docary.hxx:105
const_iterator end() const
bool empty() const
#define SW_DLLPUBLIC
Definition: swdllapi.h:28
Base object for 'Redlines' that are not of 'Ranged' type (like table row insert)
Definition: redline.hxx:269
SwVectorModifyBase & operator=(SwVectorModifyBase const &)=default
SwFormatsModifyBase(typename SwVectorModifyBase< Value >::DestructorPolicy policy=SwVectorModifyBase< Value >::DestructorPolicy::FreeElements)
Definition: docary.hxx:151
SW_DLLPUBLIC bool DeleteAllTableRedlines(SwDoc &rDoc, const SwTable &rTable, bool bSaveInUndo, RedlineType nRedlineTypeToDelete)
Definition: docredln.cxx:133
iterator end()
Definition: docary.hxx:91
SwTable is one table in the document model, containing rows (which contain cells).
Definition: swtable.hxx:113
SwVectorModifyBase(DestructorPolicy policy=DestructorPolicy::FreeElements)
Definition: docary.hxx:82
iterator insert(iterator aIt, Value const &rVal)
Definition: docary.hxx:96
void push_back(Value const &rVal)
Definition: docary.hxx:102
const sal_uInt16 idx[]
const_iterator begin() const
virtual Value GetFormat(size_t idx) const override
Definition: docary.hxx:159
size_type FindNextOfSeqNo(size_type nSttPos) const
Definition: docredln.cxx:683
const DestructorPolicy mPolicy
Definition: docary.hxx:78
bool isMoved(size_type tableIndex) const
Definition: docredln.cxx:769
SwRangeRedline * operator[](size_type idx) const
Definition: docary.hxx:269
const_iterator begin() const
Definition: docary.hxx:90
void dumpAsXml(xmlTextWriterPtr pWriter) const
Definition: docredln.cxx:89
SwTableBox is one table cell in the document model.
Definition: swtable.hxx:406
Value & operator[](size_type nPos)
Definition: docary.hxx:106
size_type FindNextSeqNo(sal_uInt16 nSeqNo, size_type nSttPos) const
Search next or previous Redline with the same Seq.
Definition: docredln.cxx:698
RedlineNotification
Definition: docary.hxx:216
void DeleteAndDestroyAll()
Definition: docredln.cxx:2032
o3tl::sorted_vector< SwRangeRedline *, CompareSwRedlineTable, o3tl::find_partialorder_ptrequals > vector_type
Definition: docary.hxx:222
RedlineType
virtual SwFormat * GetFormat(size_t idx) const =0
bool DeleteTableCellRedline(SwDoc *pDoc, const SwTableBox &rTableBox, bool bSaveInUndo, RedlineType nRedlineTypeToDelete)
Definition: docredln.cxx:253
bool Insert(SwRangeRedline *&p)
Definition: docredln.cxx:431
std::vector< Value >::size_type size_type
bool m_bHasOverlappingElements
Sometimes we load bad data, and we need to know if we can use fast binary search, or if we have to fa...
Definition: docary.hxx:229
void CheckOverlapping(vector_type::const_iterator it)
Definition: docredln.cxx:446
static constexpr size_type npos
Definition: docary.hxx:224
bool operator()(SwRangeRedline *const &lhs, SwRangeRedline *const &rhs) const
Definition: docredln.cxx:620
virtual ~SwVectorModifyBase()
Definition: docary.hxx:109
std::vector< SwExtraRedline * > m_aExtraRedlines
Definition: docary.hxx:285