From 4ca7dfddab671327be523f1ab2c050566fb02973 Mon Sep 17 00:00:00 2001 From: Robert Ramey Date: Wed, 26 Aug 2009 20:47:01 +0000 Subject: [PATCH] moved code from header to library to reduce code bloat [SVN r55791] --- build/Jamfile.v2 | 3 + doc/exceptions.html | 10 ++- src/archive_exception.cpp | 110 +++++++++++++++++++++++++++++++++ src/basic_iarchive.cpp | 4 +- src/basic_serializer_map.cpp | 2 +- src/polymorphic_oarchive.cpp | 2 - src/shared_ptr_helper.cpp | 113 ++++++++++++++++++++++++++++++++++ src/xml_archive_exception.cpp | 56 +++++++++++++++++ test/test_shared_ptr.cpp | 3 +- vc7ide/Library.vcproj | 15 ++++- 10 files changed, 307 insertions(+), 11 deletions(-) create mode 100644 src/archive_exception.cpp create mode 100644 src/shared_ptr_helper.cpp create mode 100644 src/xml_archive_exception.cpp diff --git a/build/Jamfile.v2 b/build/Jamfile.v2 index bad48047..1630713f 100644 --- a/build/Jamfile.v2 +++ b/build/Jamfile.v2 @@ -75,9 +75,12 @@ SOURCES = text_iarchive text_oarchive void_cast + archive_exception xml_grammar xml_iarchive xml_oarchive + xml_archive_exception + shared_ptr_helper ; WSOURCES = diff --git a/doc/exceptions.html b/doc/exceptions.html index 99f9af76..2987b4ae 100644 --- a/doc/exceptions.html +++ b/doc/exceptions.html @@ -36,6 +36,7 @@ http://www.boost.org/LICENSE_1_0.txt)
stream_error
invalid_class_name
unregistered_class +
multiple_code_instantiation
xml_archive_parsing_error
xml_archive_tag_mismatch
xml_archive_tag_name_error @@ -75,9 +76,11 @@ public: // to insert virus via buffer overrun method. unregistered_cast, // base - derived relationship not registered with // void_cast_register - unsupported_class_version // type saved with a version # greater than the + unsupported_class_version, // type saved with a version # greater than the // one used by the program. This indicates that the proggram // needs to be rebuilt. + multiple_code_instantiation // code for implementing serialization for some + // type has been instantiated in more than one module. } exception_code; exception_code code; archive_exception(exception_code c) : code(c) {} @@ -228,6 +231,11 @@ is described in Runtime Casting. This exception is thrown if an attempt is made to convert between two pointers whose relationship has not been registered, +

multiple_code_instantiation

+This exception is thrown when it is detected that the serialization of the same type +has been instantiated more that once. This might occur when +serialization code is instantiated in both the mainline and one or more DLLS. +

xml_archive_parsing_error

The XML generated by the serialization process is intimately coupled to the C++ class structure, relationships between objects and the serialization diff --git a/src/archive_exception.cpp b/src/archive_exception.cpp new file mode 100644 index 00000000..08cbe480 --- /dev/null +++ b/src/archive_exception.cpp @@ -0,0 +1,110 @@ +/////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8 +// archive_exception.cpp: + +// (C) Copyright 2009 Robert Ramey - http://www.rrsd.com . +// Use, modification and distribution is subject to 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 updates, documentation, and revision history. + +#if (defined _MSC_VER) && (_MSC_VER == 1200) +# pragma warning (disable : 4786) // too long name, harmless warning +#endif + +#include +#include +#include + +#define BOOST_ARCHIVE_SOURCE +#include + +namespace boost { +namespace archive { + +BOOST_ARCHIVE_DECL(BOOST_PP_EMPTY()) +archive_exception::archive_exception( + exception_code c, + const char * e1, + const char * e2 +) : + code(c) +{ + m_msg = "programming error"; + switch(code){ + case no_exception: + m_msg = "uninitialized exception"; + break; + case unregistered_class: + m_msg = "unregistered class"; + if(NULL != e1){ + m_msg += " - "; + m_msg += e1; + } + break; + case invalid_signature: + m_msg = "invalid signature"; + break; + case unsupported_version: + m_msg = "unsupported version"; + break; + case pointer_conflict: + m_msg = "pointer conflict"; + break; + case incompatible_native_format: + m_msg = "incompatible native format"; + if(NULL != e1){ + m_msg += " - "; + m_msg += e1; + } + break; + case array_size_too_short: + m_msg = "array size too short"; + break; + case stream_error: + m_msg = "stream error"; + break; + case invalid_class_name: + m_msg = "class name too long"; + break; + case unregistered_cast: + m_msg = "unregistered void cast "; + m_msg += (NULL != e1) ? e1 : "?"; + m_msg += "<-"; + m_msg += (NULL != e2) ? e2 : "?"; + break; + case unsupported_class_version: + m_msg = "class version"; + break; + case other_exception: + // if get here - it indicates a derived exception + // was sliced by passing by value in catch + m_msg = "unknown derived exception"; + break; + case multiple_code_instantiation: + m_msg = "code instantiated in more than one module"; + if(NULL != e1){ + m_msg += " - "; + m_msg += e1; + } + break; + default: + assert(false); + break; + } +} +BOOST_ARCHIVE_DECL(BOOST_PP_EMPTY()) +archive_exception::~archive_exception() throw () {} + +BOOST_ARCHIVE_DECL(const char *) +archive_exception::what( ) const throw() +{ + return m_msg.c_str(); +} +BOOST_ARCHIVE_DECL(BOOST_PP_EMPTY()) +archive_exception::archive_exception() : + code(no_exception) +{} + +} // archive +} // boost diff --git a/src/basic_iarchive.cpp b/src/basic_iarchive.cpp index 5a3773eb..662ee64e 100644 --- a/src/basic_iarchive.cpp +++ b/src/basic_iarchive.cpp @@ -28,11 +28,11 @@ namespace std{ #include #include -#include - #define BOOST_ARCHIVE_SOURCE #define BOOST_SERIALIZATION_SOURCE +#include + #include #include #include diff --git a/src/basic_serializer_map.cpp b/src/basic_serializer_map.cpp index a8b5688d..6b1fa94e 100644 --- a/src/basic_serializer_map.cpp +++ b/src/basic_serializer_map.cpp @@ -15,10 +15,10 @@ #include #include +#define BOOST_ARCHIVE_SOURCE #include #include -#define BOOST_ARCHIVE_SOURCE #include #include diff --git a/src/polymorphic_oarchive.cpp b/src/polymorphic_oarchive.cpp index 9a1d4669..b85895e2 100644 --- a/src/polymorphic_oarchive.cpp +++ b/src/polymorphic_oarchive.cpp @@ -12,8 +12,6 @@ # pragma warning (disable : 4786) // too long name, harmless warning #endif -#define BOOST_ARCHIVE_SOURCE -#include #define BOOST_ARCHIVE_SOURCE #include diff --git a/src/shared_ptr_helper.cpp b/src/shared_ptr_helper.cpp new file mode 100644 index 00000000..0918876f --- /dev/null +++ b/src/shared_ptr_helper.cpp @@ -0,0 +1,113 @@ +// MS compatible compilers support #pragma once +#if defined(_MSC_VER) && (_MSC_VER >= 1020) +# pragma once +#endif + +/////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8 +// shared_ptr_helper.hpp: serialization for boost shared pointer + +// (C) Copyright 2004-2009 Robert Ramey, Martin Ecker and Takatoshi Kondo +// Use, modification and distribution is subject to 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 updates, documentation, and revision history. + +#include +#include +#include +#include // NULL + +#define BOOST_ARCHIVE_SOURCE + +#include +#include +#include +#include +#include + +namespace boost { +namespace archive{ +namespace detail { + +/////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8 +// a common class for holding various types of shared pointers + +// returns pointer to object and an indicator whether this is a +// new entry (true) or a previous one (false) +BOOST_ARCHIVE_DECL(shared_ptr_helper::result_type) +shared_ptr_helper::get_od( + void * od, + const boost::serialization::extended_type_info * true_type, + const boost::serialization::extended_type_info * this_type +){ + // get void pointer to the most derived type + // this uniquely identifies the object referred to + od = void_downcast( + *true_type, + *this_type, + od + ); + if(NULL == od) + boost::serialization::throw_exception( + archive_exception( + archive_exception::unregistered_cast, + true_type->get_debug_info(), + this_type->get_debug_info() + ) + ); + + // make tracking array if necessary + if(NULL == m_pointers) + m_pointers = new collection_type; + + shared_ptr sp(od, null_deleter()); + std::pair result = + m_pointers->insert( + collection_type::value_type(od, sp) + ); + od = void_upcast( + *true_type, + *this_type, + result.first->first + ); + if(NULL == od) + boost::serialization::throw_exception( + archive_exception( + archive_exception::unregistered_cast, + true_type->get_debug_info(), + this_type->get_debug_info() + ) + ); + return result_type(result.first, od); +} + +// #ifdef BOOST_SERIALIZATION_SHARED_PTR_132_HPP +BOOST_ARCHIVE_DECL(void) +shared_ptr_helper::append(const boost_132::shared_ptr & t){ + if(NULL == m_pointers_132) + m_pointers_132 = new std::list >; + m_pointers_132->push_back(t); +} +// #endif +BOOST_ARCHIVE_DECL(BOOST_PP_EMPTY()) +shared_ptr_helper::shared_ptr_helper() : + m_pointers(NULL) + #ifdef BOOST_SERIALIZATION_SHARED_PTR_132_HPP + , m_pointers_132(NULL) + #endif +{} +BOOST_ARCHIVE_DECL(BOOST_PP_EMPTY()) +shared_ptr_helper::~shared_ptr_helper(){ + if(NULL != m_pointers) + delete m_pointers; + #ifdef BOOST_SERIALIZATION_SHARED_PTR_132_HPP + if(NULL != m_pointers_132) + delete m_pointers_132; + #endif +} + +} // namespace detail +} // namespace serialization +} // namespace boost + diff --git a/src/xml_archive_exception.cpp b/src/xml_archive_exception.cpp new file mode 100644 index 00000000..ad16bd9e --- /dev/null +++ b/src/xml_archive_exception.cpp @@ -0,0 +1,56 @@ +/////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8 +// xml_archive_exception.cpp: + +// (C) Copyright 2009 Robert Ramey - http://www.rrsd.com . +// Use, modification and distribution is subject to 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 updates, documentation, and revision history. + +#if (defined _MSC_VER) && (_MSC_VER == 1200) +# pragma warning (disable : 4786) // too long name, harmless warning +#endif + +#define BOOST_ARCHIVE_SOURCE + +#include +#include +#include + +#include + +namespace boost { +namespace archive { + +BOOST_ARCHIVE_DECL(BOOST_PP_EMPTY()) +xml_archive_exception::xml_archive_exception( + exception_code c, + const char * e1, + const char * e2 + ) : + archive_exception(other_exception, e1, e2) + { + m_msg = "programming error"; + switch(c){ + case xml_archive_parsing_error: + m_msg = "unrecognized XML syntax"; + break; + case xml_archive_tag_mismatch: + m_msg = "XML start/end tag mismatch"; + if(NULL != e1){ + m_msg += " - "; + m_msg += e1; + } + break; + case xml_archive_tag_name_error: + m_msg = "Invalid XML tag name"; + break; + default: + assert(false); + break; + } + } + +} // archive +} // boost diff --git a/test/test_shared_ptr.cpp b/test/test_shared_ptr.cpp index e96bdb91..a5f5f81e 100644 --- a/test/test_shared_ptr.cpp +++ b/test/test_shared_ptr.cpp @@ -23,11 +23,10 @@ namespace std{ #include #include #include +#include #include "test_tools.hpp" -#include - // This is a simple class. It contains a counter of the number // of objects of this class which have been instantiated. class A diff --git a/vc7ide/Library.vcproj b/vc7ide/Library.vcproj index 36e74283..c60bd605 100644 --- a/vc7ide/Library.vcproj +++ b/vc7ide/Library.vcproj @@ -433,6 +433,9 @@ + + - - @@ -880,6 +880,9 @@ + + @@ -955,6 +958,9 @@ + + @@ -967,6 +973,9 @@ + +