Implémente constructeur copie, move, etc...
This commit is contained in:
parent
a7876503c8
commit
bc3c789214
@ -497,7 +497,7 @@ void GOLTeamH::updateImage(uint32_t* buffer, size_t buffer_size) const
|
|||||||
auto* s_ptr{ buffer }, * e_ptr{ buffer + buffer_size };
|
auto* s_ptr{ buffer }, * e_ptr{ buffer + buffer_size };
|
||||||
|
|
||||||
// Pointeur qui se promène en mémoire.
|
// 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.
|
// On itère sur chaque éléments du tableau et on associe la couleur.
|
||||||
while (s_ptr < e_ptr) {
|
while (s_ptr < e_ptr) {
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
#include "GOL.h"
|
#include "GOL.h"
|
||||||
|
|
||||||
#include <optional>
|
#include <optional>
|
||||||
|
#include <utility>
|
||||||
|
|
||||||
// Constructeur Grid par défaut
|
// Constructeur Grid par défaut
|
||||||
GridTeamH::GridTeamH()
|
GridTeamH::GridTeamH()
|
||||||
@ -16,6 +17,58 @@ GridTeamH::GridTeamH(size_t width, size_t height, CellType initValue)
|
|||||||
resize(width, height, 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
|
// Destructeur Grid
|
||||||
GridTeamH::~GridTeamH()
|
GridTeamH::~GridTeamH()
|
||||||
{
|
{
|
||||||
@ -38,10 +91,12 @@ void GridTeamH::resize(size_t width, size_t height, CellType initValue)
|
|||||||
fill(initValue, true);
|
fill(initValue, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
void GridTeamH::dealloc()
|
void GridTeamH::dealloc() const
|
||||||
{
|
{
|
||||||
delete[] mData;
|
if (mData && mIntermediateData) {
|
||||||
delete[] mIntermediateData;
|
delete[] mData;
|
||||||
|
delete[] mIntermediateData;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Accesseur retournant la valeur d'une cellule à une certaine coordonnée.
|
// Accesseur retournant la valeur d'une cellule à une certaine coordonnée.
|
||||||
|
@ -15,15 +15,16 @@ class GridTeamH
|
|||||||
public:
|
public:
|
||||||
// Définition des types
|
// Définition des types
|
||||||
using CellType = GOL::State;
|
using CellType = GOL::State;
|
||||||
using DataType = CellType *;
|
using DataType = CellType*;
|
||||||
|
|
||||||
// Définition des constructeurs / destructeur
|
// Définition des constructeurs / destructeur
|
||||||
GridTeamH();
|
GridTeamH();
|
||||||
GridTeamH(size_t width, size_t height, CellType initValue = CellType{});
|
GridTeamH(size_t width, size_t height, CellType initValue = CellType{});
|
||||||
GridTeamH(GridTeamH const&) = delete;
|
|
||||||
GridTeamH(GridTeamH&&) = delete;
|
GridTeamH(GridTeamH const&);
|
||||||
GridTeamH& operator=(GridTeamH const&) = delete;
|
GridTeamH(GridTeamH&&) noexcept;
|
||||||
GridTeamH& operator=(GridTeamH&&) = delete;
|
GridTeamH& operator=(GridTeamH const&);
|
||||||
|
GridTeamH& operator=(GridTeamH&&) noexcept;
|
||||||
~GridTeamH();
|
~GridTeamH();
|
||||||
|
|
||||||
// Accesseurs et mutateurs de la grille
|
// Accesseurs et mutateurs de la grille
|
||||||
@ -31,10 +32,9 @@ public:
|
|||||||
size_t width() const { return mWidth; }
|
size_t width() const { return mWidth; }
|
||||||
size_t height() const { return mHeight; }
|
size_t height() const { return mHeight; }
|
||||||
size_t size() const { return mHeight * mWidth; }
|
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 resize(size_t width, size_t height, CellType initValue = CellType{});
|
||||||
void dealloc();
|
|
||||||
|
|
||||||
// Accesseurs et mutateurs des cellules
|
// Accesseurs et mutateurs des cellules
|
||||||
CellType value(int column, int row) const;
|
CellType value(int column, int row) const;
|
||||||
@ -78,4 +78,5 @@ private:
|
|||||||
std::uniform_real_distribution<> mDistribution;
|
std::uniform_real_distribution<> mDistribution;
|
||||||
|
|
||||||
void fillBorderManipulations(DataType ptr, CellType value) const;
|
void fillBorderManipulations(DataType ptr, CellType value) const;
|
||||||
|
void dealloc() const;
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user