voxelTerrain
 All Classes Functions Variables Typedefs Enumerations Pages
portable_binary_iarchive.hpp
1 #ifndef PORTABLE_BINARY_IARCHIVE_HPP
2 #define PORTABLE_BINARY_IARCHIVE_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_iarchive.hpp
16 
17 // (C) Copyright 2002-7 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 
24 #include <istream>
25 #include <boost/serialization/string.hpp>
26 #include <boost/serialization/item_version_type.hpp>
27 #include <boost/archive/archive_exception.hpp>
28 #include <boost/archive/basic_binary_iprimitive.hpp>
29 #include <boost/archive/detail/common_iarchive.hpp>
30 #include <boost/archive/shared_ptr_helper.hpp>
31 #include <boost/archive/detail/register_archive.hpp>
32 
33 #include "portable_binary_archive.hpp"
34 
36 // exception to be thrown if integer read from archive doesn't fit
37 // variable being loaded
39  public boost::archive::archive_exception
40 {
41 public:
42  typedef enum {
43  incompatible_integer_size
44  } exception_code;
45  portable_binary_iarchive_exception(exception_code c = incompatible_integer_size ) :
46  boost::archive::archive_exception(boost::archive::archive_exception::other_exception)
47  {}
48  virtual const char *what( ) const throw( )
49  {
50  const char *msg = "programmer error";
51  switch(code){
52  case incompatible_integer_size:
53  msg = "integer cannot be represented";
54  break;
55  default:
56  msg = boost::archive::archive_exception::what();
57  assert(false);
58  break;
59  }
60  return msg;
61  }
62 };
63 
65 // "Portable" input binary archive. It addresses integer size and endienness so
66 // that binary archives can be passed across systems. Note:floating point types
67 // not addressed here
69  public boost::archive::basic_binary_iprimitive<
70  portable_binary_iarchive,
71  std::istream::char_type,
72  std::istream::traits_type
73  >,
74  public boost::archive::detail::common_iarchive<
75  portable_binary_iarchive
76  >
77  ,
78  public boost::archive::detail::shared_ptr_helper
79  {
80  typedef boost::archive::basic_binary_iprimitive<
82  std::istream::char_type,
83  std::istream::traits_type
84  > primitive_base_t;
85  typedef boost::archive::detail::common_iarchive<
86  portable_binary_iarchive
87  > archive_base_t;
88 #ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
89 public:
90 #else
91  friend archive_base_t;
92  friend primitive_base_t; // since with override load below
93  friend class boost::archive::detail::interface_iarchive<
94  portable_binary_iarchive
95  >;
96  friend class boost::archive::load_access;
97 protected:
98 #endif
99  unsigned int m_flags;
100  void load_impl(boost::intmax_t & l, char maxsize);
101 
102  // default fall through for any types not specified here
103  template<class T>
104  void load(T & t){
105  boost::intmax_t l;
106  load_impl(l, sizeof(T));
107  // use cast to avoid compile time warning
108  //t = static_cast< T >(l);
109  t = T(l);
110  }
111  void load(boost::serialization::item_version_type & t){
112  boost::intmax_t l;
113  load_impl(l, sizeof(boost::serialization::item_version_type));
114  // use cast to avoid compile time warning
115  t = boost::serialization::item_version_type(l);
116  }
117  void load(boost::archive::version_type & t){
118  boost::intmax_t l;
119  load_impl(l, sizeof(boost::archive::version_type));
120  // use cast to avoid compile time warning
121  t = boost::archive::version_type(l);
122  }
123  void load(boost::archive::class_id_type & t){
124  boost::intmax_t l;
125  load_impl(l, sizeof(boost::archive::class_id_type));
126  // use cast to avoid compile time warning
127  t = boost::archive::class_id_type(static_cast<int>(l));
128  }
129  void load(std::string & t){
130  this->primitive_base_t::load(t);
131  }
132  #ifndef BOOST_NO_STD_WSTRING
133  void load(std::wstring & t){
134  this->primitive_base_t::load(t);
135  }
136  #endif
137  void load(float & t){
138  this->primitive_base_t::load(t);
139  // floats not supported
140  //BOOST_STATIC_ASSERT(false);
141  }
142  void load(double & t){
143  this->primitive_base_t::load(t);
144  // doubles not supported
145  //BOOST_STATIC_ASSERT(false);
146  }
147  void load(char & t){
148  this->primitive_base_t::load(t);
149  }
150  void load(unsigned char & t){
151  this->primitive_base_t::load(t);
152  }
153  // intermediate level to support override of operators
154  // fot templates in the absence of partial function
155  // template ordering
156  typedef boost::archive::detail::common_iarchive<portable_binary_iarchive>
157  detail_common_iarchive;
158  template<class T>
159  void load_override(T & t, BOOST_PFTO int){
160  this->detail_common_iarchive::load_override(t, 0);
161  }
162  void load_override(boost::archive::class_name_type & t, int);
163  // binary files don't include the optional information
164  void load_override(
165  boost::archive::class_id_optional_type & /* t */,
166  int
167  ){}
168 
169  void init(unsigned int flags);
170 public:
171  portable_binary_iarchive(std::istream & is, unsigned flags = 0) :
172  primitive_base_t(
173  * is.rdbuf(),
174  0 != (flags & boost::archive::no_codecvt)
175  ),
176  archive_base_t(flags),
177  m_flags(0)
178  {
179  init(flags);
180  }
181 
182  portable_binary_iarchive(
183  std::basic_streambuf<
184  std::istream::char_type,
185  std::istream::traits_type
186  > & bsb,
187  unsigned int flags
188  ) :
189  primitive_base_t(
190  bsb,
191  0 != (flags & boost::archive::no_codecvt)
192  ),
193  archive_base_t(flags),
194  m_flags(0)
195  {
196  init(flags);
197  }
198 };
199 
200 // required by export in boost version > 1.34
201 #ifdef BOOST_SERIALIZATION_REGISTER_ARCHIVE
202  BOOST_SERIALIZATION_REGISTER_ARCHIVE(portable_binary_iarchive)
203 #endif
204 
205 // required by export in boost <= 1.34
206 #define BOOST_ARCHIVE_CUSTOM_IARCHIVE_TYPES portable_binary_iarchive
207 
208 #if defined(_MSC_VER)
209 #pragma warning( pop )
210 #endif
211 
212 #endif // PORTABLE_BINARY_IARCHIVE_HPP
Definition: portable_binary_iarchive.hpp:38
Definition: portable_binary_iarchive.hpp:68