Commit f5212a32 authored by Yann Boucher's avatar Yann Boucher
Browse files

Fixed a bug related to dangling constant references with StructureReader and StructureWriter

parent d5be8094
Pipeline #77943 passed with stages
in 17 seconds
......@@ -65,7 +65,7 @@ public:
auto minmax_height = std::minmax_element(m_cells.begin(), m_cells.end(), [](const auto& a, const auto& b)
{ return a.first.y < b.first.y; });
m_height = minmax_width.second->first.y - minmax_width.first->first.y + 1;
m_height = minmax_height.second->first.y - minmax_width.first->first.y + 1;
}
//! \brief Fonction vidant la structure, la laissant vide.
void clear()
......
......@@ -69,7 +69,7 @@ protected:
private:
size_t m_idx;
const std::string& m_data;
std::string m_data;
};
//! \class RLEStructureReader
......
......@@ -22,16 +22,11 @@ class Structure;
class StructureWriter
{
public:
//! \brief Constructeur prenant en argument les données du fichier à lire sous forme de std::string
StructureWriter(const Structure& in_struct):
m_struct(in_struct)
{}
//! \brief Sauvegarde la structure sous forme d'un std::string.
virtual std::string save_structure() const = 0;
protected:
//! \brief Référence constante vers la structure à sauvegarder
const Structure& m_struct;
//! \brief Constructeur instanciant un StructureWriter.
StructureWriter() = default;
//! \brief Sauvegarde la structure 's' sous forme d'un std::string.
//! \param s Structure à sauvegarde
virtual std::string save_structure(const Structure& s) const = 0;
};
//! \class RLEStructureWrite
......@@ -39,11 +34,7 @@ protected:
class RLEStructureWriter : public StructureWriter
{
public:
RLEStructureWriter(const Structure& in_struct):
StructureWriter(in_struct)
{}
virtual std::string save_structure() const;
virtual std::string save_structure(const Structure& s) const;
private:
std::string state_to_str(unsigned state);
......@@ -54,11 +45,7 @@ private:
class JSONStructureWriter : public StructureWriter
{
public:
JSONStructureWriter(const Structure& in_struct):
StructureWriter(in_struct)
{}
virtual std::string save_structure() const;
virtual std::string save_structure(const Structure& s) const;
};
#endif // STRUCTUREWRITER_HPP
......@@ -5,7 +5,7 @@
\version 1
\brief StructureReader
**/
#define private public
#include "structurereader.hpp"
#include <QByteArray>
......@@ -87,7 +87,7 @@ int StructureReader::read_int()
int val;
try
{
val = std::stoi(&m_data.data()[m_idx], &cars_read, 10);
val = std::stoi(&m_data.c_str()[m_idx], &cars_read, 10);
}
catch (std::exception& e)
{
......
......@@ -15,12 +15,12 @@
#include <QJsonArray>
#include <QJsonDocument>
std::string JSONStructureWriter::save_structure() const
std::string JSONStructureWriter::save_structure(const Structure& s) const
{
QJsonObject root;
QJsonArray cells;
for (const auto& pair : m_struct)
for (const auto& pair : s)
{
QJsonObject entry;
entry.insert("x", pair.first.x);
......@@ -36,11 +36,11 @@ std::string JSONStructureWriter::save_structure() const
return doc.toJson().toStdString();
}
std::string RLEStructureWriter::save_structure() const
std::string RLEStructureWriter::save_structure(const Structure& s) const
{
std::string result;
result += "x = " + std::to_string(m_struct.width()) + ", ";
result += "y = " + std::to_string(m_struct.height()) + "\n";
result += "x = " + std::to_string(s.width()) + ", ";
result += "y = " + std::to_string(s.height()) + "\n";
// TODO : implement
return "<unimplemented>";
......
......@@ -45,7 +45,7 @@ void CellulutTests::test_rle_structurereader()
catch (const std::exception& ex)
{
fprintf(stderr, "Error is %s\n", ex.what());
throw;
QFAIL("Exception thrown");
}
}
......@@ -60,7 +60,7 @@ void CellulutTests::test_rle_structurereader()
catch (const std::exception& ex)
{
fprintf(stderr, "Error is %s\n", ex.what());
throw;
QFAIL("Exception thrown");
}
}
......@@ -84,7 +84,7 @@ void CellulutTests::test_rle_structurereader()
catch (const std::exception& ex)
{
fprintf(stderr, "Error is %s\n", ex.what());
throw;
QFAIL("Exception thrown");
}
}
}
......@@ -103,7 +103,7 @@ void CellulutTests::test_json_structurereader()
catch (const std::exception& ex)
{
fprintf(stderr, "Error is %s\n", ex.what());
throw;
QFAIL("Exception thrown");
}
}
}
......@@ -19,9 +19,10 @@ void CellulutTests::test_json_structurewriter()
coords.push_back({{1, 2}, 3});
coords.push_back({{-1, 2}, 1});
try
{
JSONStructureWriter writer(Structure{coords.begin(), coords.end()});
auto out = writer.save_structure();
JSONStructureWriter writer;
auto out = writer.save_structure(Structure{coords.begin(), coords.end()});
//printf("%s\n", out.c_str());
......@@ -30,4 +31,9 @@ void CellulutTests::test_json_structurewriter()
QVERIFY(std::is_permutation(struct_out.begin(), struct_out.end(), coords.begin()));
}
catch (const std::exception& ex)
{
fprintf(stderr, "Error is %s\n", ex.what());
QFAIL("Exception thrown");
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment