The http client currently uses an empty std::shared_ptr<int> as a scope-guard to run cleanup code:
|
std::shared_ptr<int> guard(nullptr, [this](auto) { |
|
if (!req_headers_.empty()) { |
|
req_headers_.clear(); |
|
} |
|
}); |
If I'm not mistaken, this will still have to allocate a shared state to store the user defined destructor in, which is quite wasteful. I'd suggest to introduce a simple scope guard for this purpose instead of abusing std::shared_ptr for this:
template <typename T>
struct scope_guard
{
T callback;
public:
explicit scope_guard(T callback) : callback(std::move(callback)) {}
public:
scope_guard(const scope_guard&) = delete;
scope_guard(scope_guard&&) noexcept = delete;
public:
~scope_guard() { callback(); }
};
You could also leave out the deleted ctors and make it an aggregate for cleaner construction :)
The http client currently uses an empty
std::shared_ptr<int>as a scope-guard to run cleanup code:yalantinglibs/include/ylt/standalone/cinatra/coro_http_client.hpp
Lines 1464 to 1468 in eec1eda
If I'm not mistaken, this will still have to allocate a shared state to store the user defined destructor in, which is quite wasteful. I'd suggest to introduce a simple scope guard for this purpose instead of abusing
std::shared_ptrfor this:You could also leave out the deleted ctors and make it an aggregate for cleaner construction :)