forked from boostorg/conversion
Merge branch 'develop'
This commit is contained in:
@ -1,20 +0,0 @@
|
||||
// boost cast.hpp header file
|
||||
//
|
||||
// (C) Copyright Antony Polukhin 2014.
|
||||
//
|
||||
// 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)
|
||||
//
|
||||
// See http://www.boost.org/libs/conversion for Documentation.
|
||||
|
||||
// This is a DEPRECATED header file!
|
||||
// Use <boost/polymorphic_cast.hpp> or <boost/numeric/conversion/cast.hpp> instead
|
||||
|
||||
#ifndef BOOST_CAST_HPP
|
||||
#define BOOST_CAST_HPP
|
||||
|
||||
# include <boost/polymorphic_cast.hpp>
|
||||
# include <boost/numeric/conversion/cast.hpp>
|
||||
|
||||
#endif // BOOST_CAST_HPP
|
@ -5,17 +5,24 @@
|
||||
#ifndef IMPLICIT_CAST_DWA200356_HPP
|
||||
# define IMPLICIT_CAST_DWA200356_HPP
|
||||
|
||||
# include <boost/mpl/identity.hpp>
|
||||
|
||||
namespace boost {
|
||||
|
||||
namespace detail {
|
||||
|
||||
template<class T> struct icast_identity
|
||||
{
|
||||
typedef T type;
|
||||
};
|
||||
|
||||
} // namespace detail
|
||||
|
||||
// implementation originally suggested by C. Green in
|
||||
// http://lists.boost.org/MailArchives/boost/msg00886.php
|
||||
|
||||
// The use of identity creates a non-deduced form, so that the
|
||||
// explicit template argument must be supplied
|
||||
template <typename T>
|
||||
inline T implicit_cast (typename mpl::identity<T>::type x) {
|
||||
inline T implicit_cast (typename boost::detail::icast_identity<T>::type x) {
|
||||
return x;
|
||||
}
|
||||
|
||||
|
@ -48,9 +48,6 @@
|
||||
# include <boost/config.hpp>
|
||||
# include <boost/assert.hpp>
|
||||
# include <typeinfo>
|
||||
# include <boost/type.hpp>
|
||||
# include <boost/limits.hpp>
|
||||
# include <boost/detail/select_type.hpp>
|
||||
|
||||
namespace boost
|
||||
{
|
||||
|
@ -10,8 +10,6 @@ import feature ;
|
||||
|
||||
project
|
||||
: requirements
|
||||
<library>/boost/test//boost_unit_test_framework
|
||||
<link>static
|
||||
<toolset>gcc-4.7:<cxxflags>-ftrapv
|
||||
<toolset>gcc-4.6:<cxxflags>-ftrapv
|
||||
<toolset>clang:<cxxflags>-ftrapv
|
||||
@ -26,6 +24,6 @@ test-suite conversion
|
||||
: [ run implicit_cast.cpp ]
|
||||
[ compile-fail implicit_cast_fail.cpp ]
|
||||
[ run cast_test.cpp ]
|
||||
[ run numeric_cast_test.cpp ]
|
||||
[ run polymorphic_cast_test.cpp ]
|
||||
[ compile-fail implicit_cast_fail2.cpp ]
|
||||
;
|
||||
|
||||
|
@ -14,14 +14,8 @@
|
||||
// 3 Aug 99 Initial Version
|
||||
|
||||
#include <iostream>
|
||||
#include <climits>
|
||||
#include <cfloat> // for DBL_MAX (Peter Schmid)
|
||||
#include <boost/polymorphic_cast.hpp>
|
||||
|
||||
# if SCHAR_MAX == LONG_MAX
|
||||
# error "This test program doesn't work if SCHAR_MAX == LONG_MAX"
|
||||
# endif
|
||||
|
||||
using namespace boost;
|
||||
using std::cout;
|
||||
|
||||
|
@ -4,10 +4,6 @@
|
||||
// http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#include <boost/implicit_cast.hpp>
|
||||
#include <boost/type.hpp>
|
||||
|
||||
#define BOOST_INCLUDE_MAIN
|
||||
#include <boost/test/test_tools.hpp>
|
||||
|
||||
using boost::implicit_cast;
|
||||
|
||||
@ -16,11 +12,9 @@ struct foo
|
||||
explicit foo(char const*) {}
|
||||
};
|
||||
|
||||
int test_main(int, char*[])
|
||||
int main()
|
||||
{
|
||||
foo x = implicit_cast<foo>("foobar");
|
||||
(void)x; // warning suppression.
|
||||
BOOST_CHECK(false); // suppressing warning about 'boost::unit_test::{anonymous}::unit_test_log' defined but not used
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
19
test/implicit_cast_fail2.cpp
Normal file
19
test/implicit_cast_fail2.cpp
Normal file
@ -0,0 +1,19 @@
|
||||
//
|
||||
// Test that implicit_cast requires a template argument
|
||||
//
|
||||
// Copyright 2014 Peter Dimov
|
||||
//
|
||||
// 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/implicit_cast.hpp>
|
||||
|
||||
int main()
|
||||
{
|
||||
int x = boost::implicit_cast( 1 );
|
||||
(void)x;
|
||||
|
||||
return 0;
|
||||
}
|
@ -1,101 +0,0 @@
|
||||
// boost utility cast test program -----------------------------------------//
|
||||
|
||||
// (C) Copyright Beman Dawes, Dave Abrahams 1999. 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)
|
||||
|
||||
// See http://www.boost.org for most recent version including documentation.
|
||||
|
||||
// Revision History
|
||||
// 28 Set 04 factored out numeric_cast<> test (Fernando Cacciola)
|
||||
// 20 Jan 01 removed use of <limits> for portability to raw GCC (David Abrahams)
|
||||
// 28 Jun 00 implicit_cast removed (Beman Dawes)
|
||||
// 30 Aug 99 value_cast replaced by numeric_cast
|
||||
// 3 Aug 99 Initial Version
|
||||
|
||||
#include <iostream>
|
||||
#include <climits>
|
||||
#include <cfloat> // for DBL_MAX (Peter Schmid)
|
||||
#include <boost/cast.hpp>
|
||||
|
||||
#include "boost/test/minimal.hpp"
|
||||
|
||||
# if SCHAR_MAX == LONG_MAX
|
||||
# error "This test program doesn't work if SCHAR_MAX == LONG_MAX"
|
||||
# endif
|
||||
|
||||
using namespace boost;
|
||||
using std::cout;
|
||||
|
||||
|
||||
int test_main( int , char * [] )
|
||||
{
|
||||
|
||||
# ifdef NDEBUG
|
||||
cout << "NDEBUG is defined\n";
|
||||
# else
|
||||
cout << "NDEBUG is not defined\n";
|
||||
# endif
|
||||
|
||||
cout << "\nBeginning tests...\n";
|
||||
|
||||
// test implicit_cast and numeric_cast -------------------------------------//
|
||||
|
||||
// tests which should succeed
|
||||
long small_value = 1;
|
||||
long small_negative_value = -1;
|
||||
long large_value = LONG_MAX;
|
||||
long large_negative_value = LONG_MIN;
|
||||
signed char c = 0;
|
||||
|
||||
c = static_cast<signed char>(large_value);
|
||||
|
||||
c = numeric_cast<signed char>( small_value );
|
||||
BOOST_CHECK( c == 1 );
|
||||
c = 0;
|
||||
c = numeric_cast<signed char>( small_value );
|
||||
BOOST_CHECK( c == 1 );
|
||||
c = 0;
|
||||
c = numeric_cast<signed char>( small_negative_value );
|
||||
BOOST_CHECK( c == -1 );
|
||||
|
||||
// These tests courtesy of Joe R NWP Swatosh<joe.r.swatosh@usace.army.mil>
|
||||
BOOST_CHECK( 0.0f == numeric_cast<float>( 0.0 ) );
|
||||
BOOST_CHECK( 0.0 == numeric_cast<double>( 0.0 ) );
|
||||
|
||||
// tests which should result in errors being detected
|
||||
|
||||
bool caught_exception = false;
|
||||
try { c = numeric_cast<signed char>( large_value ); }
|
||||
catch (bad_numeric_cast)
|
||||
{ cout<<"caught bad_numeric_cast #1\n"; caught_exception = true; }
|
||||
BOOST_CHECK ( caught_exception );
|
||||
|
||||
caught_exception = false;
|
||||
try { c = numeric_cast<signed char>( large_negative_value ); }
|
||||
catch (bad_numeric_cast)
|
||||
{ cout<<"caught bad_numeric_cast #2\n"; caught_exception = true; }
|
||||
BOOST_CHECK ( caught_exception );
|
||||
|
||||
unsigned long ul;
|
||||
caught_exception = false;
|
||||
try { ul = numeric_cast<unsigned long>( large_negative_value ); }
|
||||
catch (bad_numeric_cast)
|
||||
{ cout<<"caught bad_numeric_cast #3\n"; caught_exception = true; }
|
||||
BOOST_CHECK ( caught_exception );
|
||||
|
||||
caught_exception = false;
|
||||
try { ul = numeric_cast<unsigned long>( small_negative_value ); }
|
||||
catch (bad_numeric_cast)
|
||||
{ cout<<"caught bad_numeric_cast #4\n"; caught_exception = true; }
|
||||
BOOST_CHECK ( caught_exception );
|
||||
|
||||
caught_exception = false;
|
||||
try { numeric_cast<int>( DBL_MAX ); }
|
||||
catch (bad_numeric_cast)
|
||||
{ cout<<"caught bad_numeric_cast #5\n"; caught_exception = true; }
|
||||
BOOST_CHECK ( caught_exception );
|
||||
|
||||
(void)ul; // Supressing GCC warning about set but unused wariable
|
||||
return 0 ;
|
||||
}
|
150
test/polymorphic_cast_test.cpp
Normal file
150
test/polymorphic_cast_test.cpp
Normal file
@ -0,0 +1,150 @@
|
||||
//
|
||||
// Test boost::polymorphic_cast, boost::polymorphic_downcast
|
||||
//
|
||||
// Copyright 1999 Beman Dawes
|
||||
// Copyright 1999 Dave Abrahams
|
||||
// Copyright 2014 Peter Dimov
|
||||
//
|
||||
// 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
|
||||
//
|
||||
|
||||
#define BOOST_ENABLE_ASSERT_HANDLER
|
||||
#include <boost/polymorphic_cast.hpp>
|
||||
#include <boost/core/lightweight_test.hpp>
|
||||
#include <string>
|
||||
|
||||
static bool expect_assertion = false;
|
||||
static int assertion_failed_count = 0;
|
||||
|
||||
// BOOST_ASSERT custom handler
|
||||
void boost::assertion_failed( char const * expr, char const * function, char const * file, long line )
|
||||
{
|
||||
if( expect_assertion )
|
||||
{
|
||||
++assertion_failed_count;
|
||||
}
|
||||
else
|
||||
{
|
||||
BOOST_ERROR( "unexpected assertion" );
|
||||
|
||||
BOOST_LIGHTWEIGHT_TEST_OSTREAM
|
||||
<< file << "(" << line << "): assertion '" << expr << "' failed in function '"
|
||||
<< function << "'" << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
struct Base
|
||||
{
|
||||
virtual ~Base() {}
|
||||
virtual std::string kind() { return "Base"; }
|
||||
};
|
||||
|
||||
struct Base2
|
||||
{
|
||||
virtual ~Base2() {}
|
||||
virtual std::string kind2() { return "Base2"; }
|
||||
};
|
||||
|
||||
struct Derived : public Base, Base2
|
||||
{
|
||||
virtual std::string kind() { return "Derived"; }
|
||||
};
|
||||
|
||||
static void test_polymorphic_cast()
|
||||
{
|
||||
Base * base = new Derived;
|
||||
|
||||
Derived * derived;
|
||||
|
||||
try
|
||||
{
|
||||
derived = boost::polymorphic_cast<Derived*>( base );
|
||||
|
||||
BOOST_TEST( derived != 0 );
|
||||
|
||||
if( derived != 0 )
|
||||
{
|
||||
BOOST_TEST_EQ( derived->kind(), "Derived" );
|
||||
}
|
||||
}
|
||||
catch( std::bad_cast const& )
|
||||
{
|
||||
BOOST_ERROR( "boost::polymorphic_cast<Derived*>( base ) threw std::bad_cast" );
|
||||
}
|
||||
|
||||
Base2 * base2;
|
||||
|
||||
try
|
||||
{
|
||||
base2 = boost::polymorphic_cast<Base2*>( base ); // crosscast
|
||||
|
||||
BOOST_TEST( base2 != 0 );
|
||||
|
||||
if( base2 != 0 )
|
||||
{
|
||||
BOOST_TEST_EQ( base2->kind2(), "Base2" );
|
||||
}
|
||||
}
|
||||
catch( std::bad_cast const& )
|
||||
{
|
||||
BOOST_ERROR( "boost::polymorphic_cast<Base2*>( base ) threw std::bad_cast" );
|
||||
}
|
||||
|
||||
delete base;
|
||||
}
|
||||
|
||||
static void test_polymorphic_downcast()
|
||||
{
|
||||
Base * base = new Derived;
|
||||
|
||||
Derived * derived = boost::polymorphic_downcast<Derived*>( base );
|
||||
|
||||
BOOST_TEST( derived != 0 );
|
||||
|
||||
if( derived != 0 )
|
||||
{
|
||||
BOOST_TEST_EQ( derived->kind(), "Derived" );
|
||||
}
|
||||
|
||||
// polymorphic_downcast can't do crosscasts
|
||||
|
||||
delete base;
|
||||
}
|
||||
|
||||
static void test_polymorphic_cast_fail()
|
||||
{
|
||||
Base * base = new Base;
|
||||
|
||||
BOOST_TEST_THROWS( boost::polymorphic_cast<Derived*>( base ), std::bad_cast );
|
||||
|
||||
delete base;
|
||||
}
|
||||
|
||||
static void test_polymorphic_downcast_fail()
|
||||
{
|
||||
Base * base = new Base;
|
||||
|
||||
int old_count = assertion_failed_count;
|
||||
expect_assertion = true;
|
||||
|
||||
boost::polymorphic_downcast<Derived*>( base ); // should assert
|
||||
|
||||
BOOST_TEST_EQ( assertion_failed_count, old_count + 1 );
|
||||
expect_assertion = false;
|
||||
|
||||
delete base;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
test_polymorphic_cast();
|
||||
test_polymorphic_downcast();
|
||||
test_polymorphic_cast_fail();
|
||||
test_polymorphic_downcast_fail();
|
||||
|
||||
return boost::report_errors();
|
||||
}
|
Reference in New Issue
Block a user