Library of Assembled Shared Sources
multi_callback_0.h
Go to the documentation of this file.
1/** @file
2 * @author Bram de Greve (bram@cocamware.com)
3 * @author Tom De Muer (tom@cocamware.com)
4 *
5 * *** BEGIN LICENSE INFORMATION ***
6 *
7 * The contents of this file are subject to the Common Public Attribution License
8 * Version 1.0 (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
10 * http://lass.sourceforge.net/cpal-license. The License is based on the
11 * Mozilla Public License Version 1.1 but Sections 14 and 15 have been added to cover
12 * use of software over a computer network and provide for limited attribution for
13 * the Original Developer. In addition, Exhibit A has been modified to be consistent
14 * with Exhibit B.
15 *
16 * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
17 * WARRANTY OF ANY KIND, either express or implied. See the License for the specific
18 * language governing rights and limitations under the License.
19 *
20 * The Original Code is LASS - Library of Assembled Shared Sources.
21 *
22 * The Initial Developer of the Original Code is Bram de Greve and Tom De Muer.
23 * The Original Developer is the Initial Developer.
24 *
25 * All portions of the code written by the Initial Developer are:
26 * Copyright (C) 2004-2011 the Initial Developer.
27 * All Rights Reserved.
28 *
29 * Contributor(s):
30 *
31 * Alternatively, the contents of this file may be used under the terms of the
32 * GNU General Public License Version 2 or later (the GPL), in which case the
33 * provisions of GPL are applicable instead of those above. If you wish to allow use
34 * of your version of this file only under the terms of the GPL and not to allow
35 * others to use your version of this file under the CPAL, indicate your decision by
36 * deleting the provisions above and replace them with the notice and other
37 * provisions required by the GPL License. If you do not delete the provisions above,
38 * a recipient may use your version of this file under either the CPAL or the GPL.
39 *
40 * *** END LICENSE INFORMATION ***
41 */
42
43
44
45/** @defgroup Callback Callback
46 * @brief library to group generalized callback functions
47 * @date 2009
48 * @author Tom De Muer (contact: tom@cocamware.com)
49 *
50 */
51
52#include <algorithm>
53#include "callback_0.h"
54
55#ifndef LASS_GUARDIAN_OF_INCLUSION_UTIL_MULTI_CALLBACK_0_H
56#define LASS_GUARDIAN_OF_INCLUSION_UTIL_MULTI_CALLBACK_0_H
57
58namespace lass
59{
60namespace util
61{
62
63struct MultiCallback0
64{
65 typedef MultiCallback0 TSelf;
66 typedef Callback0 TCallback;
67
68 /** Default constructor, construct empty callback list.
69 */
70 MultiCallback0()
71 {
72 }
73
74 /** Constructor: construct multi callback with a single callback.
75 */
76 MultiCallback0(const TCallback& iCallback)
77 {
78 callbacks_.push_back(iCallback);
79 }
80
81 /** Constructor: construct multi callback with a two callbacks.
82 */
83 MultiCallback0(const TCallback& iCallback1, const TCallback& iCallback2)
84 {
85 callbacks_.push_back(iCallback1);
86 callbacks_.push_back(iCallback2);
87 }
88
89 /** copy constructor
90 */
91 MultiCallback0(const TSelf& iOther):
92 callbacks_(iOther.callbacks_)
93 {
94 }
95
96 /** move constructor
97 */
98 MultiCallback0(TSelf&& iOther) noexcept:
99 callbacks_(std::move(iOther.callbacks_))
100 {
101 }
102
103 /** assignment operator (also usable for conversion)
104 */
105 TSelf& operator=(const TSelf& iOther)
106 {
107 TSelf temp(iOther);
108 swap(temp);
109 return *this;
110 }
111
112 /** move assignment operator
113 */
114 TSelf& operator=(TSelf&& iOther) noexcept
115 {
116 callbacks_ = std::move(iOther.callbacks_);
117 return *this;
118 }
119
120 /** add a callback to the list of callbacks to be dispatched
121 */
122 void add( const TCallback& iCallback )
123 {
124 callbacks_.push_back(iCallback);
125 }
126
127 /** removes a callback from the list of callbacks to be dispatched,
128 * callbacks are tested for equivalence.
129 */
130 void remove( const TCallback& iCallback )
131 {
132 static_cast<void>(std::remove( callbacks_.begin(), callbacks_.end(), iCallback ));
133 }
134
135 /** pop a callback from the list of callbacks
136 */
137 void pop( TCallback& iCallback )
138 {
139 iCallback = callbacks_.back();
140 callbacks_.pop_back();
141 }
142
143 /** add a callback to the list of callbacks to be dispatched
144 */
145 TSelf& operator += ( const TCallback& iCallback )
146 {
147 callbacks_.push_back(iCallback);
148 return *this;
149 }
150
151 /** removes a callback from the list of callbacks to be dispatched
152 */
153 TSelf& operator -= ( const TCallback& iCallback )
154 {
155 remove(iCallback);
156 return *this;
157 }
158
159 /** the number of callbacks that will be dispatched
160 */
161 size_t size() const
162 {
163 return callbacks_.size();
164 }
165
166 /** THE operator. Executes the multi-callback.
167 */
168 void operator()() const
169 {
170 call();
171 }
172 void call() const
173 {
174 size_t nrCallbacks = callbacks_.size();
175 for (size_t i=0;i<nrCallbacks;++i)
176 callbacks_[i]();
177 }
178
179 /** retrieve a callback */
180 const TCallback& operator[](size_t index) const
181 {
182 return callbacks_.at(index);
183 }
184
185
186 // METHODS
187
188 /** Reset to empty callback.
189 */
190 void reset()
191 {
192 callbacks_.clear();
193 }
194
195 /** Returns true if no callback dispatcher is assigned to this object.
196 */
197 bool isEmpty() const
198 {
199 return callbacks_.size()==0;
200 }
201
202 /** return this->isEmpty()
203 */
204 bool operator!() const
205 {
206 return isEmpty();
207 }
208
209 /** return !this->isEmpty())
210 */
211 explicit operator bool() const
212 {
213 return !isEmpty();
214 }
215
216 /** Swaps the dispatchers of this multi_callback with the dispatchers of another.
217 */
218 void swap(TSelf& iOther)
219 {
220 callbacks_.swap(iOther.callbacks_);
221 }
222
223 /** return true if two callbacks call the same function/method,
224 * NEEDS RTTI!
225 */
226 bool operator==(const TSelf& iOther) const
227 {
228 size_t nrCallbacks = callbacks_.size();
229 if (nrCallbacks != iOther.callbacks_.size())
230 return false;
231 for (size_t i=0;i<nrCallbacks;++i)
232 if (callbacks_[i]!=iOther.callbacks_[i])
233 return false;
234 return true;
235 }
236private:
237 std::vector<TCallback> callbacks_;
238};
239
240}
241}
242
243#endif
244
245// EOF
general utility, debug facilities, ...
Library for Assembled Shared Sources.
Definition config.h:53