Serialbox  2.2.0
Data serialization library and tools for C/C++, Python and Fortran
MockArchive.cpp
Go to the documentation of this file.
1 //===-- serialbox/core/archive/MockArchive.h ----------------------------------------*- 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 
17 #include <chrono>
18 
19 namespace serialbox {
20 
21 namespace {
22 
23 template <class T>
24 void fillRandom(StorageView& storageView);
25 
26 template <>
27 void fillRandom<double>(StorageView& storageView) {
28  std::minstd_rand generator(std::chrono::high_resolution_clock::now().time_since_epoch().count());
29  std::uniform_real_distribution<double> dist(-1.0, 1.0);
30  for(auto it = storageView.begin(), end = storageView.end(); it != end; ++it) {
31  it.as<double>() = dist(generator);
32  }
33 }
34 
35 template <>
36 void fillRandom<float>(StorageView& storageView) {
37  std::minstd_rand generator(std::chrono::high_resolution_clock::now().time_since_epoch().count());
38  std::uniform_real_distribution<float> dist(-1.0f, 1.0f);
39  for(auto it = storageView.begin(), end = storageView.end(); it != end; ++it)
40  it.as<float>() = dist(generator);
41 }
42 
43 template <>
44 void fillRandom<int>(StorageView& storageView) {
45  std::minstd_rand generator(std::chrono::high_resolution_clock::now().time_since_epoch().count());
46  std::uniform_int_distribution<int> dist(0, 100);
47  for(auto it = storageView.begin(), end = storageView.end(); it != end; ++it)
48  it.as<int>() = dist(generator);
49 }
50 
51 template <>
52 void fillRandom<std::int64_t>(StorageView& storageView) {
53  std::minstd_rand generator(std::chrono::high_resolution_clock::now().time_since_epoch().count());
54  std::uniform_int_distribution<std::int64_t> dist(0, 100);
55  for(auto it = storageView.begin(), end = storageView.end(); it != end; ++it)
56  it.as<std::int64_t>() = dist(generator);
57 }
58 
59 template <>
60 void fillRandom<bool>(StorageView& storageView) {
61  std::minstd_rand generator(std::chrono::high_resolution_clock::now().time_since_epoch().count());
62  std::uniform_int_distribution<int> dist(0, 1);
63  for(auto it = storageView.begin(), end = storageView.end(); it != end; ++it)
64  it.as<bool>() = (bool)dist(generator);
65 }
66 
67 } // anonymous namespace
68 
69 const std::string MockArchive::Name = "Mock";
70 
72  : mode_(mode), directory_(""), prefix_(""), metaDataFile_("") {}
73 
74 FieldID MockArchive::write(const StorageView& storageView, const std::string& fieldID,
75  const std::shared_ptr<FieldMetainfoImpl> info) {
76  throw Exception("MockArchive does not support writing");
77  return FieldID{fieldID, 0};
78 }
79 
80 void MockArchive::read(StorageView& storageView, const FieldID& fieldID,
81  std::shared_ptr<FieldMetainfoImpl> info) const {
82  switch(storageView.type()) {
83  case TypeID::Boolean:
84  fillRandom<bool>(storageView);
85  break;
86  case TypeID::Int32:
87  fillRandom<int>(storageView);
88  break;
89  case TypeID::Int64:
90  fillRandom<std::int64_t>(storageView);
91  break;
92  case TypeID::Float32:
93  fillRandom<float>(storageView);
94  break;
95  case TypeID::Float64:
96  fillRandom<double>(storageView);
97  break;
98  default:
99  serialbox_unreachable("invalid TypeID");
100  }
101 }
102 
103 std::ostream& MockArchive::toStream(std::ostream& stream) const {
104  return (stream << "MockArchive");
105 }
106 
107 } // namespace serialbox
virtual void read(StorageView &storageView, const FieldID &fieldID, std::shared_ptr< FieldMetainfoImpl > info) const override
Read the field identified by fieldID and given by storageView from disk.
Definition: MockArchive.cpp:80
Represent a mutable view to a multi-dimensional storage.
Definition: StorageView.h:33
TypeID type() const noexcept
Get type.
Definition: StorageView.h:90
Namespace of the serialbox library.
Definition: Archive.h:25
Uniquely identifiy a field.
Definition: FieldID.h:27
virtual std::ostream & toStream(std::ostream &stream) const override
Convert the archive to stream.
virtual FieldID write(const StorageView &storageView, const std::string &fieldID, const std::shared_ptr< FieldMetainfoImpl > info) override
Write the field given by storageView to disk.
Definition: MockArchive.cpp:74
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
MockArchive(OpenModeKind mode)
Initialize the archive.
Definition: MockArchive.cpp:71