callback_2.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 template <typename RetVal, typename P1, typename P2>
00040 class CL_Callback_Impl_2
00041 {
00042 public:
00043         virtual ~CL_Callback_Impl_2()
00044         {
00045         }
00046 
00047         virtual RetVal invoke(P1 p1, P2 p2) const = 0;
00048 };
00049 
00052 template <typename RetVal, typename P1, typename P2>
00053 class CL_Callback_Impl_2_static : public CL_Callback_Impl_2<RetVal, P1, P2>
00054 {
00055 public:
00056         CL_Callback_Impl_2_static(RetVal (*static_func)(P1, P2))
00057         : static_func(static_func)
00058         {
00059         }
00060 
00061         RetVal invoke(P1 p1, P2 p2) const
00062         {
00063                 return static_func(p1, p2);
00064         }
00065 
00066         RetVal (*static_func)(P1, P2);
00067 };
00068 
00071 template <typename RetVal, typename P1, typename P2, typename UserData>
00072 class CL_Callback_Impl_2_static_user : public CL_Callback_Impl_2<RetVal, P1, P2>
00073 {
00074 public:
00075         CL_Callback_Impl_2_static_user(
00076                 RetVal (*static_func)(P1, P2, UserData), const UserData &user_data)
00077         : static_func(static_func), user_data(user_data)
00078         {
00079         }
00080 
00081         RetVal invoke(P1 p1, P2 p2) const
00082         {
00083                 return static_func(p1, p2, user_data);
00084         }
00085 
00086         RetVal (*static_func)(P1, P2, UserData);
00087 
00088         UserData user_data;
00089 };
00090 
00093 template <typename RetVal, typename P1, typename P2, typename InstanceClass>
00094 class CL_Callback_Impl_2_member : public CL_Callback_Impl_2<RetVal, P1, P2>
00095 {
00096 public:
00097         CL_Callback_Impl_2_member(InstanceClass *instance,
00098                 RetVal (InstanceClass::*member_func)(P1, P2))
00099         : instance(instance), member_func(member_func)
00100         {
00101         }
00102 
00103         RetVal invoke(P1 p1, P2 p2) const
00104         {
00105                 return (instance->*member_func)(p1, p2);
00106         }
00107 
00108         InstanceClass *instance;
00109 
00110         RetVal (InstanceClass::*member_func)(P1, P2);
00111 };
00112 
00115 template <typename RetVal, typename P1, typename P2, typename InstanceClass, typename UserData>
00116 class CL_Callback_Impl_2_member_user : public CL_Callback_Impl_2<RetVal, P1, P2>
00117 {
00118 public:
00119         CL_Callback_Impl_2_member_user(InstanceClass *instance,
00120                 RetVal (InstanceClass::*member_func)(P1, P2, UserData), const UserData &user_data)
00121         : instance(instance), member_func(member_func), user_data(user_data)
00122         {
00123         }
00124 
00125         RetVal invoke(P1 p1, P2 p2) const
00126         {
00127                 return (instance->*member_func)(p1, p2, user_data);
00128         }
00129 
00130         InstanceClass *instance;
00131 
00132         RetVal (InstanceClass::*member_func)(P1, P2, UserData);
00133 
00134         UserData user_data;
00135 };
00136 
00139 template <typename RetVal, class P1, class P2, class Functor>
00140 class CL_Callback_Impl_2_functor : public CL_Callback_Impl_2<RetVal, P1, P2>
00141 {
00142 public:
00143         CL_Callback_Impl_2_functor(Functor functor)
00144         : functor(functor)
00145         {
00146         }
00147 
00148         RetVal invoke(P1 p1, P2 p2) const
00149         {
00150                 return functor(p1, p2);
00151         }
00152 
00153         Functor functor;
00154 };
00155 
00159 template <typename RetVal, typename P1, typename P2>
00160 class CL_Callback_2
00161 {
00162 public:
00163         CL_Callback_2()
00164         {
00165         }
00166 
00167         CL_Callback_2(const CL_Callback_2 &copy)
00168         : impl(copy.impl)
00169         {
00170         }
00171 
00172         CL_Callback_2(CL_Callback_Impl_2<RetVal, P1, P2> *impl)
00173         : impl(impl)
00174         {
00175         }
00176 
00177         CL_Callback_2(RetVal (*function)(P1, P2))
00178         : impl(new CL_Callback_Impl_2_static<RetVal, P1, P2>(function))
00179         {
00180         }
00181 
00182         template<typename UserData>
00183         CL_Callback_2(RetVal (*function)(P1, P2, UserData), const UserData &user_data)
00184         : impl(new CL_Callback_Impl_2_static_user<RetVal, P1, P2, UserData>(function, user_data))
00185         {
00186         }
00187 
00188         template<class InstanceClass>
00189         CL_Callback_2(InstanceClass *instance, RetVal (InstanceClass::*function)(P1, P2))
00190         : impl(new CL_Callback_Impl_2_member<RetVal, P1, P2, InstanceClass>(instance, function))
00191         {
00192         }
00193 
00194         template<class InstanceClass, typename UserData>
00195         CL_Callback_2(InstanceClass *instance, RetVal (InstanceClass::*function)(P1, P2, UserData), const UserData &user_data)
00196         : impl(new CL_Callback_Impl_2_member_user<RetVal, P1, P2, InstanceClass, UserData>(instance, function, user_data))
00197         {
00198         }
00199 
00200         void set(RetVal (*function)(P1, P2))
00201         {
00202                 impl = CL_SharedPtr< CL_Callback_Impl_2<RetVal, P1, P2> >(new CL_Callback_Impl_2_static<RetVal, P1, P2>(function));
00203         }
00204 
00205         template<typename UserData>
00206         void set(RetVal (*function)(P1, P2, UserData), const UserData &user_data)
00207         {
00208                 impl = CL_SharedPtr< CL_Callback_Impl_2<RetVal, P1, P2> >(new CL_Callback_Impl_2_static_user<RetVal, P1, P2, UserData>(function, user_data));
00209         }
00210 
00211         template<class InstanceClass>
00212         void set(InstanceClass *instance, RetVal (InstanceClass::*function)(P1, P2))
00213         {
00214                 impl = CL_SharedPtr< CL_Callback_Impl_2<RetVal, P1, P2> >(new CL_Callback_Impl_2_member<RetVal, P1, P2, InstanceClass>(instance, function));
00215         }
00216 
00217         template<class InstanceClass, typename UserData>
00218         void set(InstanceClass *instance, RetVal (InstanceClass::*function)(P1, P2, UserData), const UserData &user_data)
00219         {
00220                 impl = CL_SharedPtr< CL_Callback_Impl_2<RetVal, P1, P2> >(new CL_Callback_Impl_2_member_user<RetVal, P1, P2, InstanceClass, UserData>(instance, function, user_data));
00221         }
00222 
00223         void clear()
00224         {
00225                 impl = CL_SharedPtr< CL_Callback_Impl_2<RetVal, P1, P2> >();
00226         }
00227 
00228         RetVal invoke(P1 p1, P2 p2) const
00229         {
00230                 return impl->invoke(p1, p2);
00231         }
00232 
00233         bool is_null() const
00234         {
00235                 return !impl;
00236         }
00237 
00238 private:
00239         CL_SharedPtr< CL_Callback_Impl_2<RetVal, P1, P2> > impl;
00240 };
00241 
00245 template <typename RetVal, typename P1, typename P2>
00246 class CL_Callback_2_functor : public CL_Callback_2<RetVal, P1, P2>
00247 {
00248 public:
00249         CL_Callback_2_functor()
00250         {
00251         }
00252 
00253         CL_Callback_2_functor(const CL_Callback_2_functor &copy)
00254         : CL_Callback_2<RetVal, P1, P2>(copy)
00255         {
00256         }
00257 
00258         template<class Functor>
00259         CL_Callback_2_functor(Functor functor)
00260         : CL_Callback_2<RetVal, P1, P2>(new CL_Callback_Impl_2_functor<RetVal, P1, P2, Functor>(functor))
00261         {
00262         }
00263 
00264 };
00265 
00266