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 <vector>
23 #include <type_traits>
24 #include <o3tl/sorted_vector.hxx>
25 
26 #include "fmtcol.hxx"
27 #include "frmfmt.hxx"
28 #include "section.hxx"
29 #include "tox.hxx"
30 #include "numrule.hxx"
31 #include "fldbas.hxx"
32 
33 class SwRangeRedline;
34 class SwExtraRedline;
35 class SwOLENode;
36 class SwTable;
37 class SwTableLine;
38 class SwTableBox;
39 struct SwPosition;
40 enum class RedlineType : sal_uInt16;
41 
44 {
45 public:
46  virtual size_t GetFormatCount() const = 0;
47  virtual SwFormat* GetFormat(size_t idx) const = 0;
48  virtual ~SwFormatsBase();
49 
50  // default linear search implementation, some subclasses will override with a more efficient search
51  virtual SwFormat* FindFormatByName(const OUString& rName) const;
52 
53  SwFormatsBase() = default;
54  SwFormatsBase(SwFormatsBase const &) = default;
55  SwFormatsBase(SwFormatsBase &&) = default;
56  SwFormatsBase & operator =(SwFormatsBase const &) = default;
57  SwFormatsBase & operator =(SwFormatsBase &&) = default;
58 };
59 
60 template<typename Value>
62 {
63 public:
64  typedef typename std::vector<Value>::iterator iterator;
65  typedef typename std::vector<Value>::const_iterator const_iterator;
66  typedef typename std::vector<Value>::size_type size_type;
67  typedef typename std::vector<Value>::value_type value_type;
68 
69 protected:
70  enum class DestructorPolicy {
73  };
74 
75 private:
76  typename std::vector<Value> mvVals;
78 
79 protected:
80  // default destructor deletes all contained elements
82  : mPolicy(policy) {}
83 
84 public:
85  bool empty() const { return mvVals.empty(); }
86  Value const& front() const { return mvVals.front(); }
87  size_t size() const { return mvVals.size(); }
88  iterator begin() { return mvVals.begin(); }
89  const_iterator begin() const { return mvVals.begin(); }
90  iterator end() { return mvVals.end(); }
91  const_iterator end() const { return mvVals.end(); }
92  void clear() { mvVals.clear(); }
93  iterator erase(iterator aIt) { return mvVals.erase(aIt); }
94  iterator erase(iterator aFirst, iterator aLast) { return mvVals.erase(aFirst, aLast); }
95  iterator insert(iterator aIt, Value const& rVal) { return mvVals.insert(aIt, rVal); }
96  template<typename TInputIterator>
97  void insert(iterator aIt, TInputIterator aFirst, TInputIterator aLast)
98  {
99  mvVals.insert(aIt, aFirst, aLast);
100  }
101  void push_back(Value const& rVal) { mvVals.push_back(rVal); }
102  void reserve(size_type nSize) { mvVals.reserve(nSize); }
103  Value const& at(size_type nPos) const { return mvVals.at(nPos); }
104  Value const& operator[](size_type nPos) const { return mvVals[nPos]; }
105  Value& operator[](size_type nPos) { return mvVals[nPos]; }
106 
107  // free any remaining child objects based on mPolicy
109  {
110  if (mPolicy == DestructorPolicy::FreeElements)
111  for(const_iterator it = begin(); it != end(); ++it)
112  delete *it;
113  }
114 
115  //TODO: These functions are apparently brittle (but the copy functions are actually used by the
116  // code; the move functions will be implicitly-defined as deleted anyway) and should probably
117  // only be used with DestructorPolicy::KeepELements:
118  SwVectorModifyBase(SwVectorModifyBase const &) = default;
120  SwVectorModifyBase & operator =(SwVectorModifyBase const &) = default;
122 
123  void DeleteAndDestroy(int aStartIdx, int aEndIdx)
124  {
125  if (aEndIdx < aStartIdx)
126  return;
127  for (const_iterator it = begin() + aStartIdx;
128  it != begin() + aEndIdx; ++it)
129  delete *it;
130  erase( begin() + aStartIdx, begin() + aEndIdx);
131  }
132 
133  size_t GetPos(Value const& p) const
134  {
135  const_iterator const it = std::find(begin(), end(), p);
136  return it == end() ? SIZE_MAX : it - begin();
137  }
138 
140  bool IsAlive(typename std::remove_pointer<Value>::type const*const p) const
141  { return std::find(begin(), end(), p) != end(); }
142 
143  static void dumpAsXml(xmlTextWriterPtr /*pWriter*/) {};
144 };
145 
146 template<typename Value>
148 {
149 protected:
152  : SwVectorModifyBase<Value>(policy) {}
153 
154 public:
155  virtual size_t GetFormatCount() const override
156  { return SwVectorModifyBase<Value>::size(); }
157 
158  virtual Value GetFormat(size_t idx) const override
160 
161  size_t GetPos(const SwFormat *p) const
162  { return SwVectorModifyBase<Value>::GetPos( static_cast<Value>( const_cast<SwFormat*>( p ) ) ); }
163 
164  // Override return type to reduce casting
165  virtual Value FindFormatByName(const OUString& rName) const override
166  { return static_cast<Value>(SwFormatsBase::FindFormatByName(rName)); }
167 };
168 
169 class SwGrfFormatColls final : public SwFormatsModifyBase<SwGrfFormatColl*>
170 {
171 public:
173 };
174 
175 
177 class SwFrameFormatsV final : public SwFormatsModifyBase<SwFrameFormat*>
178 {
179 public:
181 };
182 
183 class SwTextFormatColls final : public SwFormatsModifyBase<SwTextFormatColl*>
184 {
185 public:
187  void dumpAsXml(xmlTextWriterPtr pWriter) const;
188 };
189 
191 class SwSectionFormats final : public SwFormatsModifyBase<SwSectionFormat*>
192 {
193 public:
194  void dumpAsXml(xmlTextWriterPtr pWriter) const;
195 };
196 
197 class SwFieldTypes : public std::vector<std::unique_ptr<SwFieldType>> {
198 public:
199  void dumpAsXml(xmlTextWriterPtr pWriter) const;
200 };
201 
202 class SwTOXTypes : public std::vector<std::unique_ptr<SwTOXType>> {};
203 
204 class SwNumRuleTable final : public SwVectorModifyBase<SwNumRule*> {
205 public:
206  void dumpAsXml(xmlTextWriterPtr pWriter) const;
207 };
208 
210 {
211  bool operator()(SwRangeRedline* const &lhs, SwRangeRedline* const &rhs) const;
212 };
213 
214 // Notification type for notifying about redlines to LOK clients
216 
218 {
219 public:
223  static constexpr size_type npos = SAL_MAX_INT32;
224 private:
225  vector_type maVector;
229 public:
230  ~SwRedlineTable();
231  bool Contains(const SwRangeRedline* p) const { return maVector.find(const_cast<SwRangeRedline*>(p)) != maVector.end(); }
232  size_type GetPos(const SwRangeRedline* p) const;
233 
234  bool Insert(SwRangeRedline*& p);
235  bool Insert(SwRangeRedline*& p, size_type& rInsPos);
236  bool InsertWithValidRanges(SwRangeRedline*& p, size_type* pInsPos = nullptr);
238 
239  void Remove( size_type nPos );
240  void Remove( const SwRangeRedline* p );
241  void DeleteAndDestroy(size_type nPos);
242  void DeleteAndDestroyAll();
243 
244  void dumpAsXml(xmlTextWriterPtr pWriter) const;
245 
246  size_type FindNextOfSeqNo( size_type nSttPos ) const;
247  size_type FindPrevOfSeqNo( size_type nSttPos ) const;
251  size_type FindNextSeqNo( sal_uInt16 nSeqNo, size_type nSttPos ) const;
252  size_type FindPrevSeqNo( sal_uInt16 nSeqNo, size_type nSttPos ) const;
253 
261  const SwRangeRedline* FindAtPosition( const SwPosition& startPosition, size_type& tableIndex, bool next = true ) const;
262  // is there a redline with the same text content from the same author (near the redline),
263  // but with the opposite type (Insert or Delete). It's used to recognize tracked text moving.
264  bool isMoved(size_type tableIndex) const;
265 
266  bool empty() const { return maVector.empty(); }
267  size_type size() const { return maVector.size(); }
268  SwRangeRedline* operator[]( size_type idx ) const { return maVector[idx]; }
269  vector_type::const_iterator begin() const { return maVector.begin(); }
270  vector_type::const_iterator end() const { return maVector.end(); }
271  void Resort() { maVector.Resort(); }
272 
273  // Notifies all LOK clients when redlines are added/modified/removed
274  static void LOKRedlineNotification(RedlineNotification eType, SwRangeRedline* pRedline);
275 
276 private:
278 };
279 
282 {
283 private:
284  std::vector<SwExtraRedline*> m_aExtraRedlines;
285 
286 public:
288 
289  void Insert( SwExtraRedline* p );
290 
291  void DeleteAndDestroy( sal_uInt16 nPos);
292  void DeleteAndDestroyAll();
293 
294  void dumpAsXml(xmlTextWriterPtr pWriter) const;
295 
296  sal_uInt16 GetSize() const { return m_aExtraRedlines.size(); }
297  SwExtraRedline* GetRedline( sal_uInt16 uIndex ) const { return m_aExtraRedlines.operator[]( uIndex ); }
298 
299  SW_DLLPUBLIC bool DeleteAllTableRedlines( SwDoc& rDoc, const SwTable& rTable, bool bSaveInUndo, RedlineType nRedlineTypeToDelete );
300  bool DeleteTableRowRedline ( SwDoc* pDoc, const SwTableLine& rTableLine, bool bSaveInUndo, RedlineType nRedlineTypeToDelete );
301  bool DeleteTableCellRedline( SwDoc* pDoc, const SwTableBox& rTableBox, bool bSaveInUndo, RedlineType nRedlineTypeToDelete );
302 };
303 
304 typedef std::vector<SwOLENode*> SwOLENodes;
305 
306 #endif // INCLUDED_SW_INC_DOCARY_HXX
307 
308 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
void DeleteAndDestroy(int aStartIdx, int aEndIdx)
Definition: docary.hxx:123
void Resort()
Definition: docary.hxx:271
void insert(iterator aIt, TInputIterator aFirst, TInputIterator aLast)
Definition: docary.hxx:97
virtual Value FindFormatByName(const OUString &rName) const override
Definition: docary.hxx:165
bool InsertWithValidRanges(SwRangeRedline *&p, size_type *pInsPos=nullptr)
Definition: docredln.cxx:594
void Insert(SwExtraRedline *p)
Definition: docredln.cxx:2059
static void dumpAsXml(xmlTextWriterPtr)
Definition: docary.hxx:143
Marks a position in the document model.
Definition: pam.hxx:36
void reserve(size_type nSize)
Definition: docary.hxx:102
vector_type::const_iterator end() const
Definition: docary.hxx:270
bool Contains(const SwRangeRedline *p) const
Definition: docary.hxx:231
size_type FindPrevOfSeqNo(size_type nSttPos) const
Definition: docredln.cxx:687
iterator erase(iterator aFirst, iterator aLast)
Definition: docary.hxx:94
virtual SwFormat * FindFormatByName(const OUString &rName) const
Definition: format.cxx:767
void Remove(size_type nPos)
Definition: docredln.cxx:643
Definition: doc.hxx:187
size_t GetPos(Value const &p) const
Definition: docary.hxx:133
const_iterator find(const Value &x) const
virtual size_t GetFormatCount() const override
Definition: docary.hxx:155
SwTableLine is one table row in the document model.
Definition: swtable.hxx:357
virtual ~SwFormatsBase()
Definition: format.cxx:764
SwExtraRedline * GetRedline(sal_uInt16 uIndex) const
Definition: docary.hxx:297
std::vector< Value >::const_iterator const_iterator
Definition: docary.hxx:65
Value const & at(size_type nPos) const
Definition: docary.hxx:103
std::vector< Value > mvVals
Definition: docary.hxx:76
void dumpAsXml(xmlTextWriterPtr pWriter) const
Definition: number.cxx:1581
bool empty() const
Definition: docary.hxx:85
size_t GetPos(const SwFormat *p) const
Definition: docary.hxx:161
Array of Undo-history.
Definition: docary.hxx:191
size_type size() const
Definition: docary.hxx:267
Value
provides some methods for generic operations on lists that contain SwFormat* subclasses.
Definition: docary.hxx:43
const_iterator end() const
Definition: docary.hxx:91
bool empty() const
Definition: docary.hxx:266
Unsorted, undeleting SwFrameFormat vector.
Definition: docary.hxx:177
vector_type::const_iterator begin() const
Definition: docary.hxx:269
SwFormatsBase()=default
static void LOKRedlineNotification(RedlineNotification eType, SwRangeRedline *pRedline)
Emits LOK notification about one addition / removal of a redline item.
Definition: docredln.cxx:343
bool HasOverlappingElements() const
Definition: docary.hxx:237
void DeleteAndDestroy(sal_uInt16 nPos)
Definition: docredln.cxx:2065
void dumpAsXml(xmlTextWriterPtr pWriter) const
Definition: fmtcol.cxx:487
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:140
Base class for various Writer styles.
Definition: format.hxx:46
vector_type maVector
Definition: docary.hxx:225
sal_uInt16 GetSize() const
Definition: docary.hxx:296
Table that holds 'extra' redlines, such as 'table row insert/delete', 'paragraph moves' etc...
Definition: docary.hxx:281
size_type FindPrevSeqNo(sal_uInt16 nSeqNo, size_type nSttPos) const
Definition: docredln.cxx:718
size_type GetPos(const SwRangeRedline *p) const
Definition: docredln.cxx:627
Value const & front() const
Definition: docary.hxx:86
#define SAL_MAX_INT32
std::vector< SwOLENode * > SwOLENodes
Definition: docary.hxx:304
std::vector< Value >::value_type value_type
Definition: docary.hxx:67
void DeleteAndDestroy(size_type nPos)
Definition: docredln.cxx:672
iterator begin()
Definition: docary.hxx:88
std::vector< Value >::size_type size_type
Definition: docary.hxx:66
void dumpAsXml(xmlTextWriterPtr pWriter) const
Definition: section.cxx:1000
void DeleteAndDestroyAll()
Definition: docredln.cxx:660
virtual size_t GetFormatCount() const =0
vector_type::size_type size_type
Definition: docary.hxx:222
SwFormatsBase & operator=(SwFormatsBase const &)=default
bool DeleteTableRowRedline(SwDoc *pDoc, const SwTableLine &rTableLine, bool bSaveInUndo, RedlineType nRedlineTypeToDelete)
Definition: docredln.cxx:187
size_t size() const
Definition: docary.hxx:87
void dumpAsXml(xmlTextWriterPtr pWriter) const
Definition: fldbas.cxx:218
void dumpAsXml(xmlTextWriterPtr pWriter) const
Definition: docredln.cxx:857
std::vector< Value >::iterator iterator
Definition: docary.hxx:64
const SwRangeRedline * FindAtPosition(const SwPosition &startPosition, size_type &tableIndex, bool next=true) const
Find the redline at the given position.
Definition: docredln.cxx:739
iterator erase(iterator aIt)
Definition: docary.hxx:93
Value const & operator[](size_type nPos) const
Definition: docary.hxx:104
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:279
SwVectorModifyBase & operator=(SwVectorModifyBase const &)=default
SwFormatsModifyBase(typename SwVectorModifyBase< Value >::DestructorPolicy policy=SwVectorModifyBase< Value >::DestructorPolicy::FreeElements)
Definition: docary.hxx:150
SW_DLLPUBLIC bool DeleteAllTableRedlines(SwDoc &rDoc, const SwTable &rTable, bool bSaveInUndo, RedlineType nRedlineTypeToDelete)
Definition: docredln.cxx:136
iterator end()
Definition: docary.hxx:90
SwTable is one table in the document model, containing rows (which contain cells).
Definition: swtable.hxx:112
SwVectorModifyBase(DestructorPolicy policy=DestructorPolicy::FreeElements)
Definition: docary.hxx:81
iterator insert(iterator aIt, Value const &rVal)
Definition: docary.hxx:95
void push_back(Value const &rVal)
Definition: docary.hxx:101
const sal_uInt16 idx[]
const_iterator begin() const
virtual Value GetFormat(size_t idx) const override
Definition: docary.hxx:158
size_type FindNextOfSeqNo(size_type nSttPos) const
Definition: docredln.cxx:680
const DestructorPolicy mPolicy
Definition: docary.hxx:77
bool isMoved(size_type tableIndex) const
Definition: docredln.cxx:766
SwRangeRedline * operator[](size_type idx) const
Definition: docary.hxx:268
const_iterator begin() const
Definition: docary.hxx:89
void dumpAsXml(xmlTextWriterPtr pWriter) const
Definition: docredln.cxx:92
SwTableBox is one table cell in the document model.
Definition: swtable.hxx:418
Value & operator[](size_type nPos)
Definition: docary.hxx:105
size_type FindNextSeqNo(sal_uInt16 nSeqNo, size_type nSttPos) const
Search next or previous Redline with the same Seq.
Definition: docredln.cxx:695
RedlineNotification
Definition: docary.hxx:215
void DeleteAndDestroyAll()
Definition: docredln.cxx:2084
struct _xmlTextWriter * xmlTextWriterPtr
o3tl::sorted_vector< SwRangeRedline *, CompareSwRedlineTable, o3tl::find_partialorder_ptrequals > vector_type
Definition: docary.hxx:221
RedlineType
virtual SwFormat * GetFormat(size_t idx) const =0
bool DeleteTableCellRedline(SwDoc *pDoc, const SwTableBox &rTableBox, bool bSaveInUndo, RedlineType nRedlineTypeToDelete)
Definition: docredln.cxx:231
bool Insert(SwRangeRedline *&p)
Definition: docredln.cxx:417
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:228
void CheckOverlapping(vector_type::const_iterator it)
Definition: docredln.cxx:443
static constexpr size_type npos
Definition: docary.hxx:223
bool operator()(SwRangeRedline *const &lhs, SwRangeRedline *const &rhs) const
Definition: docredln.cxx:617
virtual ~SwVectorModifyBase()
Definition: docary.hxx:108
std::vector< SwExtraRedline * > m_aExtraRedlines
Definition: docary.hxx:284