Skip to content

Commit

Permalink
Merge pull request #27 from 5cript/fix/scope_exit
Browse files Browse the repository at this point in the history
Fixed ScopeExit class.
  • Loading branch information
5cript authored Dec 1, 2024
2 parents a8eda5b + a20be7a commit c47f730
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 12 deletions.
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
}

0 comments on commit c47f730

Please sign in to comment.