rvalue references support in error_info

This commit is contained in:
Emil Dotchevski
2015-03-24 15:44:28 -07:00
parent c1f6b743e4
commit 5a040eedf5
4 changed files with 247 additions and 2 deletions

View File

@ -12,6 +12,8 @@
#pragma warning(push,1) #pragma warning(push,1)
#endif #endif
#include <boost/config.hpp>
#include <utility>
#include <string> #include <string>
namespace namespace
@ -41,11 +43,16 @@ boost
error_info: error_info:
public exception_detail::error_info_base public exception_detail::error_info_base
{ {
public: public:
typedef T value_type; typedef T value_type;
error_info( value_type const & value ); error_info( value_type const & value );
#ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
error_info( error_info const & );
error_info( value_type && value ) BOOST_NOEXCEPT_IF(BOOST_NOEXCEPT_EXPR(value_type(std::move(value))));
error_info( error_info && x ) BOOST_NOEXCEPT_IF(BOOST_NOEXCEPT_EXPR(value_type(std::move(x.value_))));
#endif
~error_info() throw(); ~error_info() throw();
value_type const & value_type const &
@ -60,7 +67,11 @@ boost
return value_; return value_;
} }
private: private:
error_info & operator=( error_info const & );
#ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
error_info & operator=( error_info && x );
#endif
std::string name_value_string() const; std::string name_value_string() const;

View File

@ -46,6 +46,30 @@ boost
{ {
} }
#ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
template <class Tag,class T>
inline
error_info<Tag,T>::
error_info( error_info const & x ):
value_(x.value_)
{
}
template <class Tag,class T>
inline
error_info<Tag,T>::
error_info( value_type && value ) BOOST_NOEXCEPT_IF(BOOST_NOEXCEPT_EXPR(value_type(std::move(value)))):
value_(std::move(value))
{
}
template <class Tag,class T>
inline
error_info<Tag,T>::
error_info( error_info && x ) BOOST_NOEXCEPT_IF(BOOST_NOEXCEPT_EXPR(value_type(std::move(x.value_)))):
value_(std::move(x.value_))
{
}
#endif
template <class Tag,class T> template <class Tag,class T>
inline inline
error_info<Tag,T>:: error_info<Tag,T>::
@ -175,6 +199,83 @@ boost
return x; return x;
} }
#ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
template <class E,class Tag,class T>
E const & set_info( E const &, error_info<Tag,T> && );
template <class Tag,class T>
class
set_info_rv<error_info<Tag,T> >
{
template <class E,class Tag1,class T1>
friend E const & set_info( E const &, error_info<Tag1,T1> && );
template <class E>
static
E const &
set( E const & x, error_info<Tag,T> && v )
{
typedef error_info<Tag,T> error_info_tag_t;
shared_ptr<error_info_tag_t> p( new error_info_tag_t(std::move(v)) );
exception_detail::error_info_container * c=x.data_.get();
if( !c )
x.data_.adopt(c=new exception_detail::error_info_container_impl);
c->set(p,BOOST_EXCEPTION_STATIC_TYPEID(error_info_tag_t));
return x;
}
};
template <>
class
set_info_rv<throw_function>
{
template <class E,class Tag1,class T1>
friend E const & set_info( E const &, error_info<Tag1,T1> && );
template <class E>
static
E const &
set( E const & x, throw_function && y )
{
x.throw_function_=y.v_;
return x;
}
};
template <>
class
set_info_rv<throw_file>
{
template <class E,class Tag1,class T1>
friend E const & set_info( E const &, error_info<Tag1,T1> && );
template <class E>
static
E const &
set( E const & x, throw_file && y )
{
x.throw_file_=y.v_;
return x;
}
};
template <>
class
set_info_rv<throw_line>
{
template <class E,class Tag1,class T1>
friend E const & set_info( E const &, error_info<Tag1,T1> && );
template <class E>
static
E const &
set( E const & x, throw_line && y )
{
x.throw_line_=y.v_;
return x;
}
};
template <class E,class Tag,class T>
inline
E const &
set_info( E const & x, error_info<Tag,T> && v )
{
return set_info_rv<error_info<Tag,T> >::template set<E>(x,std::move(v));
}
#endif
template <class T> template <class T>
struct struct
derives_boost_exception derives_boost_exception
@ -190,6 +291,16 @@ boost
{ {
return exception_detail::set_info(x,v); return exception_detail::set_info(x,v);
} }
#ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
template <class E,class Tag,class T>
inline
typename enable_if<exception_detail::derives_boost_exception<E>,E const &>::type
operator<<( E const & x, error_info<Tag,T> && v )
{
return exception_detail::set_info(x,std::move(v));
}
#endif
} }
#if defined(_MSC_VER) && !defined(BOOST_EXCEPTION_ENABLE_WARNINGS) #if defined(_MSC_VER) && !defined(BOOST_EXCEPTION_ENABLE_WARNINGS)

View File

@ -35,6 +35,7 @@ run enable_error_info_test.cpp helper1.cpp ;
run throw_exception_test.cpp helper2.cpp ; run throw_exception_test.cpp helper2.cpp ;
run errno_test.cpp ; run errno_test.cpp ;
run error_info_test.cpp ; run error_info_test.cpp ;
run error_info_rv_test.cpp ;
run diagnostic_information_test.cpp ; run diagnostic_information_test.cpp ;
run refcount_ptr_test.cpp ; run refcount_ptr_test.cpp ;
run current_exception_cast_test.cpp ; run current_exception_cast_test.cpp ;

122
test/error_info_rv_test.cpp Normal file
View File

@ -0,0 +1,122 @@
//Copyright (c) 2006-2009 Emil Dotchevski and Reverge Studios, Inc.
//Distributed under the Boost Software License, Version 1.0. (See accompanying
//file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
#include <boost/exception/get_error_info.hpp>
#include <boost/exception/info.hpp>
#include <boost/detail/lightweight_test.hpp>
#include <boost/detail/workaround.hpp>
#include <string.h>
#ifdef BOOST_NO_CXX11_RVALUE_REFERENCES
int
main()
{
BOOST_FAIL("Platform does not support rvalue references.");
return 1;
}
#else
struct move_only;
typedef boost::error_info<struct tag_test1,move_only> test1;
struct
test_exception:
boost::exception
{
};
struct
move_only
{
explicit move_only(int x) : x_(x) { }
move_only( move_only && other ) : x_(other.x_) { }
int x_;
private:
move_only( move_only const & );
};
void
throw_test1()
{
throw test_exception() << test1(move_only(7));
}
void
throw_test_throw_file_rv()
{
throw test_exception() << std::move(boost::throw_file("throw_file"));
}
void
throw_test_throw_function_rv()
{
throw test_exception() << std::move(boost::throw_function("throw_function"));
}
void
throw_test_throw_line_rv()
{
throw test_exception() << std::move(boost::throw_line(42));
}
int
main()
{
try
{
throw_test1();
BOOST_ASSERT(false);
}
catch(
boost::exception & x )
{
BOOST_TEST(boost::get_error_info<test1>(x));
BOOST_TEST(boost::get_error_info<test1>(x)->x_ == 7);
}
catch(
... )
{
BOOST_TEST(false);
}
try
{
throw_test_throw_function_rv();
BOOST_ASSERT(false);
}
catch(
boost::exception & x )
{
BOOST_TEST(boost::get_error_info<boost::throw_function>(x) && !strcmp(*boost::get_error_info<boost::throw_function>(x),"throw_function"));
}
catch(
... )
{
BOOST_TEST(false);
}
try
{
throw_test_throw_file_rv();
BOOST_ASSERT(false);
}
catch(
boost::exception & x )
{
BOOST_TEST(boost::get_error_info<boost::throw_file>(x) && !strcmp(*boost::get_error_info<boost::throw_file>(x),"throw_file"));
}
catch(
... )
{
BOOST_TEST(false);
}
try
{
throw_test_throw_line_rv();
BOOST_ASSERT(false);
}
catch(
boost::exception & x )
{
BOOST_TEST(boost::get_error_info<boost::throw_line>(x) && *boost::get_error_info<boost::throw_line>(x)==42);
}
catch(
... )
{
BOOST_TEST(false);
}
return boost::report_errors();
}
#endif