Boost logo

Boost-Commit :

From: troyer_at_[hidden]
Date: 2008-06-14 07:21:26


Author: troyer
Date: 2008-06-14 07:21:26 EDT (Sat, 14 Jun 2008)
New Revision: 46384
URL: http://svn.boost.org/trac/boost/changeset/46384

Log:
Changed determination of when to use array optimizations to a traits class used only by the array wrapper, removing the need for deriving from the array/[io]archive base class and allowing mor specific enabling of array optimization, with less cost to users who don't need it.
Text files modified:
   trunk/boost/serialization/array.hpp | 91 +++++++++++++++++++++++----------------
   trunk/boost/serialization/valarray.hpp | 2
   trunk/boost/serialization/vector.hpp | 14 ++++-
   3 files changed, 64 insertions(+), 43 deletions(-)

Modified: trunk/boost/serialization/array.hpp
==============================================================================
--- trunk/boost/serialization/array.hpp (original)
+++ trunk/boost/serialization/array.hpp 2008-06-14 07:21:26 EDT (Sat, 14 Jun 2008)
@@ -7,15 +7,26 @@
 // http://www.boost.org/LICENSE_1_0.txt)
 
 #include <boost/serialization/nvp.hpp>
-#include <boost/serialization/split_free.hpp>
+#include <boost/serialization/split_member.hpp>
 #include <boost/serialization/wrapper.hpp>
 #include <boost/serialization/collection_size_type.hpp>
 #include <boost/archive/archive_exception.hpp>
 #include <boost/throw_exception.hpp>
+#include <boost/mpl/always.hpp>
+#include <boost/mpl/apply.hpp>
+#include <boost/mpl/bool.hpp>
+#include <boost/type_traits/remove_const.hpp>
 #include <iostream>
 
 namespace boost { namespace serialization {
 
+
+// traits to specify whether to use an optimized array serialization
+
+template <class Archive>
+struct use_array_optimization : boost::mpl::always<boost::mpl::false_> {};
+
+
 template<class T>
 class array
  : public wrapper_traits<array<T> >
@@ -30,7 +41,7 @@
     
     // default implementation
     template<class Archive>
- void serialize(Archive &ar, const unsigned int) const
+ void serialize_optimized(Archive &ar, const unsigned int, mpl::false_ ) const
     {
       // default implemention does the loop
       std::size_t c = count();
@@ -38,6 +49,37 @@
       while(0 < c--)
             ar & make_nvp("item", *t++);
     }
+
+ // optimized implementation
+ template<class Archive>
+ void serialize_optimized(Archive &ar, const unsigned int version, mpl::true_ )
+ {
+ boost::serialization::split_member(ar, *this, version);
+ }
+
+ // default implementation
+ template<class Archive>
+ void save(Archive &ar, const unsigned int version) const
+ {
+ ar.save_array(*this,version);
+ }
+
+ // default implementation
+ template<class Archive>
+ void load(Archive &ar, const unsigned int version)
+ {
+ ar.load_array(*this,version);
+ }
+
+ // default implementation
+ template<class Archive>
+ void serialize(Archive &ar, const unsigned int version)
+ {
+ typedef BOOST_DEDUCED_TYPENAME serialization::use_array_optimization<Archive>
+ ::template apply<BOOST_DEDUCED_TYPENAME remove_const<T>::type
+ >::type use_optimized;
+ serialize_optimized(ar,version,use_optimized());
+ }
     
     value_type* address() const
     {
@@ -64,44 +106,17 @@
     return array<T>(t, s);
 }
 
-/////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
-// T[N]
-
-/*
-
-template<class Archive, class U, std::size_t N>
-void save( Archive & ar, U const (& t)[N], const unsigned int file_version )
-{
- const serialization::collection_size_type count(N);
- ar << BOOST_SERIALIZATION_NVP(count);
- if (N)
- ar << serialization::make_array(&t[0],N);
-}
-
-template<class Archive, class U, std::size_t N>
-void load( Archive & ar, U (& t)[N], const unsigned int file_version )
-{
- serialization::collection_size_type count;
- ar >> BOOST_SERIALIZATION_NVP(count);
- if(count > N)
- boost::throw_exception(archive::archive_exception(
- boost::archive::archive_exception::array_size_too_short
- ));
- if (N)
- ar >> serialization::make_array(&t[0],count);
-}
-
+} } // end namespace boost::serialization
 
-// split non-intrusive serialization function member into separate
-// non intrusive save/load member functions
-template<class Archive, class U, std::size_t N>
-inline void serialize( Archive & ar, U (& t)[N], const unsigned int file_version)
-{
- boost::serialization::split_free(ar, t, file_version);
-}
-*/
 
+#define BOOST_SERIALIZATION_USE_ARRAY_OPTIMIZATION(Archive) \
+namespace boost { namespace serialization { \
+template <> struct use_array_optimization<Archive> { \
+ template <class ValueType> \
+ struct apply : boost::mpl::apply1<Archive::use_array_optimization \
+ , BOOST_DEDUCED_TYPENAME boost::remove_const<ValueType>::type \
+ >::type {}; \
+}; }}
 
-} } // end namespace boost::serialization
 
 #endif //BOOST_SERIALIZATION_ARRAY_HPP

Modified: trunk/boost/serialization/valarray.hpp
==============================================================================
--- trunk/boost/serialization/valarray.hpp (original)
+++ trunk/boost/serialization/valarray.hpp 2008-06-14 07:21:26 EDT (Sat, 14 Jun 2008)
@@ -7,7 +7,7 @@
 #endif
 
 /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
-// vector.hpp: serialization for stl vector templates
+// valarray.hpp: serialization for stl vector templates
 
 // (C) Copyright 2005 Matthias Troyer .
 // Use, modification and distribution is subject to the Boost Software

Modified: trunk/boost/serialization/vector.hpp
==============================================================================
--- trunk/boost/serialization/vector.hpp (original)
+++ trunk/boost/serialization/vector.hpp 2008-06-14 07:21:26 EDT (Sat, 14 Jun 2008)
@@ -27,7 +27,6 @@
 #include <boost/serialization/split_free.hpp>
 #include <boost/serialization/array.hpp>
 #include <boost/serialization/detail/get_data.hpp>
-#include <boost/detail/has_default_constructor.hpp>
 #include <boost/mpl/bool.hpp>
 
 namespace boost {
@@ -78,7 +77,11 @@
 ){
     const collection_size_type count(t.size());
     ar << BOOST_SERIALIZATION_NVP(count);
- if (!t.empty())
+ if(3 < ar.get_library_version()) {
+ const unsigned int item_version = version<U>::value;
+ ar << BOOST_SERIALIZATION_NVP(item_version);
+ }
+ if (!t.empty())
       ar << make_array(detail::get_data(t),t.size());
 }
 
@@ -92,6 +95,9 @@
     collection_size_type count(t.size());
     ar >> BOOST_SERIALIZATION_NVP(count);
     t.resize(count);
+ unsigned int item_version=0;
+ if(3 < ar.get_library_version())
+ ar >> BOOST_SERIALIZATION_NVP(item_version);
     if (!t.empty())
       ar >> make_array(detail::get_data(t),t.size());
   }
@@ -104,7 +110,7 @@
     const std::vector<U, Allocator> &t,
     const unsigned int file_version
 ){
- save(ar,t,file_version, boost::detail::has_default_constructor<U>());
+ save(ar,t,file_version, typename use_array_optimization<Archive>::template apply<U>::type());
 }
 
 template<class Archive, class U, class Allocator>
@@ -113,7 +119,7 @@
     std::vector<U, Allocator> &t,
     const unsigned int file_version
 ){
- load(ar,t,file_version, boost::detail::has_default_constructor<U>());
+ load(ar,t,file_version, typename use_array_optimization<Archive>::template apply<U>::type());
 }
 
 // split non-intrusive serialization function member into separate


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk