Created
October 21, 2020 20:08
-
-
Save madmongo1/d5428c9630e08605fe418b6765ab5bb4 to your computer and use it in GitHub Desktop.
guarded_executor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct guarded_executor | |
{ | |
using guard_type = std::weak_ptr< void >; | |
explicit guarded_executor(guard_type g) noexcept | |
: guard_(std::move(g)) | |
{ | |
} | |
constexpr auto operator==(guarded_executor const &) const noexcept | |
{ | |
return true; | |
} | |
constexpr auto operator!=(guarded_executor const &) const noexcept | |
{ | |
return false; | |
} | |
template < class F > | |
void execute(F &&f) const | |
{ | |
application::get_execution_context().post( | |
new basic_completion_event( | |
[guard = guard_, f = std::forward< F >(f)]() mutable { | |
if (auto lock = guard.lock()) | |
{ | |
f(); | |
lock.reset(); | |
} | |
})); | |
} | |
auto query(net::execution::context_t) | |
-> application::execution_context & | |
{ | |
return application::get_execution_context(); | |
} | |
auto query(net::execution::context_as_t< net::execution_context & >) | |
-> net::execution_context & | |
{ | |
return application::get_execution_context(); | |
} | |
auto require(net::execution::blocking_t::possibly_t) const | |
{ | |
return *this; | |
} | |
auto require(net::execution::blocking_t::never_t) const | |
{ | |
return *this; | |
} | |
auto require(net::execution::relationship_t::fork_t) const | |
{ | |
return *this; | |
} | |
auto require(net::execution::relationship_t::continuation_t) const | |
{ | |
return *this; | |
} | |
auto require(net::execution::outstanding_work_t::tracked_t) const | |
{ | |
return *this; | |
} | |
auto require(net::execution::outstanding_work_t::untracked_t) const | |
{ | |
return *this; | |
} | |
template < typename OtherAllocator > | |
auto require(net::execution::allocator_t< OtherAllocator > ) const | |
{ | |
return *this; | |
} | |
auto require(net::execution::allocator_t< void >) const | |
{ | |
return *this; | |
} | |
static constexpr net::execution::mapping_t | |
query(net::execution::mapping_t) noexcept | |
{ | |
return net::execution::mapping.thread; | |
} | |
constexpr net::execution::blocking_t | |
query(net::execution::blocking_t) const noexcept | |
{ | |
return net::execution::blocking_t(net::execution::blocking.never); | |
} | |
constexpr net::execution::relationship_t | |
query(net::execution::relationship_t) const noexcept | |
{ | |
return net::execution::relationship_t( | |
net::execution::relationship.fork); | |
} | |
static BOOST_ASIO_CONSTEXPR net::execution::outstanding_work_t | |
query(net::execution::outstanding_work_t) BOOST_ASIO_NOEXCEPT | |
{ | |
return net::execution::outstanding_work_t( | |
net::execution::outstanding_work.tracked); | |
} | |
template < typename OtherAllocator > | |
BOOST_ASIO_CONSTEXPR auto query( | |
net::execution::allocator_t< OtherAllocator >) const BOOST_ASIO_NOEXCEPT | |
{ | |
return std::allocator<void>(); | |
} | |
BOOST_ASIO_CONSTEXPR auto | |
query(net::execution::allocator_t< void >) const BOOST_ASIO_NOEXCEPT | |
{ | |
return std::allocator<void>(); | |
} | |
private: | |
guard_type guard_; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment