alibaba / alibaba/yalantinglibs
Introduce RAII-Scope Guard instead of using `std::shared_ptr`
- Dominant language
- C++
- Stars
- 2.2k
- Forks
- 327
- Avg merge
- 1d 20h
- Merged PRs (30d)
- 2
Description
The http client currently uses an empty `std::shared_ptr` as a scope-guard to run cleanup code: https://github.com/alibaba/yalantinglibs/blob/eec1edacde8a4a02ffd76b67fc658f6e9f4699a2/include/ylt/standalone/cinatra/coro_http_client.hpp#L1464-L1468
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:
```cpp
template
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 :)
Contributor guide
No contributing guide indexed for this repository
Research direction
Start in include/ylt/standalone/cinatra/coro_http_client.hpp at lines 1464-1468 and inspect how the current shared_ptr-based cleanup is used. Introduce the proposed scope guard, replace this usage while preserving cleanup behavior, and verify the HTTP client still builds and behaves correctly.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp
- Domain
- networking
- Issue type
- Refactor
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Quiet
- Clarity
- Clearly specified
- Newbie friendliness
- 55/100