Chris@16: #ifndef BOOST_ARCHIVE_BASIC_TEXT_OPRIMITIVE_HPP Chris@16: #define BOOST_ARCHIVE_BASIC_TEXT_OPRIMITIVE_HPP Chris@16: Chris@16: // MS compatible compilers support #pragma once Chris@101: #if defined(_MSC_VER) Chris@16: # pragma once Chris@16: #endif Chris@16: Chris@16: /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8 Chris@16: // basic_text_oprimitive.hpp Chris@16: Chris@16: // (C) Copyright 2002 Robert Ramey - http://www.rrsd.com . Chris@16: // Use, modification and distribution is subject to the Boost Software Chris@16: // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at Chris@16: // http://www.boost.org/LICENSE_1_0.txt) Chris@16: Chris@16: // See http://www.boost.org for updates, documentation, and revision history. Chris@16: Chris@16: // archives stored as text - note these ar templated on the basic Chris@16: // stream templates to accommodate wide (and other?) kind of characters Chris@16: // Chris@16: // note the fact that on libraries without wide characters, ostream is Chris@16: // is not a specialization of basic_ostream which in fact is not defined Chris@16: // in such cases. So we can't use basic_ostream but rather Chris@16: // use two template parameters Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include // size_t Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@101: #include Chris@101: Chris@16: #if BOOST_WORKAROUND(BOOST_DINKUMWARE_STDLIB, == 1) Chris@16: #include Chris@16: #endif Chris@16: Chris@16: #if defined(BOOST_NO_STDC_NAMESPACE) Chris@16: namespace std{ Chris@16: using ::size_t; Chris@16: #if ! defined(BOOST_DINKUMWARE_STDLIB) && ! defined(__SGI_STL_PORT) Chris@16: using ::locale; Chris@16: #endif Chris@16: } // namespace std Chris@16: #endif Chris@16: Chris@101: #include Chris@101: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include // must be the last header Chris@16: Chris@16: namespace boost { Chris@16: namespace archive { Chris@16: Chris@16: ///////////////////////////////////////////////////////////////////////// Chris@16: // class basic_text_oprimitive - output of prmitives to stream Chris@16: template Chris@16: class basic_text_oprimitive Chris@16: { Chris@16: protected: Chris@16: OStream &os; Chris@16: io::ios_flags_saver flags_saver; Chris@16: io::ios_precision_saver precision_saver; Chris@16: Chris@16: #ifndef BOOST_NO_STD_LOCALE Chris@16: boost::scoped_ptr archive_locale; Chris@16: basic_streambuf_locale_saver< Chris@101: typename OStream::char_type, Chris@101: typename OStream::traits_type Chris@16: > locale_saver; Chris@16: #endif Chris@16: Chris@16: ///////////////////////////////////////////////////////// Chris@16: // fundamental types that need special treatment Chris@16: void save(const bool t){ Chris@16: // trap usage of invalid uninitialized boolean which would Chris@16: // otherwise crash on load. Chris@16: BOOST_ASSERT(0 == static_cast(t) || 1 == static_cast(t)); Chris@16: if(os.fail()) Chris@16: boost::serialization::throw_exception( Chris@16: archive_exception(archive_exception::output_stream_error) Chris@16: ); Chris@16: os << t; Chris@16: } Chris@16: void save(const signed char t) Chris@16: { Chris@16: save(static_cast(t)); Chris@16: } Chris@16: void save(const unsigned char t) Chris@16: { Chris@16: save(static_cast(t)); Chris@16: } Chris@16: void save(const char t) Chris@16: { Chris@16: save(static_cast(t)); Chris@16: } Chris@16: #ifndef BOOST_NO_INTRINSIC_WCHAR_T Chris@16: void save(const wchar_t t) Chris@16: { Chris@16: BOOST_STATIC_ASSERT(sizeof(wchar_t) <= sizeof(int)); Chris@16: save(static_cast(t)); Chris@16: } Chris@16: #endif Chris@101: Chris@101: ///////////////////////////////////////////////////////// Chris@101: // saving of any types not listed above Chris@101: Chris@101: template Chris@101: void save_impl(const T &t, boost::mpl::bool_ &){ Chris@101: if(os.fail()) Chris@101: boost::serialization::throw_exception( Chris@101: archive_exception(archive_exception::output_stream_error) Chris@101: ); Chris@101: os << t; Chris@101: } Chris@101: Chris@101: ///////////////////////////////////////////////////////// Chris@101: // floating point types need even more special treatment Chris@101: // the following determines whether the type T is some sort Chris@101: // of floating point type. Note that we then assume that Chris@101: // the stream << operator is defined on that type - if not Chris@101: // we'll get a compile time error. This is meant to automatically Chris@101: // support synthesized types which support floating point Chris@101: // operations. Also it should handle compiler dependent types Chris@101: // such long double. Due to John Maddock. Chris@101: Chris@101: template Chris@101: struct is_float { Chris@101: typedef typename mpl::bool_< Chris@101: boost::is_floating_point::value Chris@101: || (std::numeric_limits::is_specialized Chris@101: && !std::numeric_limits::is_integer Chris@101: && !std::numeric_limits::is_exact Chris@101: && std::numeric_limits::max_exponent) Chris@101: >::type type; Chris@101: }; Chris@101: Chris@101: template Chris@101: void save_impl(const T &t, boost::mpl::bool_ &){ Chris@16: // must be a user mistake - can't serialize un-initialized data Chris@16: if(os.fail()) Chris@16: boost::serialization::throw_exception( Chris@16: archive_exception(archive_exception::output_stream_error) Chris@16: ); Chris@101: // The formulae for the number of decimla digits required is given in Chris@101: // http://www2.open-std.org/JTC1/SC22/WG21/docs/papers/2005/n1822.pdf Chris@101: // which is derived from Kahan's paper: Chris@101: // www.eecs.berkeley.edu/~wkahan/ieee754status/ieee754.ps Chris@101: // const unsigned int digits = (std::numeric_limits::digits * 3010) / 10000; Chris@101: // note: I've commented out the above because I didn't get good results. e.g. Chris@101: // in one case I got a difference of 19 units. Chris@101: #ifndef BOOST_NO_CXX11_NUMERIC_LIMITS Chris@101: const unsigned int digits = std::numeric_limits::max_digits10; Chris@101: #else Chris@101: const unsigned int digits = std::numeric_limits::digits10 + 2; Chris@101: #endif Chris@101: os << std::setprecision(digits) << std::scientific << t; Chris@16: } Chris@101: Chris@101: template Chris@101: void save(const T & t){ Chris@101: boost::io::ios_flags_saver fs(os); Chris@101: boost::io::ios_precision_saver ps(os); Chris@101: typename is_float::type tf; Chris@101: save_impl(t, tf); Chris@16: } Chris@101: Chris@16: BOOST_ARCHIVE_OR_WARCHIVE_DECL(BOOST_PP_EMPTY()) Chris@16: basic_text_oprimitive(OStream & os, bool no_codecvt); Chris@16: BOOST_ARCHIVE_OR_WARCHIVE_DECL(BOOST_PP_EMPTY()) Chris@16: ~basic_text_oprimitive(); Chris@16: public: Chris@16: // unformatted append of one character Chris@101: void put(typename OStream::char_type c){ Chris@16: if(os.fail()) Chris@16: boost::serialization::throw_exception( Chris@16: archive_exception(archive_exception::output_stream_error) Chris@16: ); Chris@16: os.put(c); Chris@16: } Chris@16: // unformatted append of null terminated string Chris@16: void put(const char * s){ Chris@16: while('\0' != *s) Chris@16: os.put(*s++); Chris@16: } Chris@16: BOOST_ARCHIVE_OR_WARCHIVE_DECL(void) Chris@16: save_binary(const void *address, std::size_t count); Chris@16: }; Chris@16: Chris@16: } //namespace boost Chris@16: } //namespace archive Chris@16: Chris@16: #include // pops abi_suffix.hpp pragmas Chris@16: Chris@16: #endif // BOOST_ARCHIVE_BASIC_TEXT_OPRIMITIVE_HPP