Bump version to 21.06.18.1
[LibreOffice.git] / sw / inc / ndindex.hxx
blob303a7801ca6f579d4e9b63cc6ebd09f80eaba8a4
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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_NDINDEX_HXX
20 #define INCLUDED_SW_INC_NDINDEX_HXX
22 #include <iostream>
24 #include <tools/solar.h>
26 #include "node.hxx"
27 #include "ring.hxx"
28 #include "ndarr.hxx"
30 /// Marks a node in the document model.
31 class SW_DLLPUBLIC SwNodeIndex final : public sw::Ring<SwNodeIndex>
33 SwNode * m_pNode;
35 // These are not allowed!
36 SwNodeIndex( SwNodes& rNds, sal_uInt16 nIdx ) = delete;
37 SwNodeIndex( SwNodes& rNds, int nIdx ) = delete;
38 void RegisterIndex( SwNodes& rNodes )
40 if(!rNodes.m_vIndices)
41 rNodes.m_vIndices = this;
42 MoveTo(rNodes.m_vIndices);
44 void DeRegisterIndex( SwNodes& rNodes )
46 if(rNodes.m_vIndices == this)
47 rNodes.m_vIndices = GetNextInRing();
48 MoveTo(nullptr);
49 if(rNodes.m_vIndices == this)
50 rNodes.m_vIndices = nullptr;
53 public:
54 SwNodeIndex( SwNodes& rNds, sal_uLong nIdx = 0 )
55 : m_pNode( rNds[ nIdx ] )
57 RegisterIndex( rNds );
59 SwNodeIndex( const SwNodeIndex& rIdx, tools::Long nDiff = 0 )
60 : sw::Ring<SwNodeIndex>()
62 if( nDiff )
63 m_pNode = rIdx.GetNodes()[ rIdx.GetIndex() + nDiff ];
64 else
65 m_pNode = rIdx.m_pNode;
66 RegisterIndex( m_pNode->GetNodes() );
69 SwNodeIndex( const SwNode& rNd, tools::Long nDiff = 0 )
71 if( nDiff )
72 m_pNode = rNd.GetNodes()[ rNd.GetIndex() + nDiff ];
73 else
74 m_pNode = const_cast<SwNode*>(&rNd);
75 RegisterIndex( m_pNode->GetNodes() );
78 virtual ~SwNodeIndex() override
79 { DeRegisterIndex( m_pNode->GetNodes() ); }
81 inline sal_uLong operator++();
82 inline sal_uLong operator--();
83 inline sal_uLong operator++(int);
84 inline sal_uLong operator--(int);
86 inline sal_uLong operator+=( sal_uLong );
87 inline sal_uLong operator-=( sal_uLong );
89 inline bool operator< ( const SwNodeIndex& ) const;
90 inline bool operator<=( const SwNodeIndex& ) const;
91 inline bool operator> ( const SwNodeIndex& ) const;
92 inline bool operator>=( const SwNodeIndex& ) const;
93 inline bool operator==( const SwNodeIndex& ) const;
94 inline bool operator!=( const SwNodeIndex& ) const;
96 inline bool operator< ( sal_uLong ) const;
97 inline bool operator<=( sal_uLong ) const;
98 inline bool operator> ( sal_uLong ) const;
99 inline bool operator>=( sal_uLong ) const;
100 inline bool operator==( sal_uLong ) const;
101 inline bool operator!=( sal_uLong ) const;
103 inline SwNodeIndex& operator=( sal_uLong );
104 inline SwNodeIndex& operator=( const SwNodeIndex& );
105 inline SwNodeIndex& operator=( const SwNode& );
107 // Return value of index as sal_uLong.
108 inline sal_uLong GetIndex() const;
110 // Enables assignments without creation of a temporary object.
111 inline SwNodeIndex& Assign( SwNodes const & rNds, sal_uLong );
112 inline SwNodeIndex& Assign( const SwNode& rNd, tools::Long nOffset = 0 );
114 // Gets pointer on NodesArray.
115 inline const SwNodes& GetNodes() const;
116 inline SwNodes& GetNodes();
118 SwNodeIndex* GetNext() { return GetNextInRing(); }
119 SwNode& GetNode() const { return *m_pNode; }
122 inline std::ostream &operator <<(std::ostream& s, const SwNodeIndex& index)
124 return s << "SwNodeIndex (node " << index.GetIndex() << ")";
127 // SwRange
129 class SW_DLLPUBLIC SwNodeRange
131 public:
132 SwNodeIndex aStart;
133 SwNodeIndex aEnd;
135 SwNodeRange( const SwNodeIndex &rS, const SwNodeIndex &rE )
136 : aStart( rS ), aEnd( rE ) {};
137 SwNodeRange( const SwNodeRange &rRange )
138 : aStart( rRange.aStart ), aEnd( rRange.aEnd ) {};
140 SwNodeRange( SwNodes& rNds, sal_uLong nSttIdx, sal_uLong nEndIdx = 0 )
141 : aStart( rNds, nSttIdx ), aEnd( rNds, nEndIdx ) {};
143 SwNodeRange( const SwNodeIndex& rS, tools::Long nSttDiff, const SwNodeIndex& rE, tools::Long nEndDiff = 0 )
144 : aStart( rS, nSttDiff ), aEnd( rE, nEndDiff ) {};
145 SwNodeRange( const SwNode& rS, tools::Long nSttDiff, const SwNode& rE, tools::Long nEndDiff = 0 )
146 : aStart( rS, nSttDiff ), aEnd( rE, nEndDiff ) {};
149 // For inlines node.hxx is needed which in turn needs this one.
150 // Therefore all inlines accessing m_pNode are implemented here.
152 inline sal_uLong SwNodeIndex::GetIndex() const
154 return m_pNode->GetIndex();
156 inline const SwNodes& SwNodeIndex::GetNodes() const
158 return m_pNode->GetNodes();
160 inline SwNodes& SwNodeIndex::GetNodes()
162 return m_pNode->GetNodes();
164 inline bool SwNodeIndex::operator< ( sal_uLong const nOther ) const
166 return m_pNode->GetIndex() < nOther;
168 inline bool SwNodeIndex::operator<=( sal_uLong const nOther ) const
170 return m_pNode->GetIndex() <= nOther;
172 inline bool SwNodeIndex::operator> ( sal_uLong const nOther ) const
174 return m_pNode->GetIndex() > nOther;
176 inline bool SwNodeIndex::operator>=( sal_uLong const nOther ) const
178 return m_pNode->GetIndex() >= nOther;
180 inline bool SwNodeIndex::operator==( sal_uLong const nOther ) const
182 return m_pNode->GetIndex() == nOther;
184 inline bool SwNodeIndex::operator!=( sal_uLong const nOther ) const
186 return m_pNode->GetIndex() != nOther;
188 inline bool SwNodeIndex::operator<( const SwNodeIndex& rIndex ) const
190 return m_pNode->GetIndex() < rIndex.GetIndex();
192 inline bool SwNodeIndex::operator<=( const SwNodeIndex& rIndex ) const
194 return m_pNode->GetIndex() <= rIndex.GetIndex();
196 inline bool SwNodeIndex::operator>( const SwNodeIndex& rIndex ) const
198 return m_pNode->GetIndex() > rIndex.GetIndex();
200 inline bool SwNodeIndex::operator>=( const SwNodeIndex& rIndex ) const
202 return m_pNode->GetIndex() >= rIndex.GetIndex();
204 inline bool SwNodeIndex::operator==( const SwNodeIndex& rIdx ) const
206 return m_pNode == rIdx.m_pNode;
208 inline bool SwNodeIndex::operator!=( const SwNodeIndex& rIdx ) const
210 return m_pNode != rIdx.m_pNode;
213 inline sal_uLong SwNodeIndex::operator++()
215 m_pNode = GetNodes()[ m_pNode->GetIndex()+1 ];
216 return m_pNode->GetIndex();
218 inline sal_uLong SwNodeIndex::operator--()
220 m_pNode = GetNodes()[ m_pNode->GetIndex()-1 ];
221 return m_pNode->GetIndex();
223 inline sal_uLong SwNodeIndex::operator++(int)
225 sal_uLong nOldIndex = m_pNode->GetIndex();
226 m_pNode = GetNodes()[ nOldIndex + 1 ];
227 return nOldIndex;
229 inline sal_uLong SwNodeIndex::operator--(int)
231 sal_uLong nOldIndex = m_pNode->GetIndex();
232 m_pNode = GetNodes()[ nOldIndex - 1 ];
233 return nOldIndex;
236 inline sal_uLong SwNodeIndex::operator+=( sal_uLong const nOffset )
238 m_pNode = GetNodes()[ m_pNode->GetIndex() + nOffset ];
239 return m_pNode->GetIndex();
241 inline sal_uLong SwNodeIndex::operator-=( sal_uLong const nOffset )
243 m_pNode = GetNodes()[ m_pNode->GetIndex() - nOffset ];
244 return m_pNode->GetIndex();
247 inline SwNodeIndex& SwNodeIndex::operator=( sal_uLong const nNew )
249 m_pNode = GetNodes()[ nNew ];
250 return *this;
253 SwNodeIndex& SwNodeIndex::operator=( const SwNodeIndex& rIdx )
255 *this = *(rIdx.m_pNode);
256 return *this;
259 SwNodeIndex& SwNodeIndex::operator=( const SwNode& rNd )
261 if (&m_pNode->GetNodes() != &rNd.GetNodes())
263 DeRegisterIndex( m_pNode->GetNodes() );
264 m_pNode = const_cast<SwNode*>(&rNd);
265 RegisterIndex( m_pNode->GetNodes() );
267 else
268 m_pNode = const_cast<SwNode*>(&rNd);
269 return *this;
272 SwNodeIndex& SwNodeIndex::Assign( SwNodes const & rNds, sal_uLong nIdx )
274 *this = *rNds[ nIdx ];
275 return *this;
278 SwNodeIndex& SwNodeIndex::Assign( const SwNode& rNd, tools::Long nOffset )
280 *this = rNd;
282 if( nOffset )
283 m_pNode = m_pNode->GetNodes()[ m_pNode->GetIndex() + nOffset ];
285 return *this;
288 #endif
290 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */