[cosmetics] update date in GPL header
[vuplus_xbmc] / xbmc / interfaces / legacy / CallbackFunction.h
1 /*
2  *      Copyright (C) 2005-2013 Team XBMC
3  *      http://www.xbmc.org
4  *
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)
8  *  any later version.
9  *
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.
14  *
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
19  *
20  */
21
22 #pragma once
23
24 #include "AddonClass.h"
25
26 namespace XBMCAddon
27 {
28   /**
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>
32    *
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>
37    */
38   class Callback : public AddonClass 
39   { 
40   protected:
41     AddonClass* addonClassObject;
42     Callback(AddonClass* _object, const char* classname) : AddonClass(classname), addonClassObject(_object) {}
43
44   public:
45     virtual void executeCallback() = 0;
46     virtual ~Callback();
47
48     AddonClass* getObject() { return addonClassObject; }
49   };
50
51   struct cb_null_type {};
52
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 {};
58   
59   /**
60    * This is the template to carry a callback to a member function
61    *  that returns 'void' (has no return) and takes no parameters.
62    */
63   template<class M> class CallbackFunction<M, cb_null_type, cb_null_type, cb_null_type, cb_null_type, cb_null_type> : public Callback
64   { 
65   public:
66     typedef void (M::*MemberFunction)();
67
68   protected:
69     MemberFunction meth;
70     M* obj;
71
72   public:
73     CallbackFunction(M* object, MemberFunction method) : 
74       Callback(object, "CallbackFunction<M>"), meth(method), obj(object) {}
75
76     virtual ~CallbackFunction() { deallocating(); }
77
78     virtual void executeCallback() { TRACE; ((*obj).*(meth))(); }
79   };
80
81   /**
82    * This is the template to carry a callback to a member function
83    *  that returns 'void' (has no return) and takes one parameter.
84    */
85   template<class M, typename P1> class CallbackFunction<M,P1, cb_null_type, cb_null_type, cb_null_type, cb_null_type> : public Callback
86   { 
87   public:
88     typedef void (M::*MemberFunction)(P1);
89
90   protected:
91     MemberFunction meth;
92     M* obj;
93     P1 param;
94
95   public:
96     CallbackFunction(M* object, MemberFunction method, P1 parameter) : 
97       Callback(object, "CallbackFunction<M,P1>"), meth(method), obj(object),
98       param(parameter) {}
99
100     virtual ~CallbackFunction() { deallocating(); }
101
102     virtual void executeCallback() { TRACE; ((*obj).*(meth))(param); }
103   };
104
105   /**
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
109    */
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
111   { 
112   public:
113     typedef void (M::*MemberFunction)(P1*);
114
115   protected:
116     MemberFunction meth;
117     M* obj;
118     AddonClass::Ref<P1> param;
119
120   public:
121     CallbackFunction(M* object, MemberFunction method, P1* parameter) : 
122       Callback(object, "CallbackFunction<M,P1>"), meth(method), obj(object),
123       param(parameter) {}
124
125     virtual ~CallbackFunction() { deallocating(); }
126
127     virtual void executeCallback() { TRACE; ((*obj).*(meth))(param); }
128   };
129
130
131   /**
132    * This is the template to carry a callback to a member function
133    *  that returns 'void' (has no return) and takes two parameters.
134    */
135   template<class M, typename P1, typename P2> class CallbackFunction<M,P1,P2, cb_null_type, cb_null_type, cb_null_type> : public Callback
136   { 
137   public:
138     typedef void (M::*MemberFunction)(P1,P2);
139
140   protected:
141     MemberFunction meth;
142     M* obj;
143     P1 param1;
144     P2 param2;
145
146   public:
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) {}
150
151     virtual ~CallbackFunction() { deallocating(); }
152
153     virtual void executeCallback() { TRACE; ((*obj).*(meth))(param1,param2); }
154   };
155
156
157 }
158
159