HDTree  0.5.2
HDTree C++ API
MapBranch.h
1 #pragma once
2 
3 namespace hdtree {
4 
17 template <typename KeyType, typename ValType>
18 class Branch<std::map<KeyType, ValType>>
19  : public AbstractBranch<std::map<KeyType, ValType>> {
21 
22  public:
30  explicit Branch(const std::string& branch_name,
31  std::map<KeyType, ValType>* handle = nullptr)
32  : AbstractBranch<std::map<KeyType, ValType>>(branch_name, handle),
33  size_{branch_name + "/" + constants::SIZE_NAME},
34  keys_{branch_name + "/keys"},
35  vals_{branch_name + "/vals"} {}
36 
47  void load() final override {
48  size_.load();
49  for (std::size_t i_map{0}; i_map < size_.get(); i_map++) {
50  keys_.load();
51  vals_.load();
52  this->handle_->emplace(keys_.get(), vals_.get());
53  }
54  }
55 
56  void attach(Reader& f) final override {
57  this->load_type_ = f.type(this->name_);
58  size_.attach(f);
59  keys_.attach(f);
60  vals_.attach(f);
61  }
62 
72  void save() final override {
73  size_.update(this->handle_->size());
74  size_.save();
75  for (auto const& [key, val] : *(this->handle_)) {
76  keys_.update(key);
77  keys_.save();
78  vals_.update(val);
79  vals_.save();
80  }
81  }
82 
83  void attach(Writer& f) final override {
84  f.structure(this->name_, this->save_type_);
85  size_.attach(f);
86  keys_.attach(f);
87  vals_.attach(f);
88  }
89 
90  private:
97 }; // Branch<std::map>
98 
99 } // namespace hdtree
#define hdtree_class_version(VERS)
define the version number for a class
Definition: ClassVersion.h:85
Type-specific base class to hold common data methods.
Definition: AbstractBranch.h:87
std::pair< std::string, int > save_type_
type this data that is being used to write
Definition: AbstractBranch.h:244
std::optional< std::pair< std::string, int > > load_type_
type this data is loading from
Definition: AbstractBranch.h:242
DataType * handle_
handle on current object in memory
Definition: AbstractBranch.h:247
std::string name_
name of branch
Definition: AbstractBranch.h:74
void attach(Writer &f) final override
pure virtual method for saving structure
Definition: MapBranch.h:83
void attach(Reader &f) final override
pure virtual method for loading data from the input file
Definition: MapBranch.h:56
void load() final override
Load a map from the input file.
Definition: MapBranch.h:47
Branch< KeyType > keys_
the data set holding the content of all the keys
Definition: MapBranch.h:94
Branch< ValType > vals_
the data set holding the content of all the vals
Definition: MapBranch.h:96
Branch(const std::string &branch_name, std::map< KeyType, ValType > *handle=nullptr)
We create three child data sets, one for the successive sizes of the maps and two to hold all the key...
Definition: MapBranch.h:30
Branch< std::size_t > size_
the data set of sizes of the vectors
Definition: MapBranch.h:92
void save() final override
Save a vector to the output file.
Definition: MapBranch.h:72
General data set.
Definition: GeneralBranch.h:46
Reading a file generated by fire.
Definition: Reader.h:24
Write the fire DataSets into a deterministic structure in the output HDF5 data file.
Definition: Writer.h:21
Geant4 does a GLOBAL definition of the keyword TRUE.
Definition: Atomic.cxx:3
Structure constants vital to serialization method.
Definition: Constants.h:15