Skip to content

Instantly share code, notes, and snippets.

@glinesbdev
Last active August 8, 2022 16:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save glinesbdev/177969e130487c969f1fc05d3385e53c to your computer and use it in GitHub Desktop.
Save glinesbdev/177969e130487c969f1fc05d3385e53c to your computer and use it in GitHub Desktop.
C++ Rule of 5
#include "LTexture.h"
LTexture::~LTexture()
{
Free();
}
LTexture::LTexture(const LTexture& other)
: mWidth(other.mWidth)
, mHeight(other.mHeight)
, mTexture(other.mTexture)
{
}
LTexture& LTexture::operator=(const LTexture& other)
{
mWidth = other.mWidth;
mHeight = other.mHeight;
mTexture = other.mTexture;
return *this;
}
LTexture::LTexture(LTexture&& other) noexcept
{
Swap(*this, other);
}
LTexture& LTexture::operator=(LTexture&& other) noexcept
{
if (this != &other)
{
Swap(*this, other);
}
return *this;
}
void Swap(LTexture& first, LTexture& second) noexcept
{
using std::swap;
swap(first.mWidth, second.mWidth);
swap(first.mHeight, second.mHeight);
swap(first.mTexture, second.mTexture);
}
// included for Destructor reference
void LTexture::Free()
{
if (mTexture)
{
SDL_DestroyTexture(mTexture);
mTexture = nullptr;
mWidth = 0;
mHeight = 0;
}
}
#include <utility>
class LTexture
{
public:
// Constructor / Destructor
LTexture() = default;
~LTexture();
// Copy Policy
LTexture(const LTexture& other);
LTexture& operator=(const LTexture& other);
// Move Policy
LTexture(LTexture&& other) noexcept;
LTexture& operator=(LTexture&& other) noexcept;
public:
void Free(); // included for Destructor reference
private:
friend void Swap(LTexture& first, LTexture& second) noexcept;
private:
SDL_Texture* mTexture{ nullptr };
int mWidth{ 0 };
int mHeight{ 0 };
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment