job.h 11.5 KB
Newer Older
schultezub's avatar
schultezub committed
1
2
// ================================================================================================
// 
schultezub's avatar
schultezub committed
3
// This file is part of the CAMPVis Software Framework.
schultezub's avatar
schultezub committed
4
5
// 
// If not explicitly stated otherwise: Copyright (C) 2012, all rights reserved,
schultezub's avatar
schultezub committed
6
//      Christian Schulte zu Berge <christian.szb@in.tum.de>
schultezub's avatar
schultezub committed
7
8
9
//      Chair for Computer Aided Medical Procedures
//      Technische Universität München
//      Boltzmannstr. 3, 85748 Garching b. München, Germany
schultezub's avatar
schultezub committed
10
// For a full list of authors and contributors, please refer to the file "AUTHORS.txt".
schultezub's avatar
schultezub committed
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// 
// The licensing of this softare is not yet resolved. Until then, redistribution in source or
// binary forms outside the CAMP chair is not permitted, unless explicitly stated in legal form.
// However, the names of the original authors and the above copyright notice must retain in its
// original state in any case.
// 
// Legal disclaimer provided by the BSD license:
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
// IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 
// AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR 
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 
// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
// 
// ================================================================================================

#ifndef JOB_H__
#define JOB_H__

#include "sigslot/sigslot.h"
#include "tgt/assert.h"

schultezub's avatar
schultezub committed
36
namespace campvis {
schultezub's avatar
schultezub committed
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51

    /**
     * Abstract base class for a job that can be executed.
     */
    class AbstractJob {
    public:
        /**
         * Pure virtual destructor.
         */
        virtual ~AbstractJob() = 0 {};

        /**
         * Executes the job, to be overwritten by subclasses.
         */
        virtual void execute() = 0;
52
53
54
55

        void operator()() {
            this->execute();
        }
schultezub's avatar
schultezub committed
56
57
    };

58
// = Specific Jobs ================================================================================
schultezub's avatar
schultezub committed
59
60
61
62
63

    /**
     * Specific job, that is calling a member function pasing no argument.
     */
    template<class T>
64
    class CallMemberFuncJob : public AbstractJob {
schultezub's avatar
schultezub committed
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
    public:
        /**
         * Creates an new job, that is calling \a callee on \a target pasing no argument.
         * \param   target  Target object
         * \param   callee  Member function to call
         */
        CallMemberFuncJob(T* target, void (T::*callee)())
            : _target(target)
            , _callee(callee)
        {
            tgtAssert(_target != 0, "Target object must not be 0.");
            tgtAssert(_callee != 0, "Target member function pointer must not be 0.");
        }

        /**
         * Destructor, nothing to do here
         */
        ~CallMemberFuncJob() {};

        /**
         * Executes this job by calling the member function.
         */
        virtual void execute() {
            (_target->*_callee)();
        }

    protected:
        T* _target;                 ///< Target object
93
        void (T::*_callee)();       ///< Member function to call
schultezub's avatar
schultezub committed
94
95
96
97
98
99
    };

    /**
     * Specific job, that is calling a member function pasing a single argument.
     */
    template<class T, class A1>
100
    class CallMemberFunc1ArgJob : public AbstractJob {
schultezub's avatar
schultezub committed
101
102
103
104
105
106
107
    public:
        /**
         * Creates an new job, that is calling \a callee on \a target pasing \a arg1 as single argument.
         * \param   target  Target object
         * \param   callee  Member function to call
         * \param   arg1    Argument to pass to \a callee
         */
108
        CallMemberFunc1ArgJob(T* target, void (T::*callee)(A1), A1 arg1)
schultezub's avatar
schultezub committed
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
            : _target(target)
            , _callee(callee)
            , _arg1(arg1)
        {
            tgtAssert(_target != 0, "Target object must not be 0.");
            tgtAssert(_callee != 0, "Target member function pointer must not be 0.");
        }

        /**
         * Destructor, nothing to do here
         */
        ~CallMemberFunc1ArgJob() {};

        /**
         * Executes this job by calling the member function.
         */
        virtual void execute() {
            (_target->*_callee)(_arg1);
        }

    protected:
        T* _target;                 ///< Target object
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
        void (T::*_callee)(A1);     ///< Member function to call
        A1 _arg1;                   ///< Argument to pass to \a callee
    };
    
    /**
     * Specific job, that is calling a member function pasing a single argument.
     */
    template<class A1>
    class CallFunc1ArgJob : public AbstractJob {
    public:
        /**
         * Creates an new job, that is calling \a callee on \a target pasing \a arg1 as single argument.
         * \param   target  Target object
         * \param   callee  Member function to call
         * \param   arg1    Argument to pass to \a callee
         */
        CallFunc1ArgJob(void (*callee)(A1), A1 arg1)
            : _callee(callee)
            , _arg1(arg1)
        {
            tgtAssert(_callee != 0, "Target member function pointer must not be 0.");
        }

        /**
         * Destructor, nothing to do here
         */
        ~CallFunc1ArgJob() {};

        /**
         * Executes this job by calling the member function.
         */
        virtual void execute() {
            (*_callee)(_arg1);
        }

    protected:
        void (*_callee)(A1);        ///< Function to call
168
        A1 _arg1;                   ///< Argument to pass to \a callee
schultezub's avatar
schultezub committed
169
    };
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
    
    /**
     * Specific job, that is calling a member function pasing a single argument.
     */
    template<class A1, class A2>
    class CallFunc2ArgJob : public AbstractJob {
    public:
        /**
         * Creates an new job, that is calling \a callee on \a target pasing \a arg1 as single argument.
         * \param   target  Target object
         * \param   callee  Member function to call
         * \param   arg1    Argument to pass to \a callee
         */
        CallFunc2ArgJob(void (*callee)(A1, A2), A1 arg1, A2 arg2)
            : _callee(callee)
            , _arg1(arg1)
            , _arg2(arg2)
        {
            tgtAssert(_callee != 0, "Target member function pointer must not be 0.");
        }

        /**
         * Destructor, nothing to do here
         */
        ~CallFunc2ArgJob() {};

        /**
         * Executes this job by calling the member function.
         */
        virtual void execute() {
            (*_callee)(_arg1, _arg2);
        }

    protected:
        void (*_callee)(A1, A2);        ///< Function to call
        A1 _arg1;                   ///< First Argument to pass to \a callee
        A2 _arg2;                   ///< Second Argument to pass to \a callee
    };
schultezub's avatar
schultezub committed
208

209
210
211
// = Helper functions for easier creation of jobs =================================================

    /**
212
     * Creates a new CallMemberFuncJob on the heap for the object \a target.
213
214
215
216
217
218
     * \note    The caller takes ownership of the returned pointer.
     * \param   target  Target object to call method from.
     * \param   callee  Pointer to method to call.
     * \return  Pointer to the newly created CallMemberFuncJob. Caller has ownership!
     */
    template<class T>
219
    CallMemberFuncJob<T>* makeJobOnHeap(T* target, void (T::*callee)()) {
220
221
222
223
        return new CallMemberFuncJob<T>(target, callee);
    }

    /**
224
225
226
227
228
229
230
231
232
233
234
235
     * Creates a new CallMemberFuncJob on the stack for the object \a target.
     * \param   target  Target object to call method from.
     * \param   callee  Pointer to method to call.
     * \return  The newly created CallMemberFuncJob
     */
    template<class T>
    CallMemberFuncJob<T> makeJob(T* target, void (T::*callee)()) {
        return CallMemberFuncJob<T>(target, callee);
    }

    /**
     * Creates a new CallMemberFunc1ArgJob on the heap  for the object \a target.
236
237
238
239
240
241
242
     * \note    The caller takes ownership of the returned pointer.
     * \param   target  Target object to call method from.
     * \param   callee  Pointer to method to call.
     * \param   arg1    First argument to pass to \callee.
     * \return  Pointer to the newly created CallMemberFunc1ArgJob. Caller has ownership!
     */
    template<class T, class A1>
243
    CallMemberFunc1ArgJob<T, A1>* makeJobOnHeap(T* target, void (T::*callee)(A1), A1 arg1) {
244
245
246
        return new CallMemberFunc1ArgJob<T, A1>(target, callee, arg1);
    }

247
248
249
250
251
252
253
254
255
256
257
258
    /**
     * Creates a new CallMemberFunc1ArgJob on the stack  for the object \a target.
     * \param   target  Target object to call method from.
     * \param   callee  Pointer to method to call.
     * \param   arg1    First argument to pass to \callee.
     * \return  The newly created CallMemberFunc1ArgJob.
     */
    template<class T, class A1>
    CallMemberFunc1ArgJob<T, A1> makeJob(T* target, void (T::*callee)(A1), A1 arg1) {
        return CallMemberFunc1ArgJob<T, A1>(target, callee, arg1);
    }

259
    /**
260
     * Creates a new CallFunc1ArgJob on the heap  for the object \a target.
261
262
263
264
     * \note    The caller takes ownership of the returned pointer.
     * \param   target  Target object to call method from.
     * \param   callee  Pointer to method to call.
     * \param   arg1    First argument to pass to \callee.
265
     * \return  Pointer to the newly created CallFunc1ArgJob. Caller has ownership!
266
267
268
269
270
271
272
     */
    template<class A1>
    CallFunc1ArgJob<A1>* makeJobOnHeap(void (*callee)(A1), A1 arg1) {
        return new CallFunc1ArgJob<A1>(callee, arg1);
    }

    /**
273
     * Creates a new CallFunc1ArgJob on the stack  for the object \a target.
274
275
276
     * \param   target  Target object to call method from.
     * \param   callee  Pointer to method to call.
     * \param   arg1    First argument to pass to \callee.
277
     * \return  The newly created CallFunc1ArgJob.
278
279
280
281
282
283
     */
    template<class A1>
    CallFunc1ArgJob<A1> makeJob(void (*callee)(A1), A1 arg1) {
        return CallFunc1ArgJob<A1>(callee, arg1);
    }

284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
    /**
     * Creates a new CallFunc2ArgJob on the heap  for the object \a target.
     * \note    The caller takes ownership of the returned pointer.
     * \param   target  Target object to call method from.
     * \param   callee  Pointer to method to call.
     * \param   arg1    First argument to pass to \callee.
     * \param   arg2    Second argument to pass to \callee.
     * \return  Pointer to the newly created CallFunc2ArgJob. Caller has ownership!
     */
    template<class A1, class A2>
    CallFunc2ArgJob<A1, A2>* makeJobOnHeap(void (*callee)(A1, A2), A1 arg1, A2 arg2) {
        return new CallFunc2ArgJob<A1, A2>(callee, arg1, arg2);
    }

    /**
     * Creates a new CallFunc2ArgJob on the stack  for the object \a target.
     * \param   target  Target object to call method from.
     * \param   callee  Pointer to method to call.
     * \param   arg2    Second argument to pass to \callee.
     * \return  The newly created CallFunc2ArgJob.
     */
    template<class A1, class A2>
    CallFunc2ArgJob<A1, A2> makeJob(void (*callee)(A1, A2), A1 arg1, A2 arg2) {
        return CallFunc2ArgJob<A1, A2>(callee, arg1, arg2);
    }
schultezub's avatar
schultezub committed
309
310
311
}

#endif // JOB_H__