fix doc example typo
[boost.git] / boost / archive / basic_text_oprimitive.hpp
blobba8770959e64601807d31384287bd8b51cdcc12e
1 #ifndef BOOST_ARCHIVE_BASIC_TEXT_OPRIMITIVE_HPP
2 #define BOOST_ARCHIVE_BASIC_TEXT_OPRIMITIVE_HPP
4 // MS compatible compilers support #pragma once
5 #if defined(_MSC_VER) && (_MSC_VER >= 1020)
6 # pragma once
7 #endif
9 /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
10 // basic_text_oprimitive.hpp
12 // (C) Copyright 2002 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.
19 // archives stored as text - note these ar templated on the basic
20 // stream templates to accommodate wide (and other?) kind of characters
22 // note the fact that on libraries without wide characters, ostream is
23 // is not a specialization of basic_ostream which in fact is not defined
24 // in such cases. So we can't use basic_ostream<OStream::char_type> but rather
25 // use two template parameters
27 #include <iomanip>
28 #include <locale>
29 #include <boost/config/no_tr1/cmath.hpp> // isnan
30 #include <cassert>
31 #include <cstddef> // size_t
33 #include <boost/config.hpp>
34 #include <boost/detail/workaround.hpp>
35 #if BOOST_WORKAROUND(BOOST_DINKUMWARE_STDLIB, == 1)
36 #include <boost/archive/dinkumware.hpp>
37 #endif
39 #if defined(BOOST_NO_STDC_NAMESPACE)
40 namespace std{
41 using ::size_t;
42 #if ! defined(BOOST_DINKUMWARE_STDLIB) && ! defined(__SGI_STL_PORT)
43 using ::locale;
44 #endif
45 } // namespace std
46 #endif
48 #include <boost/limits.hpp>
49 #include <boost/io/ios_state.hpp>
50 #include <boost/scoped_ptr.hpp>
51 #include <boost/serialization/throw_exception.hpp>
53 #include <boost/archive/archive_exception.hpp>
55 #include <boost/archive/detail/abi_prefix.hpp> // must be the last header
57 namespace boost {
58 namespace archive {
60 class save_access;
62 /////////////////////////////////////////////////////////////////////////
63 // class basic_text_oprimitive - output of prmitives to stream
64 template<class OStream>
65 class basic_text_oprimitive
67 #ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
68 protected:
69 #else
70 public:
71 #endif
72 OStream &os;
73 io::ios_flags_saver flags_saver;
74 io::ios_precision_saver precision_saver;
76 #ifndef BOOST_NO_STD_LOCALE
77 boost::scoped_ptr<std::locale> archive_locale;
78 io::basic_ios_locale_saver<
79 BOOST_DEDUCED_TYPENAME OStream::char_type, BOOST_DEDUCED_TYPENAME OStream::traits_type
80 > locale_saver;
81 #endif
83 // default saving of primitives.
84 template<class T>
85 void save(const T &t){
86 if(os.fail())
87 boost::serialization::throw_exception(
88 archive_exception(archive_exception::stream_error)
90 os << t;
93 /////////////////////////////////////////////////////////
94 // fundamental types that need special treatment
95 void save(const bool t){
96 // trap usage of invalid uninitialized boolean which would
97 // otherwise crash on load.
98 assert(0 == static_cast<int>(t) || 1 == static_cast<int>(t));
99 if(os.fail())
100 boost::serialization::throw_exception(
101 archive_exception(archive_exception::stream_error)
103 os << t;
105 void save(const signed char t)
107 if(os.fail())
108 boost::serialization::throw_exception(
109 archive_exception(archive_exception::stream_error)
111 os << static_cast<short int>(t);
113 void save(const unsigned char t)
115 if(os.fail())
116 boost::serialization::throw_exception(
117 archive_exception(archive_exception::stream_error)
119 os << static_cast<short unsigned int>(t);
121 void save(const char t)
123 if(os.fail())
124 boost::serialization::throw_exception(
125 archive_exception(archive_exception::stream_error)
127 os << static_cast<short int>(t);
129 #ifndef BOOST_NO_INTRINSIC_WCHAR_T
130 void save(const wchar_t t)
132 if(os.fail())
133 boost::serialization::throw_exception(
134 archive_exception(archive_exception::stream_error)
136 os << static_cast<int>(t);
138 #endif
139 void save(const float t)
141 // must be a user mistake - can't serialize un-initialized data
142 if(os.fail())
143 boost::serialization::throw_exception(
144 archive_exception(archive_exception::stream_error)
146 os << std::setprecision(std::numeric_limits<float>::digits10 + 2);
147 os << t;
149 void save(const double t)
151 // must be a user mistake - can't serialize un-initialized data
152 if(os.fail())
153 boost::serialization::throw_exception(
154 archive_exception(archive_exception::stream_error)
156 os << std::setprecision(std::numeric_limits<double>::digits10 + 2);
157 os << t;
159 BOOST_ARCHIVE_OR_WARCHIVE_DECL(BOOST_PP_EMPTY())
160 basic_text_oprimitive(OStream & os, bool no_codecvt);
161 BOOST_ARCHIVE_OR_WARCHIVE_DECL(BOOST_PP_EMPTY())
162 ~basic_text_oprimitive();
163 public:
164 // unformatted append of one character
165 void put(int c){
166 if(os.fail())
167 boost::serialization::throw_exception(
168 archive_exception(archive_exception::stream_error)
170 os.put(c);
172 // unformatted append of null terminated string
173 void put(const char * s){
174 if(os.fail())
175 boost::serialization::throw_exception(
176 archive_exception(archive_exception::stream_error)
178 while('\0' != *s)
179 os.put(*s++);
181 BOOST_ARCHIVE_OR_WARCHIVE_DECL(void)
182 save_binary(const void *address, std::size_t count);
185 } //namespace boost
186 } //namespace archive
188 #include <boost/archive/detail/abi_suffix.hpp> // pops abi_suffix.hpp pragmas
190 #endif // BOOST_ARCHIVE_BASIC_TEXT_OPRIMITIVE_HPP