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