summaryrefslogtreecommitdiffstats
path: root/src/util/accumulators.h
blob: 2cd51b1015e76d224a710fad96c2367960838ff3 (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
/** @file
 * Frequently used accumulators for use with libsigc++
 */
/* Authors:
 *   Krzysztof Kosiński <tweenk.pl@gmail.com>
 *
 * Copyright (C) 2009 Authors
 * Released under GNU GPL, read the file 'COPYING' for more information
 */

#ifndef SEEN_UTIL_ACCUMULATORS_H
#define SEEN_UTIL_ACCUMULATORS_H

#include <iterator>

namespace Inkscape {
namespace Util {

/**
 * Accumulator which evaluates slots in reverse connection order.
 * The slot that was connected last is evaluated first.
 */
struct Reverse {
    typedef void result_type;
    template <typename T_iterator>
    result_type operator()(T_iterator first, T_iterator last) const {
        while (first != last) *(--last);
    }
};

/**
 * Accumulator type for interruptible signals. Slots return a boolean value; emission
 * is stopped when true is returned from a slot.
 */
struct Interruptible {
    typedef bool result_type;
    template <typename T_iterator>
    result_type operator()(T_iterator first, T_iterator last) const {
        for (; first != last; ++first)
            if (*first) return true;
        return false;
    }
};

/**
 * Same as Interruptible, but the slots are called in reverse order of connection,
 * e.g. the slot that was connected last is evaluated first.
 */
struct ReverseInterruptible {
    typedef bool result_type;
    template <typename T_iterator>
    result_type operator()(T_iterator first, T_iterator last) const {
        while (first != last) {
            if (*(--last)) return true;
        }
        return false;
    }
};

/**
 * The template parameter specifies how many slots from the beginning of the list
 * should be evaluated after other slots. Useful for signals which invoke other signals
 * once complete. Undefined results if the signal does not have at least @c num_chained
 * slots before first emission.
 *
 * For example, if template param = 3, the execution order is as follows:
 * @verbatim
   8. 1. 2. 3. 4. 5. 6. 7.
   S1 S2 S3 S4 S5 S6 S7 S8 @endverbatim
 */
template <unsigned num_chained = 1>
struct Chained {
    typedef void result_type;
    template <typename T_iterator>
    result_type operator()(T_iterator first, T_iterator last) const {
        T_iterator save_first = first;
        // ARGH, iterator_traits is not defined for slot iterators!
        //std::advance(first, num_chained);
        for (unsigned i = 0; i < num_chained && first != last; ++i) ++first;
        for (; first != last; ++first) *first;
        for (unsigned i = 0; i < num_chained && save_first != last; ++i, ++save_first)
            *save_first;
    }
};

/**
 * Executes a logical OR on the results from slots.
 */
struct LogicalOr {
    typedef bool result_type;
    template <typename T_iterator>
    result_type operator()(T_iterator first, T_iterator last) const {
        bool ret = false;
        for (; first != last; ++first) ret |= *first;
        return ret;
    }
};

} // namespace Util
} // namespace Inkscape

#endif

/*
  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 :