Boost logo

Boost-Commit :

From: dwalker07_at_[hidden]
Date: 2008-08-19 04:14:41


Author: dlwalker
Date: 2008-08-19 04:14:40 EDT (Tue, 19 Aug 2008)
New Revision: 48210
URL: http://svn.boost.org/trac/boost/changeset/48210

Log:
Added Boost.S11N support to md5_digest, in non-intrusive mode; changed md5_computer s11n to non-intrusive, since it only depended ona publically-accessible reference
Text files modified:
   sandbox/md5/boost/coding/md5_computer.hpp | 69 +++++++++++++++++++++++----------------
   sandbox/md5/boost/coding/md5_digest.hpp | 66 +++++++++++++++++++++++++++++++------
   sandbox/md5/boost/coding/md5_digest_core.hpp | 32 +++++++++++------
   sandbox/md5/libs/coding/test/md5_digest_test.cpp | 69 ++++++++++++++++++++++++++++++++++++++-
   4 files changed, 181 insertions(+), 55 deletions(-)

Modified: sandbox/md5/boost/coding/md5_computer.hpp
==============================================================================
--- sandbox/md5/boost/coding/md5_computer.hpp (original)
+++ sandbox/md5/boost/coding/md5_computer.hpp 2008-08-19 04:14:40 EDT (Tue, 19 Aug 2008)
@@ -27,7 +27,6 @@
 #include <boost/concept_check.hpp> // for boost::OutputIterator
 #include <boost/cstdint.hpp> // for boost::uint_least8_t
 #include <boost/integer/integer_mask.hpp> // for boost::integer_lo_mask
-#include <boost/serialization/access.hpp> // for boost::serialization::access
 #include <boost/serialization/nvp.hpp> // for boost::serialization::make_nvp
 
 #include <algorithm> // for std::copy, swap
@@ -138,14 +137,6 @@
     //! Creates copy of \c #hashing_table using calculated, not static, values
     static hash_table_type generate_hashing_table();
 
-private:
- // Serialization
- friend class boost::serialization::access;
-
- //! Enables persistence with Boost.Serialization-compatible archives
- template < class Archive >
- void serialize( Archive &ar, const unsigned int version );
-
 }; // md5_computer
 
 
@@ -353,26 +344,6 @@
 { this->context().consume_dword( dword ); }
 
 
-// MD5 message-digest computation serialization function definition --------//
-
-/** Streams a computer to/from an archive using the Boost.Serialization
- protocols. This member function is meant to be called only by the
- Boost.Serialization system, as needed.
-
- \tparam Archive The type of \p ar. It must conform to the requirements
- Boost.Serialization expects of archiving classes.
-
- \param ar The archiving object that this object's representation will
- be streamed to/from.
- \param version The version of the persistence format for this object. (It
- should be zero, since this type just got created.)
- */
-template < class Archive >
-inline void
-md5_computer::serialize( Archive &ar, const unsigned int version )
-{ ar & boost::serialization::make_nvp( "context", this->context() ); }
-
-
 // MD5 message-digest computation miscellaneous function definitions -------//
 
 /** \brief Non-member swapping function for \c md5_computer
@@ -395,6 +366,46 @@
 
 
 } // namespace coding
+
+namespace serialization
+{
+
+
+// MD5 message-digest structure serialization template function definition -//
+
+/** \brief Enables persistence with Boost.Serialization-compatible archives for
+ \c boost::coding::md5_computer, non-member
+
+ Streams a computer to/from an archive using the Boost.Serialization
+ protocols. This function is meant to be called only by the
+ Boost.Serialization system, as needed.
+
+ \tparam Archive The type of \p ar. It must conform to the requirements
+ Boost.Serialization expects of archiving classes.
+
+ \param ar The archiving object that this object's representation will
+ be streamed to/from.
+ \param c The \c md5_computer object to be serialized or deserialized.
+ \param version The version of the persistence format for this object. (It
+ should be zero, since this type just got created.)
+
+ \relates boost::coding::md5_computer
+ */
+template < class Archive >
+inline void
+serialize( Archive &ar, coding::md5_computer &c, const unsigned int version )
+{
+ switch ( version )
+ {
+ default:
+ case 0u:
+ ar & make_nvp( "context", c.context() );
+ break;
+ }
+}
+
+
+} // namespace serialization
 } // namespace boost
 
 

Modified: sandbox/md5/boost/coding/md5_digest.hpp
==============================================================================
--- sandbox/md5/boost/coding/md5_digest.hpp (original)
+++ sandbox/md5/boost/coding/md5_digest.hpp 2008-08-19 04:14:40 EDT (Tue, 19 Aug 2008)
@@ -19,11 +19,12 @@
 #include <boost/coding_fwd.hpp>
 #include <boost/coding/md5_digest_core.hpp> // for boost::coding::md5_digest
 
-#include <boost/config.hpp> // for BOOST_USE_FACET
-#include <boost/mpl/assert.hpp> // for BOOST_MPL_ASSERT_RELATION
-#include <boost/mpl/arithmetic.hpp> // for boost::mpl::divides, modulus, times
-#include <boost/mpl/size_t.hpp> // for boost::mpl::size_t
-#include <boost/typeof/typeof.hpp> // for BOOST_AUTO
+#include <boost/config.hpp> // for BOOST_USE_FACET
+#include <boost/mpl/assert.hpp> // for BOOST_MPL_ASSERT_RELATION
+#include <boost/mpl/arithmetic.hpp> // for boost::mpl::divides,modulus,times
+#include <boost/mpl/size_t.hpp> // for boost::mpl::size_t
+#include <boost/serialization/nvp.hpp> // for boost::serialization::make_nvp
+#include <boost/typeof/typeof.hpp> // for BOOST_AUTO
 
 #include <cstdlib> // for std::div, div_t
 #include <ios> // for std::ios_base
@@ -113,7 +114,7 @@
     read, not counting how \c std::ios_base::skipws for \p i is set.)
 
     \param i The input stream to perform the reading.
- \param n The \c md5_digest object to store the read.
+ \param d The \c md5_digest object to store the read.
 
     \return \p i
 
@@ -123,7 +124,7 @@
  */
 template < typename Ch, class Tr >
 std::basic_istream<Ch, Tr> &
-operator >>( std::basic_istream<Ch, Tr> &i, md5_digest &n )
+operator >>( std::basic_istream<Ch, Tr> &i, md5_digest &d )
 {
     typename std::basic_istream<Ch, Tr>::sentry is( i );
 
@@ -186,7 +187,7 @@
         else
         {
             // Successful read
- n = temp;
+ d = temp;
         }
     }
 
@@ -206,7 +207,7 @@
     written, not counting how <code><var>o</var>.width()</code> is set.)
 
     \param o The output stream to perform the writing.
- \param n The \c md5_digest object to be written.
+ \param d The \c md5_digest object to be written.
 
     \return \p o
 
@@ -216,7 +217,7 @@
  */
 template < typename Ch, class Tr >
 std::basic_ostream<Ch, Tr> &
-operator <<( std::basic_ostream<Ch, Tr> &o, md5_digest const &n )
+operator <<( std::basic_ostream<Ch, Tr> &o, md5_digest const &d )
 {
     // The message always has an exact number of characters; plot it out.
     // (Leave an extra character for the NUL terminator.)
@@ -236,7 +237,7 @@
         std::div_t const nybble_index_parts = std::div( nybble_index,
          detail::md5_constants::nybbles_per_word::value );
 
- *p++ = digits[ 0x0F & (n.hash[ nybble_index_parts.quot ] >> (
+ *p++ = digits[ 0x0F & (d.hash[ nybble_index_parts.quot ] >> (
          detail::md5_constants::bits_per_nybble::value * ((
          nybble_index_parts.rem ) ^ 0x01) )) ];
     }
@@ -248,6 +249,49 @@
 
 
 } // namespace coding
+
+namespace serialization
+{
+
+
+// MD5 message-digest structure serialization template function definition -//
+
+/** \brief Enables persistence with Boost.Serialization-compatible archives for
+ \c boost::coding::md5_digest, non-member
+
+ Streams a message digest to/from an archive using the Boost.Serialization
+ protocols. This function is meant to be called only by the
+ Boost.Serialization system, as needed.
+
+ \tparam Archive The type of \p ar. It must conform to the requirements
+ Boost.Serialization expects of archiving classes.
+
+ \param ar The archiving object that this object's representation will
+ be streamed to/from.
+ \param d The \c md5_digest object to be serialized or deserialized.
+ \param version The version of the persistence format for this object. (It
+ should be zero, since this type just got created.)
+
+ \relates boost::coding::md5_digest
+ */
+template < class Archive >
+inline void
+serialize( Archive &ar, coding::md5_digest &d, const unsigned int version )
+{
+ switch ( version )
+ {
+ default:
+ case 0u:
+ ar & make_nvp( "word-A", d.hash[0] )
+ & make_nvp( "word-B", d.hash[1] )
+ & make_nvp( "word-C", d.hash[2] )
+ & make_nvp( "word-D", d.hash[3] );
+ break;
+ }
+}
+
+
+} // namespace serialization
 } // namespace boost
 
 

Modified: sandbox/md5/boost/coding/md5_digest_core.hpp
==============================================================================
--- sandbox/md5/boost/coding/md5_digest_core.hpp (original)
+++ sandbox/md5/boost/coding/md5_digest_core.hpp 2008-08-19 04:14:40 EDT (Tue, 19 Aug 2008)
@@ -37,10 +37,10 @@
 // I/O streaming operator functions
 template < typename Ch, class Tr >
   std::basic_istream<Ch, Tr> & operator >>( std::basic_istream<Ch, Tr> &i,
- md5_digest &n );
+ md5_digest &d );
 template < typename Ch, class Tr >
   std::basic_ostream<Ch, Tr> & operator <<( std::basic_ostream<Ch, Tr> &o,
- md5_digest const &n );
+ md5_digest const &d );
 
 
 // MD5 message-digest class declaration ------------------------------------//
@@ -51,7 +51,7 @@
     hashing operations. It is supposed to mirror the buffer described in RFC
     1321, sections 3.3&ndash;3.5. Comparisons are supported for check-summing
     purposes, but not ordering. Persistence is supported through the standard
- text stream I/O system.
+ text stream I/O system and Boost.Serialization.
 
     \see boost::coding::md5_context
     \see boost::coding::md5_computer
@@ -103,9 +103,7 @@
 
     \relates boost::coding::md5_digest
  */
-inline
-bool
-operator ==( md5_digest const &l, md5_digest const &r )
+inline bool operator ==( md5_digest const &l, md5_digest const &r )
 {
     return std::equal( l.hash, l.hash + md5_digest::words_per_digest::value,
      r.hash );
@@ -126,15 +124,25 @@
 
     \relates boost::coding::md5_digest
  */
-inline
-bool
-operator !=( md5_digest const &l, md5_digest const &r )
-{
- return !( l == r );
-}
+inline bool operator !=( md5_digest const &l, md5_digest const &r )
+{ return !( l == r ); }
 
 
 } // namespace coding
+
+namespace serialization
+{
+
+
+// More forward declarations -----------------------------------------------//
+
+// Serialization
+template < class Archive >
+ void serialize( Archive &ar, coding::md5_digest &d, const unsigned int
+ version );
+
+
+} // namespace serialization
 } // namespace boost
 
 

Modified: sandbox/md5/libs/coding/test/md5_digest_test.cpp
==============================================================================
--- sandbox/md5/libs/coding/test/md5_digest_test.cpp (original)
+++ sandbox/md5/libs/coding/test/md5_digest_test.cpp 2008-08-19 04:14:40 EDT (Tue, 19 Aug 2008)
@@ -11,8 +11,11 @@
 
 #include <boost/coding/md5.hpp>
 
-#include <boost/lexical_cast.hpp> // for boost::lexical_cast
-#include <boost/test/unit_test.hpp> // unit testing framework
+#include <boost/archive/xml_iarchive.hpp> // for boost::archive::xml_iarchive
+#include <boost/archive/xml_oarchive.hpp> // for boost::archive::xml_oarchive
+#include <boost/lexical_cast.hpp> // for boost::lexical_cast
+#include <boost/serialization/nvp.hpp> // for boost::serialization::make_nvp
+#include <boost/test/unit_test.hpp> // unit testing framework
 #include <boost/test/output/compiler_log_formatter.hpp> // for new formatter
 
 #include <cstddef> // for std::size_t
@@ -20,12 +23,27 @@
 #include <cwchar> // for WEOF, std::wint_t
 #include <iomanip> // for std::setfill, setw
 #include <ios> // for std::left, uppercase
+#include <iostream> // for std::cout
+#include <istream> // for std::basic_istream
 #include <memory> // for std::auto_ptr [for xcode_config]
-#include <ostream> // for std::ostream [for xcode_log_formatter]
+#include <ostream> // for std::endl, basic_ostream
 #include <sstream> // for std::[w](o|i)stringstream
 #include <string> // for std::string, wstring
 
 
+// Control if XML code will be printed conventionally, or just logged.
+#ifndef CONTROL_SHOW_XML
+#define CONTROL_SHOW_XML 0
+#endif
+
+// Logging
+#if CONTROL_SHOW_XML
+#define PRIVATE_SHOW_MESSAGE( m ) std::cout << m << std::endl
+#else
+#define PRIVATE_SHOW_MESSAGE( m ) BOOST_TEST_MESSAGE( m )
+#endif
+
+
 #pragma mark Intro stuff
 
 // Put any using-ed types & templates, and typedefs here
@@ -86,6 +104,26 @@
 char const zeros_s[] = "00000000000000000000000000000000";
 wchar_t const zeros_ws[] = L"00000000000000000000000000000000";
 
+// Completely read an archive from a stream
+template < typename Ch, class Tr, typename T >
+void read_xml_archive( std::basic_istream<Ch, Tr> &i, T &target, char const
+ *name )
+{
+ boost::archive::xml_iarchive ar( i );
+
+ ar >> boost::serialization::make_nvp( name, target );
+}
+
+// Completely write an archive to a stream
+template < typename Ch, class Tr, typename T >
+void write_xml_archive( std::basic_ostream<Ch, Tr> &o, T const &target, char
+ const *name )
+{
+ boost::archive::xml_oarchive ar( o );
+
+ ar << boost::serialization::make_nvp( name, target );
+}
+
 } // unnamed namespace
 
 
@@ -284,4 +322,29 @@
      lexical_cast<md5_digest>(lexical_cast<string>( md5_initial )) );
 }
 
+// Archiving test
+BOOST_AUTO_TEST_CASE( md5_digest_serialization_test )
+{
+ // Write to archive
+ std::stringstream ss;
+ md5_digest test = md5_initial;
+
+ BOOST_REQUIRE_NE( test, zeros );
+ BOOST_REQUIRE_EQUAL( test, md5_initial );
+ write_xml_archive( ss, test, "test" );
+
+ // Ensure receiving object's value is different
+ test = zeros;
+ BOOST_REQUIRE_NE( test, md5_initial );
+ BOOST_REQUIRE_EQUAL( test, zeros );
+
+ // Read from archive
+ PRIVATE_SHOW_MESSAGE( ss.str() );
+ read_xml_archive( ss, test, "test" );
+
+ // Confirm the change and proper read-back
+ BOOST_CHECK_NE( test, zeros );
+ BOOST_CHECK_EQUAL( test, md5_initial );
+}
+
 BOOST_AUTO_TEST_SUITE_END()


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