ClanLib  2.3.7
callback_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 "../System/exception.h"
35 #include "../System/sharedptr.h"
36 
40 {
41 public:
43  {
44  }
45 
46  virtual void invoke() const = 0;
47 };
48 
52 {
53 public:
56  {
57  }
58 
59  void invoke() const
60  {
61  static_func();
62  }
63 
64  void (*static_func)();
65 };
66 
69 template <typename UserData>
71 {
72 public:
74  void (*static_func)(UserData), const UserData &user_data)
75  : static_func(static_func), user_data(user_data)
76  {
77  }
78 
79  void invoke() const
80  {
82  }
83 
84  void (*static_func)(UserData);
85 
86  UserData user_data;
87 };
88 
91 template <typename InstanceClass>
93 {
94 public:
96  void (InstanceClass::*member_func)())
97  : instance(instance), member_func(member_func)
98  {
99  }
100 
101  void invoke() const
102  {
103  (instance->*member_func)();
104  }
105 
106  InstanceClass *instance;
107 
108  void (InstanceClass::*member_func)();
109 };
110 
113 template <typename InstanceClass, typename UserData>
115 {
116 public:
117  CL_Callback_Impl_v0_member_user(InstanceClass *instance,
118  void (InstanceClass::*member_func)(UserData), const UserData &user_data)
119  : instance(instance), member_func(member_func), user_data(user_data)
120  {
121  }
122 
123  void invoke() const
124  {
125  (instance->*member_func)(user_data);
126  }
127 
128  InstanceClass *instance;
129 
130  void (InstanceClass::*member_func)(UserData);
131 
132  UserData user_data;
133 };
134 
137 template <class Functor>
139 {
140 public:
142  : functor(functor)
143  {
144  }
145 
146  void invoke() const
147  {
148  functor();
149  }
150 
151  Functor functor;
152 };
153 
158 {
159 public:
161  {
162  }
163 
165  : impl(copy.impl)
166  {
167  }
168 
170  : impl(impl)
171  {
172  }
173 
174  CL_Callback_v0(void (*function)())
175  : impl(new CL_Callback_Impl_v0_static(function))
176  {
177  }
178 
179  template<typename UserData>
180  CL_Callback_v0(void (*function)(UserData), const UserData &user_data)
181  : impl(new CL_Callback_Impl_v0_static_user<UserData>(function, user_data))
182  {
183  }
184 
185  template<class InstanceClass>
186  CL_Callback_v0(InstanceClass *instance, void (InstanceClass::*function)())
187  : impl(new CL_Callback_Impl_v0_member<InstanceClass>(instance, function))
188  {
189  }
190 
191  template<class InstanceClass, typename UserData>
192  CL_Callback_v0(InstanceClass *instance, void (InstanceClass::*function)(UserData), const UserData &user_data)
193  : impl(new CL_Callback_Impl_v0_member_user<InstanceClass, UserData>(instance, function, user_data))
194  {
195  }
196 
197  void set(void (*function)())
198  {
199  impl = CL_SharedPtr< CL_Callback_Impl_v0 >(new CL_Callback_Impl_v0_static(function));
200  }
201 
202  template<typename UserData>
203  void set(void (*function)(UserData), const UserData &user_data)
204  {
205  impl = CL_SharedPtr< CL_Callback_Impl_v0 >(new CL_Callback_Impl_v0_static_user<UserData>(function, user_data));
206  }
207 
208  template<class InstanceClass>
209  void set(InstanceClass *instance, void (InstanceClass::*function)())
210  {
211  impl = CL_SharedPtr< CL_Callback_Impl_v0 >(new CL_Callback_Impl_v0_member<InstanceClass>(instance, function));
212  }
213 
214  template<class InstanceClass, typename UserData>
215  void set(InstanceClass *instance, void (InstanceClass::*function)(UserData), const UserData &user_data)
216  {
217  impl = CL_SharedPtr< CL_Callback_Impl_v0 >(new CL_Callback_Impl_v0_member_user<InstanceClass, UserData>(instance, function, user_data));
218  }
219 
220  void clear()
221  {
222  impl = CL_SharedPtr< CL_Callback_Impl_v0 >();
223  }
224 
225  void invoke() const
226  {
227  impl->invoke();
228  }
229 
230  bool is_null() const
231  {
232  return !impl;
233  }
234 
235 private:
236  CL_SharedPtr< CL_Callback_Impl_v0 > impl;
237 };
238 
243 {
244 public:
246  {
247  }
248 
250  : CL_Callback_v0(copy)
251  {
252  }
253 
254  template<class Functor>
255  CL_Callback_v0_functor(Functor functor)
256  : CL_Callback_v0(new CL_Callback_Impl_v0_functor<Functor>(functor))
257  {
258  }
259 
260 };
261 
262