ClanLib  2.3.7
callback_v3.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 
39 template <typename P1, typename P2, typename P3>
41 {
42 public:
44  {
45  }
46 
47  virtual void invoke(P1 p1, P2 p2, P3 p3) const = 0;
48 };
49 
52 template <typename P1, typename P2, typename P3>
54 {
55 public:
58  {
59  }
60 
61  void invoke(P1 p1, P2 p2, P3 p3) const
62  {
63  static_func(p1, p2, p3);
64  }
65 
66  void (*static_func)(P1, P2, P3);
67 };
68 
71 template <typename P1, typename P2, typename P3, typename UserData>
73 {
74 public:
76  void (*static_func)(P1, P2, P3, UserData), const UserData &user_data)
77  : static_func(static_func), user_data(user_data)
78  {
79  }
80 
81  void invoke(P1 p1, P2 p2, P3 p3) const
82  {
83  static_func(p1, p2, p3, user_data);
84  }
85 
86  void (*static_func)(P1, P2, P3, UserData);
87 
88  UserData user_data;
89 };
90 
93 template <typename P1, typename P2, typename P3, typename InstanceClass>
95 {
96 public:
98  void (InstanceClass::*member_func)(P1, P2, P3))
99  : instance(instance), member_func(member_func)
100  {
101  }
102 
103  void invoke(P1 p1, P2 p2, P3 p3) const
104  {
105  (instance->*member_func)(p1, p2, p3);
106  }
107 
108  InstanceClass *instance;
109 
110  void (InstanceClass::*member_func)(P1, P2, P3);
111 };
112 
115 template <typename P1, typename P2, typename P3, typename InstanceClass, typename UserData>
117 {
118 public:
119  CL_Callback_Impl_v3_member_user(InstanceClass *instance,
120  void (InstanceClass::*member_func)(P1, P2, P3, UserData), const UserData &user_data)
121  : instance(instance), member_func(member_func), user_data(user_data)
122  {
123  }
124 
125  void invoke(P1 p1, P2 p2, P3 p3) const
126  {
127  (instance->*member_func)(p1, p2, p3, user_data);
128  }
129 
130  InstanceClass *instance;
131 
132  void (InstanceClass::*member_func)(P1, P2, P3, UserData);
133 
134  UserData user_data;
135 };
136 
139 template <class P1, class P2, class P3, class Functor>
141 {
142 public:
144  : functor(functor)
145  {
146  }
147 
148  void invoke(P1 p1, P2 p2, P3 p3) const
149  {
150  functor(p1, p2, p3);
151  }
152 
153  Functor functor;
154 };
155 
159 template <typename P1, typename P2, typename P3>
161 {
162 public:
164  {
165  }
166 
168  : impl(copy.impl)
169  {
170  }
171 
173  : impl(impl)
174  {
175  }
176 
177  CL_Callback_v3(void (*function)(P1, P2, P3))
178  : impl(new CL_Callback_Impl_v3_static<P1, P2, P3>(function))
179  {
180  }
181 
182  template<typename UserData>
183  CL_Callback_v3(void (*function)(P1, P2, P3, UserData), const UserData &user_data)
184  : impl(new CL_Callback_Impl_v3_static_user<P1, P2, P3, UserData>(function, user_data))
185  {
186  }
187 
188  template<class InstanceClass>
189  CL_Callback_v3(InstanceClass *instance, void (InstanceClass::*function)(P1, P2, P3))
190  : impl(new CL_Callback_Impl_v3_member<P1, P2, P3, InstanceClass>(instance, function))
191  {
192  }
193 
194  template<class InstanceClass, typename UserData>
195  CL_Callback_v3(InstanceClass *instance, void (InstanceClass::*function)(P1, P2, P3, UserData), const UserData &user_data)
196  : impl(new CL_Callback_Impl_v3_member_user<P1, P2, P3, InstanceClass, UserData>(instance, function, user_data))
197  {
198  }
199 
200  void set(void (*function)(P1, P2, P3))
201  {
202  impl = CL_SharedPtr< CL_Callback_Impl_v3<P1, P2, P3> >(new CL_Callback_Impl_v3_static<P1, P2, P3>(function));
203  }
204 
205  template<typename UserData>
206  void set(void (*function)(P1, P2, P3, UserData), const UserData &user_data)
207  {
208  impl = CL_SharedPtr< CL_Callback_Impl_v3<P1, P2, P3> >(new CL_Callback_Impl_v3_static_user<P1, P2, P3, UserData>(function, user_data));
209  }
210 
211  template<class InstanceClass>
212  void set(InstanceClass *instance, void (InstanceClass::*function)(P1, P2, P3))
213  {
214  impl = CL_SharedPtr< CL_Callback_Impl_v3<P1, P2, P3> >(new CL_Callback_Impl_v3_member<P1, P2, P3, InstanceClass>(instance, function));
215  }
216 
217  template<class InstanceClass, typename UserData>
218  void set(InstanceClass *instance, void (InstanceClass::*function)(P1, P2, P3, UserData), const UserData &user_data)
219  {
220  impl = CL_SharedPtr< CL_Callback_Impl_v3<P1, P2, P3> >(new CL_Callback_Impl_v3_member_user<P1, P2, P3, InstanceClass, UserData>(instance, function, user_data));
221  }
222 
223  void clear()
224  {
225  impl = CL_SharedPtr< CL_Callback_Impl_v3<P1, P2, P3> >();
226  }
227 
228  void invoke(P1 p1, P2 p2, P3 p3) const
229  {
230  impl->invoke(p1, p2, p3);
231  }
232 
233  bool is_null() const
234  {
235  return !impl;
236  }
237 
238 private:
239  CL_SharedPtr< CL_Callback_Impl_v3<P1, P2, P3> > impl;
240 };
241 
245 template <typename P1, typename P2, typename P3>
246 class CL_Callback_v3_functor : public CL_Callback_v3<P1, P2, P3>
247 {
248 public:
250  {
251  }
252 
254  : CL_Callback_v3<P1, P2, P3>(copy)
255  {
256  }
257 
258  template<class Functor>
259  CL_Callback_v3_functor(Functor functor)
260  : CL_Callback_v3<P1, P2, P3>(new CL_Callback_Impl_v3_functor<P1, P2, P3, Functor>(functor))
261  {
262  }
263 
264 };
265 
266