LCOV - code coverage report
Current view: top level - co - commandFunc.h (source / functions) Hit Total Coverage
Test: Collage Lines: 18 23 78.3 %
Date: 2018-01-09 16:37:03 Functions: 46 47 97.9 %

          Line data    Source code
       1             : 
       2             : /* Copyright (c) 2006-2012, Stefan Eilemann <eile@equalizergraphics.com>
       3             :  *
       4             :  * This file is part of Collage <https://github.com/Eyescale/Collage>
       5             :  *
       6             :  * This library is free software; you can redistribute it and/or modify it under
       7             :  * the terms of the GNU Lesser General Public License version 2.1 as published
       8             :  * by the Free Software Foundation.
       9             :  *
      10             :  * This library is distributed in the hope that it will be useful, but WITHOUT
      11             :  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
      12             :  * FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
      13             :  * details.
      14             :  *
      15             :  * You should have received a copy of the GNU Lesser General Public License
      16             :  * along with this library; if not, write to the Free Software Foundation, Inc.,
      17             :  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
      18             :  */
      19             : 
      20             : #ifndef CO_COMMANDFUNC_H
      21             : #define CO_COMMANDFUNC_H
      22             : 
      23             : #include <co/types.h>
      24             : #include <lunchbox/debug.h>
      25             : 
      26             : // If you get a warning in your code, add this before in including this file:
      27             : //  #pragma warning( disable: 4407 )
      28             : // This warning is 'fixed' by https://github.com/Eyescale/Equalizer/issues/100
      29             : 
      30             : namespace co
      31             : {
      32             : /**
      33             :  * A wrapper to register a function callback on an object instance.
      34             :  *
      35             :  * This wrapper is used by the Dispatcher to register and save callback
      36             :  * methods of derived classes.
      37             :  */
      38             : template <typename T>
      39             : class CommandFunc
      40             : {
      41             : public:
      42             :     /**
      43             :      * Create a new callback to the method on the given object.
      44             :      * @version 1.0
      45             :      */
      46     1247188 :     CommandFunc(T* object, bool (T::*func)(ICommand&))
      47             :         : _object(object)
      48     1247188 :         , _func(func)
      49             :     {
      50     1247188 :     }
      51             : 
      52             :     /**
      53             :      * Create a copy of a callback to a different base class.
      54             :      * @version 1.0
      55             :      */
      56             :     template <typename O>
      57        2454 :     CommandFunc(const CommandFunc<O>& from)
      58        2454 :         : _object(_convertThis<O>(from._object))
      59        2454 :         , _func(static_cast<bool (T::*)(ICommand&)>(from._func))
      60             :     {
      61        2454 :     }
      62             : 
      63             :     /** @internal Invoke the callback. */
      64      642023 :     bool operator()(ICommand& command)
      65             :     {
      66      642023 :         LBASSERT(_object);
      67      642021 :         LBASSERT(_func);
      68      642018 :         return (_object->*_func)(command);
      69             :     }
      70             : 
      71             :     /** @internal */
      72             :     //@{
      73             :     /** @internal reset the callback. */
      74      621458 :     void clear()
      75             :     {
      76      621458 :         _object = 0;
      77      621458 :         _func = 0;
      78      621458 :     }
      79             :     /** @internal @return true if the callback is valid. */
      80      621464 :     bool isValid() const { return _object && _func; }
      81             :     // Can't make private because copy constructor needs access. Can't
      82             :     // declare friend because C++ does not allow generic template friends in
      83             :     // template classes.
      84             :     // private:
      85             :     T* _object;                  //!< @internal
      86             :     bool (T::*_func)(ICommand&); //!< @internal
      87             :     //@}
      88             : 
      89             : private:
      90             :     template <class F>
      91        2454 :     T* _convertThis(F* ptr)
      92             :     {
      93             : #ifdef _MSC_VER
      94             :         // https://github.com/Eyescale/Equalizer/issues/100
      95             :         return reinterpret_cast<T*>(ptr);
      96             : #else
      97        2454 :         return ptr;
      98             : #endif
      99             :     }
     100             : };
     101             : 
     102             : /** Output the given CommandFunc. */
     103             : template <typename T>
     104           0 : inline std::ostream& operator<<(std::ostream& os, const CommandFunc<T>& func)
     105             : {
     106           0 :     if (func.isValid())
     107           0 :         os << "CommandFunc of " << lunchbox::className(func._object);
     108             :     else
     109           0 :         os << "NULL CommandFunc";
     110           0 :     return os;
     111             : }
     112             : }
     113             : 
     114             : #endif // CO_COMMANDFUNC_H

Generated by: LCOV version 1.11