Implémente constructeur copie, move, etc...

This commit is contained in:
Timothée Leclaire-Fournier 2024-01-24 14:14:51 -05:00
parent a7876503c8
commit bc3c789214
3 changed files with 67 additions and 11 deletions

View File

@ -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<const uint8_t*>(mData.data()) };
auto* ptrGrid{ reinterpret_cast<uint8_t*>(mData.data()) };
// On itère sur chaque éléments du tableau et on associe la couleur.
while (s_ptr < e_ptr) {

View File

@ -2,6 +2,7 @@
#include "GOL.h"
#include <optional>
#include <utility>
// 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,11 +91,13 @@ void GridTeamH::resize(size_t width, size_t height, CellType initValue)
fill(initValue, true);
}
void GridTeamH::dealloc()
void GridTeamH::dealloc() const
{
if (mData && mIntermediateData) {
delete[] mData;
delete[] mIntermediateData;
}
}
// Accesseur retournant la valeur d'une cellule à une certaine coordonnée.
GridTeamH::CellType GridTeamH::value(int column, int row) const

View File

@ -20,10 +20,11 @@ public:
// 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
@ -34,7 +35,6 @@ public:
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;
};