Chris@16
|
1 #ifndef BOOST_ARCHIVE_BASIC_TEXT_OPRIMITIVE_HPP
|
Chris@16
|
2 #define BOOST_ARCHIVE_BASIC_TEXT_OPRIMITIVE_HPP
|
Chris@16
|
3
|
Chris@16
|
4 // MS compatible compilers support #pragma once
|
Chris@101
|
5 #if defined(_MSC_VER)
|
Chris@16
|
6 # pragma once
|
Chris@16
|
7 #endif
|
Chris@16
|
8
|
Chris@16
|
9 /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
|
Chris@16
|
10 // basic_text_oprimitive.hpp
|
Chris@16
|
11
|
Chris@16
|
12 // (C) Copyright 2002 Robert Ramey - http://www.rrsd.com .
|
Chris@16
|
13 // Use, modification and distribution is subject to the Boost Software
|
Chris@16
|
14 // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
|
Chris@16
|
15 // http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
16
|
Chris@16
|
17 // See http://www.boost.org for updates, documentation, and revision history.
|
Chris@16
|
18
|
Chris@16
|
19 // archives stored as text - note these ar templated on the basic
|
Chris@16
|
20 // stream templates to accommodate wide (and other?) kind of characters
|
Chris@16
|
21 //
|
Chris@16
|
22 // note the fact that on libraries without wide characters, ostream is
|
Chris@16
|
23 // is not a specialization of basic_ostream which in fact is not defined
|
Chris@16
|
24 // in such cases. So we can't use basic_ostream<OStream::char_type> but rather
|
Chris@16
|
25 // use two template parameters
|
Chris@16
|
26
|
Chris@16
|
27 #include <iomanip>
|
Chris@16
|
28 #include <locale>
|
Chris@16
|
29 #include <boost/assert.hpp>
|
Chris@16
|
30 #include <cstddef> // size_t
|
Chris@16
|
31
|
Chris@16
|
32 #include <boost/config.hpp>
|
Chris@16
|
33 #include <boost/static_assert.hpp>
|
Chris@16
|
34 #include <boost/detail/workaround.hpp>
|
Chris@101
|
35 #include <boost/io/ios_state.hpp>
|
Chris@101
|
36
|
Chris@16
|
37 #if BOOST_WORKAROUND(BOOST_DINKUMWARE_STDLIB, == 1)
|
Chris@16
|
38 #include <boost/archive/dinkumware.hpp>
|
Chris@16
|
39 #endif
|
Chris@16
|
40
|
Chris@16
|
41 #if defined(BOOST_NO_STDC_NAMESPACE)
|
Chris@16
|
42 namespace std{
|
Chris@16
|
43 using ::size_t;
|
Chris@16
|
44 #if ! defined(BOOST_DINKUMWARE_STDLIB) && ! defined(__SGI_STL_PORT)
|
Chris@16
|
45 using ::locale;
|
Chris@16
|
46 #endif
|
Chris@16
|
47 } // namespace std
|
Chris@16
|
48 #endif
|
Chris@16
|
49
|
Chris@101
|
50 #include <boost/type_traits/is_floating_point.hpp>
|
Chris@101
|
51 #include <boost/mpl/bool.hpp>
|
Chris@16
|
52 #include <boost/limits.hpp>
|
Chris@16
|
53 #include <boost/integer.hpp>
|
Chris@16
|
54 #include <boost/io/ios_state.hpp>
|
Chris@16
|
55 #include <boost/scoped_ptr.hpp>
|
Chris@16
|
56 #include <boost/serialization/throw_exception.hpp>
|
Chris@16
|
57 #include <boost/archive/archive_exception.hpp>
|
Chris@16
|
58 #include <boost/archive/basic_streambuf_locale_saver.hpp>
|
Chris@16
|
59 #include <boost/archive/detail/abi_prefix.hpp> // must be the last header
|
Chris@16
|
60
|
Chris@16
|
61 namespace boost {
|
Chris@16
|
62 namespace archive {
|
Chris@16
|
63
|
Chris@16
|
64 /////////////////////////////////////////////////////////////////////////
|
Chris@16
|
65 // class basic_text_oprimitive - output of prmitives to stream
|
Chris@16
|
66 template<class OStream>
|
Chris@16
|
67 class basic_text_oprimitive
|
Chris@16
|
68 {
|
Chris@16
|
69 protected:
|
Chris@16
|
70 OStream &os;
|
Chris@16
|
71 io::ios_flags_saver flags_saver;
|
Chris@16
|
72 io::ios_precision_saver precision_saver;
|
Chris@16
|
73
|
Chris@16
|
74 #ifndef BOOST_NO_STD_LOCALE
|
Chris@16
|
75 boost::scoped_ptr<std::locale> archive_locale;
|
Chris@16
|
76 basic_streambuf_locale_saver<
|
Chris@101
|
77 typename OStream::char_type,
|
Chris@101
|
78 typename OStream::traits_type
|
Chris@16
|
79 > locale_saver;
|
Chris@16
|
80 #endif
|
Chris@16
|
81
|
Chris@16
|
82 /////////////////////////////////////////////////////////
|
Chris@16
|
83 // fundamental types that need special treatment
|
Chris@16
|
84 void save(const bool t){
|
Chris@16
|
85 // trap usage of invalid uninitialized boolean which would
|
Chris@16
|
86 // otherwise crash on load.
|
Chris@16
|
87 BOOST_ASSERT(0 == static_cast<int>(t) || 1 == static_cast<int>(t));
|
Chris@16
|
88 if(os.fail())
|
Chris@16
|
89 boost::serialization::throw_exception(
|
Chris@16
|
90 archive_exception(archive_exception::output_stream_error)
|
Chris@16
|
91 );
|
Chris@16
|
92 os << t;
|
Chris@16
|
93 }
|
Chris@16
|
94 void save(const signed char t)
|
Chris@16
|
95 {
|
Chris@16
|
96 save(static_cast<short int>(t));
|
Chris@16
|
97 }
|
Chris@16
|
98 void save(const unsigned char t)
|
Chris@16
|
99 {
|
Chris@16
|
100 save(static_cast<short unsigned int>(t));
|
Chris@16
|
101 }
|
Chris@16
|
102 void save(const char t)
|
Chris@16
|
103 {
|
Chris@16
|
104 save(static_cast<short int>(t));
|
Chris@16
|
105 }
|
Chris@16
|
106 #ifndef BOOST_NO_INTRINSIC_WCHAR_T
|
Chris@16
|
107 void save(const wchar_t t)
|
Chris@16
|
108 {
|
Chris@16
|
109 BOOST_STATIC_ASSERT(sizeof(wchar_t) <= sizeof(int));
|
Chris@16
|
110 save(static_cast<int>(t));
|
Chris@16
|
111 }
|
Chris@16
|
112 #endif
|
Chris@101
|
113
|
Chris@101
|
114 /////////////////////////////////////////////////////////
|
Chris@101
|
115 // saving of any types not listed above
|
Chris@101
|
116
|
Chris@101
|
117 template<class T>
|
Chris@101
|
118 void save_impl(const T &t, boost::mpl::bool_<false> &){
|
Chris@101
|
119 if(os.fail())
|
Chris@101
|
120 boost::serialization::throw_exception(
|
Chris@101
|
121 archive_exception(archive_exception::output_stream_error)
|
Chris@101
|
122 );
|
Chris@101
|
123 os << t;
|
Chris@101
|
124 }
|
Chris@101
|
125
|
Chris@101
|
126 /////////////////////////////////////////////////////////
|
Chris@101
|
127 // floating point types need even more special treatment
|
Chris@101
|
128 // the following determines whether the type T is some sort
|
Chris@101
|
129 // of floating point type. Note that we then assume that
|
Chris@101
|
130 // the stream << operator is defined on that type - if not
|
Chris@101
|
131 // we'll get a compile time error. This is meant to automatically
|
Chris@101
|
132 // support synthesized types which support floating point
|
Chris@101
|
133 // operations. Also it should handle compiler dependent types
|
Chris@101
|
134 // such long double. Due to John Maddock.
|
Chris@101
|
135
|
Chris@101
|
136 template<class T>
|
Chris@101
|
137 struct is_float {
|
Chris@101
|
138 typedef typename mpl::bool_<
|
Chris@101
|
139 boost::is_floating_point<T>::value
|
Chris@101
|
140 || (std::numeric_limits<T>::is_specialized
|
Chris@101
|
141 && !std::numeric_limits<T>::is_integer
|
Chris@101
|
142 && !std::numeric_limits<T>::is_exact
|
Chris@101
|
143 && std::numeric_limits<T>::max_exponent)
|
Chris@101
|
144 >::type type;
|
Chris@101
|
145 };
|
Chris@101
|
146
|
Chris@101
|
147 template<class T>
|
Chris@101
|
148 void save_impl(const T &t, boost::mpl::bool_<true> &){
|
Chris@16
|
149 // must be a user mistake - can't serialize un-initialized data
|
Chris@16
|
150 if(os.fail())
|
Chris@16
|
151 boost::serialization::throw_exception(
|
Chris@16
|
152 archive_exception(archive_exception::output_stream_error)
|
Chris@16
|
153 );
|
Chris@101
|
154 // The formulae for the number of decimla digits required is given in
|
Chris@101
|
155 // http://www2.open-std.org/JTC1/SC22/WG21/docs/papers/2005/n1822.pdf
|
Chris@101
|
156 // which is derived from Kahan's paper:
|
Chris@101
|
157 // www.eecs.berkeley.edu/~wkahan/ieee754status/ieee754.ps
|
Chris@101
|
158 // const unsigned int digits = (std::numeric_limits<T>::digits * 3010) / 10000;
|
Chris@101
|
159 // note: I've commented out the above because I didn't get good results. e.g.
|
Chris@101
|
160 // in one case I got a difference of 19 units.
|
Chris@101
|
161 #ifndef BOOST_NO_CXX11_NUMERIC_LIMITS
|
Chris@101
|
162 const unsigned int digits = std::numeric_limits<T>::max_digits10;
|
Chris@101
|
163 #else
|
Chris@101
|
164 const unsigned int digits = std::numeric_limits<T>::digits10 + 2;
|
Chris@101
|
165 #endif
|
Chris@101
|
166 os << std::setprecision(digits) << std::scientific << t;
|
Chris@16
|
167 }
|
Chris@101
|
168
|
Chris@101
|
169 template<class T>
|
Chris@101
|
170 void save(const T & t){
|
Chris@101
|
171 boost::io::ios_flags_saver fs(os);
|
Chris@101
|
172 boost::io::ios_precision_saver ps(os);
|
Chris@101
|
173 typename is_float<T>::type tf;
|
Chris@101
|
174 save_impl(t, tf);
|
Chris@16
|
175 }
|
Chris@101
|
176
|
Chris@16
|
177 BOOST_ARCHIVE_OR_WARCHIVE_DECL(BOOST_PP_EMPTY())
|
Chris@16
|
178 basic_text_oprimitive(OStream & os, bool no_codecvt);
|
Chris@16
|
179 BOOST_ARCHIVE_OR_WARCHIVE_DECL(BOOST_PP_EMPTY())
|
Chris@16
|
180 ~basic_text_oprimitive();
|
Chris@16
|
181 public:
|
Chris@16
|
182 // unformatted append of one character
|
Chris@101
|
183 void put(typename OStream::char_type c){
|
Chris@16
|
184 if(os.fail())
|
Chris@16
|
185 boost::serialization::throw_exception(
|
Chris@16
|
186 archive_exception(archive_exception::output_stream_error)
|
Chris@16
|
187 );
|
Chris@16
|
188 os.put(c);
|
Chris@16
|
189 }
|
Chris@16
|
190 // unformatted append of null terminated string
|
Chris@16
|
191 void put(const char * s){
|
Chris@16
|
192 while('\0' != *s)
|
Chris@16
|
193 os.put(*s++);
|
Chris@16
|
194 }
|
Chris@16
|
195 BOOST_ARCHIVE_OR_WARCHIVE_DECL(void)
|
Chris@16
|
196 save_binary(const void *address, std::size_t count);
|
Chris@16
|
197 };
|
Chris@16
|
198
|
Chris@16
|
199 } //namespace boost
|
Chris@16
|
200 } //namespace archive
|
Chris@16
|
201
|
Chris@16
|
202 #include <boost/archive/detail/abi_suffix.hpp> // pops abi_suffix.hpp pragmas
|
Chris@16
|
203
|
Chris@16
|
204 #endif // BOOST_ARCHIVE_BASIC_TEXT_OPRIMITIVE_HPP
|