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