blob: ac9db1902b9330093f7c1e0569b8da5468c2e133 [file] [log] [blame]
Ed Tanouse3cb5a32018-08-08 14:16:49 -07001#pragma once
2
Iwona Klimaszewskac0a1c8a2019-07-12 18:26:38 +02003#include <functional>
4
Ed Tanouse3cb5a32018-08-08 14:16:49 -07005namespace bmcweb
6{
7
8/**
9 * AsyncResp
10 * Gathers data needed for response processing after async calls are done
11 */
Iwona Klimaszewskac0a1c8a2019-07-12 18:26:38 +020012
Ed Tanouse3cb5a32018-08-08 14:16:49 -070013class AsyncResp
14{
15 public:
16 AsyncResp(crow::Response& response) : res(response)
Gunnar Mills1214b7e2020-06-04 10:11:30 -050017 {}
Ed Tanouse3cb5a32018-08-08 14:16:49 -070018
Iwona Klimaszewskac0a1c8a2019-07-12 18:26:38 +020019 AsyncResp(crow::Response& response, std::function<void()>&& function) :
20 res(response), func(std::move(function))
Gunnar Mills1214b7e2020-06-04 10:11:30 -050021 {}
Iwona Klimaszewskac0a1c8a2019-07-12 18:26:38 +020022
Krzysztof Grobelnydab06042021-04-02 13:28:14 +000023 AsyncResp(const AsyncResp&) = delete;
24 AsyncResp(AsyncResp&&) = delete;
25
Ed Tanouse3cb5a32018-08-08 14:16:49 -070026 ~AsyncResp()
27 {
Iwona Klimaszewskac0a1c8a2019-07-12 18:26:38 +020028 if (func && res.result() == boost::beast::http::status::ok)
29 {
30 func();
31 }
32
Ed Tanouse3cb5a32018-08-08 14:16:49 -070033 res.end();
34 }
35
36 crow::Response& res;
Ed Tanous23a21a12020-07-25 04:45:05 +000037 std::function<void()> func;
Ed Tanouse3cb5a32018-08-08 14:16:49 -070038};
39
Iwona Klimaszewskac0a1c8a2019-07-12 18:26:38 +020040} // namespace bmcweb