• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *    Copyright (c) 2021, The OpenThread Authors.
3  *    All rights reserved.
4  *
5  *    Redistribution and use in source and binary forms, with or without
6  *    modification, are permitted provided that the following conditions are met:
7  *    1. Redistributions of source code must retain the above copyright
8  *       notice, this list of conditions and the following disclaimer.
9  *    2. Redistributions in binary form must reproduce the above copyright
10  *       notice, this list of conditions and the following disclaimer in the
11  *       documentation and/or other materials provided with the distribution.
12  *    3. Neither the name of the copyright holder nor the
13  *       names of its contributors may be used to endorse or promote products
14  *       derived from this software without specific prior written permission.
15  *
16  *    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  *    AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  *    IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  *    ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20  *    LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  *    CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  *    SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  *    INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  *    CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  *    ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  *    POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #ifndef OTBR_COMMON_CALLBACK_HPP_
30 #define OTBR_COMMON_CALLBACK_HPP_
31 
32 #include "openthread-br/config.h"
33 
34 #include <functional>
35 #include <type_traits>
36 
37 namespace otbr {
38 
39 template <class T> class OnceCallback;
40 
41 /**
42  * A callback which can be invoked at most once.
43  *
44  * IsNull is guaranteed to return true once the callback has been invoked.
45  *
46  * Example usage:
47  *  OnceCallback<int(int)> square([](int x) { return x * x; });
48  *  std::move(square)(5); // Returns 25.
49  *  std::move(square)(6); // Crashes since `square` has already run.
50  *  square(7); // Compiling error.
51  *
52  * Inspired by Chromium base::OnceCallback
53  * (https://chromium.googlesource.com/chromium/src.git/+/refs/heads/main/base/callback.h).
54  *
55  */
56 template <typename R, typename... Args> class OnceCallback<R(Args...)>
57 {
58 public:
59     // Constructs a new `OnceCallback` instance with a callable.
60     //
61     // This constructor is for matching std::function<> and lambda and the
62     // `std::enable_if_t` check is only required for working around gcc 4.x
63     // compiling issue which trying to instantiate this template constructor
64     // for use cases like `::mOnceCallback(aOnceCallback)`.
65     template <typename T, typename = typename std::enable_if<!std::is_same<OnceCallback, T>::value>::type>
OnceCallback(T && aFunc)66     OnceCallback(T &&aFunc)
67         : mFunc(std::forward<T>(aFunc))
68     {
69     }
70 
OnceCallback(OnceCallback && aCallback)71     OnceCallback(OnceCallback &&aCallback)
72         : mFunc(std::move(aCallback.mFunc))
73     {
74         aCallback.mFunc = nullptr;
75     }
76 
operator =(OnceCallback && aCallback)77     OnceCallback &operator=(OnceCallback &&aCallback)
78     {
79         mFunc           = std::move(aCallback.mFunc);
80         aCallback.mFunc = nullptr;
81 
82         return *this;
83     }
84 
85     OnceCallback(const OnceCallback &)            = delete;
86     OnceCallback &operator=(const OnceCallback &) = delete;
87 
operator ()(Args...) const88     R operator()(Args...) const &
89     {
90         static_assert(!sizeof(*this), "OnceCallback::() can only be invoked on a non-const "
91                                       "rvalue, i.e. std::move(callback)().");
92     }
93 
operator ()(Args...aArgs)94     R operator()(Args... aArgs) &&
95     {
96         // Move `this` to a local variable to clear internal state
97         // before invoking the callback function.
98         OnceCallback cb = std::move(*this);
99 
100         return cb.mFunc(std::forward<Args>(aArgs)...);
101     }
102 
IsNull() const103     bool IsNull() const { return mFunc == nullptr; }
104 
105 private:
106     std::function<R(Args...)> mFunc;
107 };
108 
109 } // namespace otbr
110 
111 #endif // OTBR_COMMON_CALLBACK_HPP_
112