diff --git a/GPA675Lab1GOL/GOLTeamH.cpp b/GPA675Lab1GOL/GOLTeamH.cpp index 2550278..4ecde48 100644 --- a/GPA675Lab1GOL/GOLTeamH.cpp +++ b/GPA675Lab1GOL/GOLTeamH.cpp @@ -497,7 +497,7 @@ void GOLTeamH::updateImage(uint32_t* buffer, size_t buffer_size) const auto* s_ptr{ buffer }, * e_ptr{ buffer + buffer_size }; // Pointeur qui se promène en mémoire. - auto* ptrGrid{ reinterpret_cast(mData.data()) }; + auto* ptrGrid{ reinterpret_cast(mData.data()) }; // On itère sur chaque éléments du tableau et on associe la couleur. while (s_ptr < e_ptr) { diff --git a/GPA675Lab1GOL/GridTeamH.cpp b/GPA675Lab1GOL/GridTeamH.cpp index f5c16dc..9e637cf 100644 --- a/GPA675Lab1GOL/GridTeamH.cpp +++ b/GPA675Lab1GOL/GridTeamH.cpp @@ -2,6 +2,7 @@ #include "GOL.h" #include +#include // Constructeur Grid par défaut GridTeamH::GridTeamH() @@ -16,6 +17,58 @@ GridTeamH::GridTeamH(size_t width, size_t height, CellType initValue) resize(width, height, initValue); } +GridTeamH::GridTeamH(GridTeamH const& cpy) + : GridTeamH(cpy.width(), cpy.height(), CellType::alive) +{ + mAliveCount = cpy.mAliveCount; + memcpy(mData, cpy.mData, cpy.size() * sizeof(CellType)); + memcpy(mIntermediateData, cpy.mIntermediateData, cpy.size() * sizeof(CellType)); +} + +// https://learn.microsoft.com/en-us/cpp/cpp/move-constructors-and-move-assignment-operators-cpp +GridTeamH::GridTeamH(GridTeamH&& mv) noexcept +{ + *this = std::move(mv); +} + +GridTeamH& GridTeamH::operator=(GridTeamH const& cpy) +{ + // Il ne faut pas se copier soi même. + if (this != &cpy) { + dealloc(); + + mWidth = cpy.mWidth; + mHeight = cpy.mHeight; + mAliveCount = cpy.mAliveCount; + + memcpy(mData, cpy.mData, cpy.size() * sizeof(CellType)); + memcpy(mIntermediateData, cpy.mIntermediateData, cpy.size() * sizeof(CellType)); + } + + return *this; +} + +// https://learn.microsoft.com/en-us/cpp/cpp/move-constructors-and-move-assignment-operators-cpp +GridTeamH& GridTeamH::operator=(GridTeamH&& mv) noexcept +{ + // Il ne faut pas se copier soi même. + if (this != &mv) { + dealloc(); + + mAliveCount = std::move(mv.mAliveCount); + mWidth = std::move(mv.mWidth); + mHeight = std::move(mv.mHeight); + mData = std::move(mv.mData); + mIntermediateData = std::move(mv.mIntermediateData); + + // Il faut que le destructeur de l'ancien objet soit valide. + mv.mData = nullptr; + mv.mIntermediateData = nullptr; + } + + return *this; +} + // Destructeur Grid GridTeamH::~GridTeamH() { @@ -38,10 +91,12 @@ void GridTeamH::resize(size_t width, size_t height, CellType initValue) fill(initValue, true); } -void GridTeamH::dealloc() +void GridTeamH::dealloc() const { - delete[] mData; - delete[] mIntermediateData; + if (mData && mIntermediateData) { + delete[] mData; + delete[] mIntermediateData; + } } // Accesseur retournant la valeur d'une cellule à une certaine coordonnée. diff --git a/GPA675Lab1GOL/GridTeamH.h b/GPA675Lab1GOL/GridTeamH.h index b51d579..7e83df0 100644 --- a/GPA675Lab1GOL/GridTeamH.h +++ b/GPA675Lab1GOL/GridTeamH.h @@ -15,15 +15,16 @@ class GridTeamH public: // Définition des types using CellType = GOL::State; - using DataType = CellType *; + using DataType = CellType*; // Définition des constructeurs / destructeur GridTeamH(); GridTeamH(size_t width, size_t height, CellType initValue = CellType{}); - GridTeamH(GridTeamH const&) = delete; - GridTeamH(GridTeamH&&) = delete; - GridTeamH& operator=(GridTeamH const&) = delete; - GridTeamH& operator=(GridTeamH&&) = delete; + + GridTeamH(GridTeamH const&); + GridTeamH(GridTeamH&&) noexcept; + GridTeamH& operator=(GridTeamH const&); + GridTeamH& operator=(GridTeamH&&) noexcept; ~GridTeamH(); // Accesseurs et mutateurs de la grille @@ -31,10 +32,9 @@ public: size_t width() const { return mWidth; } size_t height() const { return mHeight; } size_t size() const { return mHeight * mWidth; } - size_t aliveCount() const { return mAliveCount; } + size_t aliveCount() const { return mAliveCount; } void resize(size_t width, size_t height, CellType initValue = CellType{}); - void dealloc(); // Accesseurs et mutateurs des cellules CellType value(int column, int row) const; @@ -78,4 +78,5 @@ private: std::uniform_real_distribution<> mDistribution; void fillBorderManipulations(DataType ptr, CellType value) const; + void dealloc() const; };