fire v0.19.0
Framework for sImulation and Reconstruction of Events
fire::io::Data< DataType, Enable > Class Template Reference

General data set. More...

#include <Data.h>

Inheritance diagram for fire::io::Data< DataType, Enable >:
[legend]
Collaboration diagram for fire::io::Data< DataType, Enable >:
[legend]

Public Types

enum  SaveLoad { Both , LoadOnly , SaveOnly }
 Flag how a member variable should be accessed by serialization. More...
 

Public Member Functions

 Data (const std::string &path, Reader *input_file=nullptr, DataType *handle=nullptr)
 Attach ourselves to the input type after construction. More...
 
void load (h5::Reader &f) final override
 Loading this dataset from the file involves simply loading all of the members of the data type. More...
 
void save (Writer &f) final override
 pure virtual method for saving data More...
 
void structure (Writer &f) final override
 pure virtual method for saving structure More...
 
template<typename MemberType >
void attach (const std::string &name, MemberType &m, SaveLoad sl=SaveLoad::Both)
 Attach a member object from the our data handle. More...
 
template<typename MemberType >
void rename (const std::string &old_name, const std::string &new_name, MemberType &m)
 Rename a member variable. More...
 
- Public Member Functions inherited from fire::io::AbstractData< DataType >
 AbstractData (const std::string &path, Reader *input_file=nullptr, DataType *handle=nullptr)
 Define the dataset path and provide an optional handle. More...
 
virtual ~AbstractData ()
 Delete our object if we own it, otherwise do nothing. More...
 
virtual void clear ()
 Define the clear function here to handle the most common cases. More...
 
virtual const DataType & get () const
 Get the current in-memory data. More...
 
int version () const
 Get the version number for the type we are loading from. More...
 
virtual void update (const DataType &val)
 Update the in-memory data object with the passed value. More...
 
- Public Member Functions inherited from fire::io::BaseData
 BaseData (const std::string &path)
 Define the full in-file path to the data set of this data. More...
 
virtual ~BaseData ()=default
 virtual destructor so inherited classes can be properly destructed.
 

Private Attributes

std::vector< std::tuple< bool, bool, std::unique_ptr< BaseData > > > members_
 list of members in this dataset More...
 
Readerinput_file_
 pointer to the input file (if there is one)
 

Additional Inherited Members

- Protected Attributes inherited from fire::io::AbstractData< DataType >
std::optional< std::pair< std::string, int > > load_type_
 type this data is loading from
 
std::pair< std::string, int > save_type_
 type this data that is being used to write
 
DataType * handle_
 handle on current object in memory
 
bool owner_
 we own the object in memory
 
- Protected Attributes inherited from fire::io::BaseData
std::string path_
 path of data set
 

Detailed Description

template<typename DataType, typename Enable = void>
class fire::io::Data< DataType, Enable >

General data set.

This is the top-level data set that will be used most often. It is meant to be used by a class which registers its member variables to this set via the io::DataSet<DataType>::attach method.

More complete documentation is kept in the documentation of the fire::io namespace; nevertheless, a short example is kept here.

class MyData {
public:
MyData() = default; // required by serialization technique
// other public members
private:
friend class fire::io::access;
template<typename Data>
void attach(Data& d) {
d.attach("my_double",my_double_);
if (d.version() < 2) d.rename("old","new",new_);
else d.attach("new",new_);
}
void clear() {
my_double_ = -1; // reset to default value
new_ = 0;
}
private:
double my_double_;
int new_;
// this member doesn't appear in 'attach' so it won't end up on disk
int i_wont_be_on_disk_;
};
#define fire_class_version(VERS)
define the version number for a class
Definition: ClassVersion.h:83
virtual void clear()
Define the clear function here to handle the most common cases.
Definition: AbstractData.h:177
void attach(const std::string &name, MemberType &m, SaveLoad sl=SaveLoad::Both)
Attach a member object from the our data handle.
Definition: Data.h:188
Data(const std::string &path, Reader *input_file=nullptr, DataType *handle=nullptr)
Attach ourselves to the input type after construction.
Definition: Data.h:116
empty struct for connecting a instance of Data and the type it wraps
Definition: Access.h:22

Member Enumeration Documentation

◆ SaveLoad

template<typename DataType , typename Enable = void>
enum fire::io::Data::SaveLoad

Flag how a member variable should be accessed by serialization.

In this context, load is only called when there is a file to load from.

Enumerator
Both 

load and save the member

LoadOnly 

only load the member (read in)

SaveOnly 

only save the member (write out)

Constructor & Destructor Documentation

◆ Data()

template<typename DataType , typename Enable = void>
fire::io::Data< DataType, Enable >::Data ( const std::string path,
Reader input_file = nullptr,
DataType *  handle = nullptr 
)
inlineexplicit

Attach ourselves to the input type after construction.

After the intermediate class AbstractData does the initialization, we call the void attach(io::Data<DataType>& d) method of the data pointed to by our handle. This allows us to register its member variables with our own io::Data<DataType>::attach method.

Parameters
[in]pathfull in-file path to the data set for this data
[in]handleaddress of object already created (optional)

Member Function Documentation

◆ attach()

template<typename DataType , typename Enable = void>
template<typename MemberType >
void fire::io::Data< DataType, Enable >::attach ( const std::string name,
MemberType &  m,
SaveLoad  sl = SaveLoad::Both 
)
inline

Attach a member object from the our data handle.

We create a new child Data so that we can recursively handle complex member variable types.

Template Parameters
MemberTypetype of member variable we are attaching
Parameters
[in]namename of member variable
[in]mreference of member variable
[in]savewrite this member into output files (if the class is being written)
[in]loadload this member from an input file (if being read)

◆ load()

template<typename DataType , typename Enable = void>
void fire::io::Data< DataType, Enable >::load ( h5::Reader f)
inlinefinaloverridevirtual

Loading this dataset from the file involves simply loading all of the members of the data type.

We catch a HighFive exception early here so that we can detail to the user which class is causing the read issue. This is especially helpful in the case of containers of user types since the issue is (probably) not coming from serialization of the container.

Exceptions
Exceptionif HighFive is unable to load any of the members.
Parameters
[in]ffile to load from

Implements fire::io::AbstractData< DataType >.

◆ rename()

template<typename DataType , typename Enable = void>
template<typename MemberType >
void fire::io::Data< DataType, Enable >::rename ( const std::string old_name,
const std::string new_name,
MemberType &  m 
)
inline

Rename a member variable.

This is a simple helper-function wrapping attach which does the two calls for the user sharing the same member variable, creating one member for loading from the old_name and one member for writing to the new_name.

Template Parameters
MemberTypetype of member variable we are attaching
Parameters
[in]old_namename of member variable in version being read from file
[in]new_namename of member variable in version being written to output file
[in]mreference of member variable

◆ save()

template<typename DataType , typename Enable = void>
void fire::io::Data< DataType, Enable >::save ( Writer f)
inlinefinaloverridevirtual

pure virtual method for saving data

Parameters
[in]fWriter to save to

Implements fire::io::AbstractData< DataType >.

◆ structure()

template<typename DataType , typename Enable = void>
void fire::io::Data< DataType, Enable >::structure ( Writer f)
inlinefinaloverridevirtual

pure virtual method for saving structure

Parameters
[in]fWriter to write to

Implements fire::io::AbstractData< DataType >.

Member Data Documentation

◆ members_

template<typename DataType , typename Enable = void>
std::vector<std::tuple<bool,bool,std::unique_ptr<BaseData> > > fire::io::Data< DataType, Enable >::members_
private

list of members in this dataset

the extra boolean flags are to tell us if that member should be loaded from the input file and/or saved to the output file

This is the core of schema evolution.


The documentation for this class was generated from the following file: