File: astchangeset.h

package info (click to toggle)
kdevelop 4%3A5.6.2-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 57,892 kB
  • sloc: cpp: 278,773; javascript: 3,558; python: 3,385; sh: 1,317; ansic: 689; xml: 273; php: 95; makefile: 40; lisp: 13; sed: 12
file content (246 lines) | stat: -rw-r--r-- 6,274 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/*
   Copyright 2008 Hamish Rodda <rodda@kde.org>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License version 2 as published by the Free Software Foundation.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
 */

#ifndef KDEVPLATFORM_ASTCHANGESET_H
#define KDEVPLATFORM_ASTCHANGESET_H

#include <QVariant>

namespace KDevelop {
template <typename AstNode>
class AstChangeSet;

class AstChange;

/**
 * \short A reference to an existing read-only AST node.
 *
 * This class represents an AST node, and allows changes to be planned
 * for that node.
 *
 * \warning you must not create cyclic references.
 *
 * \author Hamish Rodda <rodda@kde.org>
 */
template <typename AstNode>
class AstNodeRef
{
    friend class AstChangeSet<AstNode>;

public:
    /// Destructor.
    virtual ~AstNodeRef()
    {
        qDeleteAll(m_changes);

        if (m_newNode)
            delete m_node;
    }

    using AstNodeList = QList<AstNodeRef*>;

    /**
     * \short Container class for a change to an AST node.
     *
     * \author Hamish Rodda <rodda@kde.org>
     */
    class AstChange
    {
public:
        enum ChangeTypes {
            ListRewrite,
            ListClear,
            ItemReplace,
            ItemMove
        };

        explicit AstChange(ChangeTypes t)
            : type(t)
            , newNode(0)
            , listOffset(-1)
            , newValue(-1)
        {
        }

        ChangeTypes type;
        // The index of the item in the node to be changed
        int nodeIndex;

        // The new node to occupy this position, if relevant
        AstNodeRef* newNode;
        // The list of nodes to occupy this position, if relevant
        AstNodeList newList;
        // The position to apply the node(s) in the list, if relevant
        int listOffset;
        // The value of the position, if relevant
        QVariant newValue;
    };

    virtual const AstNode* node() const
    {
        return m_newNode ? 0 : m_node;
    }

    virtual AstNodeRef* nodeRef() const
    {
        return m_nodeRef;
    }

    virtual AstNode* newNode() const
    {
        return m_newNode ? m_node : 0;
    }

    const QList<AstChange*>& changes() const
    {
        return m_changes;
    }

    /// Adds a change to this node reference. Takes ownership of the \a change.
    AstChange* newChange(AstChange* change)
    {
        m_changes.append(change);
        return change;
    }

    /// Removes a change from this node reference, and deletes it.
    void deleteChange(AstChange* change)
    {
        Q_ASSERT(m_changes.contains(change));
        m_changes.removeAll(change);
        delete change;
    }

protected:
    /// Default constructor. \todo is this needed?
    AstNodeRef(AstChangeSet<AstNode>* set)
        : m_changeSet(set)
        , m_node(0)
        , m_nodeRef(0)
        , m_newNode(false)
    {
    }

    /// Constructor.  Either takes an existing \a node (\a newNode = false), or a newly created \a node (\a newNode = true)
    AstNodeRef(AstChangeSet<AstNode>* set, AstNode* node, bool newNode)
        : m_changeSet(set)
        , m_node(node)
        , m_nodeRef(0)
        , m_newNode(newNode)
    {
    }

    /// Constructor.  Takes another node reference.
    AstNodeRef(AstChangeSet<AstNode>* set, AstNodeRef* original)
        : m_changeSet(set)
        , m_node(0)
        , m_nodeRef(original)
        , m_newNode(false)
    {
    }

    AstNode* m_nodeChanges;

private:
    AstChangeSet<AstNode>* m_changeSet;
    AstNode* m_node;
    AstNodeRef* m_nodeRef;
    bool m_newNode;

    QList<AstChange*> m_changes;
};

/**
 * \short A set of changes to an AST.
 *
 * This class holds a set of all changes to an AST.
 *
 * \author Hamish Rodda <rodda@kde.org>
 */
template <typename AstNode>
class AstChangeSet
{
public:
    /**
     * Constructor.
     *
     * \param topNode the top node of the read-only Ast to modify, or set to null if creating
     *                a new Ast from scratch.
     */
    AstChangeSet(const AstNode* topNode = 0)
        : m_topNode(topNode)
    {
    }

    /**
     * Destructor, deletes all nodes owned by this change set.
     */
    virtual ~AstChangeSet()
    {
        qDeleteAll(m_nodeRefs);
    }

    /**
     * Register a new node that you have created to insert at some point in this Ast.
     * You may modify this node directly.  The change set takes ownership, so that
     * the new node will be deleted when the change set is no longer needed.
     *
     * \returns the new node that has been registered.
     */
    AstNodeRef<AstNode>* registerNewNode(AstNode* node)
    {
        AstNodeRef<AstNode>* newRef = new AstNodeRef<AstNode>(this, node, true);
        m_nodeRefs.append(newRef);
        return newRef;
    }

    /**
     * Create a blank reference to a node.
     *
     * The change set takes ownership, so that
     * the new node will be deleted when the change set is no longer needed.
     *
     * \returns the new node reference
     */
    AstNodeRef<AstNode>* registerNewRef(AstNodeRef<AstNode>* ref)
    {
        m_nodeRefs.append(ref);
        return ref;
    }

    /**
     * Copy an existing node (whether from the Ast or from the change set).
     *
     * You may then modify this reference, and the modifications will be applied to the node when the change set is finalised.
     *
     * \returns a copy of \a source, which you may modify directly.
     */
    AstNodeRef<AstNode>* copyNode(AstNode* source)
    {
        AstNodeRef<AstNode>* newRef = new AstNodeRef<AstNode>(this, source, false);
        m_nodeRefs.append(newRef);
        return newRef;
    }

private:
    const AstNode* m_topNode;
    QList<AstNodeRef<AstNode>*> m_nodeRefs;
};
}

#endif // KDEVPLATFORM_ASTCHANGESET_H