From 0bff931af9e1947958a50446a58f69a28a9be9f4 Mon Sep 17 00:00:00 2001 From: Andreas Suter Date: Sat, 21 Oct 2023 13:32:44 +0200 Subject: [PATCH] switch to smart pointers in PMsr2Data where possible. --- src/classes/PMsr2Data.cpp | 83 ++++++++------------------------------- src/include/PMsr2Data.h | 10 ++--- 2 files changed, 21 insertions(+), 72 deletions(-) diff --git a/src/classes/PMsr2Data.cpp b/src/classes/PMsr2Data.cpp index 9a47eac1..edb99b74 100644 --- a/src/classes/PMsr2Data.cpp +++ b/src/classes/PMsr2Data.cpp @@ -38,6 +38,7 @@ #include #include #include +#include #include // for stripping leading whitespace in std::string #include // for to_lower() in std::string @@ -58,7 +59,6 @@ PMsr2Data::PMsr2Data(const std::string &ext) : fFileExtension(ext), fRunListFile { fRunVector.clear(); fRunVectorIter = fRunVector.end(); - fRunListFileStream = nullptr; fSaxParser = nullptr; fStartupHandler = nullptr; fDataHandler = nullptr; @@ -74,28 +74,6 @@ PMsr2Data::~PMsr2Data() fRunVector.clear(); fRunVectorIter = fRunVector.end(); fIndVar.clear(); - - if (fRunListFileStream) { - fRunListFileStream->close(); - delete fRunListFileStream; - fRunListFileStream = nullptr; - } - if (fSaxParser) { - delete fSaxParser; - fSaxParser = nullptr; - } - if (fStartupHandler) { - delete fStartupHandler; - fStartupHandler = nullptr; - } - if (fDataHandler) { - delete fDataHandler; - fDataHandler = nullptr; - } - if (fMsrHandler) { - delete fMsrHandler; - fMsrHandler = nullptr; - } } //------------------------------------------------------------- @@ -118,19 +96,17 @@ int PMsr2Data::DetermineRunNumberDigits(unsigned int runNo, bool normalMode) con { std::ostringstream strInfile; strInfile << runNo << fFileExtension << ".msr"; - std::ifstream *in = new std::ifstream(strInfile.str().c_str()); + std::unique_ptr in = std::make_unique(strInfile.str().c_str()); if (!in->is_open()) { - delete in; if (!normalMode && (runNo == *fRunVectorIter)) { std::string fileNameCopy(strInfile.str()); strInfile.clear(); strInfile.str(""); strInfile << runNo << "+global" << fFileExtension << ".msr"; - in = new std::ifstream(strInfile.str().c_str()); + in.reset(new std::ifstream(strInfile.str().c_str())); if (!in->is_open()) { std::cerr << std::endl << ">> msr2data: **ERROR** Neither the file " << fileNameCopy << " nor the file " << strInfile.str() << " can be opened! Please check!"; std::cerr << std::endl; - delete in; return -1; } } else if (runNo == *fRunVectorIter) { // the first run of the runlist was given - if it did not exist, try the rest of the runlist @@ -176,8 +152,6 @@ int PMsr2Data::DetermineRunNumberDigits(unsigned int runNo, bool normalMode) con } } in->close(); - delete in; - in = nullptr; fRunVectorIter = fRunVector.begin(); // set back the runlist-iterator which might have changed during the search for the correct file return 0; } else { @@ -187,8 +161,6 @@ int PMsr2Data::DetermineRunNumberDigits(unsigned int runNo, bool normalMode) con std::cerr << std::endl << ">> msr2data: **ERROR** this is either some template or the first existing file from the run list."; std::cerr << std::endl; in->close(); - delete in; - in = nullptr; return -2; } } @@ -198,8 +170,7 @@ int PMsr2Data::DetermineRunNumberDigits(unsigned int runNo, bool normalMode) con std::cerr << std::endl << ">> msr2data: **ERROR** Obviously it contains no RUN block..."; std::cerr << std::endl; in->close(); - delete in; - in = nullptr; + return -3; } @@ -387,7 +358,8 @@ int PMsr2Data::SetRunNumbers(const std::string &runListFile) in.close(); fRunVectorIter = fRunVector.begin(); fRunListFile = true; - fRunListFileStream = new std::ifstream(runListFile.c_str()); + fRunListFileStream = std::make_unique(runListFile.c_str()); + return 0; } @@ -402,26 +374,17 @@ int PMsr2Data::SetRunNumbers(const std::string &runListFile) int PMsr2Data::ParseXmlStartupFile() { int status; - fSaxParser = new TSAXParser(); - fStartupHandler = new PStartupHandler(); + fSaxParser = std::make_unique(); + fStartupHandler = std::make_unique(); std::string startup_path_name(fStartupHandler->GetStartupFilePath().Data()); - fSaxParser->ConnectToHandler("PStartupHandler", fStartupHandler); + fSaxParser->ConnectToHandler("PStartupHandler", fStartupHandler.get()); //status = fSaxParser->ParseFile(startup_path_name.c_str()); // parsing the file as above seems to lead to problems in certain environments; // use the parseXmlFile function instead (see PStartupHandler.cpp for the definition) - status = parseXmlFile(fSaxParser, startup_path_name.c_str()); + status = parseXmlFile(fSaxParser.get(), startup_path_name.c_str()); // check for parse errors if (status) { // error std::cerr << std::endl << ">> msr2data: **WARNING** Reading/parsing musrfit_startup.xml failed." << std::endl; - // clean up - if (fSaxParser) { - delete fSaxParser; - fSaxParser = nullptr; - } - if (fStartupHandler) { - delete fStartupHandler; - fStartupHandler = nullptr; - } } return status; } @@ -439,7 +402,7 @@ int PMsr2Data::ParseXmlStartupFile() int PMsr2Data::ReadMsrFile(const std::string &infile) const { int status; - fMsrHandler = new PMsrHandler(infile.c_str()); + fMsrHandler = std::make_unique(infile.c_str()); status = fMsrHandler->ReadMsrFile(); if (status != PMUSR_SUCCESS) { switch (status) { @@ -487,6 +450,7 @@ PMsrHandler* PMsr2Data::GetSingleRunMsrFile() const } return nullptr; } + return singleRunMsrFile; } @@ -501,19 +465,18 @@ PMsrHandler* PMsr2Data::GetSingleRunMsrFile() const int PMsr2Data::ReadRunDataFile() { if (fStartupHandler) - fDataHandler = new PRunDataHandler(fMsrHandler, fStartupHandler->GetDataPathList()); + fDataHandler = std::make_unique(fMsrHandler.get(), fStartupHandler->GetDataPathList()); else - fDataHandler = new PRunDataHandler(fMsrHandler); + fDataHandler = std::make_unique(fMsrHandler.get()); fDataHandler->ReadData(); bool success = fDataHandler->IsAllDataAvailable(); if (!success) { std::cerr << std::endl << ">> msr2data: **WARNING** Could not read all data files, will continue without the data file information..." << std::endl; - delete fDataHandler; - fDataHandler = nullptr; return 1; } + return 0; } @@ -1945,14 +1908,6 @@ int PMsr2Data::WriteOutput(const std::string &outfile, const std::vector::const_iterator fRunVectorIter; bool fRunListFile; std::vector fIndVar; - std::ifstream *fRunListFileStream; - TSAXParser *fSaxParser; - PStartupHandler *fStartupHandler; - mutable PRunDataHandler *fDataHandler; - mutable PMsrHandler *fMsrHandler; + std::unique_ptr fRunListFileStream; + std::unique_ptr fSaxParser; + std::unique_ptr fStartupHandler; + mutable std::unique_ptr fDataHandler; + mutable std::unique_ptr fMsrHandler; mutable unsigned int fNumGlobalParam; mutable unsigned int fNumSpecParam; mutable unsigned int fNumTempRunBlocks;