summaryrefslogtreecommitdiffstats
path: root/src/xml/composite-node-observer.cpp
blob: 541f289d884efcad774bf1296f12a8d0c2458b84 (plain)
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
// SPDX-License-Identifier: GPL-2.0-or-later
/** @file
 * TODO: insert short description here
 *//*
 * Authors: see git history
 *
 * Copyright (C) 2018 Authors
 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
 */
/*
 * Inkscape::XML::CompositeNodeObserver - combine multiple observers
 *
 * Copyright 2005 MenTaLguY <mental@rydia.net>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * See the file COPYING for details.
 *
 */

#include <algorithm>
#include <cstring>
#include <glib.h>

#include "xml/composite-node-observer.h"
#include "debug/event-tracker.h"
#include "debug/simple-event.h"

namespace Inkscape {

namespace XML {

void CompositeNodeObserver::notifyChildAdded(Node &node, Node &child, Node *prev)
{
    _startIteration();
    for (auto & iter : _active)
    {
        if (!iter.marked) {
            iter.observer->notifyChildAdded(node, child, prev);
        }
    }
    _finishIteration();
}

void CompositeNodeObserver::notifyChildRemoved(Node &node, Node &child,
                                                           Node *prev)
{
    _startIteration();
    for (auto & iter : _active)
    {
        if (!iter.marked) {
            iter.observer->notifyChildRemoved(node, child, prev);
        }
    }
    _finishIteration();
}

void CompositeNodeObserver::notifyChildOrderChanged(Node &node, Node &child,
                                                                Node *old_prev,
                                                                Node *new_prev)
{
    _startIteration();
    for (auto & iter : _active)
    {
        if (!iter.marked) {
            iter.observer->notifyChildOrderChanged(node, child, old_prev, new_prev);
        }
    }
    _finishIteration();
}

void CompositeNodeObserver::notifyContentChanged(
    Node &node,
    Util::ptr_shared old_content, Util::ptr_shared new_content
) {
    _startIteration();
    for (auto & iter : _active)
    {
        if (!iter.marked) {
            iter.observer->notifyContentChanged(node, old_content, new_content);
        }
    }
    _finishIteration();
}

void CompositeNodeObserver::notifyAttributeChanged(
    Node &node, GQuark name,
    Util::ptr_shared old_value, Util::ptr_shared new_value
) {
    _startIteration();
    for (auto & iter : _active)
    {
        if (!iter.marked) {
            iter.observer->notifyAttributeChanged(node, name, old_value, new_value);
        }
    }
    _finishIteration();
}

void CompositeNodeObserver::notifyElementNameChanged(Node& node, GQuark old_name, GQuark new_name)
{
    _startIteration();
    for (auto& iter : _active) {
        if (!iter.marked) {
            iter.observer->notifyElementNameChanged(node, old_name, new_name);
        }
    }
    _finishIteration();
}

void CompositeNodeObserver::add(NodeObserver &observer) {
    if (_iterating) {
        _pending.emplace_back(&observer);
    } else {
        _active.emplace_back(&observer);
    }
}

namespace {

typedef CompositeNodeObserver::ObserverRecord ObserverRecord;
typedef CompositeNodeObserver::ObserverRecordList ObserverRecordList;

template <typename ObserverPredicate>
struct unmarked_record_satisfying {
    ObserverPredicate predicate;
    unmarked_record_satisfying(ObserverPredicate p) : predicate(p) {}
    bool operator()(ObserverRecord const &record) {
        return !record.marked && predicate(record.observer);
    }
};

template <typename Predicate>
bool mark_one(ObserverRecordList &observers, unsigned &marked_count,
              Predicate p)
{
    auto found = std::find_if(
        observers.begin(), observers.end(),
        unmarked_record_satisfying<Predicate>(p)
    );

    if ( found != observers.end() ) {
        ++marked_count;
        found->marked = true;
        return true;
    } else {
        return false;
    }
}

template <typename Predicate>
bool remove_one(ObserverRecordList &observers, unsigned &/*marked_count*/,
                Predicate p)
{
    auto found = std::find_if(
        observers.begin(), observers.end(),
        unmarked_record_satisfying<Predicate>(p)
    );

    if ( found != observers.end() ) {
        // for O(1) removal
        if (observers.size() > 3) {
            *found = std::move(observers.back());
            observers.pop_back();
        } else {
            observers.erase(found);
        }
        return true;
    } else {
        return false;
    }
}

bool is_marked(ObserverRecord const &record) { return record.marked; }

void remove_all_marked(ObserverRecordList &observers, unsigned &marked_count)
{
    if (marked_count) {
        g_assert(!observers.empty());

        auto newEnd = std::remove_if(observers.begin(), observers.end(), is_marked);
        observers.erase(newEnd, observers.end());
        marked_count = 0;
    }
}

}

void CompositeNodeObserver::_finishIteration() {
    if (!--_iterating) {
        remove_all_marked(_active, _active_marked);
        remove_all_marked(_pending, _pending_marked);
        _active.insert(_active.end(), _pending.begin(), _pending.end());
        _pending.clear();

        g_assert(_pending.empty());
    }
}

namespace {

struct eql_observer {
    NodeObserver const *observer;
    eql_observer(NodeObserver const *o) : observer(o) {}
    bool operator()(NodeObserver const *other) {
        return observer == other;
    }
};

}

void CompositeNodeObserver::remove(NodeObserver &observer) {
    eql_observer p(&observer);
    if (_iterating) {
        mark_one(_active, _active_marked, p) ||
        mark_one(_pending, _pending_marked, p);
    } else {
        remove_one(_active, _active_marked, p) ||
        remove_one(_pending, _pending_marked, p);
    }
}
    
} // namespace XML
} // namespace Inkscape
/*
  Local Variables:
  mode:c++
  c-file-style:"stroustrup"
  c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
  indent-tabs-mode:nil
  fill-column:99
  End:
*/
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :