[nostalgia/common] Add constexpr and noexcept where appropriate

This commit is contained in:
Gary Talent 2021-04-23 23:24:42 -05:00
parent 4518288745
commit 2dc9ce4015
4 changed files with 108 additions and 100 deletions

View File

@ -9,34 +9,34 @@
namespace nostalgia::common { namespace nostalgia::common {
Bounds::Bounds(int x, int y, int w, int h) { Bounds::Bounds(int x, int y, int w, int h) noexcept {
this->x = x; this->x = x;
this->y = y; this->y = y;
this->width = w; this->width = w;
this->height = h; this->height = h;
} }
bool Bounds::intersects(Bounds o) const { bool Bounds::intersects(const Bounds &o) const noexcept {
return o.x2() >= x && x2() >= o.x && o.y2() >= y && y2() >= o.y; return o.x2() >= x && x2() >= o.x && o.y2() >= y && y2() >= o.y;
} }
bool Bounds::contains(int x, int y) const { bool Bounds::contains(int x, int y) const noexcept {
return x >= this->x && y >= this->y && x <= x2() && y <= y2(); return x >= this->x && y >= this->y && x <= x2() && y <= y2();
} }
int Bounds::x2() const { int Bounds::x2() const noexcept {
return x + width; return x + width;
} }
int Bounds::y2() const { int Bounds::y2() const noexcept {
return y + height; return y + height;
} }
Point Bounds::pt1() { Point Bounds::pt1() const noexcept {
return Point(x, y); return Point(x, y);
} }
Point Bounds::pt2() { Point Bounds::pt2() const noexcept {
return Point(x2(), y2()); return Point(x2(), y2());
} }

View File

@ -23,26 +23,32 @@ class Bounds {
int width = 0; int width = 0;
int height = 0; int height = 0;
Bounds() = default; constexpr Bounds() noexcept = default;
Bounds(int x, int y, int w, int h); Bounds(int x, int y, int w, int h) noexcept;
[[nodiscard]] bool intersects(Bounds other) const; [[nodiscard]]
bool intersects(const Bounds &other) const noexcept;
[[nodiscard]] bool contains(int x, int y) const; [[nodiscard]]
bool contains(int x, int y) const noexcept;
[[nodiscard]] int x2() const; [[nodiscard]]
int x2() const noexcept;
[[nodiscard]] int y2() const; [[nodiscard]]
int y2() const noexcept;
[[nodiscard]] Point pt1(); [[nodiscard]]
Point pt1() const noexcept;
[[nodiscard]] Point pt2(); [[nodiscard]]
Point pt2() const noexcept;
}; };
template<typename T> template<typename T>
ox::Error model(T *io, Bounds *obj) { constexpr ox::Error model(T *io, Bounds *obj) {
io->template setTypeInfo<Point>(); io->template setTypeInfo<Point>();
oxReturnError(io->field("x", &obj->x)); oxReturnError(io->field("x", &obj->x));
oxReturnError(io->field("y", &obj->y)); oxReturnError(io->field("y", &obj->y));

View File

@ -20,172 +20,174 @@ class Point {
int x = 0; int x = 0;
int y = 0; int y = 0;
constexpr Point() = default; constexpr Point() noexcept = default;
constexpr Point(int x, int y); constexpr Point(int x, int y) noexcept;
constexpr Point operator+(common::Point p) const; constexpr Point operator+(const common::Point &p) const noexcept;
constexpr Point operator-(common::Point p) const; constexpr Point operator-(const common::Point &p) const noexcept;
constexpr Point operator*(common::Point p) const; constexpr Point operator*(const common::Point &p) const noexcept;
constexpr Point operator/(common::Point p) const; constexpr Point operator/(const common::Point &p) const noexcept;
constexpr Point operator+=(common::Point p); constexpr Point operator+=(const common::Point &p) noexcept;
constexpr Point operator-=(common::Point p); constexpr Point operator-=(const common::Point &p) noexcept;
constexpr Point operator*=(common::Point p); constexpr Point operator*=(const common::Point &p) noexcept;
constexpr Point operator/=(common::Point p); constexpr Point operator/=(const common::Point &p) noexcept;
constexpr Point operator+(int i) const; constexpr Point operator+(int i) const noexcept;
constexpr Point operator-(int i) const; constexpr Point operator-(int i) const noexcept;
constexpr Point operator*(int i) const; constexpr Point operator*(int i) const noexcept;
constexpr Point operator/(int i) const; constexpr Point operator/(int i) const noexcept;
constexpr Point operator+=(int i); constexpr Point operator+=(int i) noexcept;
constexpr Point operator-=(int i); constexpr Point operator-=(int i) noexcept;
constexpr Point operator*=(int i); constexpr Point operator*=(int i) noexcept;
constexpr Point operator/=(int i); constexpr Point operator/=(int i) noexcept;
constexpr bool operator==(const Point&) const; constexpr bool operator==(const Point&) const noexcept;
constexpr bool operator!=(const Point&) const; constexpr bool operator!=(const Point&) const noexcept;
}; };
constexpr Point::Point(int x, int y) { constexpr Point::Point(int x, int y) noexcept {
this->x = x; this->x = x;
this->y = y; this->y = y;
} }
constexpr Point Point::operator+(common::Point p) const { constexpr Point Point::operator+(const common::Point &p) const noexcept {
p.x += x; auto out = *this;
p.y += y; out.x += x;
out.y += y;
return p; return p;
} }
constexpr Point Point::operator-(common::Point p) const { constexpr Point Point::operator-(const common::Point &p) const noexcept {
auto out = *this; auto out = *this;
out.x -= p.x; out.x -= p.x;
out.y -= p.y; out.y -= p.y;
return out; return out;
} }
constexpr Point Point::operator*(common::Point p) const { constexpr Point Point::operator*(const common::Point &p) const noexcept {
p.x *= x; auto out = *this;
p.y *= y; out.x *= x;
out.y *= y;
return p; return p;
} }
constexpr Point Point::operator/(common::Point p) const { constexpr Point Point::operator/(const common::Point &p) const noexcept {
auto out = *this; auto out = *this;
out.x /= p.x; out.x /= p.x;
out.y /= p.y; out.y /= p.y;
return out; return out;
} }
constexpr Point Point::operator+=(common::Point p) { constexpr Point Point::operator+=(const common::Point &p) noexcept {
x += p.x; x += p.x;
y += p.y; y += p.y;
return *this; return *this;
} }
constexpr Point Point::operator-=(common::Point p) { constexpr Point Point::operator-=(const common::Point &p) noexcept {
x -= p.x; x -= p.x;
y -= p.y; y -= p.y;
return *this; return *this;
} }
constexpr Point Point::operator*=(common::Point p) { constexpr Point Point::operator*=(const common::Point &p) noexcept {
x *= p.x; x *= p.x;
y *= p.y; y *= p.y;
return *this; return *this;
} }
constexpr Point Point::operator/=(common::Point p) { constexpr Point Point::operator/=(const common::Point &p) noexcept {
x /= p.x; x /= p.x;
y /= p.y; y /= p.y;
return *this; return *this;
} }
constexpr Point Point::operator+(int i) const { constexpr Point Point::operator+(int i) const noexcept {
auto out = *this; auto out = *this;
out.x += i; out.x += i;
out.y += i; out.y += i;
return out; return out;
} }
constexpr Point Point::operator-(int i) const { constexpr Point Point::operator-(int i) const noexcept {
auto out = *this; auto out = *this;
out.x -= i; out.x -= i;
out.y -= i; out.y -= i;
return out; return out;
} }
constexpr Point Point::operator*(int i) const { constexpr Point Point::operator*(int i) const noexcept {
auto out = *this; auto out = *this;
out.x *= i; out.x *= i;
out.y *= i; out.y *= i;
return out; return out;
} }
constexpr Point Point::operator/(int i) const { constexpr Point Point::operator/(int i) const noexcept {
auto out = *this; auto out = *this;
out.x /= i; out.x /= i;
out.y /= i; out.y /= i;
return out; return out;
} }
constexpr Point Point::operator+=(int i) { constexpr Point Point::operator+=(int i) noexcept {
x += i; x += i;
y += i; y += i;
return *this; return *this;
} }
constexpr Point Point::operator-=(int i) { constexpr Point Point::operator-=(int i) noexcept {
x -= i; x -= i;
y -= i; y -= i;
return *this; return *this;
} }
constexpr Point Point::operator*=(int i) { constexpr Point Point::operator*=(int i) noexcept {
x *= i; x *= i;
y *= i; y *= i;
return *this; return *this;
} }
constexpr Point Point::operator/=(int i) { constexpr Point Point::operator/=(int i) noexcept {
x /= i; x /= i;
y /= i; y /= i;
return *this; return *this;
} }
constexpr bool Point::operator==(const Point &p) const { constexpr bool Point::operator==(const Point &p) const noexcept {
return x == p.x && y == p.y; return x == p.x && y == p.y;
} }
constexpr bool Point::operator!=(const Point &p) const { constexpr bool Point::operator!=(const Point &p) const noexcept {
return x != p.x || y != p.y; return x != p.x || y != p.y;
} }
template<typename T> template<typename T>
ox::Error model(T *io, Point *obj) { constexpr ox::Error model(T *io, Point *obj) {
io->template setTypeInfo<Point>(); io->template setTypeInfo<Point>();
oxReturnError(io->field("x", &obj->x)); oxReturnError(io->field("x", &obj->x));
oxReturnError(io->field("y", &obj->y)); oxReturnError(io->field("y", &obj->y));

View File

@ -20,172 +20,172 @@ class Size {
int width = 0; int width = 0;
int height = 0; int height = 0;
constexpr Size() = default; constexpr Size() noexcept = default;
constexpr Size(int width, int height); constexpr Size(int width, int height) noexcept;
constexpr Size operator+(common::Size p) const; constexpr Size operator+(common::Size p) const noexcept;
constexpr Size operator-(common::Size p) const; constexpr Size operator-(common::Size p) const noexcept;
constexpr Size operator*(common::Size p) const; constexpr Size operator*(common::Size p) const noexcept;
constexpr Size operator/(common::Size p) const; constexpr Size operator/(common::Size p) const noexcept;
constexpr Size operator+=(common::Size p); constexpr Size operator+=(common::Size p) noexcept;
constexpr Size operator-=(common::Size p); constexpr Size operator-=(common::Size p) noexcept;
constexpr Size operator*=(common::Size p); constexpr Size operator*=(common::Size p) noexcept;
constexpr Size operator/=(common::Size p); constexpr Size operator/=(common::Size p) noexcept;
constexpr Size operator+(int i) const; constexpr Size operator+(int i) const noexcept;
constexpr Size operator-(int i) const; constexpr Size operator-(int i) const noexcept;
constexpr Size operator*(int i) const; constexpr Size operator*(int i) const noexcept;
constexpr Size operator/(int i) const; constexpr Size operator/(int i) const noexcept;
constexpr Size operator+=(int i); constexpr Size operator+=(int i) noexcept;
constexpr Size operator-=(int i); constexpr Size operator-=(int i) noexcept;
constexpr Size operator*=(int i); constexpr Size operator*=(int i) noexcept;
constexpr Size operator/=(int i); constexpr Size operator/=(int i) noexcept;
constexpr bool operator==(const Size&) const; constexpr bool operator==(const Size&) const noexcept;
constexpr bool operator!=(const Size&) const; constexpr bool operator!=(const Size&) const noexcept;
}; };
constexpr Size::Size(int width, int height) { constexpr Size::Size(int width, int height) noexcept {
this->width = width; this->width = width;
this->height = height; this->height = height;
} }
constexpr Size Size::operator+(common::Size p) const { constexpr Size Size::operator+(common::Size p) const noexcept {
p.width += width; p.width += width;
p.height += height; p.height += height;
return p; return p;
} }
constexpr Size Size::operator-(common::Size p) const { constexpr Size Size::operator-(common::Size p) const noexcept {
auto out = *this; auto out = *this;
out.width -= p.width; out.width -= p.width;
out.height -= p.height; out.height -= p.height;
return out; return out;
} }
constexpr Size Size::operator*(common::Size p) const { constexpr Size Size::operator*(common::Size p) const noexcept {
p.width *= width; p.width *= width;
p.height *= height; p.height *= height;
return p; return p;
} }
constexpr Size Size::operator/(common::Size p) const { constexpr Size Size::operator/(common::Size p) const noexcept {
auto out = *this; auto out = *this;
out.width /= p.width; out.width /= p.width;
out.height /= p.height; out.height /= p.height;
return out; return out;
} }
constexpr Size Size::operator+=(common::Size p) { constexpr Size Size::operator+=(common::Size p) noexcept {
width += p.width; width += p.width;
height += p.height; height += p.height;
return *this; return *this;
} }
constexpr Size Size::operator-=(common::Size p) { constexpr Size Size::operator-=(common::Size p) noexcept {
width -= p.width; width -= p.width;
height -= p.height; height -= p.height;
return *this; return *this;
} }
constexpr Size Size::operator*=(common::Size p) { constexpr Size Size::operator*=(common::Size p) noexcept {
width *= p.width; width *= p.width;
height *= p.height; height *= p.height;
return *this; return *this;
} }
constexpr Size Size::operator/=(common::Size p) { constexpr Size Size::operator/=(common::Size p) noexcept {
width /= p.width; width /= p.width;
height /= p.height; height /= p.height;
return *this; return *this;
} }
constexpr Size Size::operator+(int i) const { constexpr Size Size::operator+(int i) const noexcept {
auto out = *this; auto out = *this;
out.width += i; out.width += i;
out.height += i; out.height += i;
return out; return out;
} }
constexpr Size Size::operator-(int i) const { constexpr Size Size::operator-(int i) const noexcept {
auto out = *this; auto out = *this;
out.width -= i; out.width -= i;
out.height -= i; out.height -= i;
return out; return out;
} }
constexpr Size Size::operator*(int i) const { constexpr Size Size::operator*(int i) const noexcept {
auto out = *this; auto out = *this;
out.width *= i; out.width *= i;
out.height *= i; out.height *= i;
return out; return out;
} }
constexpr Size Size::operator/(int i) const { constexpr Size Size::operator/(int i) const noexcept {
auto out = *this; auto out = *this;
out.width /= i; out.width /= i;
out.height /= i; out.height /= i;
return out; return out;
} }
constexpr Size Size::operator+=(int i) { constexpr Size Size::operator+=(int i) noexcept {
width += i; width += i;
height += i; height += i;
return *this; return *this;
} }
constexpr Size Size::operator-=(int i) { constexpr Size Size::operator-=(int i) noexcept {
width -= i; width -= i;
height -= i; height -= i;
return *this; return *this;
} }
constexpr Size Size::operator*=(int i) { constexpr Size Size::operator*=(int i) noexcept {
width *= i; width *= i;
height *= i; height *= i;
return *this; return *this;
} }
constexpr Size Size::operator/=(int i) { constexpr Size Size::operator/=(int i) noexcept {
width /= i; width /= i;
height /= i; height /= i;
return *this; return *this;
} }
constexpr bool Size::operator==(const Size &p) const { constexpr bool Size::operator==(const Size &p) const noexcept {
return width == p.width && height == p.height; return width == p.width && height == p.height;
} }
constexpr bool Size::operator!=(const Size &p) const { constexpr bool Size::operator!=(const Size &p) const noexcept {
return width != p.width || height != p.height; return width != p.width || height != p.height;
} }
template<typename T> template<typename T>
ox::Error model(T *io, Size *obj) { constexpr ox::Error model(T *io, Size *obj) {
io->template setTypeInfo<Size>(); io->template setTypeInfo<Size>();
oxReturnError(io->field("width", &obj->width)); oxReturnError(io->field("width", &obj->width));
oxReturnError(io->field("height", &obj->height)); oxReturnError(io->field("height", &obj->height));