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 <algorithm>
#include <limits>
#include <memory>
#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
@ -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<std::ifstream> in = std::make_unique<std::ifstream>(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<std::ifstream>(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<TSAXParser>();
fStartupHandler = std::make_unique<PStartupHandler>();
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<PMsrHandler>(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<PRunDataHandler>(fMsrHandler.get(), fStartupHandler->GetDataPathList());
else
fDataHandler = new PRunDataHandler(fMsrHandler);
fDataHandler = std::make_unique<PRunDataHandler>(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<unsigne
std::cerr << std::endl;
fRunVectorIter++;
delete fMsrHandler;
fMsrHandler = nullptr;
if (fDataHandler) {
delete fDataHandler;
fDataHandler = nullptr;
}
// clean up some vectors
dataParamNames.clear();
dataParamLabels.clear();
@ -2454,13 +2409,7 @@ int PMsr2Data::WriteOutput(const std::string &outfile, const std::vector<unsigne
outFile.close();
if (!global || (fRunVectorIter == fRunVector.end())) {
delete fMsrHandler;
fMsrHandler = nullptr;
if (fDataHandler) {
delete fDataHandler;
fDataHandler = nullptr;
}
fDataHandler.reset();
}
msrParamList = nullptr;

View File

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