~ubuntu-branches/ubuntu/trusty/miro/trusty

« back to all changes in this revision

Viewing changes to portable/libtorrent/include/libtorrent/asio/detail/buffer_resize_guard.hpp

  • Committer: Daniel Hahler
  • Date: 2010-04-13 18:51:35 UTC
  • mfrom: (1.2.10 upstream)
  • Revision ID: ubuntu-launchpad@thequod.de-20100413185135-xi24v1diqg8w406x
Merging shared upstream rev into target branch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
//
2
 
// buffer_resize_guard.hpp
3
 
// ~~~~~~~~~~~~~~~~~~~~~~~
4
 
//
5
 
// Copyright (c) 2003-2008 Christopher M. Kohlhoff (chris at kohlhoff dot com)
6
 
//
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)
9
 
//
10
 
 
11
 
#ifndef ASIO_DETAIL_BUFFER_RESIZE_GUARD_HPP
12
 
#define ASIO_DETAIL_BUFFER_RESIZE_GUARD_HPP
13
 
 
14
 
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
15
 
# pragma once
16
 
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
17
 
 
18
 
#include "asio/detail/push_options.hpp"
19
 
 
20
 
#include "asio/detail/push_options.hpp"
21
 
#include <limits>
22
 
#include <boost/config.hpp>
23
 
#include "asio/detail/pop_options.hpp"
24
 
 
25
 
namespace asio {
26
 
namespace detail {
27
 
 
28
 
// Helper class to manage buffer resizing in an exception safe way.
29
 
template <typename Buffer>
30
 
class buffer_resize_guard
31
 
{
32
 
public:
33
 
  // Constructor.
34
 
  buffer_resize_guard(Buffer& buffer)
35
 
    : buffer_(buffer),
36
 
      old_size_(buffer.size())
37
 
  {
38
 
  }
39
 
 
40
 
  // Destructor rolls back the buffer resize unless commit was called.
41
 
  ~buffer_resize_guard()
42
 
  {
43
 
    if (old_size_
44
 
        != std::numeric_limits<size_t>::max BOOST_PREVENT_MACRO_SUBSTITUTION())
45
 
    {
46
 
      buffer_.resize(old_size_);
47
 
    }
48
 
  }
49
 
 
50
 
  // Commit the resize transaction.
51
 
  void commit()
52
 
  {
53
 
    old_size_
54
 
      = std::numeric_limits<size_t>::max BOOST_PREVENT_MACRO_SUBSTITUTION();
55
 
  }
56
 
 
57
 
private:
58
 
  // The buffer being managed.
59
 
  Buffer& buffer_;
60
 
 
61
 
  // The size of the buffer at the time the guard was constructed.
62
 
  size_t old_size_;
63
 
};
64
 
 
65
 
} // namespace detail
66
 
} // namespace asio
67
 
 
68
 
#include "asio/detail/pop_options.hpp"
69
 
 
70
 
#endif // ASIO_DETAIL_BUFFER_RESIZE_GUARD_HPP