HDTree  0.5.2
HDTree C++ API
AbstractBranch.h
1 #pragma once
2 
3 #include <boost/core/demangle.hpp> // for demangling
4 #include <limits>
5 #include <optional>
6 #include <string>
7 
8 #include "hdtree/ClassVersion.h"
9 #include "hdtree/Version.h"
10 
11 namespace hdtree {
12 
13 class Writer;
14 class Reader;
15 
26 class BaseBranch {
27  public:
31  explicit BaseBranch(const std::string& full_name) : name_{full_name} {}
32 
36  virtual ~BaseBranch() = default;
37 
43  virtual void attach(Reader& f) = 0;
44 
45  virtual void load() = 0;
46 
53  virtual void attach(Writer& f) = 0;
54 
60  virtual void save() = 0;
61 
65  virtual void clear() = 0;
66 
68  BaseBranch(const BaseBranch&) = delete;
70  BaseBranch& operator=(const BaseBranch&) = delete;
71 
72  protected:
74  std::string name_;
75 };
76 
86 template <typename DataType>
87 class AbstractBranch : public BaseBranch {
88  public:
106  explicit AbstractBranch(const std::string& branch_name,
107  DataType* handle = nullptr)
108  : BaseBranch(branch_name), owner_{handle == nullptr} {
109  if (owner_) {
110  handle_ = new DataType;
111  } else {
112  handle_ = handle;
113  }
114 
115  save_type_ = {boost::core::demangle(typeid(DataType).name()),
116  class_version<DataType>};
117  }
118 
125  virtual ~AbstractBranch() {
126  if (owner_) delete handle_;
127  }
128 
129  virtual void attach(Reader& f) = 0;
135  virtual void load() = 0;
136 
142  virtual void save() = 0;
143 
148  virtual void attach(Writer& f) = 0;
149 
171  virtual void clear() {
172  if (owner_) {
173  if constexpr (std::numeric_limits<DataType>::is_specialized) {
174  *(this->handle_) = std::numeric_limits<DataType>::min();
175  } else {
176  handle_->clear();
177  }
178  }
179  }
180 
189  virtual const DataType& get() const { return *handle_; }
190 
199  int version() const { return load_type_.value_or(save_type_).second; }
200 
218  virtual void update(const DataType& val) { *handle_ = val; }
219 
223  DataType& operator*() { return *handle_; }
224 
228  const DataType& operator*() const { return get(); }
229 
233  DataType* operator->() { return handle_; }
234 
238  const DataType* operator->() const { return handle_; }
239 
240  protected:
242  std::optional<std::pair<std::string, int>> load_type_;
244  std::pair<std::string, int> save_type_;
245 
247  DataType* handle_;
249  bool owner_;
250 }; // AbstractBranch
251 
252 } // namespace hdtree
Serialization class version deduction.
Type-specific base class to hold common data methods.
Definition: AbstractBranch.h:87
virtual const DataType & get() const
Get the current in-memory data.
Definition: AbstractBranch.h:189
virtual void load()=0
pure virtual method for loading data
virtual ~AbstractBranch()
Delete our object if we own it, otherwise do nothing.
Definition: AbstractBranch.h:125
int version() const
Get the version number for the type we are loading from.
Definition: AbstractBranch.h:199
AbstractBranch(const std::string &branch_name, DataType *handle=nullptr)
Define the dataset branch_name and provide an optional handle.
Definition: AbstractBranch.h:106
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
virtual void update(const DataType &val)
Update the in-memory data object with the passed value.
Definition: AbstractBranch.h:218
const DataType * operator->() const
Pointer access to the in-memory data type in a const manner.
Definition: AbstractBranch.h:238
DataType & operator*()
Access the in-memory data object.
Definition: AbstractBranch.h:223
virtual void attach(Writer &f)=0
pure virtual method for saving structure
DataType * operator->()
Pointer access to the in-memory data type.
Definition: AbstractBranch.h:233
bool owner_
we own the object in memory
Definition: AbstractBranch.h:249
virtual void clear()
Define the clear function here to handle the most common cases.
Definition: AbstractBranch.h:171
virtual void attach(Reader &f)=0
pure virtual method for loading data from the input file
const DataType & operator*() const
Access the in-memory data opbject in a const manner.
Definition: AbstractBranch.h:228
virtual void save()=0
pure virtual method for saving data
DataType * handle_
handle on current object in memory
Definition: AbstractBranch.h:247
Empty data base allowing recursion.
Definition: AbstractBranch.h:26
virtual void save()=0
pure virtual method for saving the current data
virtual void clear()=0
pure virtual method for resetting the current data to a blank state
virtual void attach(Reader &f)=0
pure virtual method for loading data from the input file
std::string name_
name of branch
Definition: AbstractBranch.h:74
virtual void attach(Writer &f)=0
we should persist our hierarchy into the output file
BaseBranch(const BaseBranch &)=delete
no copying
BaseBranch & operator=(const BaseBranch &)=delete
no copying
BaseBranch(const std::string &full_name)
define the full name of the branch
Definition: AbstractBranch.h:31
virtual ~BaseBranch()=default
virtual destructor so inherited classes can be properly destructed.
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