Advertisement
Guest User

Untitled

a guest
Feb 19th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.19 KB | None | 0 0
  1. template<std::size_t Bytes>
  2. class basic_decorator_type
  3. {
  4. struct base
  5. {
  6. virtual ~base() = default;
  7.  
  8. virtual
  9. void
  10. invoke(request_type&) = 0;
  11.  
  12. virtual
  13. void
  14. invoke(response_type&) = 0;
  15. };
  16.  
  17. template<class F>
  18. struct impl : base
  19. {
  20. F f_;
  21.  
  22. template<class F_>
  23. explicit
  24. impl(F_&& f)
  25. : f_(std::forward<F_>(f))
  26. {
  27. }
  28.  
  29. void
  30. invoke(request_type& req) override
  31. {
  32. this->invoke(req,
  33. beast::detail::is_invocable<
  34. F, void(request_type&)>{});
  35. }
  36.  
  37. void
  38. invoke(request_type& req, std::true_type)
  39. {
  40. f_(req);
  41. }
  42.  
  43. void
  44. invoke(request_type&, std::false_type)
  45. {
  46. }
  47.  
  48. void
  49. invoke(response_type& res) override
  50. {
  51. this->invoke(res,
  52. beast::detail::is_invocable<
  53. F, void(response_type&)>{});
  54. }
  55.  
  56. void
  57. invoke(response_type& res, std::true_type)
  58. {
  59. f_(res);
  60. }
  61.  
  62. void
  63. invoke(response_type&, std::false_type)
  64. {
  65. }
  66. };
  67.  
  68. using type = typename
  69. std::aligned_storage<Bytes>::type;
  70.  
  71. type buf_;
  72. base* base_;
  73.  
  74. bool
  75. is_inline() const noexcept
  76. {
  77. return
  78. reinterpret_cast<char const*>(base_) >=
  79. reinterpret_cast<char const*>(&buf_) &&
  80. reinterpret_cast<char const*>(base_) <
  81. reinterpret_cast<char const*>(&buf_ + 1);
  82. }
  83.  
  84. public:
  85. template<class F>
  86. basic_decorator_type(F&& f)
  87. : base_(sizeof(F) <= sizeof(buf_) ?
  88. ::new(&buf_) impl<
  89. typename std::decay<F>::type>(
  90. std::forward<F>(f)) :
  91. new impl<
  92. typename std::decay<F>::type>(
  93. std::forward<F>(f)))
  94. {
  95. }
  96.  
  97. ~basic_decorator_type()
  98. {
  99. if(is_inline())
  100. base_->~base();
  101. else
  102. delete base_;
  103. }
  104.  
  105. void
  106. operator()(request_type& req)
  107. {
  108. (*base_)(req);
  109. }
  110.  
  111. void
  112. operator()(response_type& res)
  113. {
  114. (*base_)(res);
  115. }
  116. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement