1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
|
#ifndef BOOST_SIMPLE_LOG_ARCHIVE_HPP
#define BOOST_SIMPLE_LOG_ARCHIVE_HPP
// MS compatible compilers support #pragma once
#if defined(_MSC_VER)
# 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]){
return *this << boost::serialization::make_array(
static_cast<const T *>(&t[0]),
N
);
}
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
|