Don't use allocations inside Point and Rect

Instead, use boolean validity flag, which is much more practical
This commit is contained in:
Dmitry Marakasov 2014-02-21 17:11:48 +04:00
parent 2c7cb50fd9
commit 7b4b6c051a
4 changed files with 53 additions and 121 deletions

View File

@ -21,56 +21,29 @@
#include <cassert>
#include <SDL2/SDL_rect.h>
#include <SDL2pp/Point.hh>
namespace SDL2pp {
Point::Point() {
Point::Point() : valid_(false) {
}
Point::~Point() {
}
Point::Point(int x, int y) : point_(new SDL_Point) {
point_->x = x;
point_->y = y;
Point::Point(int x, int y) : valid_(true) {
point_.x = x;
point_.y = y;
}
Point Point::Null() {
return Point();
}
Point::Point(const Point& other) {
if (!other.point_) {
point_.reset(nullptr);
} else if (point_ != other.point_) {
point_.reset(new SDL_Point);
point_->x = other.point_->x;
point_->y = other.point_->y;
}
}
Point::Point(Point&&) noexcept = default;
Point& Point::operator=(const Point& other) {
if (!other.point_) {
point_.reset(nullptr);
} else if (point_ != other.point_) {
point_.reset(new SDL_Point);
point_->x = other.point_->x;
point_->y = other.point_->y;
}
return *this;
}
Point& Point::operator=(Point&&) noexcept = default;
bool Point::operator==(const Point& other) const {
if (!point_ || !other.point_)
return point_ == other.point_; // true only if both null
return point_->x == other.point_->x && point_->y == other.point_->y;
if (!valid_ || !other.valid_)
return valid_ == other.valid_; // true only if both null
return point_.x == other.point_.x && point_.y == other.point_.y;
}
bool Point::operator!=(const Point& other) const {
@ -78,35 +51,35 @@ bool Point::operator!=(const Point& other) const {
}
SDL_Point* Point::Get() {
return point_.get();
return &point_;
}
const SDL_Point* Point::Get() const {
return point_.get();
return &point_;
}
bool Point::IsNull() const {
return point_ == nullptr;
return !valid_;
}
int Point::GetX() const {
assert(!IsNull());
return point_->x;
return point_.x;
}
void Point::SetX(int x) {
assert(!IsNull());
point_->x = x;
point_.x = x;
}
int Point::GetY() const {
assert(!IsNull());
return point_->y;
return point_.y;
}
void Point::SetY(int y) {
assert(!IsNull());
point_->y = y;
point_.y = y;
}
}

View File

@ -22,25 +22,14 @@
#ifndef SDL2PP_POINT_HH
#define SDL2PP_POINT_HH
#include <memory>
#include <SDL2/SDL_version.h>
// SDL 2.0.0 doesn't have a name for SDL_Point structure (only
// typedef), so we can't use forward declaration yet. This was
// fixed in later versions starting with 2.0.1
#if SDL_MAJOR_VERSION == 2 && SDL_MINOR_VERSION == 0 && SDL_PATCHLEVEL == 0
# include <SDL2/SDL_rect.h>
#else
struct SDL_Point;
#endif
#include <SDL2/SDL_rect.h>
namespace SDL2pp {
class Point {
private:
std::unique_ptr<SDL_Point> point_;
SDL_Point point_;
bool valid_;
private:
Point();
@ -51,10 +40,10 @@ public:
static Point Null();
Point(const Point& other);
Point(Point&&) noexcept;
Point& operator=(const Point& other);
Point& operator=(Point&&) noexcept;
Point(const Point& other) noexcept = default;
Point(Point&&) noexcept = default;
Point& operator=(const Point& other) noexcept = default;
Point& operator=(Point&&) noexcept = default;
bool operator==(const Point& other) const;
bool operator!=(const Point& other) const;

View File

@ -21,63 +21,32 @@
#include <cassert>
#include <SDL2/SDL_rect.h>
#include <SDL2pp/Rect.hh>
namespace SDL2pp {
Rect::Rect() {
Rect::Rect() : valid_(false) {
}
Rect::~Rect() {
}
Rect::Rect(int x, int y, int w, int h) : rect_(new SDL_Rect) {
rect_->x = x;
rect_->y = y;
rect_->w = w;
rect_->h = h;
Rect::Rect(int x, int y, int w, int h) : valid_(true) {
rect_.x = x;
rect_.y = y;
rect_.w = w;
rect_.h = h;
}
Rect Rect::Null() {
return Rect();
}
Rect::Rect(const Rect& other) {
if (!other.rect_) {
rect_.reset(nullptr);
} else if (rect_ != other.rect_) {
rect_.reset(new SDL_Rect);
rect_->x = other.rect_->x;
rect_->y = other.rect_->y;
rect_->w = other.rect_->w;
rect_->h = other.rect_->h;
}
}
Rect::Rect(Rect&&) noexcept = default;
Rect& Rect::operator=(const Rect& other) {
if (!other.rect_) {
rect_.reset(nullptr);
} else if (rect_ != other.rect_) {
rect_.reset(new SDL_Rect);
rect_->x = other.rect_->x;
rect_->y = other.rect_->y;
rect_->w = other.rect_->w;
rect_->h = other.rect_->h;
}
return *this;
}
Rect& Rect::operator=(Rect&&) noexcept = default;
bool Rect::operator==(const Rect& other) const {
if (!rect_ || !other.rect_)
return rect_ == other.rect_; // true only if both null
return rect_->x == other.rect_->x && rect_->y == other.rect_->y &&
rect_->w == other.rect_->w && rect_->h == other.rect_->h;
if (!valid_ || !other.valid_)
return valid_ == other.valid_; // true only if both null
return rect_.x == other.rect_.x && rect_.y == other.rect_.y &&
rect_.w == other.rect_.w && rect_.h == other.rect_.h;
}
bool Rect::operator!=(const Rect& other) const {
@ -85,11 +54,11 @@ bool Rect::operator!=(const Rect& other) const {
}
SDL_Rect* Rect::Get() {
return rect_.get();
return &rect_;
}
const SDL_Rect* Rect::Get() const {
return rect_.get();
return &rect_;
}
Rect Rect::FromCenter(int cx, int cy, int w, int h) {
@ -97,67 +66,67 @@ Rect Rect::FromCenter(int cx, int cy, int w, int h) {
}
bool Rect::IsNull() const {
return rect_ == nullptr;
return !valid_;
}
int Rect::GetX() const {
assert(!IsNull());
return rect_->x;
return rect_.x;
}
void Rect::SetX(int x) {
assert(!IsNull());
rect_->x = x;
rect_.x = x;
}
int Rect::GetY() const {
assert(!IsNull());
return rect_->y;
return rect_.y;
}
void Rect::SetY(int y) {
assert(!IsNull());
rect_->y = y;
rect_.y = y;
}
int Rect::GetW() const {
assert(!IsNull());
return rect_->w;
return rect_.w;
}
void Rect::SetW(int w) {
assert(!IsNull());
rect_->w = w;
rect_.w = w;
}
int Rect::GetH() const {
assert(!IsNull());
return rect_->h;
return rect_.h;
}
void Rect::SetH(int h) {
assert(!IsNull());
rect_->h = h;
rect_.h = h;
}
int Rect::GetX2() const {
assert(!IsNull());
return rect_->x + rect_->w - 1;
return rect_.x + rect_.w - 1;
}
void Rect::SetX2(int x2) {
assert(!IsNull());
rect_->w = x2 - rect_->x + 1;
rect_.w = x2 - rect_.x + 1;
}
int Rect::GetY2() const {
assert(!IsNull());
return rect_->y + rect_->h - 1;
return rect_.y + rect_.h - 1;
}
void Rect::SetY2(int y2) {
assert(!IsNull());
rect_->h = y2 - rect_->y + 1;
rect_.h = y2 - rect_.y + 1;
}
}

View File

@ -22,7 +22,7 @@
#ifndef SDL2PP_RECT_HH
#define SDL2PP_RECT_HH
#include <memory>
#include <SDL2/SDL_rect.h>
struct SDL_Rect;
@ -30,7 +30,8 @@ namespace SDL2pp {
class Rect {
private:
std::unique_ptr<SDL_Rect> rect_;
SDL_Rect rect_;
bool valid_;
private:
Rect();
@ -43,10 +44,10 @@ public:
static Rect FromCenter(int cx, int cy, int w, int h);
Rect(const Rect& other);
Rect(Rect&&) noexcept;
Rect& operator=(const Rect& other);
Rect& operator=(Rect&&) noexcept;
Rect(const Rect& other) noexcept = default;
Rect(Rect&&) noexcept = default;
Rect& operator=(const Rect& other) noexcept = default;
Rect& operator=(Rect&&) noexcept = default;
bool operator==(const Rect& other) const;
bool operator!=(const Rect& other) const;