ClanLib  2.3.7
signal_v0.h
Go to the documentation of this file.
1 /*
2 ** ClanLib SDK
3 ** Copyright (c) 1997-2011 The ClanLib Team
4 **
5 ** This software is provided 'as-is', without any express or implied
6 ** warranty. In no event will the authors be held liable for any damages
7 ** arising from the use of this software.
8 **
9 ** Permission is granted to anyone to use this software for any purpose,
10 ** including commercial applications, and to alter it and redistribute it
11 ** freely, subject to the following restrictions:
12 **
13 ** 1. The origin of this software must not be misrepresented; you must not
14 ** claim that you wrote the original software. If you use this software
15 ** in a product, an acknowledgment in the product documentation would be
16 ** appreciated but is not required.
17 ** 2. Altered source versions must be plainly marked as such, and must not be
18 ** misrepresented as being the original software.
19 ** 3. This notice may not be removed or altered from any source distribution.
20 **
21 ** Note: Some of the libraries ClanLib may link to may have additional
22 ** requirements or restrictions.
23 **
24 ** File Author(s):
25 **
26 ** Magnus Norddahl
27 */
28 
31 
32 #pragma once
33 
34 #include "../api_core.h"
35 #include "slot.h"
36 #include "signals_impl.h"
37 
41 {
42 public:
43  virtual void invoke() = 0;
44 };
45 
49 {
50 public:
51  CL_SlotCallback_v0_static(void (*static_func)())
52  : static_func(static_func) { return; }
53  void invoke() { static_func(); }
54  void (*static_func)();
55 };
56 
59 template <class UserData>
61 {
62 public:
63  CL_SlotCallback_v0_static_user(void (*static_func)(UserData), const UserData &user_data)
64  : static_func(static_func), user_data(user_data) { return; }
66  void (*static_func)(UserData);
67  UserData user_data;
68 };
69 
72 template <class InstanceClass>
74 {
75 public:
76  CL_SlotCallback_v0_member(InstanceClass *instance, void (InstanceClass::*member_func)())
77  : instance(instance), member_func(member_func) { return; }
78  void invoke() { (instance->*member_func)(); }
79  InstanceClass *instance;
80  void (InstanceClass::*member_func)();
81 };
82 
85 template <class InstanceClass, class UserData>
87 {
88 public:
89  CL_SlotCallback_v0_member_user(InstanceClass *instance, void (InstanceClass::*member_func)(UserData), const UserData &user_data)
90  : instance(instance), member_func(member_func), user_data(user_data) { return; }
91  void invoke() { (instance->*member_func)(user_data); }
92  InstanceClass *instance;
93  void (InstanceClass::*member_func)(UserData);
94  UserData user_data;
95 };
96 
99 template <class Functor>
101 {
102 public:
104  : functor(functor) { return; }
105  void invoke() { functor(); }
106  Functor functor;
107 };
108 
113 {
116 
117 public:
119  : impl(new CL_Signal_Impl) { return; }
120 
122  : impl(copy.impl) { return; }
123 
124 
128 
129 public:
130  CL_Slot connect(void (*function)())
131  {
132  clean_up();
133  CL_SharedPtr<CL_SlotCallback> callback(
134  new CL_SlotCallback_v0_static(function));
135  impl->connected_slots.push_back(callback);
136  return CL_Slot(callback);
137  }
138 
139  template<class UserData>
140  CL_Slot connect(void (*function)(UserData), const UserData &user_data)
141  {
142  clean_up();
143  CL_SharedPtr<CL_SlotCallback> callback(
144  new CL_SlotCallback_v0_static_user<UserData>(function, user_data));
145  impl->connected_slots.push_back(callback);
146  return CL_Slot(callback);
147  }
148 
149  template<class InstanceClass>
150  CL_Slot connect(InstanceClass *instance, void (InstanceClass::*function)())
151  {
152  clean_up();
153  CL_SharedPtr<CL_SlotCallback> callback(
154  new CL_SlotCallback_v0_member<InstanceClass>(instance, function));
155  impl->connected_slots.push_back(callback);
156  return CL_Slot(callback);
157  }
158 
159  template<class InstanceClass, class UserData>
160  CL_Slot connect(InstanceClass *instance, void (InstanceClass::*function)(UserData), const UserData &user_data)
161  {
162  clean_up();
163  CL_SharedPtr<CL_SlotCallback> callback(
164  new CL_SlotCallback_v0_member_user<InstanceClass, UserData>(instance, function, user_data));
165  impl->connected_slots.push_back(callback);
166  return CL_Slot(callback);
167  }
168 
169  template<class Functor>
170  CL_Slot connect_functor(const Functor &functor)
171  {
172  clean_up();
173  CL_SharedPtr<CL_SlotCallback> callback(
175  impl->connected_slots.push_back(callback);
176  return CL_Slot(callback);
177  }
178 
179  void invoke() const
180  {
181  std::vector< CL_SharedPtr<CL_SlotCallback> > callbacks = impl->connected_slots;
182  std::vector< CL_SharedPtr<CL_SlotCallback> >::size_type i, size;
183  size = callbacks.size();
184  for (i = 0; i < size; i++)
185  if (callbacks[i]->valid && callbacks[i]->enabled)
186  ((CL_SlotCallback_v0 *) callbacks[i].get())->invoke();
187  }
188 
189 
193 
194 private:
195  void clean_up()
196  {
197  std::vector< CL_SharedPtr<CL_SlotCallback> >::size_type i, size;
198  size = impl->connected_slots.size();
199  for (i = 0; i < size; i++)
200  {
201  if (!impl->connected_slots[i]->valid)
202  {
203  impl->connected_slots.erase(impl->connected_slots.begin()+i);
204  i--;
205  size--;
206  }
207  }
208  }
209 
210  CL_SharedPtr<CL_Signal_Impl> impl;
212 };
213 
214