VR-Forces 4.7 Class Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
DtXmlFileIo.h
Go to the documentation of this file.
1 /******************************************************************************
2 ** Copyright (c) 2017 MAK Technologies, Inc.
3 ** All rights reserved.
4 ******************************************************************************/
5 
9 
10 #ifndef DtXmlFileIo_H_
11 #define DtXmlFileIo_H_
12 
13 #include <makArchives/makArchives.h>
14 
15 #include <boost/archive/xml_iarchive.hpp>
16 #include <boost/archive/xml_oarchive.hpp>
17 #include <iostream>
18 #include <string>
19 #include <fstream>
20 
21 #include <boost/iostreams/device/mapped_file.hpp>
22 #include <boost/iostreams/stream.hpp>
23 
24 namespace makArchives
25 {
26 
30  {
31 
32  public:
33 
37  template<typename T>
38  static bool write(const std::string& filePath, const T& t, const std::string& tagName = "Record")
39  {
40  try
41  {
43  std::ofstream ofs(filePath.c_str());
44  if(!ofs.is_open())
45  {
46  return false;
47  }
48 
49  assert(ofs.good());
50  boost::archive::xml_oarchive oa(ofs);
51  oa << boost::serialization::make_nvp(tagName.c_str(), t);
52  return true;
53  }
54  catch(...)
55  {
56  return false;
57  }
58  }
59 
63  template<typename T>
64  static bool read(const std::string& filePath, T& t, const std::string& tagName = "Record")
65  {
66  try
67  {
69  boost::iostreams::stream<boost::iostreams::mapped_file_source> file_stream(filePath.c_str());
70 
71  if (file_stream.is_open())
72  {
73  boost::archive::xml_iarchive ia(file_stream);
74 
76  ia >> boost::serialization::make_nvp(tagName.c_str(), t);
77  return true;
78  }
79  else
80  {
81  return false;
82  }
83  }
84  catch( boost::archive::archive_exception& ex )
85  {
86  std::cerr << "Archive Exception during deserializing:" << filePath << std::endl;
87  std::cerr << ex.what() << std::endl;
88  return false;
89  }
90  catch(...)
91  {
92  return false;
93  }
94  }
95 
96 
97  private:
98 
100  DtXmlFileIo();
101 
102  };
103 
104 }
105 
106 #endif
107 

Document ID: Generated on Fri Apr 26 21:53:14 EDT 2019 from SVN revision 197883
Copyright © 2005-2019 VT MAK. All Rights Reserved (www.mak.com)