1 /* 2 * Copyright (c) 2024, 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 /** 30 * @file 31 * This file includes definitions for chained async task. 32 * The utility class is used to support the usage of a `Then`-style chained async operations. 33 */ 34 35 #ifndef OTBR_AGENT_ASYNC_TASK_HPP_ 36 #define OTBR_AGENT_ASYNC_TASK_HPP_ 37 38 #include <functional> 39 #include <memory> 40 41 #include <openthread/error.h> 42 43 namespace otbr { 44 namespace Ncp { 45 46 class AsyncTask; 47 using AsyncTaskPtr = std::shared_ptr<AsyncTask>; 48 49 class AsyncTask 50 { 51 public: 52 using ThenHandler = std::function<void(AsyncTaskPtr)>; 53 using ResultHandler = std::function<void(otError, const std::string &)>; 54 55 /** 56 * Constructor. 57 * 58 * @param[in] The error handler called when the result is not OT_ERROR_NONE; 59 */ 60 AsyncTask(const ResultHandler &aResultHandler); 61 62 /** 63 * Destructor. 64 */ 65 ~AsyncTask(void); 66 67 /** 68 * Trigger the initial action of the chained async operations. 69 * 70 * This method should be called to trigger the chained async operations. 71 */ 72 void Run(void); 73 74 /** 75 * Set the result of the previous async operation. 76 * 77 * This method should be called when the result of the previous async operation is ready. 78 * This method will pass the result to next operation. 79 * 80 * @param[in] aError The result for the previous async operation. 81 */ 82 void SetResult(otError aError, const std::string &aErrorInfo); 83 84 /** 85 * Set the initial operation of the chained async operations. 86 * 87 * @param[in] aFirst A reference to a function object for the initial action. 88 * 89 * @returns A shared pointer to a AsyncTask object created in this method. 90 */ 91 AsyncTaskPtr &First(const ThenHandler &aFirst); 92 93 /** 94 * Set the next operation of the chained async operations. 95 * 96 * @param[in] aThen A reference to a function object for the next action. 97 * 98 * @returns A shared pointer to a AsyncTask object created in this method. 99 */ 100 AsyncTaskPtr &Then(const ThenHandler &aThen); 101 102 private: 103 union 104 { 105 ThenHandler mThen; // Only valid when `mNext` is not nullptr 106 ResultHandler mResultHandler; // Only valid when `mNext` is nullptr 107 }; 108 AsyncTaskPtr mNext; 109 }; 110 111 } // namespace Ncp 112 } // namespace otbr 113 114 #endif // OTBR_AGENT_ASYNC_TASK_HPP_ 115