From 4095e87282393e302b7e01238ca3ae39bae73288 Mon Sep 17 00:00:00 2001 From: gereon Date: Thu, 6 Dec 2012 21:39:00 +0100 Subject: [PATCH] changing pointer to std::shared_ptr parsers return some kind of object and it is not clear who owns this object, i.e. who is responsible to delete it. --- src/models/Dtmc.h | 19 ++++++------------- src/models/GraphTransitions.h | 7 ++++--- src/parser/readLabFile.cpp | 2 +- src/parser/readLabFile.h | 6 ++++-- src/parser/readTraFile.cpp | 2 +- src/parser/readTraFile.h | 6 ++++-- src/utility/ioUtility.cpp | 2 +- test/parser/read_lab_file_test.cpp | 7 ++++--- test/parser/read_tra_file_test.cpp | 4 +--- 9 files changed, 26 insertions(+), 29 deletions(-) diff --git a/src/models/Dtmc.h b/src/models/Dtmc.h index 8ac46ffee..83bba3d6e 100644 --- a/src/models/Dtmc.h +++ b/src/models/Dtmc.h @@ -9,6 +9,7 @@ #define DTMC_H_ #include +#include #include "AtomicPropositionsLabeling.h" #include "GraphTransitions.h" @@ -35,10 +36,8 @@ public: * @param stateLabeling The labeling that assigns a set of atomic * propositions to each state. */ - Dtmc(mrmc::storage::SquareSparseMatrix* probabilityMatrix, mrmc::models::AtomicPropositionsLabeling* stateLabeling) - : backwardTransitions(nullptr) { - this->probabilityMatrix = probabilityMatrix; - this->stateLabeling = stateLabeling; + Dtmc(std::shared_ptr> probabilityMatrix, std::shared_ptr stateLabeling) + : probabilityMatrix(probabilityMatrix), stateLabeling(stateLabeling), backwardTransitions(nullptr) { } //! Copy Constructor @@ -58,12 +57,6 @@ public: * Destructor. Frees the matrix and labeling associated with this DTMC. */ ~Dtmc() { - if (this->probabilityMatrix != nullptr) { - delete this->probabilityMatrix; - } - if (this->stateLabeling != nullptr) { - delete this->stateLabeling; - } if (this->backwardTransitions != nullptr) { delete this->backwardTransitions; } @@ -102,7 +95,7 @@ public: * @return A pointer to the matrix representing the transition probability * function. */ - mrmc::storage::SquareSparseMatrix* getTransitionProbabilityMatrix() const { + std::shared_ptr> getTransitionProbabilityMatrix() const { return this->probabilityMatrix; } @@ -146,10 +139,10 @@ public: private: /*! A matrix representing the transition probability function of the DTMC. */ - mrmc::storage::SquareSparseMatrix* probabilityMatrix; + std::shared_ptr> probabilityMatrix; /*! The labeling of the states of the DTMC. */ - mrmc::models::AtomicPropositionsLabeling* stateLabeling; + std::shared_ptr stateLabeling; /*! * A data structure that stores the predecessors for all states. This is diff --git a/src/models/GraphTransitions.h b/src/models/GraphTransitions.h index 7b60e92f3..4563bfc59 100644 --- a/src/models/GraphTransitions.h +++ b/src/models/GraphTransitions.h @@ -11,6 +11,7 @@ #include "src/storage/SquareSparseMatrix.h" #include +#include namespace mrmc { @@ -38,7 +39,7 @@ public: * @param forward If set to true, this objects will store the graph structure * of the backwards transition relation. */ - GraphTransitions(mrmc::storage::SquareSparseMatrix* transitionMatrix, bool forward) + GraphTransitions(std::shared_ptr> transitionMatrix, bool forward) : successorList(nullptr), stateIndications(nullptr), numberOfStates(transitionMatrix->getRowCount()), numberOfNonZeroTransitions(transitionMatrix->getNonZeroEntryCount()) { if (forward) { this->initializeForward(transitionMatrix); @@ -86,7 +87,7 @@ private: * Initializes this graph transitions object using the forward transition * relation given by means of a sparse matrix. */ - void initializeForward(mrmc::storage::SquareSparseMatrix* transitionMatrix) { + void initializeForward(std::shared_ptr> transitionMatrix) { this->successorList = new uint_fast64_t[numberOfNonZeroTransitions]; this->stateIndications = new uint_fast64_t[numberOfStates + 1]; @@ -108,7 +109,7 @@ private: * relation, whose forward transition relation is given by means of a sparse * matrix. */ - void initializeBackward(mrmc::storage::SquareSparseMatrix* transitionMatrix) { + void initializeBackward(std::shared_ptr> transitionMatrix) { this->successorList = new uint_fast64_t[numberOfNonZeroTransitions](); this->stateIndications = new uint_fast64_t[numberOfStates + 1](); diff --git a/src/parser/readLabFile.cpp b/src/parser/readLabFile.cpp index ebcacd099..42a7566e2 100644 --- a/src/parser/readLabFile.cpp +++ b/src/parser/readLabFile.cpp @@ -106,7 +106,7 @@ LabParser::LabParser(uint_fast64_t node_count, const char * filename) /* * create labeling object with given node and proposition count */ - this->labeling = new mrmc::models::AtomicPropositionsLabeling(node_count, proposition_count); + this->labeling = std::shared_ptr(new mrmc::models::AtomicPropositionsLabeling(node_count, proposition_count)); /* * second run: add propositions and node labels to labeling diff --git a/src/parser/readLabFile.h b/src/parser/readLabFile.h index 67aec1178..5aa142d4d 100644 --- a/src/parser/readLabFile.h +++ b/src/parser/readLabFile.h @@ -6,6 +6,8 @@ #include "src/parser/parser.h" +#include + namespace mrmc { namespace parser { @@ -20,13 +22,13 @@ class LabParser : Parser public: LabParser(uint_fast64_t node_count, const char* filename); - mrmc::models::AtomicPropositionsLabeling* getLabeling() + std::shared_ptr getLabeling() { return this->labeling; } private: - mrmc::models::AtomicPropositionsLabeling* labeling; + std::shared_ptr labeling; }; } // namespace parser diff --git a/src/parser/readTraFile.cpp b/src/parser/readTraFile.cpp index 0871bfebd..6a73bff0a 100644 --- a/src/parser/readTraFile.cpp +++ b/src/parser/readTraFile.cpp @@ -166,7 +166,7 @@ TraParser::TraParser(const char * filename) * non-zero elements has to be specified (which is non_zero, computed by make_first_pass) */ LOG4CPLUS_INFO(logger, "Attempting to create matrix of size " << (maxnode+1) << " x " << (maxnode+1) << "."); - this->matrix = new mrmc::storage::SquareSparseMatrix(maxnode + 1); + this->matrix = std::shared_ptr>(new mrmc::storage::SquareSparseMatrix(maxnode + 1)); if (this->matrix == NULL) { LOG4CPLUS_ERROR(logger, "Could not create matrix of size " << (maxnode+1) << " x " << (maxnode+1) << "."); diff --git a/src/parser/readTraFile.h b/src/parser/readTraFile.h index 7ec735123..c46167e37 100644 --- a/src/parser/readTraFile.h +++ b/src/parser/readTraFile.h @@ -5,6 +5,8 @@ #include "src/parser/parser.h" +#include + namespace mrmc { namespace parser { @@ -20,13 +22,13 @@ class TraParser : Parser public: TraParser(const char* filename); - mrmc::storage::SquareSparseMatrix* getMatrix() + std::shared_ptr> getMatrix() { return this->matrix; } private: - mrmc::storage::SquareSparseMatrix* matrix; + std::shared_ptr> matrix; uint_fast32_t firstPass(char* buf, uint_fast32_t &maxnode); diff --git a/src/utility/ioUtility.cpp b/src/utility/ioUtility.cpp index 6a9a0808f..01872a3c9 100644 --- a/src/utility/ioUtility.cpp +++ b/src/utility/ioUtility.cpp @@ -16,7 +16,7 @@ namespace mrmc { namespace utility { void dtmcToDot(mrmc::models::Dtmc* dtmc, std::string filename) { - mrmc::storage::SquareSparseMatrix* matrix = dtmc->getTransitionProbabilityMatrix(); + std::shared_ptr> matrix(dtmc->getTransitionProbabilityMatrix()); double* diagonal_storage = matrix->getDiagonalStoragePointer(); std::ofstream file; diff --git a/test/parser/read_lab_file_test.cpp b/test/parser/read_lab_file_test.cpp index 8a14c872a..24eb98356 100644 --- a/test/parser/read_lab_file_test.cpp +++ b/test/parser/read_lab_file_test.cpp @@ -12,6 +12,8 @@ #include "src/exceptions/file_IO_exception.h" #include "src/exceptions/wrong_file_format.h" +#include + TEST(ReadLabFileTest, NonExistingFileTest) { //No matter what happens, please don't create a file with the name "nonExistingFile.not"! :-) ASSERT_THROW(mrmc::parser::LabParser(0,MRMC_CPP_TESTS_BASE_PATH "/nonExistingFile.not"), mrmc::exceptions::file_IO_exception); @@ -19,12 +21,12 @@ TEST(ReadLabFileTest, NonExistingFileTest) { TEST(ReadLabFileTest, ParseTest) { //This test is based on a test case from the original MRMC. - mrmc::models::AtomicPropositionsLabeling* labeling = NULL; + mrmc::parser::LabParser* parser; //Parsing the file ASSERT_NO_THROW(parser = new mrmc::parser::LabParser(12, MRMC_CPP_TESTS_BASE_PATH "/parser/lab_files/pctl_general_input_01.lab")); - labeling = parser->getLabeling(); + std::shared_ptr labeling(parser->getLabeling()); //Checking whether all propositions are in the labelling @@ -77,7 +79,6 @@ TEST(ReadLabFileTest, ParseTest) { ASSERT_FALSE(labeling->stateHasAtomicProposition(smth,11)); //Deleting the labeling - delete labeling; delete parser; } else { FAIL(); diff --git a/test/parser/read_tra_file_test.cpp b/test/parser/read_tra_file_test.cpp index 5e8e230f8..509ea79d8 100644 --- a/test/parser/read_tra_file_test.cpp +++ b/test/parser/read_tra_file_test.cpp @@ -22,10 +22,9 @@ TEST(ReadTraFileTest, NonExistingFileTest) { /* The following test case is based on one of the original MRMC test cases */ TEST(ReadTraFileTest, ParseFileTest1) { - mrmc::storage::SquareSparseMatrix *result = NULL; mrmc::parser::TraParser* parser; ASSERT_NO_THROW(parser = new mrmc::parser::TraParser(MRMC_CPP_TESTS_BASE_PATH "/parser/tra_files/csl_general_input_01.tra")); - result = parser->getMatrix(); + std::shared_ptr> result = parser->getMatrix(); if (result != NULL) { double val = 0; @@ -63,7 +62,6 @@ TEST(ReadTraFileTest, ParseFileTest1) { ASSERT_TRUE(result->getValue(4,4,&val)); ASSERT_EQ(val,0); - delete result; delete parser; } else { FAIL();