Browse Source

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.
tempestpy_adaptions
gereon 12 years ago
parent
commit
4095e87282
  1. 19
      src/models/Dtmc.h
  2. 7
      src/models/GraphTransitions.h
  3. 2
      src/parser/readLabFile.cpp
  4. 6
      src/parser/readLabFile.h
  5. 2
      src/parser/readTraFile.cpp
  6. 6
      src/parser/readTraFile.h
  7. 2
      src/utility/ioUtility.cpp
  8. 7
      test/parser/read_lab_file_test.cpp
  9. 4
      test/parser/read_tra_file_test.cpp

19
src/models/Dtmc.h

@ -9,6 +9,7 @@
#define DTMC_H_ #define DTMC_H_
#include <ostream> #include <ostream>
#include <memory>
#include "AtomicPropositionsLabeling.h" #include "AtomicPropositionsLabeling.h"
#include "GraphTransitions.h" #include "GraphTransitions.h"
@ -35,10 +36,8 @@ public:
* @param stateLabeling The labeling that assigns a set of atomic * @param stateLabeling The labeling that assigns a set of atomic
* propositions to each state. * propositions to each state.
*/ */
Dtmc(mrmc::storage::SquareSparseMatrix<T>* probabilityMatrix, mrmc::models::AtomicPropositionsLabeling* stateLabeling)
: backwardTransitions(nullptr) {
this->probabilityMatrix = probabilityMatrix;
this->stateLabeling = stateLabeling;
Dtmc(std::shared_ptr<mrmc::storage::SquareSparseMatrix<T>> probabilityMatrix, std::shared_ptr<mrmc::models::AtomicPropositionsLabeling> stateLabeling)
: probabilityMatrix(probabilityMatrix), stateLabeling(stateLabeling), backwardTransitions(nullptr) {
} }
//! Copy Constructor //! Copy Constructor
@ -58,12 +57,6 @@ public:
* Destructor. Frees the matrix and labeling associated with this DTMC. * Destructor. Frees the matrix and labeling associated with this DTMC.
*/ */
~Dtmc() { ~Dtmc() {
if (this->probabilityMatrix != nullptr) {
delete this->probabilityMatrix;
}
if (this->stateLabeling != nullptr) {
delete this->stateLabeling;
}
if (this->backwardTransitions != nullptr) { if (this->backwardTransitions != nullptr) {
delete this->backwardTransitions; delete this->backwardTransitions;
} }
@ -102,7 +95,7 @@ public:
* @return A pointer to the matrix representing the transition probability * @return A pointer to the matrix representing the transition probability
* function. * function.
*/ */
mrmc::storage::SquareSparseMatrix<T>* getTransitionProbabilityMatrix() const {
std::shared_ptr<mrmc::storage::SquareSparseMatrix<T>> getTransitionProbabilityMatrix() const {
return this->probabilityMatrix; return this->probabilityMatrix;
} }
@ -146,10 +139,10 @@ public:
private: private:
/*! A matrix representing the transition probability function of the DTMC. */ /*! A matrix representing the transition probability function of the DTMC. */
mrmc::storage::SquareSparseMatrix<T>* probabilityMatrix;
std::shared_ptr<mrmc::storage::SquareSparseMatrix<T>> probabilityMatrix;
/*! The labeling of the states of the DTMC. */ /*! The labeling of the states of the DTMC. */
mrmc::models::AtomicPropositionsLabeling* stateLabeling;
std::shared_ptr<mrmc::models::AtomicPropositionsLabeling> stateLabeling;
/*! /*!
* A data structure that stores the predecessors for all states. This is * A data structure that stores the predecessors for all states. This is

7
src/models/GraphTransitions.h

@ -11,6 +11,7 @@
#include "src/storage/SquareSparseMatrix.h" #include "src/storage/SquareSparseMatrix.h"
#include <algorithm> #include <algorithm>
#include <memory>
namespace mrmc { namespace mrmc {
@ -38,7 +39,7 @@ public:
* @param forward If set to true, this objects will store the graph structure * @param forward If set to true, this objects will store the graph structure
* of the backwards transition relation. * of the backwards transition relation.
*/ */
GraphTransitions(mrmc::storage::SquareSparseMatrix<T>* transitionMatrix, bool forward)
GraphTransitions(std::shared_ptr<mrmc::storage::SquareSparseMatrix<T>> transitionMatrix, bool forward)
: successorList(nullptr), stateIndications(nullptr), numberOfStates(transitionMatrix->getRowCount()), numberOfNonZeroTransitions(transitionMatrix->getNonZeroEntryCount()) { : successorList(nullptr), stateIndications(nullptr), numberOfStates(transitionMatrix->getRowCount()), numberOfNonZeroTransitions(transitionMatrix->getNonZeroEntryCount()) {
if (forward) { if (forward) {
this->initializeForward(transitionMatrix); this->initializeForward(transitionMatrix);
@ -86,7 +87,7 @@ private:
* Initializes this graph transitions object using the forward transition * Initializes this graph transitions object using the forward transition
* relation given by means of a sparse matrix. * relation given by means of a sparse matrix.
*/ */
void initializeForward(mrmc::storage::SquareSparseMatrix<T>* transitionMatrix) {
void initializeForward(std::shared_ptr<mrmc::storage::SquareSparseMatrix<T>> transitionMatrix) {
this->successorList = new uint_fast64_t[numberOfNonZeroTransitions]; this->successorList = new uint_fast64_t[numberOfNonZeroTransitions];
this->stateIndications = new uint_fast64_t[numberOfStates + 1]; 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 * relation, whose forward transition relation is given by means of a sparse
* matrix. * matrix.
*/ */
void initializeBackward(mrmc::storage::SquareSparseMatrix<T>* transitionMatrix) {
void initializeBackward(std::shared_ptr<mrmc::storage::SquareSparseMatrix<T>> transitionMatrix) {
this->successorList = new uint_fast64_t[numberOfNonZeroTransitions](); this->successorList = new uint_fast64_t[numberOfNonZeroTransitions]();
this->stateIndications = new uint_fast64_t[numberOfStates + 1](); this->stateIndications = new uint_fast64_t[numberOfStates + 1]();

2
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 * create labeling object with given node and proposition count
*/ */
this->labeling = new mrmc::models::AtomicPropositionsLabeling(node_count, proposition_count);
this->labeling = std::shared_ptr<mrmc::models::AtomicPropositionsLabeling>(new mrmc::models::AtomicPropositionsLabeling(node_count, proposition_count));
/* /*
* second run: add propositions and node labels to labeling * second run: add propositions and node labels to labeling

6
src/parser/readLabFile.h

@ -6,6 +6,8 @@
#include "src/parser/parser.h" #include "src/parser/parser.h"
#include <memory>
namespace mrmc { namespace mrmc {
namespace parser { namespace parser {
@ -20,13 +22,13 @@ class LabParser : Parser
public: public:
LabParser(uint_fast64_t node_count, const char* filename); LabParser(uint_fast64_t node_count, const char* filename);
mrmc::models::AtomicPropositionsLabeling* getLabeling()
std::shared_ptr<mrmc::models::AtomicPropositionsLabeling> getLabeling()
{ {
return this->labeling; return this->labeling;
} }
private: private:
mrmc::models::AtomicPropositionsLabeling* labeling;
std::shared_ptr<mrmc::models::AtomicPropositionsLabeling> labeling;
}; };
} // namespace parser } // namespace parser

2
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) * 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) << "."); LOG4CPLUS_INFO(logger, "Attempting to create matrix of size " << (maxnode+1) << " x " << (maxnode+1) << ".");
this->matrix = new mrmc::storage::SquareSparseMatrix<double>(maxnode + 1);
this->matrix = std::shared_ptr<mrmc::storage::SquareSparseMatrix<double>>(new mrmc::storage::SquareSparseMatrix<double>(maxnode + 1));
if (this->matrix == NULL) if (this->matrix == NULL)
{ {
LOG4CPLUS_ERROR(logger, "Could not create matrix of size " << (maxnode+1) << " x " << (maxnode+1) << "."); LOG4CPLUS_ERROR(logger, "Could not create matrix of size " << (maxnode+1) << " x " << (maxnode+1) << ".");

6
src/parser/readTraFile.h

@ -5,6 +5,8 @@
#include "src/parser/parser.h" #include "src/parser/parser.h"
#include <memory>
namespace mrmc { namespace mrmc {
namespace parser { namespace parser {
@ -20,13 +22,13 @@ class TraParser : Parser
public: public:
TraParser(const char* filename); TraParser(const char* filename);
mrmc::storage::SquareSparseMatrix<double>* getMatrix()
std::shared_ptr<mrmc::storage::SquareSparseMatrix<double>> getMatrix()
{ {
return this->matrix; return this->matrix;
} }
private: private:
mrmc::storage::SquareSparseMatrix<double>* matrix;
std::shared_ptr<mrmc::storage::SquareSparseMatrix<double>> matrix;
uint_fast32_t firstPass(char* buf, uint_fast32_t &maxnode); uint_fast32_t firstPass(char* buf, uint_fast32_t &maxnode);

2
src/utility/ioUtility.cpp

@ -16,7 +16,7 @@ namespace mrmc {
namespace utility { namespace utility {
void dtmcToDot(mrmc::models::Dtmc<double>* dtmc, std::string filename) { void dtmcToDot(mrmc::models::Dtmc<double>* dtmc, std::string filename) {
mrmc::storage::SquareSparseMatrix<double>* matrix = dtmc->getTransitionProbabilityMatrix();
std::shared_ptr<mrmc::storage::SquareSparseMatrix<double>> matrix(dtmc->getTransitionProbabilityMatrix());
double* diagonal_storage = matrix->getDiagonalStoragePointer(); double* diagonal_storage = matrix->getDiagonalStoragePointer();
std::ofstream file; std::ofstream file;

7
test/parser/read_lab_file_test.cpp

@ -12,6 +12,8 @@
#include "src/exceptions/file_IO_exception.h" #include "src/exceptions/file_IO_exception.h"
#include "src/exceptions/wrong_file_format.h" #include "src/exceptions/wrong_file_format.h"
#include <memory>
TEST(ReadLabFileTest, NonExistingFileTest) { TEST(ReadLabFileTest, NonExistingFileTest) {
//No matter what happens, please don't create a file with the name "nonExistingFile.not"! :-) //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); 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) { TEST(ReadLabFileTest, ParseTest) {
//This test is based on a test case from the original MRMC. //This test is based on a test case from the original MRMC.
mrmc::models::AtomicPropositionsLabeling* labeling = NULL;
mrmc::parser::LabParser* parser; mrmc::parser::LabParser* parser;
//Parsing the file //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")); 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<mrmc::models::AtomicPropositionsLabeling> labeling(parser->getLabeling());
//Checking whether all propositions are in the labelling //Checking whether all propositions are in the labelling
@ -77,7 +79,6 @@ TEST(ReadLabFileTest, ParseTest) {
ASSERT_FALSE(labeling->stateHasAtomicProposition(smth,11)); ASSERT_FALSE(labeling->stateHasAtomicProposition(smth,11));
//Deleting the labeling //Deleting the labeling
delete labeling;
delete parser; delete parser;
} else { } else {
FAIL(); FAIL();

4
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 /* The following test case is based on one of the original MRMC test cases
*/ */
TEST(ReadTraFileTest, ParseFileTest1) { TEST(ReadTraFileTest, ParseFileTest1) {
mrmc::storage::SquareSparseMatrix<double> *result = NULL;
mrmc::parser::TraParser* parser; 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")); 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<mrmc::storage::SquareSparseMatrix<double>> result = parser->getMatrix();
if (result != NULL) { if (result != NULL) {
double val = 0; double val = 0;
@ -63,7 +62,6 @@ TEST(ReadTraFileTest, ParseFileTest1) {
ASSERT_TRUE(result->getValue(4,4,&val)); ASSERT_TRUE(result->getValue(4,4,&val));
ASSERT_EQ(val,0); ASSERT_EQ(val,0);
delete result;
delete parser; delete parser;
} else { } else {
FAIL(); FAIL();

Loading…
Cancel
Save