Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2024-11-15 09:02:44

0001 //
0002 // detail/buffer_resize_guard.hpp
0003 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
0004 //
0005 // Copyright (c) 2003-2023 Christopher M. Kohlhoff (chris at kohlhoff dot com)
0006 //
0007 // Distributed under the Boost Software License, Version 1.0. (See accompanying
0008 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0009 //
0010 
0011 #ifndef BOOST_ASIO_DETAIL_BUFFER_RESIZE_GUARD_HPP
0012 #define BOOST_ASIO_DETAIL_BUFFER_RESIZE_GUARD_HPP
0013 
0014 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
0015 # pragma once
0016 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
0017 
0018 #include <boost/asio/detail/config.hpp>
0019 #include <boost/asio/detail/limits.hpp>
0020 
0021 #include <boost/asio/detail/push_options.hpp>
0022 
0023 namespace boost {
0024 namespace asio {
0025 namespace detail {
0026 
0027 // Helper class to manage buffer resizing in an exception safe way.
0028 template <typename Buffer>
0029 class buffer_resize_guard
0030 {
0031 public:
0032   // Constructor.
0033   buffer_resize_guard(Buffer& buffer)
0034     : buffer_(buffer),
0035       old_size_(buffer.size())
0036   {
0037   }
0038 
0039   // Destructor rolls back the buffer resize unless commit was called.
0040   ~buffer_resize_guard()
0041   {
0042     if (old_size_ != (std::numeric_limits<size_t>::max)())
0043     {
0044       buffer_.resize(old_size_);
0045     }
0046   }
0047 
0048   // Commit the resize transaction.
0049   void commit()
0050   {
0051     old_size_ = (std::numeric_limits<size_t>::max)();
0052   }
0053 
0054 private:
0055   // The buffer being managed.
0056   Buffer& buffer_;
0057 
0058   // The size of the buffer at the time the guard was constructed.
0059   size_t old_size_;
0060 };
0061 
0062 } // namespace detail
0063 } // namespace asio
0064 } // namespace boost
0065 
0066 #include <boost/asio/detail/pop_options.hpp>
0067 
0068 #endif // BOOST_ASIO_DETAIL_BUFFER_RESIZE_GUARD_HPP