Code

Fixed const/non-const mismatch loop.
[inkscape.git] / src / util / accumulators.h
1 /** @file
2  * Frequently used accumulators for use with libsigc++
3  */
4 /* Authors:
5  *   Krzysztof KosiƄski <tweenk.pl@gmail.com>
6  *
7  * Copyright (C) 2009 Authors
8  * Released under GNU GPL, read the file 'COPYING' for more information
9  */
11 #ifndef SEEN_UTIL_ACCUMULATORS_H
12 #define SEEN_UTIL_ACCUMULATORS_H
14 #include <iterator>
16 namespace Inkscape {
17 namespace Util {
19 /**
20  * Accumulator which evaluates slots in reverse connection order.
21  * The slot that was connected last is evaluated first.
22  */
23 struct Reverse {
24     typedef void result_type;
25     template <typename T_iterator>
26     result_type operator()(T_iterator first, T_iterator last) const {
27         while (first != last) *(--last);
28     }
29 };
31 /**
32  * Accumulator type for interruptible signals. Slots return a boolean value; emission
33  * is stopped when true is returned from a slot.
34  */
35 struct Interruptible {
36     typedef bool result_type;
37     template <typename T_iterator>
38     result_type operator()(T_iterator first, T_iterator last) const {
39         for (; first != last; ++first)
40             if (*first) return true;
41         return false;
42     }
43 };
45 /**
46  * Same as Interruptible, but the slots are called in reverse order of connection,
47  * e.g. the slot that was connected last is evaluated first.
48  */
49 struct ReverseInterruptible {
50     typedef bool result_type;
51     template <typename T_iterator>
52     result_type operator()(T_iterator first, T_iterator last) const {
53         while (first != last) {
54             if (*(--last)) return true;
55         }
56         return false;
57     }
58 };
60 /**
61  * The template parameter specifies how many slots from the beginning of the list
62  * should be evaluated after other slots. Useful for signals which invoke other signals
63  * once complete. Undefined results if the signal does not have at least @c num_chained
64  * slots before first emission.
65  *
66  * For example, if template param = 3, the execution order is as follows:
67  * @verbatim
68    8. 1. 2. 3. 4. 5. 6. 7.
69    S1 S2 S3 S4 S5 S6 S7 S8 @endverbatim
70  */
71 template <unsigned num_chained = 1>
72 struct Chained {
73     typedef void result_type;
74     template <typename T_iterator>
75     result_type operator()(T_iterator first, T_iterator last) const {
76         T_iterator save_first = first;
77         // ARGH, iterator_traits is not defined for slot iterators!
78         //std::advance(first, num_chained);
79         for (unsigned i = 0; i < num_chained && first != last; ++i) ++first;
80         for (; first != last; ++first) *first;
81         for (unsigned i = 0; i < num_chained && save_first != last; ++i, ++save_first)
82             *save_first;
83     }
84 };
86 /**
87  * Executes a logical OR on the results from slots.
88  */
89 struct LogicalOr {
90     typedef bool result_type;
91     template <typename T_iterator>
92     result_type operator()(T_iterator first, T_iterator last) const {
93         bool ret = false;
94         for (; first != last; ++first) ret |= *first;
95         return ret;
96     }
97 };
99 } // namespace Util
100 } // namespace Inkscape
102 #endif
104 /*
105   Local Variables:
106   mode:c++
107   c-file-style:"stroustrup"
108   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
109   indent-tabs-mode:nil
110   fill-column:99
111   End:
112 */
113 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :