LCOV - code coverage report
Current view: top level - libs/bsw/cpp2someip/include/someip - Closure.h (source / functions) Coverage Total Hit
Test: coverage.info Lines: 100.0 % 37 37
Test Date: 2026-09-11 12:05:06 Functions: 80.0 % 30 24

            Line data    Source code
       1              : /********************************************************************************
       2              :  * Copyright (c) 2026 Accenture
       3              :  *
       4              :  * This program and the accompanying materials are made available under the
       5              :  * terms of the Apache License Version 2.0 which is available at
       6              :  * https://www.apache.org/licenses/LICENSE-2.0
       7              :  *
       8              :  * SPDX-License-Identifier: Apache-2.0
       9              :  ********************************************************************************/
      10              : 
      11              : #pragma once
      12              : 
      13              : #include "someip/util/Delegate.h"
      14              : 
      15              : namespace someip
      16              : {
      17              : /**
      18              :  * Base class for all Callback implementations
      19              :  */
      20              : template<typename ParamType, typename ResultType = void>
      21              : class Closure
      22              : {
      23              : public:
      24           17 :     virtual ~Closure() = default;
      25              : 
      26              :     virtual ResultType operator()(ParamType param) = 0;
      27              : };
      28              : 
      29              : template<typename ParamType, typename ResultType = void>
      30              : class Callback : public Closure<ParamType, ResultType>
      31              : {
      32              : public:
      33              :     using closure_type  = Closure<ParamType, ResultType>;
      34              :     using self_type     = Callback<ParamType, ResultType>;
      35              :     using function_type = typename ::common::MFunctionPtrT<ResultType, ParamType>::Type;
      36              : 
      37            2 :     explicit Callback(void* const callee = nullptr, function_type const mfp = 0L)
      38            2 :     : Closure<ParamType, ResultType>(), _pCallee(callee), _pCallbackFunction(mfp)
      39            2 :     {}
      40              : 
      41            2 :     ~Callback() override = default;
      42              : 
      43              :     template<class T, ResultType (T::*MethodPointer)(ParamType)>
      44            1 :     static Callback fromObject(T& callee)
      45              :     {
      46            1 :         Callback const c(&callee, &methodCaller<T, MethodPointer>);
      47            1 :         return c;
      48              :     }
      49              : 
      50            2 :     ResultType operator()(ParamType param) override
      51              :     {
      52            2 :         return (*_pCallbackFunction)(_pCallee, param);
      53              :     }
      54              : 
      55              :     template<ResultType (*FunctionPointer)(ParamType)>
      56            1 :     static Callback fromFunction()
      57              :     {
      58            1 :         Callback const c(nullptr, &function_stub<FunctionPointer>);
      59            1 :         return c;
      60              :     }
      61              : 
      62              : private:
      63              :     template<class T, ResultType (T::*MethodPointer)(ParamType)>
      64            1 :     static ResultType methodCaller(void* const pCallee, ParamType const p1)
      65              :     {
      66            1 :         T* const p = static_cast<T*>(pCallee);
      67            1 :         return (p->*MethodPointer)(p1);
      68              :     }
      69              : 
      70              :     template<ResultType (*FunctionPointer)(ParamType)>
      71            1 :     static ResultType function_stub(void* const, ParamType const p1)
      72              :     {
      73              :         static_assert((FunctionPointer != nullptr), "FunctionPointer must not be a null.");
      74            1 :         return (FunctionPointer)(p1);
      75              :     }
      76              : 
      77              :     void* _pCallee;
      78              :     function_type _pCallbackFunction;
      79              : };
      80              : 
      81              : template<typename BoundParamType, typename ParamType, typename ResultType = void>
      82              : class BoundCallback : public Closure<ParamType, ResultType>
      83              : {
      84              : public:
      85              :     using closure_type = Closure<ParamType, ResultType>;
      86              :     using self_type    = BoundCallback<BoundParamType, ParamType, ResultType>;
      87              :     using function_type =
      88              :         typename ::common::MFunctionPtrT<ResultType, BoundParamType&, ParamType>::Type;
      89              : 
      90           16 :     explicit BoundCallback(
      91              :         void* const callee      = nullptr,
      92              :         function_type const mfp = nullptr,
      93              :         BoundParamType const p1 = BoundParamType())
      94           16 :     : Closure<ParamType, ResultType>(), _pCallee(callee), _pCallbackFunction(mfp), _param1(p1)
      95           16 :     {}
      96              : 
      97            7 :     ~BoundCallback() override = default;
      98              : 
      99              :     template<class T, ResultType (T::*MethodPointer)(BoundParamType&, ParamType)>
     100            4 :     static BoundCallback fromObject(T& callee, BoundParamType const p1)
     101              :     {
     102            4 :         BoundCallback const c(&callee, &methodCaller<T, MethodPointer>, p1);
     103            4 :         return c;
     104              :     }
     105              : 
     106            4 :     ResultType operator()(ParamType param) override
     107              :     {
     108            4 :         return (*_pCallbackFunction)(_pCallee, _param1, param);
     109              :     }
     110              : 
     111              :     template<ResultType (*FunctionPointer)(BoundParamType&, ParamType)>
     112            3 :     static BoundCallback fromFunction(BoundParamType const p1)
     113              :     {
     114            3 :         BoundCallback const c(nullptr, &function_stub<FunctionPointer>, p1);
     115            3 :         return c;
     116              :     }
     117              : 
     118            1 :     BoundParamType const& getParam1() const { return _param1; }
     119              : 
     120            2 :     BoundParamType& getParam1() { return _param1; }
     121              : 
     122              : private:
     123              :     template<class T, ResultType (T::*MethodPointer)(BoundParamType&, ParamType)>
     124            3 :     static ResultType methodCaller(void* const pCallee, BoundParamType& p1, ParamType const p2)
     125              :     {
     126            3 :         T* const p = static_cast<T*>(pCallee);
     127            3 :         return (p->*MethodPointer)(p1, p2);
     128              :     }
     129              : 
     130              :     template<ResultType (*FunctionPointer)(BoundParamType&, ParamType)>
     131            1 :     static ResultType function_stub(void* const, BoundParamType& p1, ParamType const p2)
     132              :     {
     133              :         static_assert((FunctionPointer != nullptr), "FunctionPointer must not be a null.");
     134            1 :         return (FunctionPointer)(p1, p2);
     135              :     }
     136              : 
     137              :     void* _pCallee;
     138              :     function_type _pCallbackFunction;
     139              :     BoundParamType _param1;
     140              : };
     141              : 
     142              : } // namespace someip
        

Generated by: LCOV version 2.0-1