Changed to heap allocation and boost smart pointers

This commit is contained in:
Kristian Flikka 2013-03-26 10:00:06 +01:00
parent 990648e95b
commit 2f7cc609d0
7 changed files with 46 additions and 48 deletions

View File

@ -23,10 +23,12 @@ namespace Opm {
Parser::Parser() {
}
void Parser::parse(const std::string &path, RawDeck& outputDeck) {
RawDeckPtr Parser::parse(const std::string &path) {
Logger::info("Starting parsing of file: " + path);
outputDeck.readDataIntoDeck(path);
RawDeckPtr rawDeck(new RawDeck());
rawDeck -> readDataIntoDeck(path);
Logger::info("Done parsing of file: " + path);
return rawDeck;
}
Parser::~Parser() {

View File

@ -21,6 +21,7 @@
#define PARSER_H
#include <string>
#include <fstream>
#include <boost/shared_ptr.hpp>
#include "Logger.hpp"
#include "data/RawKeyword.hpp"
@ -32,11 +33,13 @@ namespace Opm {
class Parser {
public:
Parser();
void parse(const std::string &path, RawDeck& outputDeck);
RawDeckPtr parse(const std::string &path);
virtual ~Parser();
private:
//Logger m_logger;
};
typedef boost::shared_ptr<Parser> ParserPtr;
} // namespace Opm
#endif /* PARSER_H */

View File

@ -26,13 +26,13 @@ namespace Opm {
RawDeck::RawDeck() {
}
RawKeyword* RawDeck::getKeyword(const std::string& keyword) {
for(std::list<RawKeyword*>::iterator it = m_keywords.begin(); it != m_keywords.end(); it++) {
RawKeywordPtr RawDeck::getKeyword(const std::string& keyword) {
for(std::list<RawKeywordPtr>::iterator it = m_keywords.begin(); it != m_keywords.end(); it++) {
if ((*it)->getKeyword() == keyword) {
return (*it);
}
}
return NULL;
return RawKeywordPtr();
}
void RawDeck::readDataIntoDeck(const std::string& path) {
@ -43,10 +43,10 @@ namespace Opm {
std::string line;
std::string keywordString;
RawKeyword* currentRawKeyword;
RawKeywordPtr currentRawKeyword;
while (std::getline(inputstream, line)) {
if (currentRawKeyword -> tryGetValidKeyword(line, keywordString)) {
currentRawKeyword = new RawKeyword(keywordString);
if (RawKeyword::tryGetValidKeyword(line, keywordString)) {
currentRawKeyword = RawKeywordPtr(new RawKeyword(keywordString));
m_keywords.push_back(currentRawKeyword);
} else if (currentRawKeyword != NULL) {
addRawRecordStringToRawKeyword(line, currentRawKeyword);
@ -55,7 +55,7 @@ namespace Opm {
inputstream.close();
}
void RawDeck::addRawRecordStringToRawKeyword(const std::string& line, RawKeyword* currentRawKeyword) {
void RawDeck::addRawRecordStringToRawKeyword(const std::string& line, RawKeywordPtr currentRawKeyword) {
if (looksLikeData(line)) {
currentRawKeyword->addRawRecordString(line);
}
@ -90,9 +90,5 @@ namespace Opm {
}
RawDeck::~RawDeck() {
while(!m_keywords.empty()) {
delete m_keywords.front();
m_keywords.pop_front();
}
}
}

View File

@ -9,6 +9,7 @@
#define RAWDECK_HPP
#include <list>
#include <iostream>
#include <boost/shared_ptr.hpp>
#include "opm/parser/eclipse/Logger.hpp"
#include "RawKeyword.hpp"
@ -18,15 +19,17 @@ namespace Opm {
public:
RawDeck();
void readDataIntoDeck(const std::string& path);
RawKeyword* getKeyword(const std::string& keyword);
RawKeywordPtr getKeyword(const std::string& keyword);
unsigned int getNumberOfKeywords();
virtual ~RawDeck();
private:
std::list<RawKeyword*> m_keywords;
void addRawRecordStringToRawKeyword(const std::string& line, RawKeyword* currentRawKeyword);
std::list<RawKeywordPtr> m_keywords;
void addRawRecordStringToRawKeyword(const std::string& line, RawKeywordPtr currentRawKeyword);
bool looksLikeData(const std::string& line);
void checkInputFile(const std::string& inputPath);
};
typedef boost::shared_ptr<RawDeck> RawDeckPtr;
}
#endif /* RAWDECK_HPP */

View File

@ -23,7 +23,6 @@
#include <iostream>
namespace Opm {
RawKeyword::RawKeyword() {
}

View File

@ -11,6 +11,7 @@
#include <string>
#include <utility>
#include <list>
#include <boost/shared_ptr.hpp>
#include "opm/parser/eclipse/Logger.hpp"
#include "RawRecord.hpp"
@ -20,7 +21,7 @@ namespace Opm {
public:
RawKeyword();
RawKeyword(const std::string& keyword);
bool tryGetValidKeyword(const std::string& line, std::string& result);
static bool tryGetValidKeyword(const std::string& line, std::string& result);
std::string getKeyword();
void getRecords(std::list<RawRecord>& records);
unsigned int getNumberOfRecords();
@ -34,6 +35,8 @@ namespace Opm {
std::string m_partialRecordString;
static bool isValidKeyword(const std::string& keywordCandidate);
};
typedef boost::shared_ptr<RawKeyword> RawKeywordPtr;
}
#endif /* RAWKEYWORD_HPP */

View File

@ -35,46 +35,40 @@ BOOST_AUTO_TEST_CASE(Initializing) {
}
BOOST_AUTO_TEST_CASE(ParseWithInvalidInputFileThrows) {
Parser *parser = new Parser();
RawDeck rawDeck;
BOOST_REQUIRE_THROW(parser -> parse("nonexistingfile.asdf", rawDeck), std::invalid_argument);
delete parser;
ParserPtr parser(new Parser());
BOOST_REQUIRE_THROW(parser -> parse("nonexistingfile.asdf"), std::invalid_argument);
}
BOOST_AUTO_TEST_CASE(ParseWithValidFileSetOnParseCallNoThrow) {
boost::filesystem::path singleKeywordFile("testdata/small.data");
Parser *parser = new Parser();
RawDeck rawDeck;
ParserPtr parser(new Parser());
BOOST_REQUIRE_NO_THROW(parser -> parse(singleKeywordFile.string()));
BOOST_REQUIRE_NO_THROW(parser -> parse(singleKeywordFile.string(), rawDeck));
delete parser;
}
BOOST_AUTO_TEST_CASE(ParseWithInValidFileSetOnParseCallThrows) {
boost::filesystem::path singleKeywordFile("testdata/nosuchfile.data");
Parser parser;
RawDeck rawDeck;
BOOST_REQUIRE_THROW(parser.parse(singleKeywordFile.string(), rawDeck), std::invalid_argument);
ParserPtr parser(new Parser());
BOOST_REQUIRE_THROW(parser -> parse(singleKeywordFile.string()), std::invalid_argument);
}
BOOST_AUTO_TEST_CASE(ParseFileWithFewKeywords) {
boost::filesystem::path singleKeywordFile("testdata/small.data");
Parser parser;
RawDeck rawDeck;
ParserPtr parser(new Parser());
parser.parse(singleKeywordFile.string(), rawDeck);
BOOST_REQUIRE_EQUAL((unsigned)4, rawDeck.getNumberOfKeywords());
RawDeckPtr rawDeck = parser -> parse(singleKeywordFile.string());
BOOST_REQUIRE_EQUAL((unsigned) 4, rawDeck -> getNumberOfKeywords());
RawKeyword* matchingKeyword = rawDeck.getKeyword("A");
RawKeywordPtr matchingKeyword = rawDeck -> getKeyword("A");
BOOST_REQUIRE_EQUAL("A", matchingKeyword->getKeyword());
std::list<RawRecord> records;
matchingKeyword->getRecords(records);
BOOST_REQUIRE_EQUAL((unsigned) 1, records.size());
RawRecord theRecord = records.front();
std::string recordString;
theRecord.getRecord(recordString);
@ -86,11 +80,10 @@ BOOST_AUTO_TEST_CASE(ParseFileWithFewKeywords) {
BOOST_AUTO_TEST_CASE(ParseFileWithManyKeywords) {
boost::filesystem::path multipleKeywordFile("testdata/gurbat_trimmed.DATA");
Parser parser;
RawDeck rawDeck;
parser.parse(multipleKeywordFile.string(), rawDeck);
BOOST_REQUIRE_EQUAL((unsigned)18, rawDeck.getNumberOfKeywords());
ParserPtr parser(new Parser());
RawDeckPtr rawDeck = parser -> parse(multipleKeywordFile.string());
BOOST_REQUIRE_EQUAL((unsigned) 18, rawDeck -> getNumberOfKeywords());
}
//NOTE: needs statoil dataset
@ -98,9 +91,8 @@ BOOST_AUTO_TEST_CASE(ParseFileWithManyKeywords) {
BOOST_AUTO_TEST_CASE(ParseFullTestFile) {
boost::filesystem::path multipleKeywordFile("testdata/ECLIPSE.DATA");
Parser parser;
RawDeck rawDeck;
parser.parse(multipleKeywordFile.string(), rawDeck);
BOOST_REQUIRE_EQUAL((unsigned)73, rawDeck.getNumberOfKeywords());
ParserPtr parser(new Parser());
RawDeckPtr rawDeck = parser -> parse(multipleKeywordFile.string());
BOOST_REQUIRE_EQUAL((unsigned) 73, rawDeck -> getNumberOfKeywords());
}