job.h 16.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
/**********************************************************************
 *                                                                    *
 * cgt - CAMP Graphics Toolbox, Copyright (C) 2012-2014               *
 *     Chair for Computer Aided Medical Procedures                    *
 *     Technische Universitaet Muenchen, Germany.                     *
 *     <http://campar.in.tum.de/>                                     *
 *                                                                    *
 * forked from tgt - Tiny Graphics Toolbox, Copyright (C) 2006-2011   *
 *     Visualization and Computer Graphics Group, Department of       *
 *     Computer Science, University of Muenster, Germany.             *
 *     <http://viscg.uni-muenster.de>                                 *
 *                                                                    *
 * This file is part of the cgt library. This library is free         *
 * software; you can redistribute it and/or modify it under the terms *
 * of the GNU Lesser General Public License version 2.1 as published  *
 * by the Free Software Foundation.                                   *
 *                                                                    *
 * This library is distributed in the hope that it will be useful,    *
 * but WITHOUT ANY WARRANTY; without even the implied warranty of     *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the       *
 * GNU Lesser General Public License for more details.                *
 *                                                                    *
 * You should have received a copy of the GNU Lesser General Public   *
 * License in the file "LICENSE.txt" along with this library.         *
 * If not, see <http://www.gnu.org/licenses/>.                        *
 *                                                                    *
 **********************************************************************/

schultezub's avatar
schultezub committed
29 30 31
#ifndef JOB_H__
#define JOB_H__

32 33
#include "cgt/assert.h"
#include "cgt/types.h"
34

35 36
#include <functional>

37
namespace cgt {
schultezub's avatar
schultezub committed
38 39 40 41

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

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

54 55 56
        /**
         * Executes the job, calls execute().
         */
57 58 59
        void operator()() {
            this->execute();
        }
60 61 62 63 64 65 66 67 68 69 70 71 72 73


        /**
         * Overloading the new operator to create signal handles in signal_manager's memory pool.
         * \param   size    Number of bytes to allocate.
         */
        static void* operator new(std::size_t size) throw(std::bad_alloc);

        /**
         * Overloading the delete operator to correctly remove signal handles from signal_manager's memory pool.
         * \param   rawMemory   Pointer to object to delete
         * \param   size        Number of bytes
         */
        static void operator delete(void* rawMemory, std::size_t size) throw();
schultezub's avatar
schultezub committed
74 75
    };

76
// = Specific Jobs ================================================================================
schultezub's avatar
schultezub committed
77 78 79 80 81

    /**
     * Specific job, that is calling a member function pasing no argument.
     */
    template<class T>
82
    class CallMemberFuncJob : public AbstractJob {
schultezub's avatar
schultezub committed
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)
        {
93 94
            cgtAssert(_target != 0, "Target object must not be 0.");
            cgtAssert(_callee != 0, "Target member function pointer must not be 0.");
schultezub's avatar
schultezub committed
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
        }

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

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

    protected:
        T* _target;                 ///< Target object
111
        void (T::*_callee)();       ///< Member function to call
schultezub's avatar
schultezub committed
112 113 114 115 116 117
    };

    /**
     * Specific job, that is calling a member function pasing a single argument.
     */
    template<class T, class A1>
118
    class CallMemberFunc1ArgJob : public AbstractJob {
schultezub's avatar
schultezub committed
119 120 121 122 123 124 125
    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
         */
126
        CallMemberFunc1ArgJob(T* target, void (T::*callee)(A1), A1 arg1)
schultezub's avatar
schultezub committed
127 128 129 130
            : _target(target)
            , _callee(callee)
            , _arg1(arg1)
        {
131 132
            cgtAssert(_target != 0, "Target object must not be 0.");
            cgtAssert(_callee != 0, "Target member function pointer must not be 0.");
schultezub's avatar
schultezub committed
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
        }

        /**
         * 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
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
        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)
        {
169
            cgtAssert(_callee != 0, "Target member function pointer must not be 0.");
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
        }

        /**
         * 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
186
        A1 _arg1;                   ///< Argument to pass to \a callee
schultezub's avatar
schultezub committed
187
    };
188

189 190 191
    /**
     * Specific job, that is calling a member function pasing a single argument.
     */
192 193
    template<class T, class A1, class A2>
    class CallMemberFunc2ArgJob : public AbstractJob {
194 195
    public:
        /**
196
         * Creates an new job, that is calling \a callee on \a target pasing \a arg1 and \a arg2 as arguments.
197 198
         * \param   target  Target object
         * \param   callee  Member function to call
199 200 201 202 203 204 205 206 207
         * \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)
        {
208 209
            cgtAssert(_target != 0, "Target object must not be 0.");
            cgtAssert(_callee != 0, "Target member function pointer must not be 0.");
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
        }

        /**
         * 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
242 243 244 245 246 247
         */
        CallFunc2ArgJob(void (*callee)(A1, A2), A1 arg1, A2 arg2)
            : _callee(callee)
            , _arg1(arg1)
            , _arg2(arg2)
        {
248
            cgtAssert(_callee != 0, "Target member function pointer must not be 0.");
249 250 251 252 253 254 255 256
        }

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

        /**
257
         * Executes this job by calling the function.
258 259 260 261 262 263
         */
        virtual void execute() {
            (*_callee)(_arg1, _arg2);
        }

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

269 270 271 272

    /**
     * specific job that evaluates a function object
     */
273
    class CGT_API CallFunctionObjectJob : public AbstractJob {
274
    public:
275
        explicit CallFunctionObjectJob(std::function<void(void)> fn) 
276 277 278 279 280 281 282 283 284 285 286 287 288 289
        : _fn(fn)
        {
        }

        ~CallFunctionObjectJob() {};

        virtual void execute() {
            _fn();
        };

    protected:
        std::function<void(void)> _fn;
    };

290 291 292
// = Helper functions for easier creation of jobs =================================================

    /**
293
     * Creates a new CallMemberFuncJob on the heap for the object \a target.
294 295 296 297 298 299
     * \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>
300
    CallMemberFuncJob<T>* makeJobOnHeap(T* target, void (T::*callee)()) {
301 302 303 304
        return new CallMemberFuncJob<T>(target, callee);
    }

    /**
305 306 307 308 309 310 311 312 313 314 315 316
     * 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.
317 318 319 320 321 322 323
     * \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>
324
    CallMemberFunc1ArgJob<T, A1>* makeJobOnHeap(T* target, void (T::*callee)(A1), A1 arg1) {
325 326 327
        return new CallMemberFunc1ArgJob<T, A1>(target, callee, arg1);
    }

328 329 330 331 332 333 334 335 336 337 338 339
    /**
     * 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);
    }

340
    /**
341
     * Creates a new CallFunc1ArgJob on the heap  for the object \a target.
342 343 344 345
     * \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.
346
     * \return  Pointer to the newly created CallFunc1ArgJob. Caller has ownership!
347 348 349 350 351 352 353
     */
    template<class A1>
    CallFunc1ArgJob<A1>* makeJobOnHeap(void (*callee)(A1), A1 arg1) {
        return new CallFunc1ArgJob<A1>(callee, arg1);
    }

    /**
354
     * Creates a new CallFunc1ArgJob on the stack  for the object \a target.
355 356 357
     * \param   target  Target object to call method from.
     * \param   callee  Pointer to method to call.
     * \param   arg1    First argument to pass to \callee.
358
     * \return  The newly created CallFunc1ArgJob.
359 360 361 362 363 364
     */
    template<class A1>
    CallFunc1ArgJob<A1> makeJob(void (*callee)(A1), A1 arg1) {
        return CallFunc1ArgJob<A1>(callee, arg1);
    }

365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
    /**
     * 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);
    }

390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
    /**
     * 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);
    }
415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432

    /**
     * Creates a new CallFunctionObjectJob
     * \NOTE when the compiler decides not to inline this function, there will be linking errors
     *     in that case, try removing this function (it is only scarcely used)
     */
    inline CallFunctionObjectJob makeJob(std::function<void(void)> fn) {
        return CallFunctionObjectJob(fn);
    }

    /**
     * creates a new CallFunctionObjectJob on the Heap
     * \NOTE when the compiler decides not to inline this function, there will be linking errors
     *     in that case, try removing this function (it is only scarcely used)
     */
    inline CallFunctionObjectJob * makeJobOnHeap(std::function<void(void)> fn) {
        return new CallFunctionObjectJob(fn);
    }
schultezub's avatar
schultezub committed
433 434 435
}

#endif // JOB_H__