job.h 14.3 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

    /**
     * Abstract base class for a job that can be executed.
     */
    class AbstractJob {
    public:
        /**
         * Pure virtual destructor.
         */
schultezub's avatar
schultezub committed
46
        virtual ~AbstractJob() {};
schultezub's avatar
schultezub committed
47
48
49
50
51

        /**
         * 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
    /**
     * Specific job, that is calling a member function pasing a single argument.
     */
174
175
    template<class T, class A1, class A2>
    class CallMemberFunc2ArgJob : public AbstractJob {
176
177
    public:
        /**
178
         * Creates an new job, that is calling \a callee on \a target pasing \a arg1 and \a arg2 as arguments.
179
180
         * \param   target  Target object
         * \param   callee  Member function to call
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
         * \param   arg1    First argument to pass to \a callee
         * \param   arg2    Second argument to pass to \a callee
         */
        CallMemberFunc2ArgJob(T* target, void (T::*callee)(A1, A2), A1 arg1, A2 arg2)
            : _target(target)
            , _callee(callee)
            , _arg1(arg1)
            , _arg2(arg2)
        {
            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
         */
        ~CallMemberFunc2ArgJob() {};

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

    protected:
        T* _target;                 ///< Target object
        void (T::*_callee)(A1, A2); ///< Member function to call
        A1 _arg1;                   ///< First argument to pass to \a callee
        A2 _arg2;                   ///< Second argument to pass to \a callee
    };
    
    /**
     * Specific job, that is calling a function passing two arguments.
     */
    template<class A1, class A2>
    class CallFunc2ArgJob : public AbstractJob {
    public:
        /**
         * Creates an new job, that is calling \a callee passing \a arg1 and \a arg2 as arguments.
         * \param   callee  Function to call
         * \param   arg1    First argument to pass to \a callee
         * \param   arg2    Second argument to pass to \a callee
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
         */
        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() {};

        /**
239
         * Executes this job by calling the function.
240
241
242
243
244
245
         */
        virtual void execute() {
            (*_callee)(_arg1, _arg2);
        }

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

251
252
253
// = Helper functions for easier creation of jobs =================================================

    /**
254
     * Creates a new CallMemberFuncJob on the heap for the object \a target.
255
256
257
258
259
260
     * \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>
261
    CallMemberFuncJob<T>* makeJobOnHeap(T* target, void (T::*callee)()) {
262
263
264
265
        return new CallMemberFuncJob<T>(target, callee);
    }

    /**
266
267
268
269
270
271
272
273
274
275
276
277
     * 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.
278
279
280
281
282
283
284
     * \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>
285
    CallMemberFunc1ArgJob<T, A1>* makeJobOnHeap(T* target, void (T::*callee)(A1), A1 arg1) {
286
287
288
        return new CallMemberFunc1ArgJob<T, A1>(target, callee, arg1);
    }

289
290
291
292
293
294
295
296
297
298
299
300
    /**
     * 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);
    }

301
    /**
302
     * Creates a new CallFunc1ArgJob on the heap  for the object \a target.
303
304
305
306
     * \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.
307
     * \return  Pointer to the newly created CallFunc1ArgJob. Caller has ownership!
308
309
310
311
312
313
314
     */
    template<class A1>
    CallFunc1ArgJob<A1>* makeJobOnHeap(void (*callee)(A1), A1 arg1) {
        return new CallFunc1ArgJob<A1>(callee, arg1);
    }

    /**
315
     * Creates a new CallFunc1ArgJob on the stack  for the object \a target.
316
317
318
     * \param   target  Target object to call method from.
     * \param   callee  Pointer to method to call.
     * \param   arg1    First argument to pass to \callee.
319
     * \return  The newly created CallFunc1ArgJob.
320
321
322
323
324
325
     */
    template<class A1>
    CallFunc1ArgJob<A1> makeJob(void (*callee)(A1), A1 arg1) {
        return CallFunc1ArgJob<A1>(callee, arg1);
    }

326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
    /**
     * Creates a new CallMemberFunc1ArgJob 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.
     * \return  Pointer to the newly created CallMemberFunc1ArgJob. Caller has ownership!
     */
    template<class T, class A1, class A2>
    CallMemberFunc2ArgJob<T, A1, A2>* makeJobOnHeap(T* target, void (T::*callee)(A1, A2), A1 arg1, A2 arg2) {
        return new CallMemberFunc2ArgJob<T, A1, A2>(target, callee, arg1, arg2);
    }

    /**
     * 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, class A2>
    CallMemberFunc2ArgJob<T, A1, A2> makeJob(T* target, void (T::*callee)(A1, A2), A1 arg1, A2 arg2) {
        return CallMemberFunc2ArgJob<T, A1, A2>(target, callee, arg1, arg2);
    }

351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
    /**
     * 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
376
377
378
}

#endif // JOB_H__