]> git.mxchange.org Git - simgear.git/blob - simgear/structure/callback.hxx
Remove stray ';'
[simgear.git] / simgear / structure / callback.hxx
1 /**************************************************************************
2  * callback.hxx -- Wrapper classes to treat function and method pointers
3  * as objects.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License as
7  * published by the Free Software Foundation; either version 2 of the
8  * License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18  *
19  * $Id$
20  **************************************************************************/
21
22 #ifndef _SG_CALLBACK_HXX
23 #define _SG_CALLBACK_HXX
24
25 /**
26  * Abstract base class for all callbacks.
27  */
28 class SGCallback
29 {
30 public:
31
32     /**
33      * 
34      */
35     virtual ~SGCallback() {}
36
37     /**
38      * 
39      */
40     virtual SGCallback* clone() const = 0;
41
42     /**
43      * Execute the callback function.
44      */
45     virtual void operator()() = 0;
46
47 protected:
48     /**
49      * 
50      */
51     SGCallback() {}
52
53 private:
54     // Not implemented.
55     void operator=( const SGCallback& );
56 };
57
58 /**
59  * Callback for invoking a file scope function.
60  */
61 template< typename Fun >
62 class SGFunctionCallback : public SGCallback
63 {
64 public:
65     /**
66      * 
67      */
68     SGFunctionCallback( const Fun& fun )
69         : SGCallback(), f_(fun) {}
70
71     SGCallback* clone() const
72     {
73         return new SGFunctionCallback( *this );
74     }
75
76     void operator()() { f_(); }
77
78 private:
79     // Not defined.
80     SGFunctionCallback();
81
82 private:
83     Fun f_;
84 };
85
86 /**
87  * Callback for invoking a member function.
88  */
89 template< class ObjPtr, typename MemFn >
90 class SGMethodCallback : public SGCallback
91 {
92 public:
93
94     /**
95      * 
96      */
97     SGMethodCallback( const ObjPtr& pObj, MemFn pMemFn )
98         : SGCallback(),
99           pObj_(pObj),
100           pMemFn_(pMemFn)
101     {
102     }
103
104     /**
105      * 
106      */
107     SGCallback* clone() const
108     {
109         return new SGMethodCallback( *this );
110     }
111
112     /**
113      * 
114      */
115     void operator()()
116     {
117         ((*pObj_).*pMemFn_)();
118     }
119
120 private:
121     // Not defined.
122     SGMethodCallback();
123
124 private:
125     ObjPtr pObj_;
126     MemFn pMemFn_;
127 };
128
129 /**
130  * Helper template functions.
131  */
132
133 template< typename Fun >
134 SGCallback*
135 make_callback( const Fun& fun )
136 {
137     return new SGFunctionCallback<Fun>(fun);
138 }
139
140 template< class ObjPtr, typename MemFn >
141 SGCallback*
142 make_callback( const ObjPtr& pObj, MemFn pMemFn )
143 {
144     return new SGMethodCallback<ObjPtr,MemFn>(pObj, pMemFn );
145 }
146
147 #endif // _SG_CALLBACK_HXX
148