VR-Vantage API Documentation
portable_binary_oarchive.hpp
Go to the documentation of this file.
1 #ifndef PORTABLE_BINARY_OARCHIVE_HPP
2 #define PORTABLE_BINARY_OARCHIVE_HPP
3 
4 // MS compatible compilers support #pragma once
5 #if defined(_MSC_VER) && (_MSC_VER >= 1020)
6 # pragma once
7 #endif
8 
9 #if defined(_MSC_VER)
10 #pragma warning( push )
11 #pragma warning( disable : 4244 )
12 #endif
13 
15 // portable_binary_oarchive.hpp
16 
17 // (C) Copyright 2002 Robert Ramey - http://www.rrsd.com .
18 // Use, modification and distribution is subject to the Boost Software
19 // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
20 // http://www.boost.org/LICENSE_1_0.txt)
21 
22 // See http://www.boost.org for updates, documentation, and revision history.
23 
25 
26 #include <ostream>
27 #include <boost/config.hpp>
28 #include <boost/serialization/string.hpp>
29 #include <boost/archive/archive_exception.hpp>
30 #include <boost/archive/basic_binary_oprimitive.hpp>
31 #include <boost/archive/detail/common_oarchive.hpp>
32 #include <boost/archive/detail/register_archive.hpp>
33 #include <boost/archive/detail/auto_link_archive.hpp>
34 
36 
38 // exception to be thrown if integer read from archive doesn't fit
39 // variable being loaded
41  public virtual boost::archive::archive_exception
42 {
43 public:
44  typedef enum {
45  invalid_flags
46  } exception_code;
48  {}
49  virtual const char *what( ) const throw( )
50  {
51  const char *msg = "programmer error";
52  switch(code){
53  case invalid_flags:
54  msg = "cannot be both big and little endian";
55  default:
56  boost::archive::archive_exception::what();
57  }
58  return msg;
59  }
60 };
61 
63 
64 //class parchive_op
65 //{
66 //public:
67 // // the & operator
68 // portable_binary_oarchive & operator&(int & t){
69 // return * this->This() << const_cast<const int &>(t);
70 // }
71 //};
73 // "Portable" output binary archive. This is a variation of the native binary
74 // archive. it addresses integer size and endienness so that binary archives can
75 // be passed across systems. Note:floating point types not addressed here
76 
78  public boost::archive::basic_binary_oprimitive<
79  portable_binary_oarchive,
80  std::ostream::char_type,
81  std::ostream::traits_type
82  >,
83  public boost::archive::detail::common_oarchive<
84  portable_binary_oarchive
85  >
86 {
87  typedef boost::archive::basic_binary_oprimitive<
89  std::ostream::char_type,
90  std::ostream::traits_type
92  friend class boost::archive::basic_binary_oprimitive<
94  std::ostream::char_type,
95  std::ostream::traits_type
96  >;
97  typedef boost::archive::detail::common_oarchive<
98  portable_binary_oarchive
100  friend class boost::archive::detail::common_oarchive<
101  portable_binary_oarchive
102  >;
103 //#ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
104 //public:
105 //#else
106  //friend archive_base_t;
107  //friend primitive_base_t; // since with override save below
108  friend class boost::archive::detail::interface_oarchive<
109  portable_binary_oarchive
110  >;
111  friend class boost::archive::save_access;
112  //friend class parchive_op;
113 public:
114 //#endif
115  unsigned int m_flags;
116  void save_impl(const boost::intmax_t l, const char maxsize);
117  // add base class to the places considered when matching
118  // save function to a specific set of arguments. Note, this didn't
119  // work on my MSVC 7.0 system so we use the sure-fire method below
120  // using archive_base_t::save;
121 
122  // default fall through for any types not specified here
123  template<class T>
124  void save(const T & t){
125  save_impl(t, sizeof(T));
126  }
127  void save(const std::string & t){
128  this->primitive_base_t::save(t);
129  }
130  #ifndef BOOST_NO_STD_WSTRING
131  void save(const std::wstring & t){
132  this->primitive_base_t::save(t);
133  }
134  #endif
135  void save(const float & t){
136  this->primitive_base_t::save(t);
137  // floats not supported
138  //BOOST_STATIC_ASSERT(false);
139  }
140  void save(const double & t){
141  this->primitive_base_t::save(t);
142  // doubles not supported
143  //BOOST_STATIC_ASSERT(false);
144  }
145  void save(const char & t){
146  this->primitive_base_t::save(t);
147  }
148  void save(const unsigned char & t){
149  this->primitive_base_t::save(t);
150  }
151 
152  // default processing - kick back to base class. Note the
153  // extra stuff to get it passed borland compilers
154  typedef boost::archive::detail::common_oarchive<portable_binary_oarchive>
156  template<class T>
157  void save_override(T & t, BOOST_PFTO int){
158  this->detail_common_oarchive::save_override(t, 0);
159  }
160  // explicitly convert to char * to avoid compile ambiguities
161  void save_override(const boost::archive::class_name_type & t, int){
162  const std::string s(t);
163  * this << s;
164  }
165  // binary files don't include the optional information
166  void save_override(
167  const boost::archive::class_id_optional_type & /* t */,
168  int
169  ){}
170 
171  void init(unsigned int flags);
172 public:
173  portable_binary_oarchive(std::ostream & os, unsigned flags = 0) :
175  * os.rdbuf(),
176  0 != (flags & boost::archive::no_codecvt)
177  ),
178  archive_base_t(flags),
179  m_flags(flags & (endian_big | endian_little))
180  {
181  init(flags);
182  }
183 
185  std::basic_streambuf<
186  std::ostream::char_type,
187  std::ostream::traits_type
188  > & bsb,
189  unsigned int flags
190  ) :
192  bsb,
193  0 != (flags & boost::archive::no_codecvt)
194  ),
195  archive_base_t(flags),
196  m_flags(0)
197  {
198  init(flags);
199  }
200 };
201 
202 // required by export in boost version > 1.34
203 #ifdef BOOST_SERIALIZATION_REGISTER_ARCHIVE
204  BOOST_SERIALIZATION_REGISTER_ARCHIVE(portable_binary_oarchive)
205 #endif
206 
207 // required by export in boost <= 1.34
208 #define BOOST_ARCHIVE_CUSTOM_OARCHIVE_TYPES portable_binary_oarchive
209 
210 #if defined(_MSC_VER)
211 #pragma warning( pop )
212 #endif
213 
214 #endif // PORTABLE_BINARY_OARCHIVE_HPP


Copyright © 2005-2013 VT MÄK Inc. All Rights Reserved (www.mak.com)