~aleteoryx/muditaos

muditaos/module-utils/utility/Utility.hpp -rw-r--r-- 3.4 KiB
a405cad6Aleteoryx trim readme 6 days ago
                                                                                
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// Copyright (c) 2017-2024, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/blob/master/LICENSE.md

#pragma once

#include <functional>
#include <utility>
#include <type_traits>

namespace utility
{
    using Guard = std::function<bool()>;

    /// Invokes a function if specified conditions are met.
    /// \tparam F       Function signature
    /// \tparam Args    Function arguments
    /// \param guard    Conditions to be met
    /// \param func     Callable object
    /// \param args     Function arguments
    /// \return If the function returns void - a flag indicating whether the conditions were met and the function has
    /// been called. If the function returns a type - a pair of: a flag indicating whether the conditions were met, and
    /// a function's return value.
    template <typename F, typename... Args>
    auto conditionally_invoke(const Guard &guard,
                              const F &func,
                              Args... args) noexcept(std::is_nothrow_invocable_v<F, Args...>)
    {
        using ResultType              = typename std::invoke_result_t<F, Args...>;
        constexpr auto isVoidFunction = std::is_void_v<ResultType>;
        if (!guard()) {
            if constexpr (isVoidFunction) {
                return false;
            }
            else {
                return std::pair(false, ResultType{});
            }
        }

        if constexpr (isVoidFunction) {
            std::invoke(func, std::forward<Args>(args)...);
            return true;
        }
        else {
            return std::pair(true, std::invoke(func, std::forward<Args>(args)...));
        }
    }

    /// Invokes a member function if specified conditions are met.
    /// \tparam T       Class which contains the member function F
    /// \tparam F       Function signature
    /// \tparam Args    Function arguments
    /// \param guard    Conditions to be met
    /// \param self     Pointer to the class T instance
    /// \param func     Callable object
    /// \param args     Function arguments
    /// \return If the function returns void - a flag indicating whether the conditions were met and the function has
    /// been called. If the function returns a type - a pair of: a flag indicating whether the conditions were met, and
    /// a function's return value.
    template <class T,
              typename F,
              typename... Args,
              typename std::enable_if_t<std::is_invocable_v<F, T, Args...>, bool> = false>
    auto conditionally_invoke(const Guard &guard,
                              T &&self,
                              const F &func,
                              Args... args) noexcept(std::is_nothrow_invocable_v<F, T, Args...>)
    {
        using ResultType              = typename std::invoke_result_t<F, T, Args...>;
        constexpr auto isVoidFunction = std::is_void_v<ResultType>;
        if (!guard()) {
            if constexpr (isVoidFunction) {
                return false;
            }
            else {
                return std::pair(false, ResultType{});
            }
        }

        if constexpr (isVoidFunction) {
            std::invoke(func, std::forward<T>(self), std::forward<Args>(args)...);
            return true;
        }
        else {
            return std::pair(true, std::invoke(func, std::forward<T>(self), std::forward<Args>(args)...));
        }
    }
} // namespace utility