callback_v0.h

Go to the documentation of this file.
00001 /*
00002 **  ClanLib SDK
00003 **  Copyright (c) 1997-2011 The ClanLib Team
00004 **
00005 **  This software is provided 'as-is', without any express or implied
00006 **  warranty.  In no event will the authors be held liable for any damages
00007 **  arising from the use of this software.
00008 **
00009 **  Permission is granted to anyone to use this software for any purpose,
00010 **  including commercial applications, and to alter it and redistribute it
00011 **  freely, subject to the following restrictions:
00012 **
00013 **  1. The origin of this software must not be misrepresented; you must not
00014 **     claim that you wrote the original software. If you use this software
00015 **     in a product, an acknowledgment in the product documentation would be
00016 **     appreciated but is not required.
00017 **  2. Altered source versions must be plainly marked as such, and must not be
00018 **     misrepresented as being the original software.
00019 **  3. This notice may not be removed or altered from any source distribution.
00020 **
00021 **  Note: Some of the libraries ClanLib may link to may have additional
00022 **  requirements or restrictions.
00023 **
00024 **  File Author(s):
00025 **
00026 **    Magnus Norddahl
00027 */
00028 
00031 
00032 #pragma once
00033 
00034 #include "../System/exception.h"
00035 #include "../System/sharedptr.h"
00036 
00039 class CL_Callback_Impl_v0
00040 {
00041 public:
00042         virtual ~CL_Callback_Impl_v0()
00043         {
00044         }
00045 
00046         virtual void invoke() const = 0;
00047 };
00048 
00051 class CL_Callback_Impl_v0_static : public CL_Callback_Impl_v0
00052 {
00053 public:
00054         CL_Callback_Impl_v0_static(void (*static_func)())
00055         : static_func(static_func)
00056         {
00057         }
00058 
00059         void invoke() const
00060         {
00061                 static_func();
00062         }
00063 
00064         void (*static_func)();
00065 };
00066 
00069 template <typename UserData>
00070 class CL_Callback_Impl_v0_static_user : public CL_Callback_Impl_v0
00071 {
00072 public:
00073         CL_Callback_Impl_v0_static_user(
00074                 void (*static_func)(UserData), const UserData &user_data)
00075         : static_func(static_func), user_data(user_data)
00076         {
00077         }
00078 
00079         void invoke() const
00080         {
00081                 static_func(user_data);
00082         }
00083 
00084         void (*static_func)(UserData);
00085 
00086         UserData user_data;
00087 };
00088 
00091 template <typename InstanceClass>
00092 class CL_Callback_Impl_v0_member : public CL_Callback_Impl_v0
00093 {
00094 public:
00095         CL_Callback_Impl_v0_member(InstanceClass *instance,
00096                 void (InstanceClass::*member_func)())
00097         : instance(instance), member_func(member_func)
00098         {
00099         }
00100 
00101         void invoke() const
00102         {
00103                 (instance->*member_func)();
00104         }
00105 
00106         InstanceClass *instance;
00107 
00108         void (InstanceClass::*member_func)();
00109 };
00110 
00113 template <typename InstanceClass, typename UserData>
00114 class CL_Callback_Impl_v0_member_user : public CL_Callback_Impl_v0
00115 {
00116 public:
00117         CL_Callback_Impl_v0_member_user(InstanceClass *instance,
00118                 void (InstanceClass::*member_func)(UserData), const UserData &user_data)
00119         : instance(instance), member_func(member_func), user_data(user_data)
00120         {
00121         }
00122 
00123         void invoke() const
00124         {
00125                 (instance->*member_func)(user_data);
00126         }
00127 
00128         InstanceClass *instance;
00129 
00130         void (InstanceClass::*member_func)(UserData);
00131 
00132         UserData user_data;
00133 };
00134 
00137 template <class Functor>
00138 class CL_Callback_Impl_v0_functor : public CL_Callback_Impl_v0
00139 {
00140 public:
00141         CL_Callback_Impl_v0_functor(Functor functor)
00142         : functor(functor)
00143         {
00144         }
00145 
00146         void invoke() const
00147         {
00148                 functor();
00149         }
00150 
00151         Functor functor;
00152 };
00153 
00157 class CL_Callback_v0
00158 {
00159 public:
00160         CL_Callback_v0()
00161         {
00162         }
00163 
00164         CL_Callback_v0(const CL_Callback_v0 &copy)
00165         : impl(copy.impl)
00166         {
00167         }
00168 
00169         CL_Callback_v0(CL_Callback_Impl_v0 *impl)
00170         : impl(impl)
00171         {
00172         }
00173 
00174         CL_Callback_v0(void (*function)())
00175         : impl(new CL_Callback_Impl_v0_static(function))
00176         {
00177         }
00178 
00179         template<typename UserData>
00180         CL_Callback_v0(void (*function)(UserData), const UserData &user_data)
00181         : impl(new CL_Callback_Impl_v0_static_user<UserData>(function, user_data))
00182         {
00183         }
00184 
00185         template<class InstanceClass>
00186         CL_Callback_v0(InstanceClass *instance, void (InstanceClass::*function)())
00187         : impl(new CL_Callback_Impl_v0_member<InstanceClass>(instance, function))
00188         {
00189         }
00190 
00191         template<class InstanceClass, typename UserData>
00192         CL_Callback_v0(InstanceClass *instance, void (InstanceClass::*function)(UserData), const UserData &user_data)
00193         : impl(new CL_Callback_Impl_v0_member_user<InstanceClass, UserData>(instance, function, user_data))
00194         {
00195         }
00196 
00197         void set(void (*function)())
00198         {
00199                 impl = CL_SharedPtr< CL_Callback_Impl_v0 >(new CL_Callback_Impl_v0_static(function));
00200         }
00201 
00202         template<typename UserData>
00203         void set(void (*function)(UserData), const UserData &user_data)
00204         {
00205                 impl = CL_SharedPtr< CL_Callback_Impl_v0 >(new CL_Callback_Impl_v0_static_user<UserData>(function, user_data));
00206         }
00207 
00208         template<class InstanceClass>
00209         void set(InstanceClass *instance, void (InstanceClass::*function)())
00210         {
00211                 impl = CL_SharedPtr< CL_Callback_Impl_v0 >(new CL_Callback_Impl_v0_member<InstanceClass>(instance, function));
00212         }
00213 
00214         template<class InstanceClass, typename UserData>
00215         void set(InstanceClass *instance, void (InstanceClass::*function)(UserData), const UserData &user_data)
00216         {
00217                 impl = CL_SharedPtr< CL_Callback_Impl_v0 >(new CL_Callback_Impl_v0_member_user<InstanceClass, UserData>(instance, function, user_data));
00218         }
00219 
00220         void clear()
00221         {
00222                 impl = CL_SharedPtr< CL_Callback_Impl_v0 >();
00223         }
00224 
00225         void invoke() const
00226         {
00227                 impl->invoke();
00228         }
00229 
00230         bool is_null() const
00231         {
00232                 return !impl;
00233         }
00234 
00235 private:
00236         CL_SharedPtr< CL_Callback_Impl_v0 > impl;
00237 };
00238 
00242 class CL_Callback_v0_functor : public CL_Callback_v0
00243 {
00244 public:
00245         CL_Callback_v0_functor()
00246         {
00247         }
00248 
00249         CL_Callback_v0_functor(const CL_Callback_v0_functor &copy)
00250         : CL_Callback_v0(copy)
00251         {
00252         }
00253 
00254         template<class Functor>
00255         CL_Callback_v0_functor(Functor functor)
00256         : CL_Callback_v0(new CL_Callback_Impl_v0_functor<Functor>(functor))
00257         {
00258         }
00259 
00260 };
00261 
00262