Serial.h
Go to the documentation of this file.
1 // (C) Copyright Renaud Detry 2007-2015.
2 // Distributed under the GNU General Public License and under the
3 // BSD 3-Clause License (See accompanying file LICENSE.txt).
4 
5 /** @file */
6 
7 #ifndef NUKLEI_SERIAL_H
8 #define NUKLEI_SERIAL_H
9 
10 #include <string>
11 #include <list>
12 #include <nuklei/Common.h>
13 
14 namespace nuklei {
15 
16  class SerialError : public Error
17  {
18  public:
19  SerialError(const std::string &s) : Error(s) {}
20  };
21 
22  struct Serial
23  {
24 
25  // The template methods of this class are defined in Serial.cpp.
26  // One will then instantiate them as needed in that same file.
27  // The reason for this is the dramatically long compilation time,
28  // due to the effort in processing the boost serialization library,
29  // and also due to the depth of the class hierarchy (each of these
30  // functions instantiates the serialize() template methods of almost
31  // all classes in the Nuklei library -- from Graph to Location...).
32 
33  template<typename T>
34  static void readObject(T &object, const std::string& filename,
35  const std::string &typeName);
36 
37  template<typename T>
38  static void readObject(T &object, const std::string& filename)
39  {
40  NUKLEI_TRACE_BEGIN();
41  std::string errorsCat = std::string("Error at input for file `") +
42  filename + "'\nErrors at each format attempt were:";
43 
44  // ideally, each block should try and catch
45  // boost::archive::archive_exception's.
46 
47  std::list<std::string> formats;
48 
49  // Parsing a non-bbin(c) file with the bbin(c) readers now prints
50  // a malloc error.
51  // Disabling bbin in automatic parsing.
52  // bbin can still be read if specifically requested in method above.
53  formats.push_back("bbinc");
54  formats.push_back("bxmlc");
55  //formats.push_back("bbin");
56  formats.push_back("bxml");
57 
58  for (std::list<std::string>::const_iterator i = formats.begin();
59  i != formats.end(); ++i)
60  {
61  try {
62  readObject<T>(object, filename, *i);
63  return;
64  } catch (std::exception &e) {
65  errorsCat += "\n" + std::string(e.what());
66  }
67  }
68 
69  throw SerialError(errorsCat);
70  NUKLEI_TRACE_END();
71  }
72 
73  template<typename T>
74  static void writeObject(const T &object, const std::string& filename,
75  const std::string &typeName = SERIALIZATION_DEFAULT_BOOST_ARCHIVE,
76  const int precision = PRECISION);
77 
78  typedef enum { BOOSTXML = 0, BOOSTXML_COMPRESSED,
79  BOOSTBIN, BOOSTBIN_COMPRESSED,
80  UNKNOWN } Type;
81  static const Type defaultType = BOOSTBIN_COMPRESSED;
82  static const std::string TypeNames[];
83  };
84 
85 }
86 
87 #endif
88 
Public namespace.
Definition: Color.cpp:9
Nuklei-related errors.
Definition: Common.h:205
Definition: Serial.h:16
Definition: Serial.h:22
© Copyright 2007-2013 Renaud Detry.
Distributed under the terms of the GNU General Public License (GPL).
(See accompanying file LICENSE.txt or copy at http://www.gnu.org/copyleft/gpl.html.)
Revised Sun Sep 13 2020 19:10:06.