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_NDINDEX_HXX
20 #define INCLUDED_SW_INC_NDINDEX_HXX
25 #include <tools/solar.h>
34 /// Marks a node in the document model.
35 class SW_DLLPUBLIC SwNodeIndex SAL_FINAL
: public sw::Ring
<SwNodeIndex
>
39 // These are not allowed!
40 SwNodeIndex( SwNodes
& rNds
, sal_uInt16 nIdx
) SAL_DELETED_FUNCTION
;
41 SwNodeIndex( SwNodes
& rNds
, int nIdx
) SAL_DELETED_FUNCTION
;
42 void RegisterIndex( SwNodes
& rNodes
)
45 rNodes
.vIndices
= this;
46 MoveTo(rNodes
.vIndices
);
48 void DeRegisterIndex( SwNodes
& rNodes
)
50 if(rNodes
.vIndices
== this)
51 rNodes
.vIndices
= GetNextInRing();
53 if(rNodes
.vIndices
== this)
54 rNodes
.vIndices
= nullptr;
58 SwNodeIndex( SwNodes
& rNds
, sal_uLong nIdx
= 0 )
61 RegisterIndex( rNds
);
63 SwNodeIndex( const SwNodeIndex
& rIdx
, long nDiff
= 0 )
64 : sw::Ring
<SwNodeIndex
>()
67 pNd
= rIdx
.GetNodes()[ rIdx
.GetIndex() + nDiff
];
70 RegisterIndex( pNd
->GetNodes() );
73 SwNodeIndex( const SwNode
& rNd
, long nDiff
= 0 )
76 pNd
= rNd
.GetNodes()[ rNd
.GetIndex() + nDiff
];
78 pNd
= const_cast<SwNode
*>(&rNd
);
79 RegisterIndex( pNd
->GetNodes() );
82 virtual ~SwNodeIndex()
83 { DeRegisterIndex( pNd
->GetNodes() ); };
85 inline sal_uLong
operator++();
86 inline sal_uLong
operator--();
87 inline sal_uLong
operator++(int);
88 inline sal_uLong
operator--(int);
90 inline sal_uLong
operator+=( sal_uLong
);
91 inline sal_uLong
operator-=( sal_uLong
);
92 inline sal_uLong
operator+=( const SwNodeIndex
& );
93 inline sal_uLong
operator-=( const SwNodeIndex
& );
95 inline bool operator< ( const SwNodeIndex
& ) const;
96 inline bool operator<=( const SwNodeIndex
& ) const;
97 inline bool operator> ( const SwNodeIndex
& ) const;
98 inline bool operator>=( const SwNodeIndex
& ) const;
99 inline bool operator==( const SwNodeIndex
& ) const;
100 inline bool operator!=( const SwNodeIndex
& ) const;
102 inline bool operator< ( sal_uLong nWert
) const;
103 inline bool operator<=( sal_uLong nWert
) const;
104 inline bool operator> ( sal_uLong nWert
) const;
105 inline bool operator>=( sal_uLong nWert
) const;
106 inline bool operator==( sal_uLong nWert
) const;
107 inline bool operator!=( sal_uLong nWert
) const;
109 inline SwNodeIndex
& operator=( sal_uLong
);
110 inline SwNodeIndex
& operator=( const SwNodeIndex
& );
111 inline SwNodeIndex
& operator=( const SwNode
& );
113 // Return value of index as sal_uLong.
114 inline sal_uLong
GetIndex() const;
116 // Enables assignments without creation of a temporary object.
117 inline SwNodeIndex
& Assign( SwNodes
& rNds
, sal_uLong
);
118 inline SwNodeIndex
& Assign( const SwNode
& rNd
, long nOffset
= 0 );
120 // Gets pointer on NodesArray.
121 inline const SwNodes
& GetNodes() const;
122 inline SwNodes
& GetNodes();
124 SwNode
& GetNode() const { return *pNd
; }
127 inline std::ostream
&operator <<(std::ostream
& s
, const SwNodeIndex
& index
)
129 return s
<< "SwNodeIndex (node " << index
.GetIndex() << ")";
134 class SW_DLLPUBLIC SwNodeRange
140 SwNodeRange( const SwNodeIndex
&rS
, const SwNodeIndex
&rE
)
141 : aStart( rS
), aEnd( rE
) {};
142 SwNodeRange( const SwNodeRange
&rRange
)
143 : aStart( rRange
.aStart
), aEnd( rRange
.aEnd
) {};
145 SwNodeRange( SwNodes
& rNds
, sal_uLong nSttIdx
= 0, sal_uLong nEndIdx
= 0 )
146 : aStart( rNds
, nSttIdx
), aEnd( rNds
, nEndIdx
) {};
148 SwNodeRange( const SwNodeIndex
& rS
, long nSttDiff
, const SwNodeIndex
& rE
, long nEndDiff
= 0 )
149 : aStart( rS
, nSttDiff
), aEnd( rE
, nEndDiff
) {};
150 SwNodeRange( const SwNode
& rS
, long nSttDiff
, const SwNode
& rE
, long nEndDiff
= 0 )
151 : aStart( rS
, nSttDiff
), aEnd( rE
, nEndDiff
) {};
154 // For inlines node.hxx is needed which in turn needs this one.
155 // Therefore all inlines accessing pNd are implemented here.
157 inline sal_uLong
SwNodeIndex::GetIndex() const
159 return pNd
->GetIndex();
161 inline const SwNodes
& SwNodeIndex::GetNodes() const
163 return pNd
->GetNodes();
165 inline SwNodes
& SwNodeIndex::GetNodes()
167 return pNd
->GetNodes();
169 inline bool SwNodeIndex::operator< ( sal_uLong nWert
) const
171 return pNd
->GetIndex() < nWert
;
173 inline bool SwNodeIndex::operator<=( sal_uLong nWert
) const
175 return pNd
->GetIndex() <= nWert
;
177 inline bool SwNodeIndex::operator> ( sal_uLong nWert
) const
179 return pNd
->GetIndex() > nWert
;
181 inline bool SwNodeIndex::operator>=( sal_uLong nWert
) const
183 return pNd
->GetIndex() >= nWert
;
185 inline bool SwNodeIndex::operator==( sal_uLong nWert
) const
187 return pNd
->GetIndex() == nWert
;
189 inline bool SwNodeIndex::operator!=( sal_uLong nWert
) const
191 return pNd
->GetIndex() != nWert
;
193 inline bool SwNodeIndex::operator<( const SwNodeIndex
& rIndex
) const
195 return pNd
->GetIndex() < rIndex
.GetIndex();
197 inline bool SwNodeIndex::operator<=( const SwNodeIndex
& rIndex
) const
199 return pNd
->GetIndex() <= rIndex
.GetIndex();
201 inline bool SwNodeIndex::operator>( const SwNodeIndex
& rIndex
) const
203 return pNd
->GetIndex() > rIndex
.GetIndex();
205 inline bool SwNodeIndex::operator>=( const SwNodeIndex
& rIndex
) const
207 return pNd
->GetIndex() >= rIndex
.GetIndex();
209 inline bool SwNodeIndex::operator==( const SwNodeIndex
& rIdx
) const
211 return pNd
== rIdx
.pNd
;
213 inline bool SwNodeIndex::operator!=( const SwNodeIndex
& rIdx
) const
215 return pNd
!= rIdx
.pNd
;
218 inline sal_uLong
SwNodeIndex::operator++()
220 return ( pNd
= GetNodes()[ pNd
->GetIndex()+1 ] )->GetIndex();
222 inline sal_uLong
SwNodeIndex::operator--()
224 return ( pNd
= GetNodes()[ pNd
->GetIndex()-1 ] )->GetIndex();
226 inline sal_uLong
SwNodeIndex::operator++(int)
228 sal_uLong nOldIndex
= pNd
->GetIndex();
229 pNd
= GetNodes()[ nOldIndex
+ 1 ];
232 inline sal_uLong
SwNodeIndex::operator--(int)
234 sal_uLong nOldIndex
= pNd
->GetIndex();
235 pNd
= GetNodes()[ nOldIndex
- 1 ];
239 inline sal_uLong
SwNodeIndex::operator+=( sal_uLong nWert
)
241 return ( pNd
= GetNodes()[ pNd
->GetIndex() + nWert
] )->GetIndex();
243 inline sal_uLong
SwNodeIndex::operator-=( sal_uLong nWert
)
245 return ( pNd
= GetNodes()[ pNd
->GetIndex() - nWert
] )->GetIndex();
247 inline sal_uLong
SwNodeIndex::operator+=( const SwNodeIndex
& rIndex
)
249 return ( pNd
= GetNodes()[ pNd
->GetIndex() + rIndex
.GetIndex() ] )->GetIndex();
251 inline sal_uLong
SwNodeIndex::operator-=( const SwNodeIndex
& rIndex
)
253 return ( pNd
= GetNodes()[ pNd
->GetIndex() - rIndex
.GetIndex() ] )->GetIndex();
256 inline SwNodeIndex
& SwNodeIndex::operator=( sal_uLong nWert
)
258 pNd
= GetNodes()[ nWert
];
262 SwNodeIndex
& SwNodeIndex::operator=( const SwNodeIndex
& rIdx
)
268 SwNodeIndex
& SwNodeIndex::operator=( const SwNode
& rNd
)
270 if( &pNd
->GetNodes() != &rNd
.GetNodes() )
272 DeRegisterIndex( pNd
->GetNodes() );
273 pNd
= const_cast<SwNode
*>(&rNd
);
274 RegisterIndex( pNd
->GetNodes() );
277 pNd
= const_cast<SwNode
*>(&rNd
);
281 SwNodeIndex
& SwNodeIndex::Assign( SwNodes
& rNds
, sal_uLong nIdx
)
283 *this = *rNds
[ nIdx
];
287 SwNodeIndex
& SwNodeIndex::Assign( const SwNode
& rNd
, long nOffset
)
292 pNd
= pNd
->GetNodes()[ pNd
->GetIndex() + nOffset
];
299 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */