mirror of
https://github.com/boostorg/core.git
synced 2025-05-11 13:13:55 +00:00
Implement boost::exchange
This commit is contained in:
parent
9445d08ea7
commit
2cd4753a02
47
include/boost/core/exchange.hpp
Normal file
47
include/boost/core/exchange.hpp
Normal file
@ -0,0 +1,47 @@
|
||||
/*
|
||||
Copyright 2018 Glen Joseph Fernandes
|
||||
(glenjofe@gmail.com)
|
||||
|
||||
Distributed under the Boost Software License, Version 1.0.
|
||||
(http://www.boost.org/LICENSE_1_0.txt)
|
||||
*/
|
||||
#ifndef BOOST_CORE_EXCHANGE_HPP
|
||||
#define BOOST_CORE_EXCHANGE_HPP
|
||||
|
||||
#include <boost/config.hpp>
|
||||
#if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
|
||||
#include <utility>
|
||||
#endif
|
||||
|
||||
namespace boost {
|
||||
|
||||
#if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
|
||||
template<class T, class U = T>
|
||||
BOOST_CXX14_CONSTEXPR inline T exchange(T& t, U&& u)
|
||||
{
|
||||
T v = std::move(t);
|
||||
t = std::forward<U>(u);
|
||||
return v;
|
||||
}
|
||||
#else
|
||||
namespace detail {
|
||||
|
||||
template<class T>
|
||||
struct exchanger {
|
||||
typedef T type;
|
||||
};
|
||||
|
||||
} /* detail */
|
||||
|
||||
template<class T>
|
||||
inline T exchange(T& t, const typename detail::exchanger<T>::type& u)
|
||||
{
|
||||
T v = t;
|
||||
t = u;
|
||||
return v;
|
||||
}
|
||||
#endif
|
||||
|
||||
} /* boost */
|
||||
|
||||
#endif
|
@ -121,5 +121,8 @@ run pointer_traits_rebind_test.cpp ;
|
||||
run pointer_traits_pointer_to_test.cpp ;
|
||||
run to_address_test.cpp ;
|
||||
|
||||
run exchange_test.cpp ;
|
||||
run exchange_move_test.cpp ;
|
||||
|
||||
use-project /boost/core/swap : ./swap ;
|
||||
build-project ./swap ;
|
||||
|
73
test/exchange_move_test.cpp
Normal file
73
test/exchange_move_test.cpp
Normal file
@ -0,0 +1,73 @@
|
||||
/*
|
||||
Copyright 2018 Glen Joseph Fernandes
|
||||
(glenjofe@gmail.com)
|
||||
|
||||
Distributed under the Boost Software License, Version 1.0.
|
||||
(http://www.boost.org/LICENSE_1_0.txt)
|
||||
*/
|
||||
#include <boost/config.hpp>
|
||||
#if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
|
||||
#include <boost/core/exchange.hpp>
|
||||
#include <boost/core/lightweight_test.hpp>
|
||||
|
||||
class C1 {
|
||||
public:
|
||||
explicit C1(int i)
|
||||
: i_(i) { }
|
||||
C1(C1&& c)
|
||||
: i_(c.i_) { }
|
||||
C1& operator=(C1&& c) {
|
||||
i_ = c.i_;
|
||||
return *this;
|
||||
}
|
||||
int i() const {
|
||||
return i_;
|
||||
}
|
||||
private:
|
||||
C1(const C1&);
|
||||
C1& operator=(const C1&);
|
||||
int i_;
|
||||
};
|
||||
|
||||
void test1()
|
||||
{
|
||||
C1 x(1);
|
||||
BOOST_TEST(boost::exchange(x, C1(2)).i() == 1);
|
||||
BOOST_TEST(x.i() == 2);
|
||||
}
|
||||
|
||||
class C2 {
|
||||
public:
|
||||
explicit C2(int i)
|
||||
: i_(i) { }
|
||||
operator C1() const {
|
||||
return C1(i_);
|
||||
}
|
||||
int i() const {
|
||||
return i_;
|
||||
}
|
||||
private:
|
||||
C2(const C2&);
|
||||
C2& operator=(const C2&);
|
||||
int i_;
|
||||
};
|
||||
|
||||
void test2()
|
||||
{
|
||||
C1 x(1);
|
||||
BOOST_TEST(boost::exchange(x, C2(2)).i() == 1);
|
||||
BOOST_TEST(x.i() == 2);
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
test1();
|
||||
test2();
|
||||
return boost::report_errors();
|
||||
}
|
||||
#else
|
||||
int main()
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
#endif
|
65
test/exchange_test.cpp
Normal file
65
test/exchange_test.cpp
Normal file
@ -0,0 +1,65 @@
|
||||
/*
|
||||
Copyright 2018 Glen Joseph Fernandes
|
||||
(glenjofe@gmail.com)
|
||||
|
||||
Distributed under the Boost Software License, Version 1.0.
|
||||
(http://www.boost.org/LICENSE_1_0.txt)
|
||||
*/
|
||||
#include <boost/core/exchange.hpp>
|
||||
#include <boost/core/lightweight_test.hpp>
|
||||
|
||||
void test1()
|
||||
{
|
||||
int i = 1;
|
||||
BOOST_TEST(boost::exchange(i, 2) == 1);
|
||||
BOOST_TEST(i == 2);
|
||||
}
|
||||
|
||||
class C1 {
|
||||
public:
|
||||
explicit C1(int i)
|
||||
: i_(i) { }
|
||||
int i() const {
|
||||
return i_;
|
||||
}
|
||||
private:
|
||||
int i_;
|
||||
};
|
||||
|
||||
void test2()
|
||||
{
|
||||
C1 x(1);
|
||||
BOOST_TEST(boost::exchange(x, C1(2)).i() == 1);
|
||||
BOOST_TEST(x.i() == 2);
|
||||
}
|
||||
|
||||
class C2 {
|
||||
public:
|
||||
explicit C2(int i)
|
||||
: i_(i) { }
|
||||
operator C1() const {
|
||||
return C1(i_);
|
||||
}
|
||||
int i() const {
|
||||
return i_;
|
||||
}
|
||||
private:
|
||||
C2(const C2&);
|
||||
C2& operator=(const C2&);
|
||||
int i_;
|
||||
};
|
||||
|
||||
void test3()
|
||||
{
|
||||
C1 x(1);
|
||||
BOOST_TEST(boost::exchange(x, C2(2)).i() == 1);
|
||||
BOOST_TEST(x.i() == 2);
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
test1();
|
||||
test2();
|
||||
test3();
|
||||
return boost::report_errors();
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user