]> git.mxchange.org Git - flightgear.git/blob - src/Include/fg_callback.hxx
Merge branches 'jmt/spatial', 'jmt/ref_ptr', 'jmt/navradio' and 'jmt/gps'
[flightgear.git] / src / Include / fg_callback.hxx
1 /**************************************************************************
2  * fg_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 _FG_CALLBACK_HXX
23 #define _FG_CALLBACK_HXX
24
25 /**
26  * Abstract base class for all FlightGear callbacks.
27  */
28 class fgCallback
29 {
30 public:
31
32     /**
33      * 
34      */
35     virtual ~fgCallback() {}
36
37     /**
38      * 
39      */
40     virtual fgCallback* clone() const = 0;
41
42     /**
43      * Execute the callback function.
44      */
45     virtual void operator()() = 0;
46
47 protected:
48     /**
49      * 
50      */
51     fgCallback() {}
52
53 private:
54     // Not implemented.
55     void operator=( const fgCallback& );
56 };
57
58 /**
59  * Callback for invoking a file scope function.
60  */
61 template< typename Fun >
62 class fgFunctionCallback : public fgCallback
63 {
64 public:
65     /**
66      * 
67      */
68     fgFunctionCallback( const Fun& fun )
69         : fgCallback(), f_(fun) {}
70
71     fgCallback* clone() const
72     {
73         return new fgFunctionCallback( *this );
74     }
75
76     void operator()() { f_(); }
77
78 private:
79     // Not defined.
80     fgFunctionCallback();
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 fgMethodCallback : public fgCallback
91 {
92 public:
93
94     /**
95      * 
96      */
97     fgMethodCallback( const ObjPtr& pObj, MemFn pMemFn )
98         : fgCallback(),
99           pObj_(pObj),
100           pMemFn_(pMemFn)
101     {
102     }
103
104     /**
105      * 
106      */
107     fgCallback* clone() const
108     {
109         return new fgMethodCallback( *this );
110     }
111
112     /**
113      * 
114      */
115     void operator()()
116     {
117         ((*pObj_).*pMemFn_)();
118     }
119
120 private:
121     // Not defined.
122     fgMethodCallback();
123
124 private:
125     ObjPtr pObj_;
126     MemFn pMemFn_;
127 };
128
129 /**
130  * Helper template functions.
131  */
132
133 template< typename Fun >
134 fgCallback*
135 make_callback( const Fun& fun )
136 {
137     return new fgFunctionCallback<Fun>(fun);
138 }
139
140 template< class ObjPtr, typename MemFn >
141 fgCallback*
142 make_callback( const ObjPtr& pObj, MemFn pMemFn )
143 {
144     return new fgMethodCallback<ObjPtr,MemFn>(pObj, pMemFn );
145 }
146
147 #endif // _FG_CALLBACK_HXX
148