Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed ScopeExit class. #27

Merged
merged 1 commit into from
Dec 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs_source/sphinx/http/server.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ int main()

// stop the thread_pool on scope exit to guarantee that all asynchronous tasks are finished before the server is
// destroyed.
const auto shutdownPool = Roar::ScopeExit{[&pool]() {
const auto shutdownPool = Roar::ScopeExit{[&pool]() noexcept {
pool.stop();
pool.join();
}};
Expand Down Expand Up @@ -141,7 +141,7 @@ int main()

// stop the thread_pool on scope exit to guarantee that all asynchronous tasks are finished before the server is
// destroyed.
const auto shutdownPool = Roar::ScopeExit{[&pool]() {
const auto shutdownPool = Roar::ScopeExit{[&pool]() noexcept {
pool.stop();
pool.join();
}};
Expand Down
2 changes: 1 addition & 1 deletion examples/100_continue/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ int main()

// Create server.
Roar::Server server{{.executor = pool.executor()}};
const auto shutdownPool = Roar::ScopeExit{[&pool]() {
const auto shutdownPool = Roar::ScopeExit{[&pool]() noexcept {
pool.stop();
pool.join();
}};
Expand Down
2 changes: 1 addition & 1 deletion examples/most_minimal/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ int main()

// stop the thread_pool on scope exit to guarantee that all asynchronous tasks are finished before the server is
// destroyed.
const auto shutdownPool = Roar::ScopeExit{[&pool]() {
const auto shutdownPool = Roar::ScopeExit{[&pool]() noexcept {
pool.stop();
pool.join();
}};
Expand Down
2 changes: 1 addition & 1 deletion examples/serve_directory/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ int main()

// Create server.
Roar::Server server{{.executor = pool.executor()}};
const auto shutdownPool = Roar::ScopeExit{[&pool]() {
const auto shutdownPool = Roar::ScopeExit{[&pool]() noexcept {
pool.stop();
pool.join();
}};
Expand Down
2 changes: 1 addition & 1 deletion examples/simple_server/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ int main()
.executor = pool.executor(),
.onError = onAsynchronousError,
}};
const auto shutdownPool = Roar::ScopeExit{[&pool]() {
const auto shutdownPool = Roar::ScopeExit{[&pool]() noexcept {
pool.stop();
pool.join();
}};
Expand Down
39 changes: 33 additions & 6 deletions include/roar/utility/scope_exit.hpp
Original file line number Diff line number Diff line change
@@ -1,25 +1,52 @@
#pragma once

#include <boost/preprocessor/cat.hpp>

#include <functional>
#include <type_traits>
#include <utility>

#define ROAR_SE_UNIQUE_IDENTIFIER BOOST_PP_CAT(roar_se_unique_identifier_, __COUNTER__)

namespace Roar
{
template <typename EF>
class ScopeExit
{
public:
ScopeExit(EF&& func)
: onExit_(std::forward<EF>(func))
template <typename FunctionT>
requires std::is_nothrow_invocable_v<FunctionT>
explicit ScopeExit(FunctionT&& func)
: onExit_(std::forward<FunctionT>(func))
{}
~ScopeExit()
{
onExit_();
if (onExit_)
onExit_();
}
ScopeExit(ScopeExit&& other) = delete;
ScopeExit& operator=(ScopeExit&& other) = delete;
ScopeExit(const ScopeExit&) = delete;
ScopeExit& operator=(const ScopeExit&) = delete;
void disarm()
{
onExit_ = {};
}

private:
std::function<void()> onExit_;
};
template <typename T>
ScopeExit(T) -> ScopeExit<T>;

namespace Detail
{
struct MakeScopeExitImpl
{
template <typename FunctionT>
auto operator->*(FunctionT&& fn) const
{
return ScopeExit{std::forward<FunctionT>(fn)};
}
};
}

#define ROAR_ON_SCOPE_EXIT auto ROAR_SE_UNIQUE_IDENTIFIER = ::Roar::Detail::MakeScopeExitImpl{}->*[&]() noexcept
}
Loading