2 // detail/buffer_resize_guard.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2017 Christopher M. Kohlhoff (chris at kohlhoff dot com)
7 // Distributed under the Boost Software License, Version 1.0. (See accompanying
8 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
11 #ifndef BOOST_ASIO_DETAIL_BUFFER_RESIZE_GUARD_HPP
12 #define BOOST_ASIO_DETAIL_BUFFER_RESIZE_GUARD_HPP
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
18 #include <boost/asio/detail/config.hpp>
19 #include <boost/asio/detail/limits.hpp>
21 #include <boost/asio/detail/push_options.hpp>
27 // Helper class to manage buffer resizing in an exception safe way.
28 template <typename Buffer>
29 class buffer_resize_guard
33 buffer_resize_guard(Buffer& buffer)
35 old_size_(buffer.size())
39 // Destructor rolls back the buffer resize unless commit was called.
40 ~buffer_resize_guard()
42 if (old_size_ != (std::numeric_limits<size_t>::max)())
44 buffer_.resize(old_size_);
48 // Commit the resize transaction.
51 old_size_ = (std::numeric_limits<size_t>::max)();
55 // The buffer being managed.
58 // The size of the buffer at the time the guard was constructed.
66 #include <boost/asio/detail/pop_options.hpp>
68 #endif // BOOST_ASIO_DETAIL_BUFFER_RESIZE_GUARD_HPP