From e10535f53c342a8f9bdc9ff49d125cf48540832d Mon Sep 17 00:00:00 2001 From: Oliver Ruebel Date: Thu, 19 Sep 2024 13:17:22 -0700 Subject: [PATCH] Update members in BaseIO and HDF5IO --- src/BaseIO.cpp | 16 +++++--- src/BaseIO.hpp | 12 +++--- src/hdf5/HDF5IO.cpp | 97 ++++++++++++++++++++++----------------------- src/hdf5/HDF5IO.hpp | 18 ++++----- 4 files changed, 72 insertions(+), 71 deletions(-) diff --git a/src/BaseIO.cpp b/src/BaseIO.cpp index 353f709d..26b39049 100644 --- a/src/BaseIO.cpp +++ b/src/BaseIO.cpp @@ -31,9 +31,10 @@ const BaseDataType BaseDataType::DSTR = BaseDataType(T_STR, DEFAULT_STR_SIZE); // BaseIO -BaseIO::BaseIO() - : readyToOpen(true) - , opened(false) +BaseIO::BaseIO(const std::string& filename) + : m_filename(filename) + , m_readyToOpen(true) + , m_opened(false) { } @@ -41,12 +42,17 @@ BaseIO::~BaseIO() {} bool BaseIO::isOpen() const { - return opened; + return m_opened; +} + +std::string BaseIO::getFileName() +{ + return this->m_filename; } bool BaseIO::isReadyToOpen() const { - return readyToOpen; + return m_readyToOpen; } bool BaseIO::canModifyObjects() diff --git a/src/BaseIO.hpp b/src/BaseIO.hpp index 19d47cb1..268695e3 100644 --- a/src/BaseIO.hpp +++ b/src/BaseIO.hpp @@ -94,7 +94,7 @@ class BaseIO /** * @brief Constructor for the BaseIO class. */ - BaseIO(); + BaseIO(const std::string& filename); /** * @brief Copy constructor is deleted to prevent construction-copying. @@ -115,7 +115,7 @@ class BaseIO * @brief Returns the full path to the file. * @return The full path to the file. */ - virtual std::string getFileName() = 0; + virtual std::string getFileName(); /** * @brief Opens the file for writing. @@ -346,12 +346,12 @@ class BaseIO */ bool isReadyToOpen() const; +protected: /** * @brief The name of the file. */ - const std::string filename; + const std::string m_filename; -protected: /** * @brief Creates a new group if it does not already exist. * @param path The location of the group in the file. @@ -362,12 +362,12 @@ class BaseIO /** * @brief Whether the file is ready to be opened. */ - bool readyToOpen; + bool m_readyToOpen; /** * @brief Whether the file is currently open. */ - bool opened; + bool m_opened; }; /** diff --git a/src/hdf5/HDF5IO.cpp b/src/hdf5/HDF5IO.cpp index 0a518820..36eee1a5 100644 --- a/src/hdf5/HDF5IO.cpp +++ b/src/hdf5/HDF5IO.cpp @@ -15,12 +15,9 @@ using namespace H5; using namespace AQNWB::HDF5; // HDF5IO - -HDF5IO::HDF5IO() {} - -HDF5IO::HDF5IO(const std::string& fileName, const bool disableSWMRMode) - : filename(fileName) - , disableSWMRMode(disableSWMRMode) +HDF5IO::HDF5IO(const std::string& filename, const bool disableSWMRMode) + : BaseIO(filename) + , m_disableSWMRMode(disableSWMRMode) { } @@ -31,7 +28,7 @@ HDF5IO::~HDF5IO() std::string HDF5IO::getFileName() { - return filename; + return this->m_filename; } Status HDF5IO::open() @@ -47,7 +44,7 @@ Status HDF5IO::open(bool newfile) { int accFlags = 0; - if (opened) + if (this->m_opened) return Status::Failure; FileAccPropList fapl = FileAccPropList::DEFAULT; @@ -58,19 +55,19 @@ Status HDF5IO::open(bool newfile) else accFlags = H5F_ACC_RDWR; - file = std::make_unique( + this->m_file = std::make_unique( getFileName(), accFlags, FileCreatPropList::DEFAULT, fapl); - opened = true; + this->m_opened = true; return Status::Success; } Status HDF5IO::close() { - if (this->file != nullptr && opened) { - this->file->close(); - this->file = nullptr; - this->opened = false; + if (this->m_file != nullptr && this->m_opened) { + this->m_file->close(); + this->m_file = nullptr; + this->m_opened = false; } return Status::Success; @@ -86,7 +83,7 @@ Status checkStatus(int status) Status HDF5IO::flush() { - int status = H5Fflush(this->file->getId(), H5F_SCOPE_GLOBAL); + int status = H5Fflush(this->m_file->getId(), H5F_SCOPE_GLOBAL); return checkStatus(status); } @@ -103,18 +100,18 @@ Status HDF5IO::createAttribute(const BaseDataType& type, DataType H5type; DataType origType; - if (!opened) + if (!this->m_opened) return Status::Failure; // open the group or dataset H5O_type_t objectType = getObjectType(path); switch (objectType) { case H5O_TYPE_GROUP: - gloc = file->openGroup(path); + gloc = this->m_file->openGroup(path); loc = &gloc; break; case H5O_TYPE_DATASET: - dloc = file->openDataSet(path); + dloc = this->m_file->openDataSet(path); loc = &dloc; break; default: @@ -178,7 +175,7 @@ Status HDF5IO::createAttribute(const std::vector& data, Attribute attr; hsize_t dims[1]; - if (!opened) + if (!this->m_opened) return Status::Failure; StrType H5type(PredType::C_S1, maxSize); @@ -188,11 +185,11 @@ Status HDF5IO::createAttribute(const std::vector& data, H5O_type_t objectType = getObjectType(path); switch (objectType) { case H5O_TYPE_GROUP: - gloc = file->openGroup(path); + gloc = this->m_file->openGroup(path); loc = &gloc; break; case H5O_TYPE_DATASET: - dloc = file->openDataSet(path); + dloc = this->m_file->openDataSet(path); loc = &dloc; break; default: @@ -235,18 +232,18 @@ Status HDF5IO::createReferenceAttribute(const std::string& referencePath, DataSet dloc; Attribute attr; - if (!opened) + if (!this->m_opened) return Status::Failure; // open the group or dataset H5O_type_t objectType = getObjectType(path); switch (objectType) { case H5O_TYPE_GROUP: - gloc = file->openGroup(path); + gloc = this->m_file->openGroup(path); loc = &gloc; break; case H5O_TYPE_DATASET: - dloc = file->openDataSet(path); + dloc = this->m_file->openDataSet(path); loc = &dloc; break; default: @@ -263,7 +260,7 @@ Status HDF5IO::createReferenceAttribute(const std::string& referencePath, hobj_ref_t* rdata = new hobj_ref_t[sizeof(hobj_ref_t)]; - file->reference(rdata, referencePath.c_str()); + this->m_file->reference(rdata, referencePath.c_str()); attr.write(H5::PredType::STD_REF_OBJ, rdata); delete[] rdata; @@ -283,10 +280,10 @@ Status HDF5IO::createReferenceAttribute(const std::string& referencePath, Status HDF5IO::createGroup(const std::string& path) { - if (!opened) + if (!this->m_opened) return Status::Failure; try { - file->createGroup(path); + this->m_file->createGroup(path); } catch (FileIException error) { error.printErrorStack(); } catch (GroupIException error) { @@ -297,10 +294,10 @@ Status HDF5IO::createGroup(const std::string& path) Status HDF5IO::createGroupIfDoesNotExist(const std::string& path) { - if (!opened) + if (!this->m_opened) return Status::Failure; try { - file->childObjType(path); + this->m_file->childObjType(path); } catch (FileIException) { return createGroup(path); } @@ -310,11 +307,11 @@ Status HDF5IO::createGroupIfDoesNotExist(const std::string& path) /** Creates a link to another location in the file */ Status HDF5IO::createLink(const std::string& path, const std::string& reference) { - if (!opened) + if (!this->m_opened) return Status::Failure; herr_t error = H5Lcreate_soft(reference.c_str(), - file->getLocId(), + this->m_file->getLocId(), path.c_str(), H5P_DEFAULT, H5P_DEFAULT); @@ -325,7 +322,7 @@ Status HDF5IO::createLink(const std::string& path, const std::string& reference) Status HDF5IO::createReferenceDataSet( const std::string& path, const std::vector& references) { - if (!opened) + if (!this->m_opened) return Status::Failure; const hsize_t size = references.size(); @@ -333,12 +330,12 @@ Status HDF5IO::createReferenceDataSet( hobj_ref_t* rdata = new hobj_ref_t[size * sizeof(hobj_ref_t)]; for (SizeType i = 0; i < size; i++) { - file->reference(&rdata[i], references[i].c_str()); + this->m_file->reference(&rdata[i], references[i].c_str()); } hid_t space = H5Screate_simple(1, &size, NULL); - hid_t dset = H5Dcreate(file->getLocId(), + hid_t dset = H5Dcreate(this->m_file->getLocId(), path.c_str(), H5T_STD_REF_OBJ, space, @@ -360,7 +357,7 @@ Status HDF5IO::createReferenceDataSet( Status HDF5IO::createStringDataSet(const std::string& path, const std::string& value) { - if (!opened) + if (!this->m_opened) return Status::Failure; std::unique_ptr dataset; @@ -368,7 +365,7 @@ Status HDF5IO::createStringDataSet(const std::string& path, DataSpace dSpace(H5S_SCALAR); dataset = - std::make_unique(file->createDataSet(path, H5type, dSpace)); + std::make_unique(this->m_file->createDataSet(path, H5type, dSpace)); dataset->write(value.c_str(), H5type); return Status::Success; @@ -377,7 +374,7 @@ Status HDF5IO::createStringDataSet(const std::string& path, Status HDF5IO::createStringDataSet(const std::string& path, const std::vector& values) { - if (!opened) + if (!this->m_opened) return Status::Failure; std::vector cStrs; @@ -397,11 +394,11 @@ Status HDF5IO::createStringDataSet(const std::string& path, Status HDF5IO::startRecording() { - if (!opened) + if (!this->m_opened) return Status::Failure; - if (!disableSWMRMode) { - herr_t status = H5Fstart_swmr_write(this->file->getId()); + if (!this->m_disableSWMRMode) { + herr_t status = H5Fstart_swmr_write(this->m_file->getId()); return checkStatus(status); } return Status::Success; @@ -410,7 +407,7 @@ Status HDF5IO::startRecording() Status HDF5IO::stopRecording() { // if SWMR mode is disabled, stopping the recording will leave the file open - if (!disableSWMRMode) { + if (!this->m_disableSWMRMode) { close(); // SWMR mode cannot be disabled so close the file } else { this->flush(); @@ -420,13 +417,13 @@ Status HDF5IO::stopRecording() bool HDF5IO::canModifyObjects() { - if (!opened) + if (!this->m_opened) return false; // Check if we are in SWMR mode bool inSWMRMode = false; unsigned int intent; - herr_t status = H5Fget_intent(this->file->getId(), &intent); + herr_t status = H5Fget_intent(this->m_file->getId(), &intent); bool statusOK = (status >= 0); if (statusOK) { inSWMRMode = (intent & (H5F_ACC_SWMR_READ | H5F_ACC_SWMR_WRITE)); @@ -439,7 +436,7 @@ bool HDF5IO::canModifyObjects() bool HDF5IO::objectExists(const std::string& path) { - htri_t exists = H5Lexists(file->getId(), path.c_str(), H5P_DEFAULT); + htri_t exists = H5Lexists(this->m_file->getId(), path.c_str(), H5P_DEFAULT); if (exists > 0) { return true; } else { @@ -452,11 +449,11 @@ std::unique_ptr HDF5IO::getDataSet( { std::unique_ptr data; - if (!opened) + if (!this->m_opened) return nullptr; try { - data = std::make_unique(file->openDataSet(path)); + data = std::make_unique(this->m_file->openDataSet(path)); return std::make_unique(std::move(data)); } catch (DataSetIException error) { error.printErrorStack(); @@ -480,7 +477,7 @@ std::unique_ptr HDF5IO::createArrayDataSet( DSetCreatPropList prop; DataType H5type = getH5Type(type); - if (!opened) + if (!this->m_opened) return nullptr; SizeType dimension = size.size(); @@ -509,7 +506,7 @@ std::unique_ptr HDF5IO::createArrayDataSet( prop.setChunk(static_cast(dimension), chunk_dims.data()); data = std::make_unique( - file->createDataSet(path, H5type, dSpace, prop)); + this->m_file->createDataSet(path, H5type, dSpace, prop)); return std::make_unique(std::move(data)); } @@ -520,11 +517,11 @@ H5O_type_t HDF5IO::getObjectType(const std::string& path) // get whether path is a dataset or group H5O_info_t objInfo; // Structure to hold information about the object H5Oget_info_by_name( - this->file->getId(), path.c_str(), &objInfo, H5O_INFO_BASIC, H5P_DEFAULT); + this->m_file->getId(), path.c_str(), &objInfo, H5O_INFO_BASIC, H5P_DEFAULT); #else // get whether path is a dataset or group H5O_info_t objInfo; // Structure to hold information about the object - H5Oget_info_by_name(this->file->getId(), path.c_str(), &objInfo, H5P_DEFAULT); + H5Oget_info_by_name(this-m_>file->getId(), path.c_str(), &objInfo, H5P_DEFAULT); #endif H5O_type_t objectType = objInfo.type; diff --git a/src/hdf5/HDF5IO.hpp b/src/hdf5/HDF5IO.hpp index 26ffc799..528ef43c 100644 --- a/src/hdf5/HDF5IO.hpp +++ b/src/hdf5/HDF5IO.hpp @@ -32,11 +32,6 @@ class HDF5RecordingData; // declare here because gets used in HDF5IO class class HDF5IO : public BaseIO { public: - /** - * @brief Default constructor for the HDF5IO class. - */ - HDF5IO(); - /** * @brief Constructor for the HDF5IO class that takes a file name as input. * @param fileName The name of the HDF5 file. @@ -264,8 +259,6 @@ class HDF5IO : public BaseIO static H5::DataType getH5Type(BaseDataType type); protected: - std::string filename; - /** * @brief Creates a new group if it does not exist. * @param path The location in the file of the group. @@ -274,9 +267,14 @@ class HDF5IO : public BaseIO Status createGroupIfDoesNotExist(const std::string& path) override; private: - std::unique_ptr file; - bool disableSWMRMode; // when set do not use SWMR mode when opening the HDF5 - // file + /** + * @brief the HDF5 file + */ + std::unique_ptr m_file; + /** + * @brief When set do not use SWMR mode when opening the HDF5 file + */ + bool m_disableSWMRMode; }; /**