SerialDefinitions.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_SERIALDEFINITIONS_H
8 #define NUKLEI_SERIALDEFINITIONS_H
9 
10 #include <nuklei/Serial.h>
12 #include <nuklei/Match.h>
13 
14 #include <boost/iostreams/filtering_stream.hpp>
15 #include <boost/iostreams/filter/gzip.hpp>
16 #include <boost/iostreams/device/file.hpp>
17 
18 namespace nuklei {
19 
20  //const unsigned int bsFlags = 0;
21  //static const unsigned int bsFlags = numify<unsigned int>(getenv("BSFLAGS"));
22 
23  // To compile a serialization stub, `serialize' methods have to be
24  // instantiated through the whole class hierarchy, three times (once
25  // for each boost archive -- xml, text, bin). This is painfully
26  // slow, we no like slow, we do it once here rather than once in
27  // each Product.
28 
29  template<typename T>
30  void Serial::readObject(T &object, const std::string& filename,
31  const std::string &typeName)
32  {
33  NUKLEI_TRACE_BEGIN();
34  Type type = typeFromName<Serial>(typeName);
35 
36  NUKLEI_UNIQUE_PTR<std::istream> ifs;
37  if (type == BOOSTXML_COMPRESSED || type == BOOSTBIN_COMPRESSED)
38  {
39  {
40  std::ifstream testIfs(filename.c_str());
41  uint8_t byte1, byte2;
42  testIfs.read((char*)(&byte1), 1);
43  testIfs.read((char*)(&byte2), 1);
44  if ((byte1) != 0x1f || (byte2) != 0x8b)
45  throw SerialError("File does note appear to be GZIP");
46  }
47  NUKLEI_UNIQUE_PTR<boost::iostreams::filtering_istream>
48  decompressingIfs(new boost::iostreams::filtering_istream);
49  decompressingIfs->push(boost::iostreams::gzip_decompressor());
50  decompressingIfs->push(boost::iostreams::file_source(filename));
51  ifs = NUKLEI_MOVE(decompressingIfs);
52  }
53  else if (type == BOOSTXML || type == BOOSTBIN)
54  {
55  ifs.reset(new std::ifstream(filename.c_str()));
56  }
57  else throw SerialError(std::string("Unknown output format `") + typeName);
58 
59  NUKLEI_ASSERT(ifs->good());
60 
61  switch (type)
62  {
63  case BOOSTXML:
64  case BOOSTXML_COMPRESSED:
65  {
66  NUKLEI_SERIALIZATION_XML_IARCHIVE ia(*ifs);
67  ia & NUKLEI_SERIALIZATION_NVP(object);
68  // This check always fails:
69  //if (ifs->peek() != EOF) throw SerialError("EOF not reached");
70  break;
71  }
72  case BOOSTBIN:
73  case BOOSTBIN_COMPRESSED:
74  {
75  NUKLEI_SERIALIZATION_BINARY_IARCHIVE ia(*ifs);
76  ia & NUKLEI_SERIALIZATION_NVP(object);
77  if (ifs->peek() != EOF) throw SerialError("EOF not reached");
78  break;
79  }
80  default:
81  {
82  throw SerialError(std::string("Unknown output format `") + typeName);
83  break;
84  }
85  }
86  NUKLEI_TRACE_END();
87  }
88 
89  template<typename T>
90  void Serial::writeObject(const T &object, const std::string& filename,
91  const std::string &typeName,
92  const int precision)
93  {
94  NUKLEI_TRACE_BEGIN();
95  Type type = typeFromName<Serial>(typeName);
96 
97  NUKLEI_UNIQUE_PTR<std::ostream> ofs;
98  if (type == BOOSTXML_COMPRESSED || type == BOOSTBIN_COMPRESSED)
99  {
100  NUKLEI_UNIQUE_PTR<boost::iostreams::filtering_ostream>
101  decompressingOfs(new boost::iostreams::filtering_ostream);
102  decompressingOfs->push(boost::iostreams::gzip_compressor());
103  decompressingOfs->push(boost::iostreams::file_sink(filename));
104  ofs = NUKLEI_MOVE(decompressingOfs);
105  }
106  else if (type == BOOSTXML || type == BOOSTBIN)
107  {
108  ofs.reset(new std::ofstream(filename.c_str()));
109  }
110  else throw SerialError(std::string("Unknown output format `") + typeName);
111 
112  NUKLEI_ASSERT(ofs->good());
113  object.assertConsistency();
114 
115  switch (type)
116  {
117  case BOOSTXML:
118  case BOOSTXML_COMPRESSED:
119  {
120  NUKLEI_SERIALIZATION_XML_OARCHIVE oa(*ofs);
121  oa & NUKLEI_SERIALIZATION_NVP(object);
122  break;
123  }
124  case BOOSTBIN:
125  case BOOSTBIN_COMPRESSED:
126  {
127  NUKLEI_SERIALIZATION_BINARY_OARCHIVE oa(*ofs);
128  oa & NUKLEI_SERIALIZATION_NVP(object);
129  break;
130  }
131  default:
132  {
133  throw SerialError(std::string("Unknown output format `") + typeName + "'.");
134  break;
135  }
136  }
137  NUKLEI_TRACE_END();
138  }
139 
140 }
141 
142 #endif
143 
Public namespace.
Definition: Color.cpp:9
#define NUKLEI_ASSERT(expression)
Throws an Error if expression is not true.
Definition: Common.h:113
© 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.