2 * Copyright (C) 2005-2013 Team XBMC
5 * This Program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2, or (at your option)
10 * This Program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with XBMC; see the file COPYING. If not, write to
17 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
18 * http://www.gnu.org/copyleft/gpl.html
24 #include "AddonClass.h"
29 * <p>This is the parent class for the class templates that hold
30 * a callback. A callback is essentially a templatized
31 * functor (functoid?) for a call to a member function.</p>
33 * <p>This class combined with the attending CallbackHandlers should make
34 * sure that the AddonClass isn't in the midst of deallocating when
35 * the callback executes. In this way the Callback class acts as
36 * a weak reference.</p>
38 class Callback : public AddonClass
41 AddonClass* addonClassObject;
42 Callback(AddonClass* _object, const char* classname) : AddonClass(classname), addonClassObject(_object) {}
45 virtual void executeCallback() = 0;
48 AddonClass* getObject() { return addonClassObject; }
51 struct cb_null_type {};
53 // stub type template to be partial specialized
54 template<typename M = cb_null_type, typename T1 = cb_null_type,
55 typename T2 = cb_null_type, typename T3 = cb_null_type,
56 typename T4 = cb_null_type, typename Extraneous = cb_null_type>
57 class CallbackFunction {};
60 * This is the template to carry a callback to a member function
61 * that returns 'void' (has no return) and takes no parameters.
63 template<class M> class CallbackFunction<M, cb_null_type, cb_null_type, cb_null_type, cb_null_type, cb_null_type> : public Callback
66 typedef void (M::*MemberFunction)();
73 CallbackFunction(M* object, MemberFunction method) :
74 Callback(object, "CallbackFunction<M>"), meth(method), obj(object) {}
76 virtual ~CallbackFunction() { deallocating(); }
78 virtual void executeCallback() { TRACE; ((*obj).*(meth))(); }
82 * This is the template to carry a callback to a member function
83 * that returns 'void' (has no return) and takes one parameter.
85 template<class M, typename P1> class CallbackFunction<M,P1, cb_null_type, cb_null_type, cb_null_type, cb_null_type> : public Callback
88 typedef void (M::*MemberFunction)(P1);
96 CallbackFunction(M* object, MemberFunction method, P1 parameter) :
97 Callback(object, "CallbackFunction<M,P1>"), meth(method), obj(object),
100 virtual ~CallbackFunction() { deallocating(); }
102 virtual void executeCallback() { TRACE; ((*obj).*(meth))(param); }
106 * This is the template to carry a callback to a member function
107 * that returns 'void' (has no return) and takes one parameter
108 * that can be held in an AddonClass::Ref
110 template<class M, typename P1> class CallbackFunction<M,AddonClass::Ref<P1>, cb_null_type, cb_null_type, cb_null_type, cb_null_type> : public Callback
113 typedef void (M::*MemberFunction)(P1*);
118 AddonClass::Ref<P1> param;
121 CallbackFunction(M* object, MemberFunction method, P1* parameter) :
122 Callback(object, "CallbackFunction<M,P1>"), meth(method), obj(object),
125 virtual ~CallbackFunction() { deallocating(); }
127 virtual void executeCallback() { TRACE; ((*obj).*(meth))(param); }
132 * This is the template to carry a callback to a member function
133 * that returns 'void' (has no return) and takes two parameters.
135 template<class M, typename P1, typename P2> class CallbackFunction<M,P1,P2, cb_null_type, cb_null_type, cb_null_type> : public Callback
138 typedef void (M::*MemberFunction)(P1,P2);
147 CallbackFunction(M* object, MemberFunction method, P1 parameter, P2 parameter2) :
148 Callback(object, "CallbackFunction<M,P1,P2>"), meth(method), obj(object),
149 param1(parameter), param2(parameter2) {}
151 virtual ~CallbackFunction() { deallocating(); }
153 virtual void executeCallback() { TRACE; ((*obj).*(meth))(param1,param2); }