00001 // 00002 // buffer_resize_guard.hpp 00003 // ~~~~~~~~~~~~~~~~~~~~~~~ 00004 // 00005 // Copyright (c) 2003-2008 Christopher M. Kohlhoff (chris at kohlhoff dot com) 00006 // 00007 // Distributed under the Boost Software License, Version 1.0. (See accompanying 00008 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 00009 // 00010 00011 #ifndef ASIO_DETAIL_BUFFER_RESIZE_GUARD_HPP 00012 #define ASIO_DETAIL_BUFFER_RESIZE_GUARD_HPP 00013 00014 #if defined(_MSC_VER) && (_MSC_VER >= 1200) 00015 # pragma once 00016 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200) 00017 00018 #include "asio/detail/push_options.hpp" 00019 00020 #include "asio/detail/push_options.hpp" 00021 #include <limits> 00022 #include <boost/config.hpp> 00023 #include "asio/detail/pop_options.hpp" 00024 00025 namespace asio { 00026 namespace detail { 00027 00028 // Helper class to manage buffer resizing in an exception safe way. 00029 template <typename Buffer> 00030 class buffer_resize_guard 00031 { 00032 public: 00033 // Constructor. 00034 buffer_resize_guard(Buffer& buffer) 00035 : buffer_(buffer), 00036 old_size_(buffer.size()) 00037 { 00038 } 00039 00040 // Destructor rolls back the buffer resize unless commit was called. 00041 ~buffer_resize_guard() 00042 { 00043 if (old_size_ 00044 != std::numeric_limits<size_t>::max BOOST_PREVENT_MACRO_SUBSTITUTION()) 00045 { 00046 buffer_.resize(old_size_); 00047 } 00048 } 00049 00050 // Commit the resize transaction. 00051 void commit() 00052 { 00053 old_size_ 00054 = std::numeric_limits<size_t>::max BOOST_PREVENT_MACRO_SUBSTITUTION(); 00055 } 00056 00057 private: 00058 // The buffer being managed. 00059 Buffer& buffer_; 00060 00061 // The size of the buffer at the time the guard was constructed. 00062 size_t old_size_; 00063 }; 00064 00065 } // namespace detail 00066 } // namespace asio 00067 00068 #include "asio/detail/pop_options.hpp" 00069 00070 #endif // ASIO_DETAIL_BUFFER_RESIZE_GUARD_HPP