Serialbox  2.2.0
Data serialization library and tools for C/C++, Python and Fortran
ArchiveFactory.cpp
Go to the documentation of this file.
1 //===-- serialbox/core/archive/ArchiveFactory.cpp -----------------------------------*- C++ -*-===//
2 //
3 // S E R I A L B O X
4 //
5 // This file is distributed under terms of BSD license.
6 // See LICENSE.txt for more information
7 //
8 //===------------------------------------------------------------------------------------------===//
9 //
13 //===------------------------------------------------------------------------------------------===//
14 
21 
22 namespace serialbox {
23 
24 std::unique_ptr<Archive> ArchiveFactory::create(const std::string& name, OpenModeKind mode,
25  const std::string& directory,
26  const std::string& prefix) {
27  if(name == BinaryArchive::Name) {
28  return std::make_unique<BinaryArchive>(mode, directory, prefix);
29  } else if(name == MockArchive::Name) {
30  return std::make_unique<MockArchive>(mode);
31 #ifdef SERIALBOX_HAS_NETCDF
32  } else if(name == NetCDFArchive::Name) {
33  return std::make_unique<NetCDFArchive>(mode, directory, prefix);
34 #endif
35  } else {
36  std::stringstream ss;
37  ss << "cannot create Archive '" << name << "': archive does not exist or is not registred.\n";
38  ss << "Registered archives:\n";
39  for(const auto& archive : ArchiveFactory::registeredArchives())
40  ss << " " << archive << "\n";
41  throw Exception(ss.str().c_str());
42  }
43 }
44 
45 std::vector<std::string> ArchiveFactory::registeredArchives() {
46  std::vector<std::string> archives{BinaryArchive::Name, MockArchive::Name
47 #ifdef SERIALBOX_HAS_NETCDF
48  ,
50 #endif
51  };
52  return archives;
53 }
54 
55 std::string ArchiveFactory::archiveFromExtension(std::string filename) {
56  std::string extension = filesystem::path(filename).extension().string();
57 
58  if(extension == ".dat" || extension == ".bin")
59  return BinaryArchive::Name;
60 #ifdef SERIALBOX_HAS_NETCDF
61  else if(extension == ".nc")
62  return NetCDFArchive::Name;
63 #endif
64  else
65  throw Exception("cannot deduce Archive from file extension: %s", filename);
66  serialbox_unreachable("invalid file extension");
67 }
68 
69 //===------------------------------------------------------------------------------------------===//
70 // Writing
71 //===------------------------------------------------------------------------------------------===//
72 
73 void ArchiveFactory::writeToFile(std::string filename, const StorageView& storageView,
74  std::string archiveName, std::string fieldname) {
75 
76  LOG(info) << "Attempting to write field \"" << fieldname << "\" via \"" << archiveName
77  << "\" archive from " << filename;
78 
79  if(archiveName == BinaryArchive::Name) {
80  BinaryArchive::writeToFile(filename, storageView);
81  }
82 #ifdef SERIALBOX_HAS_NETCDF
83  else if(archiveName == NetCDFArchive::Name) {
84  NetCDFArchive::writeToFile(filename, storageView, fieldname);
85  }
86 #endif
87  else
88  throw Exception("cannot use Archive '%s': archive does not exist or is not registred",
89  filename);
90 
91  LOG(info) << "Successfully wrote field \"" << fieldname << "\" to " << filename;
92 }
93 
94 //===------------------------------------------------------------------------------------------===//
95 // Reading
96 //===------------------------------------------------------------------------------------------===//
97 
98 void ArchiveFactory::readFromFile(std::string filename, StorageView& storageView,
99  std::string archiveName, std::string fieldname) {
100 
101  LOG(info) << "Attempting to read field \"" << fieldname << "\" via \"" << archiveName
102  << "\" archive from " << filename;
103 
104  if(archiveName == BinaryArchive::Name) {
105  BinaryArchive::readFromFile(filename, storageView);
106  }
107 #ifdef SERIALBOX_HAS_NETCDF
108  else if(archiveName == NetCDFArchive::Name) {
109  NetCDFArchive::readFromFile(filename, storageView, fieldname);
110  }
111 #endif
112  else
113  throw Exception("cannot use Archive '%s': archive does not exist or is not registred",
114  filename);
115 
116  LOG(info) << "Successfully read field \"" << fieldname << "\" to " << filename;
117 }
118 
119 } // namespace serialbox
static std::string archiveFromExtension(std::string filename)
Deduce the name of the archive according to the extension of the filename
static void writeToFile(std::string filename, const StorageView &storageView)
Directly write field (given by storageView) to file.
static const std::string Name
Name of the binary archive.
Definition: BinaryArchive.h:35
static void readFromFile(std::string filename, StorageView &storageView)
Directly read field (given by storageView) from file.
static const std::string Name
Name of the NetCDF archive.
Definition: NetCDFArchive.h:38
#define LOG(severity)
Logging infrastructure.
Definition: Logging.h:102
Represent a mutable view to a multi-dimensional storage.
Definition: StorageView.h:33
static std::vector< std::string > registeredArchives()
Get a vector of strings of the registered archives.
Namespace of the serialbox library.
Definition: Archive.h:25
static void writeToFile(std::string filename, const StorageView &storageView, const std::string &field)
Directly write field (given by storageView) to file.
static std::unique_ptr< Archive > create(const std::string &name, OpenModeKind mode, const std::string &directory, const std::string &prefix)
Construct an instance of the archive ´name´
static void readFromFile(std::string filename, StorageView &storageView, std::string archiveName, std::string fieldname)
Directly read field (given by storageView) from file.
static void writeToFile(std::string filename, const StorageView &storageView, std::string archiveName, std::string fieldname)
Directly write field (given by storageView) to file.
static void readFromFile(std::string filename, StorageView &storageView, const std::string &field)
Directly read field (given by storageView) from file.
OpenModeKind
Policy for opening files in the Serializer and Archive.
Definition: Type.h:40
static const std::string Name
Name of the Mock archive.
Definition: MockArchive.h:36
#define serialbox_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition: Unreachable.h:41
Exception class which stores a human-readable error description.
Definition: Exception.h:30