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
27 #include "nodeoffset.hxx"
29 /// Marks a node in the document model.
30 class SAL_WARN_UNUSED SW_DLLPUBLIC SwNodeIndex final
: public sw::Ring
<SwNodeIndex
>
36 if(!m_pNode
->m_vIndices
)
38 #if defined(__GNUC__) && (__GNUC__ == 12 || __GNUC__ == 13)
39 #pragma GCC diagnostic push
40 #pragma GCC diagnostic ignored "-Wdangling-pointer"
42 m_pNode
->m_vIndices
= this;
43 #if defined(__GNUC__) && (__GNUC__ == 12 || __GNUC__ == 13)
44 #pragma GCC diagnostic pop
47 MoveTo(m_pNode
->m_vIndices
);
49 void DeRegisterIndex()
51 if(m_pNode
->m_vIndices
== this)
52 m_pNode
->m_vIndices
= GetNextInRing();
54 if(m_pNode
->m_vIndices
== this)
55 m_pNode
->m_vIndices
= nullptr;
58 SwNodeIndex(SwNode
* pNode
) : m_pNode(pNode
) { RegisterIndex(); }
61 SwNodeIndex( SwNodes
& rNds
, sal_Int32 nIdx
) : SwNodeIndex(rNds
, SwNodeOffset(nIdx
)) {}
62 explicit SwNodeIndex( SwNodes
& rNds
, SwNodeOffset nIdx
= SwNodeOffset(0) )
63 : SwNodeIndex( rNds
[ nIdx
] ) {}
65 SwNodeIndex( const SwNodeIndex
& rIdx
, sal_Int32 nDiff
) : SwNodeIndex(rIdx
, SwNodeOffset(nDiff
)) {}
66 SwNodeIndex( const SwNodeIndex
& rIdx
, SwNodeOffset nDiff
= SwNodeOffset(0) )
67 : SwNodeIndex( nDiff
? rIdx
.GetNodes()[ rIdx
.GetIndex() + nDiff
] : rIdx
.m_pNode
) {}
69 SwNodeIndex( const SwNode
& rNd
, sal_Int32 nDiff
) : SwNodeIndex(rNd
, SwNodeOffset(nDiff
)) {}
70 explicit SwNodeIndex( const SwNode
& rNd
)
71 : SwNodeIndex( const_cast<SwNode
*>(&rNd
) ) {}
72 explicit SwNodeIndex( const SwNode
& rNd
, SwNodeOffset nDiff
)
73 : SwNodeIndex( nDiff
? *rNd
.GetNodes()[ rNd
.GetIndex() + nDiff
] : rNd
) {}
75 virtual ~SwNodeIndex() override
{ DeRegisterIndex(); }
77 SwNodeIndex
& operator++() { return operator+=(SwNodeOffset(1)); }
78 SwNodeIndex
& operator--() { return operator-=(SwNodeOffset(1)); }
80 SwNodeIndex
& operator+=( SwNodeOffset nOffset
) { return operator=(GetIndex() + nOffset
); }
81 SwNodeIndex
& operator-=( SwNodeOffset nOffset
) { return operator=(GetIndex() - nOffset
); }
83 bool operator<( const SwNodeIndex
& rIndex
) const { return operator<(rIndex
.GetNode()); }
84 bool operator<=( const SwNodeIndex
& rIndex
) const { return operator<=(rIndex
.GetNode()); }
85 bool operator>( const SwNodeIndex
& rIndex
) const { return operator>(rIndex
.GetNode()); }
86 bool operator>=( const SwNodeIndex
& rIndex
) const { return operator>=(rIndex
.GetNode()); }
87 bool operator==( const SwNodeIndex
& rIndex
) const { return operator==(rIndex
.GetNode()); }
88 bool operator!=( const SwNodeIndex
& rIndex
) const { return operator!=(rIndex
.GetNode()); }
90 bool operator<( SwNodeOffset nOther
) const { return GetIndex() < nOther
; }
91 bool operator<=( SwNodeOffset nOther
) const { return GetIndex() <= nOther
; }
92 bool operator>( SwNodeOffset nOther
) const { return GetIndex() > nOther
; }
93 bool operator>=( SwNodeOffset nOther
) const { return GetIndex() >= nOther
; }
94 bool operator==( SwNodeOffset nOther
) const { return GetIndex() == nOther
; }
95 bool operator!=( SwNodeOffset nOther
) const { return GetIndex() != nOther
; }
97 bool operator<( const SwNode
& rNd
) const { assert(&GetNodes() == &rNd
.GetNodes()); return operator<(rNd
.GetIndex()); }
98 bool operator<=( const SwNode
& rNd
) const { return operator==(rNd
) || operator<(rNd
); }
99 bool operator>( const SwNode
& rNd
) const { assert(&GetNodes() == &rNd
.GetNodes()); return operator>(rNd
.GetIndex()); }
100 bool operator>=( const SwNode
& rNd
) const { return operator==(rNd
) || operator>(rNd
); }
101 bool operator==( const SwNode
& rNd
) const { return m_pNode
== &rNd
; }
102 bool operator!=( const SwNode
& rNd
) const { return m_pNode
!= &rNd
; }
104 inline SwNodeIndex
& operator=( SwNodeOffset
);
105 SwNodeIndex
& operator=( const SwNodeIndex
& rIdx
) { return operator=(*rIdx
.m_pNode
); }
106 inline SwNodeIndex
& operator=( const SwNode
& );
108 // Return value of index as SwNodeOffset.
109 SwNodeOffset
GetIndex() const { return m_pNode
->GetIndex(); }
111 // Enables assignments without creation of a temporary object.
112 SwNodeIndex
& Assign( SwNodes
const & rNds
, SwNodeOffset nIdx
) { return operator=(*rNds
[nIdx
]); }
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 const SwNodes
& GetNodes() const { return m_pNode
->GetNodes(); }
118 SwNodes
& GetNodes() { return m_pNode
->GetNodes(); }
120 SwNode
& GetNode() const { return *m_pNode
; }
123 inline std::ostream
&operator <<(std::ostream
& s
, const SwNodeIndex
& index
)
125 return s
<< "SwNodeIndex (node " << sal_Int32(index
.GetIndex()) << ")";
130 class SW_DLLPUBLIC SwNodeRange
136 SwNodeRange( const SwNodeIndex
&rS
, const SwNodeIndex
&rE
)
137 : aStart( rS
), aEnd( rE
) {}
138 SwNodeRange( const SwNode
&rS
, const SwNode
&rE
)
139 : aStart( rS
), aEnd( rE
) {}
140 SwNodeRange( const SwNodeRange
&rRange
) = default;
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 SwNodeIndex
& SwNodeIndex::operator=( SwNodeOffset
const nNew
)
156 auto pNewNode
= GetNodes()[ nNew
];
157 if (pNewNode
!= m_pNode
)
160 m_pNode
= GetNodes()[ nNew
];
166 SwNodeIndex
& SwNodeIndex::operator=( const SwNode
& rNd
)
171 m_pNode
= const_cast<SwNode
*>(&rNd
);
177 SwNodeIndex
& SwNodeIndex::Assign( const SwNode
& rNd
, SwNodeOffset nOffset
)
179 const SwNode
* pNewNode
;
181 pNewNode
= rNd
.GetNodes()[ rNd
.GetIndex() + nOffset
];
184 return operator=(*pNewNode
);
189 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */