Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r60275 - trunk/libs/serialization/example
From: ramey_at_[hidden]
Date: 2010-03-06 21:53:06


Author: ramey
Date: 2010-03-06 21:53:05 EST (Sat, 06 Mar 2010)
New Revision: 60275
URL: http://svn.boost.org/trac/boost/changeset/60275

Log:
Add new demos for log_archive, simple_log_archive and trivial_archive
Added:
   trunk/libs/serialization/example/demo_log.cpp (contents, props changed)
   trunk/libs/serialization/example/demo_simple_log.cpp (contents, props changed)
   trunk/libs/serialization/example/demo_trivial_archive.cpp (contents, props changed)
   trunk/libs/serialization/example/log_archive.cpp (contents, props changed)
   trunk/libs/serialization/example/log_archive.hpp (contents, props changed)
   trunk/libs/serialization/example/simple_log_archive.hpp (contents, props changed)
Text files modified:
   trunk/libs/serialization/example/Jamfile.v2 | 5 ++++-
   trunk/libs/serialization/example/demo_gps.hpp | 3 +--
   2 files changed, 5 insertions(+), 3 deletions(-)

Modified: trunk/libs/serialization/example/Jamfile.v2
==============================================================================
--- trunk/libs/serialization/example/Jamfile.v2 (original)
+++ trunk/libs/serialization/example/Jamfile.v2 2010-03-06 21:53:05 EST (Sat, 06 Mar 2010)
@@ -42,12 +42,15 @@
     # demos
     [ demo_bsl_run demo ]
     [ demo_bsl_run demo_auto_ptr ]
+ [ demo_bsl_run demo_dll ]
     [ demo_bsl_run demo_exception ]
- [ demo_bsl_run demo_fast_archive ]
+ [ demo_bsl_run demo_log ]
     [ demo_bsl_run demo_pimpl : demo_pimpl_A ]
     [ demo_bsl_run demo_polymorphic : demo_polymorphic_A ]
     [ demo_bsl_run demo_portable_archive : portable_binary_iarchive portable_binary_oarchive ]
     [ demo_bsl_run demo_shared_ptr ]
+ [ demo_bsl_run demo_simple_log ]
+ [ demo_bsl_run demo_trival_archive ]
     [ demo_bsl_run demo_xml ]
     [ demo_bsl_run demo_xml_save ]
     [ demo_bsl_run demo_xml_load : : <dependency>demo_xml_save ]

Modified: trunk/libs/serialization/example/demo_gps.hpp
==============================================================================
--- trunk/libs/serialization/example/demo_gps.hpp (original)
+++ trunk/libs/serialization/example/demo_gps.hpp 2010-03-06 21:53:05 EST (Sat, 06 Mar 2010)
@@ -10,12 +10,11 @@
 // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
 // http://www.boost.org/LICENSE_1_0.txt)
 
-
-#include <string>
 #include <iomanip>
 #include <iostream>
 #include <fstream>
 
+#include <boost/serialization/string.hpp>
 #include <boost/serialization/nvp.hpp>
 #include <boost/serialization/utility.hpp>
 #include <boost/serialization/list.hpp>

Added: trunk/libs/serialization/example/demo_log.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/serialization/example/demo_log.cpp 2010-03-06 21:53:05 EST (Sat, 06 Mar 2010)
@@ -0,0 +1,76 @@
+/////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
+//
+// demo_log.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)
+
+#include <iostream>
+#include <cstdio>
+
+#include "demo_gps.hpp"
+#include "log_archive.hpp"
+
+int main(int argc, char *argv[])
+{
+ // make the schedule
+ bus_schedule schedule;
+
+ // fill in the data
+ // make a few stops
+ bus_stop *bs0 = new bus_stop_corner(
+ gps_position(34, 135, 52.560f),
+ gps_position(134, 22, 78.30f),
+ "24th Street", "10th Avenue"
+ );
+ bus_stop *bs1 = new bus_stop_corner(
+ gps_position(35, 137, 23.456f),
+ gps_position(133, 35, 54.12f),
+ "State street", "Cathedral Vista Lane"
+ );
+ bus_stop *bs2 = new bus_stop_destination(
+ gps_position(35, 136, 15.456f),
+ gps_position(133, 32, 15.300f),
+ "White House"
+ );
+ bus_stop *bs3 = new bus_stop_destination(
+ gps_position(35, 134, 48.789f),
+ gps_position(133, 32, 16.230f),
+ "Lincoln Memorial"
+ );
+
+ // make a routes
+ bus_route route0;
+ route0.append(bs0);
+ route0.append(bs1);
+ route0.append(bs2);
+
+ // add trips to schedule
+ schedule.append("bob", 6, 24, &route0);
+ schedule.append("bob", 9, 57, &route0);
+ schedule.append("alice", 11, 02, &route0);
+
+ // make aother routes
+ bus_route route1;
+ route1.append(bs3);
+ route1.append(bs2);
+ route1.append(bs1);
+
+ // add trips to schedule
+ schedule.append("ted", 7, 17, &route1);
+ schedule.append("ted", 9, 38, &route1);
+ schedule.append("alice", 11, 47, &route1);
+
+ // display the complete schedule
+ log_archive oa(std::cout);
+ //oa << BOOST_SERIALIZATION_NVP(schedule);
+ oa << schedule;
+
+ delete bs0;
+ delete bs1;
+ delete bs2;
+ delete bs3;
+ return 0;
+}

Added: trunk/libs/serialization/example/demo_simple_log.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/serialization/example/demo_simple_log.cpp 2010-03-06 21:53:05 EST (Sat, 06 Mar 2010)
@@ -0,0 +1,75 @@
+/////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
+//
+// demo_log.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)
+
+#include <iostream>
+#include <cstdio>
+
+#include "demo_gps.hpp"
+#include "simple_log_archive.hpp"
+
+int main(int argc, char *argv[])
+{
+ // make the schedule
+ bus_schedule schedule;
+
+ // fill in the data
+ // make a few stops
+ bus_stop *bs0 = new bus_stop_corner(
+ gps_position(34, 135, 52.560f),
+ gps_position(134, 22, 78.30f),
+ "24th Street", "10th Avenue"
+ );
+ bus_stop *bs1 = new bus_stop_corner(
+ gps_position(35, 137, 23.456f),
+ gps_position(133, 35, 54.12f),
+ "State street", "Cathedral Vista Lane"
+ );
+ bus_stop *bs2 = new bus_stop_destination(
+ gps_position(35, 136, 15.456f),
+ gps_position(133, 32, 15.300f),
+ "White House"
+ );
+ bus_stop *bs3 = new bus_stop_destination(
+ gps_position(35, 134, 48.789f),
+ gps_position(133, 32, 16.230f),
+ "Lincoln Memorial"
+ );
+
+ // make a routes
+ bus_route route0;
+ route0.append(bs0);
+ route0.append(bs1);
+ route0.append(bs2);
+
+ // add trips to schedule
+ schedule.append("bob", 6, 24, &route0);
+ schedule.append("bob", 9, 57, &route0);
+ schedule.append("alice", 11, 02, &route0);
+
+ // make aother routes
+ bus_route route1;
+ route1.append(bs3);
+ route1.append(bs2);
+ route1.append(bs1);
+
+ // add trips to schedule
+ schedule.append("ted", 7, 17, &route1);
+ schedule.append("ted", 9, 38, &route1);
+ schedule.append("alice", 11, 47, &route1);
+
+ // display the complete schedule
+ simple_log_archive log(std::cout);
+ log << schedule;
+
+ delete bs0;
+ delete bs1;
+ delete bs2;
+ delete bs3;
+ return 0;
+}

Added: trunk/libs/serialization/example/demo_trivial_archive.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/serialization/example/demo_trivial_archive.cpp 2010-03-06 21:53:05 EST (Sat, 06 Mar 2010)
@@ -0,0 +1,94 @@
+/////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
+//
+// demo_trivial_archive.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)
+
+#include <cstddef> // std::size_t
+#include <boost/mpl/bool.hpp>
+
+/////////////////////////////////////////////////////////////////////////
+// class trivial_oarchive
+class trivial_oarchive {
+
+public:
+ //////////////////////////////////////////////////////////
+ // public interface used by programs that use the
+ // serialization library
+ typedef boost::mpl::bool_<true> is_saving;
+ typedef boost::mpl::bool_<false> is_loading;
+ template<class T> register_type(){}
+ template<class T> trivial_oarchive & operator<<(const T & t){
+ return *this;
+ }
+ template<class T> trivial_oarchive & operator&(const T & t){
+ return *this << t;
+ }
+ void save_binary(void *address, std::size_t count){};
+};
+
+#include "demo_gps.hpp"
+
+int main(int argc, char *argv[])
+{
+ // make the schedule
+ bus_schedule schedule;
+
+ // fill in the data
+ // make a few stops
+ bus_stop *bs0 = new bus_stop_corner(
+ gps_position(34, 135, 52.560f),
+ gps_position(134, 22, 78.30f),
+ "24th Street", "10th Avenue"
+ );
+ bus_stop *bs1 = new bus_stop_corner(
+ gps_position(35, 137, 23.456f),
+ gps_position(133, 35, 54.12f),
+ "State street", "Cathedral Vista Lane"
+ );
+ bus_stop *bs2 = new bus_stop_destination(
+ gps_position(35, 136, 15.456f),
+ gps_position(133, 32, 15.300f),
+ "White House"
+ );
+ bus_stop *bs3 = new bus_stop_destination(
+ gps_position(35, 134, 48.789f),
+ gps_position(133, 32, 16.230f),
+ "Lincoln Memorial"
+ );
+
+ // make a routes
+ bus_route route0;
+ route0.append(bs0);
+ route0.append(bs1);
+ route0.append(bs2);
+
+ // add trips to schedule
+ schedule.append("bob", 6, 24, &route0);
+ schedule.append("bob", 9, 57, &route0);
+ schedule.append("alice", 11, 02, &route0);
+
+ // make aother routes
+ bus_route route1;
+ route1.append(bs3);
+ route1.append(bs2);
+ route1.append(bs1);
+
+ // add trips to schedule
+ schedule.append("ted", 7, 17, &route1);
+ schedule.append("ted", 9, 38, &route1);
+ schedule.append("alice", 11, 47, &route1);
+
+ // display the complete schedule
+ trivial_oarchive ta;
+ ta << schedule;
+
+ delete bs0;
+ delete bs1;
+ delete bs2;
+ delete bs3;
+ return 0;
+}

Added: trunk/libs/serialization/example/log_archive.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/serialization/example/log_archive.cpp 2010-03-06 21:53:05 EST (Sat, 06 Mar 2010)
@@ -0,0 +1,32 @@
+/////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
+// log_archive.cpp:
+
+// (C) Copyright 2002 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 "log_archive.hpp"
+#include <boost/archive/detail/archive_serializer_map.hpp>
+
+// explicitly instantiate for this type of xml stream
+#include <boost/archive/impl/archive_serializer_map.ipp>
+#include <boost/archive/impl/basic_xml_oarchive.ipp>
+#include <boost/archive/impl/xml_oarchive_impl.ipp>
+
+namespace boost {
+namespace archive {
+
+template class detail::archive_serializer_map<log_archive>;
+template class basic_xml_oarchive<log_archive> ;
+template class xml_oarchive_impl<log_archive> ;
+
+} // namespace archive
+} // namespace boost

Added: trunk/libs/serialization/example/log_archive.hpp
==============================================================================
--- (empty file)
+++ trunk/libs/serialization/example/log_archive.hpp 2010-03-06 21:53:05 EST (Sat, 06 Mar 2010)
@@ -0,0 +1,76 @@
+#ifndef LOG_ARCHIVE_HPP
+#define LOG_ARCHIVE_HPP
+
+// MS compatible compilers support #pragma once
+#if defined(_MSC_VER) && (_MSC_VER >= 1020)
+# pragma once
+#endif
+
+/////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
+// log_archive.hpp
+
+// (C) Copyright 2010 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.
+
+#include <boost/archive/xml_oarchive.hpp>
+
+/////////////////////////////////////////////////////////////////////////
+// log data to an output stream. This illustrates a simpler implemenation
+// of text output which is useful for getting a formatted display of
+// any serializable class. Intended to be useful as a debugging aid.
+class log_archive :
+ /* protected ? */
+ public boost::archive::xml_oarchive_impl<log_archive>
+{
+ typedef boost::archive::xml_oarchive_impl<log_archive> base;
+ // give serialization implementation access to this clas
+ friend class boost::archive::detail::interface_oarchive<log_archive>;
+ friend class boost::archive::basic_xml_oarchive<log_archive>;
+ friend boost::archive::save_access;
+
+ /////////////////////////////////////////////////////////////////////
+ // Override functions defined in basic_xml_oarchive
+
+ // Anything not an attribute and not a name-value pair is an
+ // error and should be trapped here. Note the usage of
+ // function argument matching (BOOST_PFTO) to permit usage
+ // with compilers which fail to properly support
+ // function template ordering
+ template<class T>
+ void save_override(T & t, BOOST_PFTO int){
+ // make it a name-value pair and pass it on.
+ // this permits this to be used even with data types which
+ // are not wrapped with the name
+ base::save_override(boost::serialization::make_nvp(NULL, t), 0);
+ }
+ template<class T>
+ void save_override(const boost::serialization::nvp<T> & t, int){
+ // this is here to remove the "const" requirement. Since
+ // this class is to be used only for output, it's not required.
+ base::save_override(t, 0);
+ }
+ // specific overrides for attributes - not name value pairs so we
+ // want to trap them before the above "fall through"
+ // since we don't want to see these in the output - make them no-ops.
+ void save_override(const boost::archive::object_id_type & t, int){}
+ void save_override(const boost::archive::object_reference_type & t, int){}
+ void save_override(const boost::archive::version_type & t, int){}
+ void save_override(const boost::archive::class_id_type & t, int){}
+ void save_override(const boost::archive::class_id_optional_type & t, int){}
+ void save_override(const boost::archive::class_id_reference_type & t, int){}
+ void save_override(const boost::archive::class_name_type & t, int){}
+ void save_override(const boost::archive::tracking_type & t, int){}
+public:
+ log_archive(std::ostream & os, unsigned int flags = 0) :
+ boost::archive::xml_oarchive_impl<log_archive>(
+ os,
+ flags | boost::archive::no_header
+ )
+ {}
+};
+
+#endif // LOG_ARCHIVE_HPP

Added: trunk/libs/serialization/example/simple_log_archive.hpp
==============================================================================
--- (empty file)
+++ trunk/libs/serialization/example/simple_log_archive.hpp 2010-03-06 21:53:05 EST (Sat, 06 Mar 2010)
@@ -0,0 +1,166 @@
+#ifndef BOOST_SIMPLE_LOG_ARCHIVE_HPP
+#define BOOST_SIMPLE_LOG_ARCHIVE_HPP
+
+// MS compatible compilers support #pragma once
+#if defined(_MSC_VER) && (_MSC_VER >= 1020)
+# pragma once
+#endif
+
+/////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
+// simple_log_archive.hpp
+
+// (C) Copyright 2010 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.
+
+#include <ostream>
+#include <cstddef> // std::size_t
+
+#include <boost/config.hpp>
+#if defined(BOOST_NO_STDC_NAMESPACE)
+namespace std{
+ using ::size_t;
+} // namespace std
+#endif
+
+#include <boost/type_traits/is_enum.hpp>
+#include <boost/mpl/bool.hpp>
+#include <boost/mpl/eval_if.hpp>
+#include <boost/mpl/int.hpp>
+#include <boost/mpl/equal_to.hpp>
+#include <boost/serialization/nvp.hpp>
+#include <boost/serialization/array.hpp>
+#include <boost/serialization/string.hpp>
+#include <boost/serialization/access.hpp>
+
+/////////////////////////////////////////////////////////////////////////
+// log data to an output stream. This illustrates a simpler implemenation
+// of text output which is useful for getting a formatted display of
+// any serializable class. Intended to be useful as a debugging aid.
+class simple_log_archive {
+ std::ostream & m_os;
+ unsigned int m_depth;
+
+ template<class Archive>
+ struct save_enum_type {
+ template<class T>
+ static void invoke(Archive &ar, const T &t){
+ ar.m_os << static_cast<int>(t);
+ }
+ };
+ template<class Archive>
+ struct save_primitive {
+ template<class T>
+ static void invoke(Archive & ar, const T & t){
+ ar.m_os << t;
+ }
+ };
+ template<class Archive>
+ struct save_only {
+ template<class T>
+ static void invoke(Archive & ar, const T & t){
+ // make sure call is routed through the highest interface that might
+ // be specialized by the user.
+ boost::serialization::serialize_adl(
+ ar,
+ const_cast<T &>(t),
+ ::boost::serialization::version<T>::value
+ );
+ }
+ };
+ template<class T>
+ void save(const T &t){
+ typedef
+ BOOST_DEDUCED_TYPENAME boost::mpl::eval_if<boost::is_enum<T>,
+ boost::mpl::identity<save_enum_type<simple_log_archive> >,
+ //else
+ BOOST_DEDUCED_TYPENAME boost::mpl::eval_if<
+ // if its primitive
+ boost::mpl::equal_to<
+ boost::serialization::implementation_level<T>,
+ boost::mpl::int_<boost::serialization::primitive_type>
+ >,
+ boost::mpl::identity<save_primitive<simple_log_archive> >,
+ // else
+ boost::mpl::identity<save_only<simple_log_archive> >
+ > >::type typex;
+ typex::invoke(*this, t);
+ }
+ #ifndef BOOST_NO_STD_WSTRING
+ void save(const std::wstring &ws){
+ m_os << "wide string types not suported in log archive";
+ }
+ #endif
+
+public:
+ ///////////////////////////////////////////////////
+ // Implement requirements for archive concept
+
+ typedef boost::mpl::bool_<false> is_loading;
+ typedef boost::mpl::bool_<true> is_saving;
+
+ // this can be a no-op since we ignore pointer polymorphism
+ template<class T>
+ void register_type(const T * = NULL){}
+
+ unsigned int get_library_version(){
+ return 0;
+ }
+
+ void
+ save_binary(const void *address, std::size_t count){
+ m_os << "save_binary not implemented";
+ }
+
+ // the << operators
+ template<class T>
+ simple_log_archive & operator<<(T const & t){
+ m_os << ' ';
+ save(t);
+ return * this;
+ }
+ template<class T>
+ simple_log_archive & operator<<(T * const t){
+ m_os << " ->";
+ if(NULL == t)
+ m_os << " null";
+ else
+ *this << * t;
+ return * this;
+ }
+ template<class T, int N>
+ simple_log_archive & operator<<(const T (&t)[N]){
+ ar << serialization::make_array(
+ static_cast<value_type const*>(&t[0]),count
+ );
+ }
+ template<class T>
+ simple_log_archive & operator<<(const ::boost::serialization::nvp<T> & t){
+ m_os << '\n'; // start line with each named object
+ // indent according to object depth
+ for(unsigned int i = 0; i < m_depth; ++i)
+ m_os << ' ';
+ ++m_depth;
+ m_os << t.name(); // output the name of the object
+ * this << t.const_value();
+ --m_depth;
+ return * this;
+ }
+
+ // the & operator
+ template<class T>
+ simple_log_archive & operator&(const T & t){
+ return * this << t;
+ }
+ ///////////////////////////////////////////////
+
+ simple_log_archive(std::ostream & os) :
+ m_os(os),
+ m_depth(0)
+ {}
+};
+
+#endif // BOOST_SIMPLE_LOG_ARCHIVE_HPP


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