123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363 |
- #ifndef ASIO_EXECUTOR_HPP
- #define ASIO_EXECUTOR_HPP
- #if defined(_MSC_VER) && (_MSC_VER >= 1200)
- # pragma once
- #endif
- #include "asio/detail/config.hpp"
- #if !defined(ASIO_NO_TS_EXECUTORS)
- #include <new>
- #include <typeinfo>
- #include "asio/detail/cstddef.hpp"
- #include "asio/detail/executor_function.hpp"
- #include "asio/detail/memory.hpp"
- #include "asio/detail/throw_exception.hpp"
- #include "asio/execution_context.hpp"
- #include "asio/detail/push_options.hpp"
- namespace asio {
- class bad_executor
- : public std::exception
- {
- public:
-
- ASIO_DECL bad_executor() noexcept;
-
- ASIO_DECL virtual const char* what() const
- noexcept;
- };
- class executor
- {
- public:
-
- executor() noexcept
- : impl_(0)
- {
- }
-
- executor(nullptr_t) noexcept
- : impl_(0)
- {
- }
-
- executor(const executor& other) noexcept
- : impl_(other.clone())
- {
- }
-
- executor(executor&& other) noexcept
- : impl_(other.impl_)
- {
- other.impl_ = 0;
- }
-
- template <typename Executor>
- executor(Executor e);
-
-
- executor(std::nothrow_t, const executor& other) noexcept
- : impl_(other.clone())
- {
- }
-
-
- executor(std::nothrow_t, executor&& other) noexcept
- : impl_(other.impl_)
- {
- other.impl_ = 0;
- }
-
- template <typename Executor>
- executor(std::nothrow_t, Executor e) noexcept;
-
-
- template <typename Executor, typename Allocator>
- executor(allocator_arg_t, const Allocator& a, Executor e);
-
- ~executor()
- {
- destroy();
- }
-
- executor& operator=(const executor& other) noexcept
- {
- destroy();
- impl_ = other.clone();
- return *this;
- }
-
- executor& operator=(executor&& other) noexcept
- {
- destroy();
- impl_ = other.impl_;
- other.impl_ = 0;
- return *this;
- }
-
- executor& operator=(nullptr_t) noexcept
- {
- destroy();
- impl_ = 0;
- return *this;
- }
-
-
- template <typename Executor>
- executor& operator=(Executor&& e) noexcept
- {
- executor tmp(static_cast<Executor&&>(e));
- destroy();
- impl_ = tmp.impl_;
- tmp.impl_ = 0;
- return *this;
- }
-
- execution_context& context() const noexcept
- {
- return get_impl()->context();
- }
-
- void on_work_started() const noexcept
- {
- get_impl()->on_work_started();
- }
-
- void on_work_finished() const noexcept
- {
- get_impl()->on_work_finished();
- }
-
-
- template <typename Function, typename Allocator>
- void dispatch(Function&& f, const Allocator& a) const;
-
-
- template <typename Function, typename Allocator>
- void post(Function&& f, const Allocator& a) const;
-
-
- template <typename Function, typename Allocator>
- void defer(Function&& f, const Allocator& a) const;
- struct unspecified_bool_type_t {};
- typedef void (*unspecified_bool_type)(unspecified_bool_type_t);
- static void unspecified_bool_true(unspecified_bool_type_t) {}
-
- operator unspecified_bool_type() const noexcept
- {
- return impl_ ? &executor::unspecified_bool_true : 0;
- }
-
-
- #if !defined(ASIO_NO_TYPEID) || defined(GENERATING_DOCUMENTATION)
- const std::type_info& target_type() const noexcept
- {
- return impl_ ? impl_->target_type() : typeid(void);
- }
- #else
- const void* target_type() const noexcept
- {
- return impl_ ? impl_->target_type() : 0;
- }
- #endif
-
-
- template <typename Executor>
- Executor* target() noexcept;
-
-
- template <typename Executor>
- const Executor* target() const noexcept;
-
- friend bool operator==(const executor& a,
- const executor& b) noexcept
- {
- if (a.impl_ == b.impl_)
- return true;
- if (!a.impl_ || !b.impl_)
- return false;
- return a.impl_->equals(b.impl_);
- }
-
- friend bool operator!=(const executor& a,
- const executor& b) noexcept
- {
- return !(a == b);
- }
- private:
- #if !defined(GENERATING_DOCUMENTATION)
- typedef detail::executor_function function;
- template <typename, typename> class impl;
- #if !defined(ASIO_NO_TYPEID)
- typedef const std::type_info& type_id_result_type;
- #else
- typedef const void* type_id_result_type;
- #endif
- template <typename T>
- static type_id_result_type type_id()
- {
- #if !defined(ASIO_NO_TYPEID)
- return typeid(T);
- #else
- static int unique_id;
- return &unique_id;
- #endif
- }
-
- class impl_base
- {
- public:
- virtual impl_base* clone() const noexcept = 0;
- virtual void destroy() noexcept = 0;
- virtual execution_context& context() noexcept = 0;
- virtual void on_work_started() noexcept = 0;
- virtual void on_work_finished() noexcept = 0;
- virtual void dispatch(function&&) = 0;
- virtual void post(function&&) = 0;
- virtual void defer(function&&) = 0;
- virtual type_id_result_type target_type() const noexcept = 0;
- virtual void* target() noexcept = 0;
- virtual const void* target() const noexcept = 0;
- virtual bool equals(const impl_base* e) const noexcept = 0;
- protected:
- impl_base(bool fast_dispatch) : fast_dispatch_(fast_dispatch) {}
- virtual ~impl_base() {}
- private:
- friend class executor;
- const bool fast_dispatch_;
- };
-
- impl_base* get_impl() const
- {
- if (!impl_)
- {
- bad_executor ex;
- asio::detail::throw_exception(ex);
- }
- return impl_;
- }
-
- impl_base* clone() const noexcept
- {
- return impl_ ? impl_->clone() : 0;
- }
-
- void destroy() noexcept
- {
- if (impl_)
- impl_->destroy();
- }
- impl_base* impl_;
- #endif
- };
- }
- ASIO_USES_ALLOCATOR(asio::executor)
- #include "asio/detail/pop_options.hpp"
- #include "asio/impl/executor.hpp"
- #if defined(ASIO_HEADER_ONLY)
- # include "asio/impl/executor.ipp"
- #endif
- #endif
- #endif
|