LCOV - code coverage report
Current view: top level - AP_HAL/utility - functor.h (source / functions) Hit Total Coverage
Test: lcov.info Lines: 21 21 100.0 %
Date: 2026-09-09 11:02:08 Functions: 436 633 68.9 %

          Line data    Source code
       1             : /*
       2             :  * Copyright (C) 2015  Intel Corporation. All rights reserved.
       3             :  *
       4             :  * This file is free software: you can redistribute it and/or modify it
       5             :  * under the terms of the GNU General Public License as published by the
       6             :  * Free Software Foundation, either version 3 of the License, or
       7             :  * (at your option) any later version.
       8             :  *
       9             :  * This file is distributed in the hope that it will be useful, but
      10             :  * WITHOUT ANY WARRANTY; without even the implied warranty of
      11             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
      12             :  * See the GNU General Public License for more details.
      13             :  *
      14             :  * You should have received a copy of the GNU General Public License along
      15             :  * with this program.  If not, see <http://www.gnu.org/licenses/>.
      16             :  */
      17             : 
      18             : #pragma once
      19             : 
      20             : #include <type_traits>
      21             : 
      22             : #define FUNCTOR_TYPEDEF(name, rettype, ...) \
      23             :     typedef Functor<rettype, ## __VA_ARGS__> name
      24             : 
      25             : #define FUNCTOR_DECLARE(name, rettype, ...) \
      26             :     Functor<rettype, ## __VA_ARGS__> name
      27             : 
      28             : #define FUNCTOR_BIND(obj, func, rettype, ...) \
      29             :     Functor<rettype, ## __VA_ARGS__>::bind<std::remove_reference<decltype(*obj)>::type, func>(obj)
      30             : 
      31             : #define FUNCTOR_BIND_MEMBER(func, rettype, ...) \
      32             :     Functor<rettype, ## __VA_ARGS__>::bind<std::remove_reference<decltype(*this)>::type, func>(this)
      33             : 
      34             : template <class RetType, class... Args>
      35             : class Functor
      36             : {
      37             : public:
      38      373530 :     constexpr Functor(void *obj, RetType (*method)(void *obj, Args...))
      39      373530 :         : _obj(obj)
      40      373530 :         , _method(method)
      41             :     {
      42      373530 :     }
      43             : 
      44             :     // Allow to construct an empty Functor
      45      137969 :     constexpr Functor(decltype(nullptr))
      46      137969 :         : Functor(nullptr, nullptr) { }
      47             : 
      48      108621 :     constexpr Functor()
      49      108621 :         : Functor(nullptr, nullptr) { }
      50             : 
      51             :     // Call the method on the obj this Functor is bound to
      52  3775758806 :     RetType operator()(Args... args) const
      53             :     {
      54  3775758806 :         return _method(_obj, args...);
      55             :     }
      56             : 
      57             :     // Compare if the two Functors are calling the same method in the same
      58             :     // object
      59      123498 :     inline bool operator==(const Functor<RetType, Args...>& rhs)
      60             :     {
      61      123498 :         return _obj == rhs._obj && _method == rhs._method;
      62             :     }
      63       83779 :     inline bool operator!=(const Functor<RetType, Args...>& rhs)
      64             :     {
      65       83779 :         return _obj != rhs._obj || _method != rhs._method;
      66             :     }
      67             : 
      68             :     // Allow to check if there's a method set in the Functor
      69  2770971861 :     explicit operator bool() const
      70             :     {
      71  2770971861 :         return _method != nullptr;
      72             :     }
      73             : 
      74             :     template<class T, RetType (T::*method)(Args...)>
      75      126940 :     static constexpr Functor bind(T *obj)
      76             :     {
      77      126940 :         return { obj, method_wrapper<T, method> };
      78             :     }
      79             : 
      80             : private:
      81             :     void *_obj;
      82             :     RetType (*_method)(void *obj, Args...);
      83             : 
      84             :     template<class T, RetType (T::*method)(Args...)>
      85  3775758806 :     static RetType method_wrapper(void *obj, Args... args)
      86             :     {
      87  3775758806 :         T *t = static_cast<T*>(obj);
      88  3775758806 :         return (t->*method)(args...);
      89             :     }
      90             : };

Generated by: LCOV version 1.14