#include <cstddef> // NULL
#include <cstdio> // remove
+#include <cstring> // strcmp
#include <boost/config.hpp>
#if defined(BOOST_NO_STDC_NAMESPACE)
namespace std{
}
};
+struct type1 {
+ template<typename Archive>
+ void serialize(Archive&, unsigned int ver) {
+ BOOST_CHECK(ver == 1);
+ }
+};
+struct type2 {
+ template<typename Archive>
+ void serialize(Archive&, unsigned int ver) {
+ BOOST_CHECK(ver == 2);
+ }
+};
+
+BOOST_CLASS_VERSION(type1, 1);
+BOOST_CLASS_VERSION(type2, 2);
+
// save unregistered polymorphic classes
void save_unregistered1(const char *testfile)
{
test_oarchive oa(os, TEST_ARCHIVE_FLAGS);
polymorphic_base *rb1 = new polymorphic_derived1;
-
+
// registration IS necessary when serializing a polymorphic class
// through pointer to the base class
bool except = false;
BOOST_CATCH(boost::archive::archive_exception aex){
except = true;
BOOST_CHECK_MESSAGE(
- NULL == rb1,
+ NULL == rb1,
"failed load resulted in a non-null pointer"
);
}
delete rb2;
}
+// store a pointer from slot0
+void save_unregistered_pointer(const char *testfile)
+{
+ test_ostream os(testfile, TEST_STREAM_FLAGS);
+ test_oarchive oa(os, TEST_ARCHIVE_FLAGS);
+
+ oa.register_type(static_cast<type2 *>(NULL));
+
+ type1 instance1;
+ type2 *pointer2 = new type2;
+
+ BOOST_TRY {
+ oa & BOOST_SERIALIZATION_NVP(instance1) & BOOST_SERIALIZATION_NVP(pointer2);
+ }
+ BOOST_CATCH(...) {
+ BOOST_CHECK_MESSAGE(false, "unexpected exception");
+ }
+ BOOST_CATCH_END
+
+ delete pointer2;
+}
+
+// load a pointer from slot0 which has no pointer serializer
+void load_unregistered_pointer(const char *testfile)
+{
+ test_istream is(testfile);
+ test_iarchive ia(is);
+
+ type1 instance1;
+ type2 *pointer2(NULL);
+
+ bool except = false;
+ BOOST_TRY {
+ ia & BOOST_SERIALIZATION_NVP(instance1) & BOOST_SERIALIZATION_NVP(pointer2);
+ }
+ BOOST_CATCH(boost::archive::archive_exception aex){
+ except = true;
+ BOOST_CHECK_MESSAGE(
+ std::strcmp(aex.what(), "unregistered class") == 0,
+ "incorrect exception"
+ );
+ }
+ BOOST_CATCH_END
+ BOOST_CHECK_MESSAGE(except, "lack of registration not detected !");
+ BOOST_CHECK_MESSAGE(NULL == pointer2, "expected failed load");
+
+ delete pointer2;
+}
+
int
test_main( int /* argc */, char* /* argv */[] )
{
load_unregistered2(testfile);
save_registered(testfile);
load_registered(testfile);
+ save_unregistered_pointer(testfile);
+ load_unregistered_pointer(testfile);
std::remove(testfile);
return EXIT_SUCCESS;
}