Imported Upstream version 1.72.0
[platform/upstream/boost.git] / libs / serialization / example / portable_binary_oarchive.cpp
1 /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
2 // portable_binary_oarchive.cpp
3
4 // (C) Copyright 2002-7 Robert Ramey - http://www.rrsd.com . 
5 // Use, modification and distribution is subject to the Boost Software
6 // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
7 // http://www.boost.org/LICENSE_1_0.txt)
8
9 //  See http://www.boost.org for updates, documentation, and revision history.
10
11 #include <ostream>
12 #include <boost/predef/other/endian.h>
13 #include "portable_binary_oarchive.hpp"
14
15 void 
16 portable_binary_oarchive::save_impl(
17     const boost::intmax_t l,
18     const char maxsize
19 ){
20     char size = 0;
21
22     if(l == 0){
23         this->primitive_base_t::save(size);
24         return;
25     }
26
27     boost::intmax_t ll;
28     bool negative = (l < 0);
29     if(negative)
30         ll = -l;
31     else
32         ll = l;
33
34     do{
35         ll >>= CHAR_BIT;
36         ++size;
37     }while(ll != 0);
38
39     this->primitive_base_t::save(
40         static_cast<char>(negative ? -size : size)
41     );
42
43     if(negative)
44         ll = -l;
45     else
46         ll = l;
47     char * cptr = reinterpret_cast<char *>(& ll);
48     #if BOOST_ENDIAN_BIG_BYTE
49         cptr += (sizeof(boost::intmax_t) - size);
50         if(m_flags & endian_little)
51             reverse_bytes(size, cptr);
52     #else
53         if(m_flags & endian_big)
54             reverse_bytes(size, cptr);
55     #endif
56     this->primitive_base_t::save_binary(cptr, size);
57 }
58
59 void 
60 portable_binary_oarchive::init(unsigned int flags) {
61     if(m_flags == (endian_big | endian_little)){
62         boost::serialization::throw_exception(
63             portable_binary_oarchive_exception()
64         );
65     }
66     if(0 == (flags & boost::archive::no_header)){
67         // write signature in an archive version independent manner
68         const std::string file_signature(
69             boost::archive::BOOST_ARCHIVE_SIGNATURE()
70         );
71         * this << file_signature;
72         // write library version
73         const boost::archive::library_version_type v(
74             boost::archive::BOOST_ARCHIVE_VERSION()
75         );
76         * this << v;
77     }
78     save(static_cast<unsigned char>(m_flags >> CHAR_BIT));
79 }
80
81 #include <boost/archive/impl/archive_serializer_map.ipp>
82 #include <boost/archive/impl/basic_binary_oprimitive.ipp>
83
84 namespace boost {
85 namespace archive {
86
87 namespace detail {
88     template class archive_serializer_map<portable_binary_oarchive>;
89 }
90
91 template class basic_binary_oprimitive<
92     portable_binary_oarchive,
93     std::ostream::char_type, 
94     std::ostream::traits_type
95 > ;
96
97 } // namespace archive
98 } // namespace boost