• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com)
3 //
4 // Distributed under the Boost Software License, Version 1.0. (See accompanying
5 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 //
7 // Official repository: https://github.com/boostorg/beast
8 //
9 
10 #ifndef BOOST_BEAST_CORE_DETAIL_BUFFER_HPP
11 #define BOOST_BEAST_CORE_DETAIL_BUFFER_HPP
12 
13 #include <boost/beast/core/error.hpp>
14 #include <boost/asio/buffer.hpp>
15 #include <boost/optional.hpp>
16 #include <stdexcept>
17 
18 namespace boost {
19 namespace beast {
20 namespace detail {
21 
22 template<
23     class DynamicBuffer,
24     class ErrorValue>
25 auto
dynamic_buffer_prepare_noexcept(DynamicBuffer & buffer,std::size_t size,error_code & ec,ErrorValue ev)26 dynamic_buffer_prepare_noexcept(
27     DynamicBuffer& buffer,
28     std::size_t size,
29     error_code& ec,
30     ErrorValue ev) ->
31         boost::optional<typename
32         DynamicBuffer::mutable_buffers_type>
33 {
34     if(buffer.max_size() - buffer.size() < size)
35     {
36         // length error
37         ec = ev;
38         return boost::none;
39     }
40     boost::optional<typename
41         DynamicBuffer::mutable_buffers_type> result;
42     result.emplace(buffer.prepare(size));
43     ec = {};
44     return result;
45 }
46 
47 template<
48     class DynamicBuffer,
49     class ErrorValue>
50 auto
dynamic_buffer_prepare(DynamicBuffer & buffer,std::size_t size,error_code & ec,ErrorValue ev)51 dynamic_buffer_prepare(
52     DynamicBuffer& buffer,
53     std::size_t size,
54     error_code& ec,
55     ErrorValue ev) ->
56         boost::optional<typename
57         DynamicBuffer::mutable_buffers_type>
58 {
59 #ifndef BOOST_NO_EXCEPTIONS
60     try
61     {
62         boost::optional<typename
63             DynamicBuffer::mutable_buffers_type> result;
64         result.emplace(buffer.prepare(size));
65         ec = {};
66         return result;
67     }
68     catch(std::length_error const&)
69     {
70         ec = ev;
71     }
72     return boost::none;
73 
74 #else
75     return dynamic_buffer_prepare_noexcept(
76         buffer, size, ec, ev);
77 #endif
78 }
79 
80 } // detail
81 } // beast
82 } // boost
83 
84 #endif
85