Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_34_1/boost/archive/basic_text_oprimitive.hpp @ 47

Last change on this file since 47 was 29, checked in by landauf, 16 years ago

updated boost from 1_33_1 to 1_34_1

File size: 5.4 KB
Line 
1#ifndef BOOST_ARCHIVE_BASIC_TEXT_OPRIMITIVE_HPP
2#define BOOST_ARCHIVE_BASIC_TEXT_OPRIMITIVE_HPP
3
4// MS compatible compilers support #pragma once
5#if defined(_MSC_VER) && (_MSC_VER >= 1020)
6# pragma once
7#endif
8
9/////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
10// basic_text_oprimitive.hpp
11
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)
16
17//  See http://www.boost.org for updates, documentation, and revision history.
18
19// archives stored as text - note these ar templated on the basic
20// stream templates to accommodate wide (and other?) kind of characters
21//
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
26
27#include <iomanip>
28#include <locale>
29#include <cstddef> // size_t
30#include <cmath> // isnan
31#include <cassert>
32
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
38
39#if defined(BOOST_NO_STDC_NAMESPACE)
40namespace 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
47
48#include <boost/limits.hpp>
49#include <boost/io/ios_state.hpp>
50#include <boost/scoped_ptr.hpp>
51#include <boost/throw_exception.hpp>
52#include <boost/archive/archive_exception.hpp>
53
54#include <boost/archive/detail/abi_prefix.hpp> // must be the last header
55
56namespace boost {
57namespace archive {
58
59class save_access;
60
61/////////////////////////////////////////////////////////////////////////
62// class basic_text_oprimitive - output of prmitives to stream
63template<class OStream>
64class basic_text_oprimitive
65{
66#ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
67protected:
68#else
69public:
70#endif
71    OStream &os;
72    io::ios_flags_saver flags_saver;
73    io::ios_precision_saver precision_saver;
74    boost::scoped_ptr<std::locale> archive_locale;
75    io::basic_ios_locale_saver<
76        BOOST_DEDUCED_TYPENAME OStream::char_type, BOOST_DEDUCED_TYPENAME OStream::traits_type
77    > locale_saver;
78
79    // default saving of primitives.
80    template<class T>
81    void save(const T &t){
82        if(os.fail())
83            boost::throw_exception(archive_exception(archive_exception::stream_error));
84        os << t;
85    }
86
87    /////////////////////////////////////////////////////////
88    // fundamental types that need special treatment
89    void save(const bool t){
90        // trap usage of invalid uninitialized boolean which would
91        // otherwise crash on load.
92#ifndef NDEBUG
93        int i = t;
94        assert(0 == i || 1 == i);
95#endif
96        if(os.fail())
97            boost::throw_exception(archive_exception(archive_exception::stream_error));
98        os << t;
99    }
100    void save(const signed char t)
101    {
102        if(os.fail())
103            boost::throw_exception(archive_exception(archive_exception::stream_error));
104        os << static_cast<short int>(t);
105    }
106    void save(const unsigned char t)
107    {
108        if(os.fail())
109            boost::throw_exception(archive_exception(archive_exception::stream_error));
110        os << static_cast<short unsigned int>(t);
111    }
112    void save(const char t)
113    {
114        if(os.fail())
115            boost::throw_exception(archive_exception(archive_exception::stream_error));
116        os << static_cast<short int>(t);
117    }
118    #ifndef BOOST_NO_INTRINSIC_WCHAR_T
119    void save(const wchar_t t)
120    {
121        if(os.fail())
122            boost::throw_exception(archive_exception(archive_exception::stream_error));
123        os << static_cast<int>(t);
124    }
125    #endif
126    void save(const float t)
127    {
128        // must be a user mistake - can't serialize un-initialized data
129        if(os.fail())
130            boost::throw_exception(archive_exception(archive_exception::stream_error));
131        os << std::setprecision(std::numeric_limits<float>::digits10 + 2);
132        os << t;
133    }
134    void save(const double t)
135    {
136        // must be a user mistake - can't serialize un-initialized data
137        if(os.fail())
138            boost::throw_exception(archive_exception(archive_exception::stream_error));
139        os << std::setprecision(std::numeric_limits<double>::digits10 + 2);
140        os << t;
141    }
142    BOOST_ARCHIVE_OR_WARCHIVE_DECL(BOOST_PP_EMPTY())
143    basic_text_oprimitive(OStream & os, bool no_codecvt);
144    BOOST_ARCHIVE_OR_WARCHIVE_DECL(BOOST_PP_EMPTY()) 
145    ~basic_text_oprimitive();
146public:
147    // unformatted append of one character
148    void put(int c){
149        if(os.fail())
150            boost::throw_exception(archive_exception(archive_exception::stream_error));
151        os.put(c);
152    }
153    // unformatted append of null terminated string
154    void put(const char * s){
155        if(os.fail())
156            boost::throw_exception(archive_exception(archive_exception::stream_error));
157        while('\0' != *s)
158            os.put(*s++);
159    }
160    BOOST_ARCHIVE_OR_WARCHIVE_DECL(void) 
161    save_binary(const void *address, std::size_t count);
162};
163
164} //namespace boost
165} //namespace archive
166
167#include <boost/archive/detail/abi_suffix.hpp> // pops abi_suffix.hpp pragmas
168
169#endif // BOOST_ARCHIVE_BASIC_TEXT_OPRIMITIVE_HPP
Note: See TracBrowser for help on using the repository browser.