#include <exception>
#include <boost/serialization/smart_cast.hpp>
#include "test_tools.hpp"
#include <boost/noncopyable.hpp>
using namespace boost::serialization;
class Base1 : public boost::noncopyable
{
char a;
};
class Base2
{
int b;
};
#ifdef BOOST_MSVC
# pragma warning(push)
# pragma warning(disable : 4511 4512)
#endif
class Derived : public Base1, public Base2
{
long c;
};
#ifdef BOOST_MSVC
#pragma warning(pop)
#endif
void test_static_reference_cast_2(){
Derived d;
Base1 & b1 = static_cast<Base1 &>(d);
Base2 & b2 = static_cast<Base2 &>(d);
Base1 & scb1 = smart_cast<Base1 &, Derived &>(d);
Base2 & scb2 = smart_cast<Base2 &, Derived &>(d);
BOOST_CHECK_EQUAL(& b1, & scb1);
BOOST_CHECK_EQUAL(& b2, & scb2);
BOOST_CHECK_EQUAL(& b2,(
& smart_cast<Base2 &, Derived &>(
smart_cast<Derived &, Base1 &>(b1)
))
);
}
void test_static_reference_cast_1(){
Derived d;
Base1 & b1 = static_cast<Base1 &>(d);
Base2 & b2 = static_cast<Base2 &>(d);
Base1 & scb1 = smart_cast_reference<Base1 &>(d);
Base2 & scb2 = smart_cast_reference<Base2 &>(d);
BOOST_CHECK_EQUAL(& b1, & scb1);
BOOST_CHECK_EQUAL(& b2, & scb2);
BOOST_CHECK_EQUAL(& d, & (smart_cast_reference<Derived &>(b1)));
BOOST_CHECK_EQUAL(& d, & (smart_cast_reference<Derived &>(b2)));
BOOST_CHECK_EQUAL(& b2,(
& smart_cast_reference<Base2 &>(
smart_cast_reference<Derived &>(b1)
))
);
}
void test_static_pointer_cast(){
Derived d;
Derived *pD = & d;
Base1 *pB1 = pD;
Base2 *pB2 = pD;
BOOST_CHECK_EQUAL(pB1, smart_cast<Base1 *>(pD));
BOOST_CHECK_EQUAL(pB2, smart_cast<Base2 *>(pD));
BOOST_CHECK_EQUAL(pD, smart_cast<Derived *>(pB1));
BOOST_CHECK_EQUAL(pD, smart_cast<Derived *>(pB2));
BOOST_CHECK_EQUAL(pB2,
smart_cast<Base2 *>(
smart_cast<Derived *>(pB1)
)
);
}
class VBase1 : public boost::noncopyable
{
char a;
public:
virtual ~VBase1(){};
};
class VBase2
{
int b;
public:
virtual ~VBase2(){};
};
#ifdef BOOST_MSVC
# pragma warning(push)
# pragma warning(disable : 4511 4512)
#endif
class VDerived : public VBase1, public VBase2
{
long c;
public:
virtual ~VDerived(){};
};
#ifdef BOOST_MSVC
#pragma warning(pop)
#endif
void test_dynamic_reference_cast_2(){
VDerived d;
VBase1 &b1 = dynamic_cast<VBase1 &>(d);
VBase2 &b2 = static_cast<VBase2 &>(d);
VBase1 & vb1 = smart_cast<VBase1 &, VDerived &>(d);
BOOST_CHECK_EQUAL(& b1, & vb1);
BOOST_CHECK_EQUAL(& b2, (& smart_cast<VBase2 &, VDerived &>(d)));
BOOST_CHECK_EQUAL(& d, (& smart_cast<VDerived &, VBase1 &>(b1)));
BOOST_CHECK_EQUAL(& d, (& smart_cast<VDerived &, VBase2 &>(b2)));
BOOST_CHECK_EQUAL(& b2, (& smart_cast<VBase2 &, VBase1 &>(b1)));
BOOST_CHECK_EQUAL(& b2, (
& smart_cast<VBase2 &, VDerived &>(
smart_cast<VDerived &, VBase1 &>(b1)
))
);
}
void test_dynamic_reference_cast_1(){
VDerived d;
VBase1 &b1 = dynamic_cast<VBase1 &>(d);
VBase2 &b2 = static_cast<VBase2 &>(d);
VBase1 & vb1 = smart_cast_reference<VBase1 &>(d);
BOOST_CHECK_EQUAL(& b1, & vb1);
BOOST_CHECK_EQUAL(& b2, (& smart_cast_reference<VBase2 &>(d)));
BOOST_CHECK_EQUAL(& d, (& smart_cast_reference<VDerived &>(b1)));
BOOST_CHECK_EQUAL(& d, (& smart_cast_reference<VDerived &>(b2)));
BOOST_CHECK_EQUAL(& b2, (& smart_cast_reference<VBase2 &>(b1)));
BOOST_CHECK_EQUAL(& b2, (
& smart_cast_reference<VBase2 &>(
smart_cast_reference<VDerived &>(b1)
))
);
}
void test_dynamic_pointer_cast(){
VDerived d;
VDerived *pD = & d;
VBase1 *pB1 = pD;
VBase2 *pB2 = pD;
BOOST_CHECK_EQUAL(pB1, smart_cast<VBase1 *>(pD));
BOOST_CHECK_EQUAL(pB2, smart_cast<VBase2 *>(pD));
BOOST_CHECK_EQUAL(pD, smart_cast<VDerived *>(pB1));
BOOST_CHECK_EQUAL(pD, smart_cast<VDerived *>(pB2));
BOOST_CHECK_EQUAL(pB2, smart_cast<VBase2 *>(pB1));
BOOST_CHECK_EQUAL(pB2,
smart_cast<VBase2 *>(
smart_cast<VDerived *>(pB1)
)
);
}
int
test_main(int , char * [])
{
test_static_reference_cast_2();
test_static_reference_cast_1();
test_static_pointer_cast();
test_dynamic_reference_cast_2();
test_dynamic_reference_cast_1();
test_dynamic_pointer_cast();
return EXIT_SUCCESS;
}