VR-Forces Developer's Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
DtBinaryFileIo.h
Go to the documentation of this file.
1 /******************************************************************************
2 ** Copyright (c) 2026 MAK Technologies, Inc.
3 ** All rights reserved.
4 ******************************************************************************/
5 
9 
10 #ifndef DtBinaryFileIo_H_
11 #define DtBinaryFileIo_H_
12 
13 #include <makArchives/makArchives.h>
14 
15 #include <boost/archive/binary_iarchive.hpp>
16 #include <boost/archive/binary_oarchive.hpp>
17 #include <cstdio>
18 #include <iostream>
19 #include <fstream>
20 
21 namespace makArchives
22 {
23 
27  {
28 
29  public:
30 
34  template<typename T>
35  static bool write(const std::string& filePath, const T& t, const std::string& tagName = "Record")
36  {
37  // Write to a temp file first so that a crash or power loss during
38  // the write leaves the original file intact. The rename at the end
39  // is a metadata-only operation and is much shorter than the write.
40  std::string tmpPath = filePath + ".tmp";
41  try
42  {
43  {
44  std::ofstream ofs(tmpPath.c_str(), std::ios::out|std::ios::binary);
45  if(!ofs.is_open())
46  {
47  return false;
48  }
49 
50  assert(ofs.good());
51  boost::archive::binary_oarchive oa(ofs);
52  oa << boost::serialization::make_nvp(tagName.c_str(), t);
53  }
54  // On Windows std::rename fails when the destination exists,
55  // so remove it first. The window between the two calls is tiny.
56  std::remove(filePath.c_str());
57  if(std::rename(tmpPath.c_str(), filePath.c_str()) != 0)
58  {
59  std::remove(tmpPath.c_str());
60  return false;
61  }
62  return true;
63  }
64  catch(...)
65  {
66  std::remove(tmpPath.c_str());
67  return false;
68  }
69  }
70 
74  template<typename T>
75  static bool read(const std::string& filePath, T& t, const std::string& tagName = "Record")
76  {
77  try
78  {
80  std::ifstream ifs(filePath.c_str(), std::ios::in|std::ios::binary);
81  if(!ifs.is_open())
82  {
83  return false;
84  }
85 
86  assert(ifs.good());
87  boost::archive::binary_iarchive ia(ifs);
89  ia >> boost::serialization::make_nvp(tagName.c_str(), t);
90  return true;
91  }
92  catch(...)
93  {
94  return false;
95  }
96  }
97 
98 
99  private:
100 
102  DtBinaryFileIo();
103 
104  };
105 }
106 
107 #endif
108 
static bool write(const std::string &filePath, const T &t, const std::string &tagName="Record")
Definition: DtBinaryFileIo.h:35
Contains DLL export macros.
A DtBinaryFileIo has the static functions for reading and writing to an Binary archive.
Definition: DtBinaryFileIo.h:26
static bool read(const std::string &filePath, T &t, const std::string &tagName="Record")
Definition: DtBinaryFileIo.h:75
#define DT_DLL_MAKARCHIVES
Definition: makArchives.h:31

Document ID: Generated on Thu Oct 15 21:27:02 EDT 2026 from SVN revision 296421
Copyright © 2005-2026 MAK Technologies. All Rights Reserved (www.mak.com)