Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- template<std::size_t Bytes>
- class basic_decorator_type
- {
- struct base
- {
- virtual ~base() = default;
- virtual
- void
- invoke(request_type&) = 0;
- virtual
- void
- invoke(response_type&) = 0;
- };
- template<class F>
- struct impl : base
- {
- F f_;
- template<class F_>
- explicit
- impl(F_&& f)
- : f_(std::forward<F_>(f))
- {
- }
- void
- invoke(request_type& req) override
- {
- this->invoke(req,
- beast::detail::is_invocable<
- F, void(request_type&)>{});
- }
- void
- invoke(request_type& req, std::true_type)
- {
- f_(req);
- }
- void
- invoke(request_type&, std::false_type)
- {
- }
- void
- invoke(response_type& res) override
- {
- this->invoke(res,
- beast::detail::is_invocable<
- F, void(response_type&)>{});
- }
- void
- invoke(response_type& res, std::true_type)
- {
- f_(res);
- }
- void
- invoke(response_type&, std::false_type)
- {
- }
- };
- using type = typename
- std::aligned_storage<Bytes>::type;
- type buf_;
- base* base_;
- bool
- is_inline() const noexcept
- {
- return
- reinterpret_cast<char const*>(base_) >=
- reinterpret_cast<char const*>(&buf_) &&
- reinterpret_cast<char const*>(base_) <
- reinterpret_cast<char const*>(&buf_ + 1);
- }
- public:
- template<class F>
- basic_decorator_type(F&& f)
- : base_(sizeof(F) <= sizeof(buf_) ?
- ::new(&buf_) impl<
- typename std::decay<F>::type>(
- std::forward<F>(f)) :
- new impl<
- typename std::decay<F>::type>(
- std::forward<F>(f)))
- {
- }
- ~basic_decorator_type()
- {
- if(is_inline())
- base_->~base();
- else
- delete base_;
- }
- void
- operator()(request_type& req)
- {
- (*base_)(req);
- }
- void
- operator()(response_type& res)
- {
- (*base_)(res);
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement