1 #ifndef BOOST_SIMPLE_LOG_ARCHIVE_HPP
2 #define BOOST_SIMPLE_LOG_ARCHIVE_HPP
4 // MS compatible compilers support #pragma once
9 /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
10 // simple_log_archive.hpp
12 // (C) Copyright 2010 Robert Ramey - http://www.rrsd.com .
13 // Use, modification and distribution is subject to the Boost Software
14 // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
15 // http://www.boost.org/LICENSE_1_0.txt)
17 // See http://www.boost.org for updates, documentation, and revision history.
20 #include <cstddef> // std::size_t
22 #include <boost/config.hpp>
23 #if defined(BOOST_NO_STDC_NAMESPACE)
29 #include <boost/type_traits/is_enum.hpp>
30 #include <boost/mpl/bool.hpp>
31 #include <boost/mpl/eval_if.hpp>
32 #include <boost/mpl/int.hpp>
33 #include <boost/mpl/equal_to.hpp>
34 #include <boost/serialization/nvp.hpp>
35 #include <boost/serialization/array.hpp>
36 #include <boost/serialization/string.hpp>
37 #include <boost/serialization/access.hpp>
39 /////////////////////////////////////////////////////////////////////////
40 // log data to an output stream. This illustrates a simpler implemenation
41 // of text output which is useful for getting a formatted display of
42 // any serializable class. Intended to be useful as a debugging aid.
43 class simple_log_archive {
47 template<class Archive>
48 struct save_enum_type {
50 static void invoke(Archive &ar, const T &t){
51 ar.m_os << static_cast<int>(t);
54 template<class Archive>
55 struct save_primitive {
57 static void invoke(Archive & ar, const T & t){
61 template<class Archive>
64 static void invoke(Archive & ar, const T & t){
65 // make sure call is routed through the highest interface that might
66 // be specialized by the user.
67 boost::serialization::serialize_adl(
70 ::boost::serialization::version< T >::value
75 void save(const T &t){
77 BOOST_DEDUCED_TYPENAME boost::mpl::eval_if<boost::is_enum< T >,
78 boost::mpl::identity<save_enum_type<simple_log_archive> >,
80 BOOST_DEDUCED_TYPENAME boost::mpl::eval_if<
83 boost::serialization::implementation_level< T >,
84 boost::mpl::int_<boost::serialization::primitive_type>
86 boost::mpl::identity<save_primitive<simple_log_archive> >,
88 boost::mpl::identity<save_only<simple_log_archive> >
90 typex::invoke(*this, t);
92 #ifndef BOOST_NO_STD_WSTRING
93 void save(const std::wstring &ws){
94 m_os << "wide string types not suported in log archive";
99 ///////////////////////////////////////////////////
100 // Implement requirements for archive concept
102 typedef boost::mpl::bool_<false> is_loading;
103 typedef boost::mpl::bool_<true> is_saving;
105 // this can be a no-op since we ignore pointer polymorphism
107 void register_type(const T * = NULL){}
109 unsigned int get_library_version(){
114 save_binary(const void *address, std::size_t count){
115 m_os << "save_binary not implemented";
120 simple_log_archive & operator<<(T const & t){
126 simple_log_archive & operator<<(T * const t){
134 template<class T, int N>
135 simple_log_archive & operator<<(const T (&t)[N]){
136 return *this << boost::serialization::make_array(
137 static_cast<const T *>(&t[0]),
142 simple_log_archive & operator<<(const boost::serialization::nvp< T > & t){
143 m_os << '\n'; // start line with each named object
144 // indent according to object depth
145 for(unsigned int i = 0; i < m_depth; ++i)
148 m_os << t.name(); // output the name of the object
149 * this << t.const_value();
156 simple_log_archive & operator&(const T & t){
159 ///////////////////////////////////////////////
161 simple_log_archive(std::ostream & os) :
167 #endif // BOOST_SIMPLE_LOG_ARCHIVE_HPP