Basic compiler undetected typo
[LibreOffice.git] / sw / inc / ndindex.hxx
blob7d03b0bcadaf4f55cc2c8c47fd5f808a895744ec
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"
29 #include "nodeoffset.hxx"
31 /// Marks a node in the document model.
32 class SW_DLLPUBLIC SwNodeIndex final : public sw::Ring<SwNodeIndex>
34 SwNode * m_pNode;
36 void RegisterIndex( SwNodes& rNodes )
38 if(!rNodes.m_vIndices)
39 rNodes.m_vIndices = this;
40 MoveTo(rNodes.m_vIndices);
42 void DeRegisterIndex( SwNodes& rNodes )
44 if(rNodes.m_vIndices == this)
45 rNodes.m_vIndices = GetNextInRing();
46 MoveTo(nullptr);
47 if(rNodes.m_vIndices == this)
48 rNodes.m_vIndices = nullptr;
51 public:
52 SwNodeIndex( SwNodes& rNds, sal_Int32 nIdx ) : SwNodeIndex(rNds, SwNodeOffset(nIdx)) {}
53 SwNodeIndex( SwNodes& rNds, SwNodeOffset nIdx = SwNodeOffset(0) )
54 : m_pNode( rNds[ nIdx ] )
56 RegisterIndex( rNds );
58 SwNodeIndex( const SwNodeIndex& rIdx, sal_Int32 nDiff ) : SwNodeIndex(rIdx, SwNodeOffset(nDiff)) {}
59 SwNodeIndex( const SwNodeIndex& rIdx, SwNodeOffset nDiff = SwNodeOffset(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, sal_Int32 nDiff ) : SwNodeIndex(rNd, SwNodeOffset(nDiff)) {}
70 SwNodeIndex( const SwNode& rNd, SwNodeOffset nDiff = SwNodeOffset(0) )
72 if( nDiff )
73 m_pNode = rNd.GetNodes()[ rNd.GetIndex() + nDiff ];
74 else
75 m_pNode = const_cast<SwNode*>(&rNd);
76 RegisterIndex( m_pNode->GetNodes() );
79 virtual ~SwNodeIndex() override
80 { DeRegisterIndex( m_pNode->GetNodes() ); }
82 inline SwNodeOffset operator++();
83 inline SwNodeOffset operator--();
84 inline SwNodeOffset operator++(int);
85 inline SwNodeOffset operator--(int);
87 inline SwNodeOffset operator+=( SwNodeOffset );
88 inline SwNodeOffset operator-=( SwNodeOffset );
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;
95 inline bool operator!=( const SwNodeIndex& ) const;
97 inline bool operator< ( SwNodeOffset ) const;
98 inline bool operator<=( SwNodeOffset ) const;
99 inline bool operator> ( SwNodeOffset ) const;
100 inline bool operator>=( SwNodeOffset ) const;
101 inline bool operator==( SwNodeOffset ) const;
102 inline bool operator!=( SwNodeOffset ) const;
104 inline SwNodeIndex& operator=( SwNodeOffset );
105 inline SwNodeIndex& operator=( const SwNodeIndex& );
106 inline SwNodeIndex& operator=( const SwNode& );
108 // Return value of index as SwNodeOffset.
109 inline SwNodeOffset GetIndex() const;
111 // Enables assignments without creation of a temporary object.
112 inline SwNodeIndex& Assign( SwNodes const & rNds, SwNodeOffset );
113 SwNodeIndex& Assign( const SwNode& rNd, sal_Int32 nOffset ) { return Assign(rNd, SwNodeOffset(nOffset)); }
114 inline SwNodeIndex& Assign( const SwNode& rNd, SwNodeOffset nOffset = SwNodeOffset(0) );
116 // Gets pointer on NodesArray.
117 inline const SwNodes& GetNodes() const;
118 inline SwNodes& GetNodes();
120 SwNodeIndex* GetNext() { return GetNextInRing(); }
121 SwNode& GetNode() const { return *m_pNode; }
124 inline std::ostream &operator <<(std::ostream& s, const SwNodeIndex& index)
126 return s << "SwNodeIndex (node " << sal_Int32(index.GetIndex()) << ")";
129 // SwRange
131 class SW_DLLPUBLIC SwNodeRange
133 public:
134 SwNodeIndex aStart;
135 SwNodeIndex aEnd;
137 SwNodeRange( const SwNodeIndex &rS, const SwNodeIndex &rE )
138 : aStart( rS ), aEnd( rE ) {};
139 SwNodeRange( const SwNodeRange &rRange )
140 : aStart( rRange.aStart ), aEnd( rRange.aEnd ) {};
142 SwNodeRange( SwNodes& rNds, SwNodeOffset nSttIdx, SwNodeOffset nEndIdx = SwNodeOffset(0) )
143 : aStart( rNds, nSttIdx ), aEnd( rNds, nEndIdx ) {};
145 SwNodeRange( const SwNodeIndex& rS, SwNodeOffset nSttDiff, const SwNodeIndex& rE, SwNodeOffset nEndDiff = SwNodeOffset(0) )
146 : aStart( rS, nSttDiff ), aEnd( rE, nEndDiff ) {};
147 SwNodeRange( const SwNode& rS, SwNodeOffset nSttDiff, const SwNode& rE, SwNodeOffset nEndDiff = SwNodeOffset(0) )
148 : aStart( rS, nSttDiff ), aEnd( rE, nEndDiff ) {};
151 // For inlines node.hxx is needed which in turn needs this one.
152 // Therefore all inlines accessing m_pNode are implemented here.
154 inline SwNodeOffset SwNodeIndex::GetIndex() const
156 return m_pNode->GetIndex();
158 inline const SwNodes& SwNodeIndex::GetNodes() const
160 return m_pNode->GetNodes();
162 inline SwNodes& SwNodeIndex::GetNodes()
164 return m_pNode->GetNodes();
166 inline bool SwNodeIndex::operator< ( SwNodeOffset const nOther ) const
168 return m_pNode->GetIndex() < nOther;
170 inline bool SwNodeIndex::operator<=( SwNodeOffset const nOther ) const
172 return m_pNode->GetIndex() <= nOther;
174 inline bool SwNodeIndex::operator> ( SwNodeOffset const nOther ) const
176 return m_pNode->GetIndex() > nOther;
178 inline bool SwNodeIndex::operator>=( SwNodeOffset const nOther ) const
180 return m_pNode->GetIndex() >= nOther;
182 inline bool SwNodeIndex::operator==( SwNodeOffset const nOther ) const
184 return m_pNode->GetIndex() == nOther;
186 inline bool SwNodeIndex::operator!=( SwNodeOffset const nOther ) const
188 return m_pNode->GetIndex() != nOther;
190 inline bool SwNodeIndex::operator<( const SwNodeIndex& rIndex ) const
192 return m_pNode->GetIndex() < rIndex.GetIndex();
194 inline bool SwNodeIndex::operator<=( const SwNodeIndex& rIndex ) const
196 return m_pNode->GetIndex() <= rIndex.GetIndex();
198 inline bool SwNodeIndex::operator>( const SwNodeIndex& rIndex ) const
200 return m_pNode->GetIndex() > rIndex.GetIndex();
202 inline bool SwNodeIndex::operator>=( const SwNodeIndex& rIndex ) const
204 return m_pNode->GetIndex() >= rIndex.GetIndex();
206 inline bool SwNodeIndex::operator==( const SwNodeIndex& rIdx ) const
208 return m_pNode == rIdx.m_pNode;
210 inline bool SwNodeIndex::operator!=( const SwNodeIndex& rIdx ) const
212 return m_pNode != rIdx.m_pNode;
215 inline SwNodeOffset SwNodeIndex::operator++()
217 m_pNode = GetNodes()[ m_pNode->GetIndex() + 1 ];
218 return m_pNode->GetIndex();
220 inline SwNodeOffset SwNodeIndex::operator--()
222 m_pNode = GetNodes()[ m_pNode->GetIndex() - 1 ];
223 return m_pNode->GetIndex();
225 inline SwNodeOffset SwNodeIndex::operator++(int)
227 SwNodeOffset nOldIndex = m_pNode->GetIndex();
228 m_pNode = GetNodes()[ nOldIndex + 1 ];
229 return nOldIndex;
231 inline SwNodeOffset SwNodeIndex::operator--(int)
233 SwNodeOffset nOldIndex = m_pNode->GetIndex();
234 m_pNode = GetNodes()[ nOldIndex - 1 ];
235 return nOldIndex;
238 inline SwNodeOffset SwNodeIndex::operator+=( SwNodeOffset const nOffset )
240 m_pNode = GetNodes()[ m_pNode->GetIndex() + nOffset ];
241 return m_pNode->GetIndex();
243 inline SwNodeOffset SwNodeIndex::operator-=( SwNodeOffset const nOffset )
245 m_pNode = GetNodes()[ m_pNode->GetIndex() - nOffset ];
246 return m_pNode->GetIndex();
249 inline SwNodeIndex& SwNodeIndex::operator=( SwNodeOffset const nNew )
251 m_pNode = GetNodes()[ nNew ];
252 return *this;
255 SwNodeIndex& SwNodeIndex::operator=( const SwNodeIndex& rIdx )
257 *this = *(rIdx.m_pNode);
258 return *this;
261 SwNodeIndex& SwNodeIndex::operator=( const SwNode& rNd )
263 if (&m_pNode->GetNodes() != &rNd.GetNodes())
265 DeRegisterIndex( m_pNode->GetNodes() );
266 m_pNode = const_cast<SwNode*>(&rNd);
267 RegisterIndex( m_pNode->GetNodes() );
269 else
270 m_pNode = const_cast<SwNode*>(&rNd);
271 return *this;
274 SwNodeIndex& SwNodeIndex::Assign( SwNodes const & rNds, SwNodeOffset nIdx )
276 *this = *rNds[ nIdx ];
277 return *this;
280 SwNodeIndex& SwNodeIndex::Assign( const SwNode& rNd, SwNodeOffset nOffset )
282 *this = rNd;
284 if( nOffset )
285 m_pNode = m_pNode->GetNodes()[ m_pNode->GetIndex() + nOffset ];
287 return *this;
290 #endif
292 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */