Use the exchange() idiom in move constructors

fix #1203
This commit is contained in:
Glen Fernandes
2018-07-22 08:49:03 -04:00
committed by Vinnie Falco
parent c4d1bfe7af
commit 8ed039200d
8 changed files with 28 additions and 34 deletions

View File

@ -1,3 +1,9 @@
Version 179:
* Use the exchange() idiom in move constructors
--------------------------------------------------------------------------------
Version 178:
* Use static_cast instead

View File

@ -24,6 +24,7 @@
# endif
#endif
#include <boost/core/exchange.hpp>
#include <limits>
#include <fcntl.h>
#include <sys/types.h>
@ -65,9 +66,8 @@ file_posix::
inline
file_posix::
file_posix(file_posix&& other)
: fd_(other.fd_)
: fd_(boost::exchange(other.fd_, -1))
{
other.fd_ = -1;
}
inline

View File

@ -10,6 +10,7 @@
#ifndef BOOST_BEAST_CORE_IMPL_FILE_STDIO_IPP
#define BOOST_BEAST_CORE_IMPL_FILE_STDIO_IPP
#include <boost/core/exchange.hpp>
#include <limits>
namespace boost {
@ -26,9 +27,8 @@ file_stdio::
inline
file_stdio::
file_stdio(file_stdio&& other)
: f_(other.f_)
: f_(boost::exchange(other.f_, nullptr))
{
other.f_ = nullptr;
}
inline

View File

@ -10,6 +10,7 @@
#ifndef BOOST_BEAST_CORE_IMPL_FILE_WIN32_IPP
#define BOOST_BEAST_CORE_IMPL_FILE_WIN32_IPP
#include <boost/core/exchange.hpp>
#include <boost/winapi/access_rights.hpp>
#include <boost/winapi/error_codes.hpp>
#include <boost/winapi/file_management.hpp>
@ -61,9 +62,8 @@ file_win32::
inline
file_win32::
file_win32(file_win32&& other)
: h_(other.h_)
: h_(boost::exchange(other.h_, boost::winapi::INVALID_HANDLE_VALUE_))
{
other.h_ = boost::winapi::INVALID_HANDLE_VALUE_;
}
inline

View File

@ -10,6 +10,7 @@
#ifndef BOOST_BEAST_IMPL_FLAT_BUFFER_HPP
#define BOOST_BEAST_IMPL_FLAT_BUFFER_HPP
#include <boost/core/exchange.hpp>
#include <boost/assert.hpp>
#include <boost/throw_exception.hpp>
#include <stdexcept>
@ -86,18 +87,14 @@ basic_flat_buffer<Allocator>::
basic_flat_buffer(basic_flat_buffer&& other)
: detail::empty_base_optimization<base_alloc_type>(
std::move(other.member()))
, begin_(other.begin_)
, in_(other.in_)
, out_(other.out_)
, begin_(boost::exchange(other.begin_, nullptr))
, in_(boost::exchange(other.in_, nullptr))
, out_(boost::exchange(other.out_, nullptr))
, last_(out_)
, end_(other.end_)
, end_(boost::exchange(other.end_, nullptr))
, max_(other.max_)
{
other.begin_ = nullptr;
other.in_ = nullptr;
other.out_ = nullptr;
other.last_ = nullptr;
other.end_ = nullptr;
}
template<class Allocator>

View File

@ -11,6 +11,7 @@
#define BOOST_BEAST_IMPL_MULTI_BUFFER_IPP
#include <boost/beast/core/detail/type_traits.hpp>
#include <boost/core/exchange.hpp>
#include <boost/assert.hpp>
#include <boost/throw_exception.hpp>
#include <algorithm>
@ -462,20 +463,16 @@ basic_multi_buffer(basic_multi_buffer&& other)
: detail::empty_base_optimization<
base_alloc_type>(std::move(other.member()))
, max_(other.max_)
, in_size_(other.in_size_)
, in_pos_(other.in_pos_)
, out_pos_(other.out_pos_)
, out_end_(other.out_end_)
, in_size_(boost::exchange(other.in_size_, 0))
, in_pos_(boost::exchange(other.in_pos_, 0))
, out_pos_(boost::exchange(other.out_pos_, 0))
, out_end_(boost::exchange(other.out_end_, 0))
{
auto const at_end =
other.out_ == other.list_.end();
list_ = std::move(other.list_);
out_ = at_end ? list_.end() : other.out_;
other.in_size_ = 0;
other.out_ = other.list_.end();
other.in_pos_ = 0;
other.out_pos_ = 0;
other.out_end_ = 0;
}
template<class Allocator>

View File

@ -18,6 +18,7 @@
#include <boost/beast/http/rfc7230.hpp>
#include <boost/beast/http/status.hpp>
#include <boost/beast/http/chunk_encode.hpp>
#include <boost/core/exchange.hpp>
#include <boost/throw_exception.hpp>
#include <stdexcept>
#include <string>
@ -363,11 +364,9 @@ basic_fields(basic_fields&& other) noexcept
std::move(other.member()))
, set_(std::move(other.set_))
, list_(std::move(other.list_))
, method_(other.method_)
, target_or_reason_(other.target_or_reason_)
, method_(boost::exchange(other.method_, {}))
, target_or_reason_(boost::exchange(other.target_or_reason_, {}))
{
other.method_ = {};
other.target_or_reason_ = {};
}
template<class Allocator>

View File

@ -20,6 +20,7 @@
#include <boost/beast/core/detail/integer_sequence.hpp>
#include <boost/align/aligned_alloc.hpp>
#include <boost/asio/buffer.hpp>
#include <boost/core/exchange.hpp>
#include <atomic>
#include <cstdint>
#include <memory>
@ -50,9 +51,8 @@ public:
soft_mutex& operator=(soft_mutex const&) = delete;
soft_mutex(soft_mutex&& other) noexcept
: id_(other.id_)
: id_(boost::exchange(other.id_, 0))
{
other.id_ = 0;
}
soft_mutex& operator=(soft_mutex&& other) noexcept
@ -156,12 +156,7 @@ struct stream_prng
}
prng_ref(prng_ref&& other)
: p_([&other]
{
auto p = other.p_;
other.p_ = nullptr;
return p;
}())
: p_(boost::exchange(other.p_, nullptr))
{
}