Coverage Report

Created: 2025-11-16 09:57

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/sw/inc/contentindex.hxx
Line
Count
Source
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
#pragma once
20
21
#include <sal/types.h>
22
#include "swdllapi.h"
23
24
#include <o3tl/typed_flags_set.hxx>
25
26
#include <iostream>
27
28
class SwContentNode;
29
30
/// enum to allow us to cast without dynamic_cast (for performance)
31
enum class SwContentIndexOwnerType { Redline, Mark };
32
33
/// Pure abstract class for a pointer to something that "owns" a SwContentIndex
34
class ISwContentIndexOwner
35
{
36
public:
37
    virtual ~ISwContentIndexOwner();
38
    virtual SwContentIndexOwnerType GetOwnerType() const = 0;
39
};
40
41
/// Marks a character position inside a document model content node (SwContentNode)
42
class SAL_WARN_UNUSED SW_DLLPUBLIC SwContentIndex
43
{
44
private:
45
    friend class SwContentIndexReg;
46
47
    sal_Int32 m_nIndex;
48
    SwContentNode * m_pContentNode;
49
    // doubly linked list of Indexes registered at m_pIndexReg
50
    SwContentIndex * m_pNext;
51
    SwContentIndex * m_pPrev;
52
53
    /// This is either
54
    /// (*) nullptr
55
    /// (*) the SwRangeRedline (if any) that contains this SwContentIndex, via SwPosition and SwPaM
56
    /// (*) the sw::mark::MarkBase that owns this position to allow fast lookup of marks of an SwContentIndexReg.
57
    ISwContentIndexOwner * m_pOwner = nullptr;
58
59
    SwContentIndex& ChgValue( const SwContentIndex& rIdx, sal_Int32 nNewValue );
60
    void Init(sal_Int32 const nIdx);
61
    void Remove();
62
63
public:
64
    explicit SwContentIndex(const SwContentNode * pContentNode, sal_Int32 const nIdx = 0);
65
    SwContentIndex( const SwContentIndex & );
66
    SwContentIndex( const SwContentIndex &, short nDiff );
67
93.6M
    ~SwContentIndex() { Remove(); }
68
69
    SwContentIndex& operator=( sal_Int32 const );
70
    SwContentIndex& operator=( const SwContentIndex & );
71
72
    sal_Int32 operator++();
73
    sal_Int32 operator--();
74
    sal_Int32 operator--(int);
75
76
    sal_Int32 operator+=( sal_Int32 const );
77
    sal_Int32 operator-=( sal_Int32 const );
78
79
    bool operator< ( const SwContentIndex& ) const;
80
    bool operator<=( const SwContentIndex& ) const;
81
    bool operator> ( const SwContentIndex& ) const;
82
    bool operator>=( const SwContentIndex& ) const;
83
84
0
    bool operator< ( sal_Int32 const nVal ) const { return m_nIndex <  nVal; }
85
0
    bool operator<=( sal_Int32 const nVal ) const { return m_nIndex <= nVal; }
86
0
    bool operator> ( sal_Int32 const nVal ) const { return m_nIndex >  nVal; }
87
61.3M
    bool operator>=( sal_Int32 const nVal ) const { return m_nIndex >= nVal; }
88
3.13M
    bool operator==( sal_Int32 const nVal ) const { return m_nIndex == nVal; }
89
0
    bool operator!=( sal_Int32 const nVal ) const { return m_nIndex != nVal; }
90
91
    bool operator==( const SwContentIndex& rSwContentIndex ) const
92
2.45M
    {
93
2.45M
        return (m_nIndex    == rSwContentIndex.m_nIndex)
94
1.22M
            && (m_pContentNode == rSwContentIndex.m_pContentNode);
95
2.45M
    }
96
97
    bool operator!=( const SwContentIndex& rSwContentIndex ) const
98
1.86M
    {
99
1.86M
        return (m_nIndex    != rSwContentIndex.m_nIndex)
100
1.27M
            || (m_pContentNode != rSwContentIndex.m_pContentNode);
101
1.86M
    }
102
103
360M
    sal_Int32 GetIndex() const { return m_nIndex; }
104
105
    // Assignments without creating a temporary object.
106
    SwContentIndex &Assign(const SwContentNode *, sal_Int32);
107
108
    // Returns pointer to SwContentNode (for RTTI at SwContentIndexReg).
109
144M
    const SwContentNode* GetContentNode() const { return m_pContentNode; }
110
170M
    const SwContentIndex* GetNext() const { return m_pNext; }
111
112
174M
    ISwContentIndexOwner* GetOwner() const { return m_pOwner; }
113
    void SetOwner(ISwContentIndexOwner* pOwner)
114
1.95M
    {
115
1.95M
        assert(m_pOwner == nullptr && "there can be only one owner");
116
1.95M
        m_pOwner = pOwner;
117
1.95M
    }
118
};
119
120
SW_DLLPUBLIC std::ostream& operator <<(std::ostream& s, const SwContentIndex& index);
121
122
/// Helper base class for SwContentNode to manage the list of attached SwContentIndex
123
class SAL_WARN_UNUSED SAL_LOPLUGIN_ANNOTATE("crosscast") SwContentIndexReg
124
{
125
    friend class SwContentIndex;
126
127
    const SwContentIndex * m_pFirst;
128
    const SwContentIndex * m_pLast;
129
130
public:
131
    enum class UpdateMode {
132
        Default = 0,
133
        Negative = (1<<0),
134
        Delete = (1<<1),
135
        Replace = (1<<2),
136
    };
137
138
protected:
139
    virtual void Update( SwContentIndex const & rPos, const sal_Int32 nChangeLen,
140
            UpdateMode eMode);
141
142
56.0k
    bool HasAnyIndex() const { return nullptr != m_pFirst; }
143
144
    SwContentIndexReg();
145
public:
146
    virtual ~SwContentIndexReg();
147
148
    void MoveTo( SwContentNode& rArr );
149
69.1M
    const SwContentIndex* GetFirstIndex() const { return m_pFirst; }
150
};
151
152
namespace o3tl
153
{
154
    template<> struct typed_flags<SwContentIndexReg::UpdateMode> : is_typed_flags<SwContentIndexReg::UpdateMode, 0x07> {};
155
}
156
157
#ifndef DBG_UTIL
158
159
inline sal_Int32 SwContentIndex::operator++()
160
0
{
161
0
    return ChgValue( *this, m_nIndex+1 ).m_nIndex;
162
0
}
163
164
inline sal_Int32 SwContentIndex::operator--()
165
0
{
166
0
    return ChgValue( *this, m_nIndex-1 ).m_nIndex;
167
0
}
168
169
inline sal_Int32 SwContentIndex::operator--(int)
170
0
{
171
0
    sal_Int32 const nOldIndex = m_nIndex;
172
0
    ChgValue( *this, m_nIndex-1 );
173
0
    return nOldIndex;
174
0
}
175
176
inline sal_Int32 SwContentIndex::operator+=( sal_Int32 const nVal )
177
540k
{
178
540k
    return ChgValue( *this, m_nIndex + nVal ).m_nIndex;
179
540k
}
180
181
inline sal_Int32 SwContentIndex::operator-=( sal_Int32 const nVal )
182
0
{
183
0
    return ChgValue( *this, m_nIndex - nVal ).m_nIndex;
184
0
}
185
186
inline bool SwContentIndex::operator< ( const SwContentIndex& rIndex ) const
187
6.29M
{
188
6.29M
    return m_nIndex <  rIndex.m_nIndex;
189
6.29M
}
190
191
inline bool SwContentIndex::operator<=( const SwContentIndex& rIndex ) const
192
16.8M
{
193
16.8M
    return m_nIndex <= rIndex.m_nIndex;
194
16.8M
}
195
196
inline bool SwContentIndex::operator> ( const SwContentIndex& rIndex ) const
197
9.84M
{
198
9.84M
    return m_nIndex >  rIndex.m_nIndex;
199
9.84M
}
200
201
inline bool SwContentIndex::operator>=( const SwContentIndex& rIndex ) const
202
440k
{
203
440k
    return m_nIndex >= rIndex.m_nIndex;
204
440k
}
205
206
inline SwContentIndex& SwContentIndex::operator= ( sal_Int32 const nVal )
207
6.34M
{
208
6.34M
    if (m_nIndex != nVal)
209
3.29M
    {
210
3.29M
        ChgValue( *this, nVal );
211
3.29M
    }
212
6.34M
    return *this;
213
6.34M
}
214
215
#endif // ifndef DBG_UTIL
216
217
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */