VR-Forces Developer's Guide
 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  assert(ofs.good());
49 
50  boost::archive::xml_oarchive oa(ofs);
51  oa << boost::serialization::make_nvp(tagName.c_str(), t);
52  // xml_oarchive destructor appends closing tag
53  }
54  catch(...)
55  {
56  return false;
57  }
58 
59  return true;
60  }
61 
65  template<typename T>
66  static bool read(const std::string& filePath, T& t, const std::string& tagName = "Record")
67  {
68  try
69  {
71  std::ifstream ifs(filePath.c_str());
72  if (!ifs.is_open())
73  {
74  return false;
75  }
76  assert(ifs.good());
77 
78  boost::archive::xml_iarchive ia(ifs);
80  ia >> boost::serialization::make_nvp(tagName.c_str(), t);
81  }
82  catch (boost::archive::archive_exception& ex)
83  {
84  std::cerr << "Archive Exception during deserializing:" << filePath << std::endl;
85  std::cerr << ex.what() << std::endl;
86  return false;
87  }
88  catch (...)
89  {
90  return false;
91  }
92 
93  return true;
94  }
95 
96  private:
97 
99  DtXmlFileIo();
100 
101  };
102 
103 }
104 
105 #endif
106 
Contains DLL export macros.
static bool read(const std::string &filePath, T &t, const std::string &tagName="Record")
Definition: DtXmlFileIo.h:66
A DtXmlFileIo has the static functions for reading and writing to an XML archive. ...
Definition: DtXmlFileIo.h:29
#define DT_DLL_MAKARCHIVES
Definition: makArchives.h:31
static bool write(const std::string &filePath, const T &t, const std::string &tagName="Record")
Definition: DtXmlFileIo.h:38

Document ID: Generated on Thu Oct 23 22:29:17 EDT 2025 from SVN revision 280951
Copyright © 2005-2024 MAK Technologies. All Rights Reserved (www.mak.com)