switch to smart pointers in PMsr2Data where possible.

This commit is contained in:
suter_a 2023-10-21 13:32:44 +02:00
parent 5f66baa1e2
commit bb8055f3e2
2 changed files with 21 additions and 72 deletions

View File

@ -38,6 +38,7 @@
#include <iomanip> #include <iomanip>
#include <algorithm> #include <algorithm>
#include <limits> #include <limits>
#include <memory>
#include <boost/algorithm/string/trim.hpp> // for stripping leading whitespace in std::string #include <boost/algorithm/string/trim.hpp> // for stripping leading whitespace in std::string
#include <boost/algorithm/string/case_conv.hpp> // for to_lower() in std::string #include <boost/algorithm/string/case_conv.hpp> // for to_lower() in std::string
@ -58,7 +59,6 @@ PMsr2Data::PMsr2Data(const std::string &ext) : fFileExtension(ext), fRunListFile
{ {
fRunVector.clear(); fRunVector.clear();
fRunVectorIter = fRunVector.end(); fRunVectorIter = fRunVector.end();
fRunListFileStream = nullptr;
fSaxParser = nullptr; fSaxParser = nullptr;
fStartupHandler = nullptr; fStartupHandler = nullptr;
fDataHandler = nullptr; fDataHandler = nullptr;
@ -74,28 +74,6 @@ PMsr2Data::~PMsr2Data()
fRunVector.clear(); fRunVector.clear();
fRunVectorIter = fRunVector.end(); fRunVectorIter = fRunVector.end();
fIndVar.clear(); 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; std::ostringstream strInfile;
strInfile << runNo << fFileExtension << ".msr"; strInfile << runNo << fFileExtension << ".msr";
std::ifstream *in = new std::ifstream(strInfile.str().c_str()); std::unique_ptr<std::ifstream> in = std::make_unique<std::ifstream>(strInfile.str().c_str());
if (!in->is_open()) { if (!in->is_open()) {
delete in;
if (!normalMode && (runNo == *fRunVectorIter)) { if (!normalMode && (runNo == *fRunVectorIter)) {
std::string fileNameCopy(strInfile.str()); std::string fileNameCopy(strInfile.str());
strInfile.clear(); strInfile.clear();
strInfile.str(""); strInfile.str("");
strInfile << runNo << "+global" << fFileExtension << ".msr"; 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()) { 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 << ">> msr2data: **ERROR** Neither the file " << fileNameCopy << " nor the file " << strInfile.str() << " can be opened! Please check!";
std::cerr << std::endl; std::cerr << std::endl;
delete in;
return -1; 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 } 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(); 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 fRunVectorIter = fRunVector.begin(); // set back the runlist-iterator which might have changed during the search for the correct file
return 0; return 0;
} else { } 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 << ">> msr2data: **ERROR** this is either some template or the first existing file from the run list.";
std::cerr << std::endl; std::cerr << std::endl;
in->close(); in->close();
delete in;
in = nullptr;
return -2; 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 << ">> msr2data: **ERROR** Obviously it contains no RUN block...";
std::cerr << std::endl; std::cerr << std::endl;
in->close(); in->close();
delete in;
in = nullptr;
return -3; return -3;
} }
@ -387,7 +358,8 @@ int PMsr2Data::SetRunNumbers(const std::string &runListFile)
in.close(); in.close();
fRunVectorIter = fRunVector.begin(); fRunVectorIter = fRunVector.begin();
fRunListFile = true; fRunListFile = true;
fRunListFileStream = new std::ifstream(runListFile.c_str()); fRunListFileStream = std::make_unique<std::ifstream>(runListFile.c_str());
return 0; return 0;
} }
@ -402,26 +374,17 @@ int PMsr2Data::SetRunNumbers(const std::string &runListFile)
int PMsr2Data::ParseXmlStartupFile() int PMsr2Data::ParseXmlStartupFile()
{ {
int status; int status;
fSaxParser = new TSAXParser(); fSaxParser = std::make_unique<TSAXParser>();
fStartupHandler = new PStartupHandler(); fStartupHandler = std::make_unique<PStartupHandler>();
std::string startup_path_name(fStartupHandler->GetStartupFilePath().Data()); 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()); //status = fSaxParser->ParseFile(startup_path_name.c_str());
// parsing the file as above seems to lead to problems in certain environments; // parsing the file as above seems to lead to problems in certain environments;
// use the parseXmlFile function instead (see PStartupHandler.cpp for the definition) // 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 // check for parse errors
if (status) { // error if (status) { // error
std::cerr << std::endl << ">> msr2data: **WARNING** Reading/parsing musrfit_startup.xml failed." << std::endl; 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; return status;
} }
@ -439,7 +402,7 @@ int PMsr2Data::ParseXmlStartupFile()
int PMsr2Data::ReadMsrFile(const std::string &infile) const int PMsr2Data::ReadMsrFile(const std::string &infile) const
{ {
int status; int status;
fMsrHandler = new PMsrHandler(infile.c_str()); fMsrHandler = std::make_unique<PMsrHandler>(infile.c_str());
status = fMsrHandler->ReadMsrFile(); status = fMsrHandler->ReadMsrFile();
if (status != PMUSR_SUCCESS) { if (status != PMUSR_SUCCESS) {
switch (status) { switch (status) {
@ -487,6 +450,7 @@ PMsrHandler* PMsr2Data::GetSingleRunMsrFile() const
} }
return nullptr; return nullptr;
} }
return singleRunMsrFile; return singleRunMsrFile;
} }
@ -501,19 +465,18 @@ PMsrHandler* PMsr2Data::GetSingleRunMsrFile() const
int PMsr2Data::ReadRunDataFile() int PMsr2Data::ReadRunDataFile()
{ {
if (fStartupHandler) if (fStartupHandler)
fDataHandler = new PRunDataHandler(fMsrHandler, fStartupHandler->GetDataPathList()); fDataHandler = std::make_unique<PRunDataHandler>(fMsrHandler.get(), fStartupHandler->GetDataPathList());
else else
fDataHandler = new PRunDataHandler(fMsrHandler); fDataHandler = std::make_unique<PRunDataHandler>(fMsrHandler.get());
fDataHandler->ReadData(); fDataHandler->ReadData();
bool success = fDataHandler->IsAllDataAvailable(); bool success = fDataHandler->IsAllDataAvailable();
if (!success) { if (!success) {
std::cerr << std::endl << ">> msr2data: **WARNING** Could not read all data files, will continue without the data file information..." << std::endl; 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 1;
} }
return 0; return 0;
} }
@ -1945,14 +1908,6 @@ int PMsr2Data::WriteOutput(const std::string &outfile, const std::vector<unsigne
std::cerr << std::endl; std::cerr << std::endl;
fRunVectorIter++; fRunVectorIter++;
delete fMsrHandler;
fMsrHandler = nullptr;
if (fDataHandler) {
delete fDataHandler;
fDataHandler = nullptr;
}
// clean up some vectors // clean up some vectors
dataParamNames.clear(); dataParamNames.clear();
dataParamLabels.clear(); dataParamLabels.clear();
@ -2454,13 +2409,7 @@ int PMsr2Data::WriteOutput(const std::string &outfile, const std::vector<unsigne
outFile.close(); outFile.close();
if (!global || (fRunVectorIter == fRunVector.end())) { if (!global || (fRunVectorIter == fRunVector.end())) {
delete fMsrHandler; fDataHandler.reset();
fMsrHandler = nullptr;
if (fDataHandler) {
delete fDataHandler;
fDataHandler = nullptr;
}
} }
msrParamList = nullptr; msrParamList = nullptr;

View File

@ -86,11 +86,11 @@ class PMsr2Data
mutable std::vector<unsigned int>::const_iterator fRunVectorIter; mutable std::vector<unsigned int>::const_iterator fRunVectorIter;
bool fRunListFile; bool fRunListFile;
std::vector<std::string> fIndVar; std::vector<std::string> fIndVar;
std::ifstream *fRunListFileStream; std::unique_ptr<std::ifstream> fRunListFileStream;
TSAXParser *fSaxParser; std::unique_ptr<TSAXParser> fSaxParser;
PStartupHandler *fStartupHandler; std::unique_ptr<PStartupHandler> fStartupHandler;
mutable PRunDataHandler *fDataHandler; mutable std::unique_ptr<PRunDataHandler> fDataHandler;
mutable PMsrHandler *fMsrHandler; mutable std::unique_ptr<PMsrHandler> fMsrHandler;
mutable unsigned int fNumGlobalParam; mutable unsigned int fNumGlobalParam;
mutable unsigned int fNumSpecParam; mutable unsigned int fNumSpecParam;
mutable unsigned int fNumTempRunBlocks; mutable unsigned int fNumTempRunBlocks;