Skip to content

Commit 8b336c3

Browse files
authored
Merge pull request #570 from vizzuhq/remove_this_eq
Remove "*this = " expressions from codebase
2 parents 8c9a2f7 + 3c7f9ed commit 8b336c3

25 files changed

+146
-175
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
### Fixed
66

77
- Legend title bottomPadding extended.
8+
- ColorGradient fromString not increasing position prohibited.
89

910
### Added
1011

src/base/anim/duration.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,13 @@ double Duration::sec() const { return msec() / 1000.0; }
6363

6464
Duration &Duration::operator+=(const Duration &other)
6565
{
66-
*this = *this + other;
66+
static_cast<Base &>(*this) += other;
6767
return *this;
6868
}
6969

7070
Duration &Duration::operator-=(const Duration &other)
7171
{
72-
*this = *this - other;
72+
static_cast<Base &>(*this) -= other;
7373
return *this;
7474
}
7575

src/base/anim/easing.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ Easing::Easing(const std::string &name)
1616
{
1717
if (name.empty()) return;
1818

19-
auto nameCopy = name;
20-
Text::SmartString::trim(nameCopy);
21-
22-
if (nameCopy == "none") { func = &Easing::none; }
19+
if (auto nameCopy = Text::SmartString::trim_view(name);
20+
nameCopy == "none") {
21+
func = &Easing::none;
22+
}
2323
else if (nameCopy == "linear") {
2424
func = &Easing::linear;
2525
}
@@ -45,7 +45,7 @@ Easing::Easing(const std::string &name)
4545
func = EasingGradient::Bezier(Geom::Point{0.42, 0},
4646
Geom::Point{0.58, 1});
4747
}
48-
else if (const Text::FuncString f(nameCopy, false);
48+
else if (const Text::FuncString f(name);
4949
f.getName() == "cubic-bezier") {
5050
if (f.getParams().size() != 4)
5151
throw std::logic_error("parameter count missmatch");

src/base/anim/easinggradient.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,6 @@
66

77
#include "base/geom/bezier.h"
88
#include "base/geom/point.h"
9-
#include "base/math/segmentedfunc.h"
10-
#include "base/math/segmentedfunc.tpp"
11-
12-
template struct Math::SegmentedFunction<double, Anim::EasingGradient>;
139

1410
namespace Anim
1511
{

src/base/geom/point.h

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,11 @@ struct Point
113113

114114
Point &operator/=(double divisor)
115115
{
116-
if (Math::Floating::is_zero(divisor))
117-
return *this = Invalid();
116+
if (Math::Floating::is_zero(divisor)) {
117+
x = NAN;
118+
y = NAN;
119+
return *this;
120+
}
118121
x /= divisor;
119122
y /= divisor;
120123
return *this;
@@ -130,8 +133,11 @@ struct Point
130133
Point &operator/=(const Point &other)
131134
{
132135
using Math::Floating::is_zero;
133-
if (is_zero(other.x) || is_zero(other.y))
134-
return *this = Invalid();
136+
if (is_zero(other.x) || is_zero(other.y)) {
137+
x = NAN;
138+
y = NAN;
139+
return *this;
140+
}
135141
x /= other.x;
136142
y /= other.y;
137143
return *this;

src/base/gfx/color.cpp

Lines changed: 22 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -22,55 +22,45 @@ Color::operator std::string() const
2222
return res;
2323
}
2424

25-
Color::Color(const std::string &string)
25+
Color Color::fromString(const std::string &string)
2626
{
27-
if (string.empty()) { *this = Transparent(); }
28-
else if (string[0] == '#' && string.size() == 9) {
29-
red = Text::Character::hex(&string[1]) / 255.0;
30-
green = Text::Character::hex(&string[3]) / 255.0;
31-
blue = Text::Character::hex(&string[5]) / 255.0;
32-
alpha = Text::Character::hex(&string[7]) / 255.0;
27+
if (string.empty()) return Transparent();
28+
if (string[0] == '#' && string.size() == 9) {
29+
return {Text::Character::hex(&string[1]) / 255.0,
30+
Text::Character::hex(&string[3]) / 255.0,
31+
Text::Character::hex(&string[5]) / 255.0,
32+
Text::Character::hex(&string[7]) / 255.0};
3333
}
34-
else if (string[0] == '#' && string.size() == 7) {
35-
red = Text::Character::hex(&string[1]) / 255.0;
36-
green = Text::Character::hex(&string[3]) / 255.0;
37-
blue = Text::Character::hex(&string[5]) / 255.0;
38-
alpha = 1.0;
34+
if (string[0] == '#' && string.size() == 7) {
35+
return {Text::Character::hex(&string[1]) / 255.0,
36+
Text::Character::hex(&string[3]) / 255.0,
37+
Text::Character::hex(&string[5]) / 255.0};
3938
}
40-
else if (string[0] == '#' && string.size() == 4) {
39+
if (string[0] == '#' && string.size() == 4) {
4140
auto r = Text::Character::fromHex(string[1]);
4241
auto g = Text::Character::fromHex(string[2]);
4342
auto b = Text::Character::fromHex(string[3]);
44-
red = ((r << 4U) + r) / 255.0;
45-
green = ((g << 4U) + g) / 255.0;
46-
blue = ((b << 4U) + b) / 255.0;
47-
alpha = 1.0;
43+
return {((r << 4U) + r) / 255.0,
44+
((g << 4U) + g) / 255.0,
45+
((b << 4U) + b) / 255.0};
4846
}
49-
else if (const Text::FuncString f(string, false); !f.isEmpty()) {
47+
if (const Text::FuncString f(string); !f.isEmpty()) {
5048
using Conv::parse;
5149

52-
if (f.getName() == "rgb") {
53-
auto ps = f.getParams();
54-
if (ps.size() != 3)
55-
throw std::logic_error("invalid color string");
56-
*this = RGBA(parse<uint32_t>(ps.at(0)),
50+
auto &&ps = f.getParams();
51+
if (f.getName() == "rgb" && ps.size() == 3) {
52+
return RGBA(parse<uint32_t>(ps.at(0)),
5753
parse<uint32_t>(ps.at(1)),
5854
parse<uint32_t>(ps.at(2)));
5955
}
60-
else if (f.getName() == "rgba") {
61-
auto ps = f.getParams();
62-
if (ps.size() != 4)
63-
throw std::logic_error("invalid color string");
64-
*this = RGBA(parse<uint32_t>(ps.at(0)),
56+
if (f.getName() == "rgba" && ps.size() == 4) {
57+
return RGBA(parse<uint32_t>(ps.at(0)),
6558
parse<uint32_t>(ps.at(1)),
6659
parse<uint32_t>(ps.at(2)),
6760
static_cast<uint8_t>(parse<double>(ps.at(3)) * 255));
6861
}
69-
else
70-
throw std::logic_error("invalid color string");
7162
}
72-
else
73-
throw std::logic_error("invalid color string");
63+
throw std::logic_error("invalid color string");
7464
}
7565

7666
Color Color::RGB(uint32_t rgb)

src/base/gfx/color.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ namespace Gfx
1313

1414
struct Color
1515
{
16-
double red;
17-
double green;
18-
double blue;
19-
double alpha;
16+
double red{};
17+
double green{};
18+
double blue{};
19+
double alpha{};
2020

21-
Color() { red = green = blue = alpha = 0.0; }
21+
Color() = default;
2222

2323
Color(double red,
2424
double green,
@@ -33,7 +33,7 @@ struct Color
3333
Color(const Color &) = default;
3434
Color &operator=(const Color &) = default;
3535

36-
explicit Color(const std::string &string);
36+
static Color fromString(const std::string &string);
3737

3838
static Color RGB(uint32_t rgb);
3939
static Color RGBA(uint32_t rgba);

src/base/gfx/colorgradient.cpp

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,28 @@
44
#include <stdexcept>
55
#include <string>
66

7-
#include "base/math/segmentedfunc.h"
8-
#include "base/math/segmentedfunc.tpp"
97
#include "base/text/smartstring.h"
108

119
#include "color.h"
1210

13-
template struct Math::SegmentedFunction<Gfx::Color,
14-
Gfx::ColorGradient>;
15-
1611
namespace Gfx
1712
{
1813

1914
ColorGradient::ColorGradient(const std::string &stoplist)
2015
{
21-
auto stopStrings = Text::SmartString::split(stoplist, ',', true);
22-
auto pos = 0.0;
23-
for (auto &stopString : stopStrings) {
24-
auto parts = Text::SmartString::split(stopString, ' ', true);
25-
if (parts.size() == 2) {
26-
pos = std::stod(parts[1]);
27-
stops.emplace_back(pos, Color(parts[0]));
28-
}
29-
else
30-
throw std::logic_error(
31-
"invalid gradient stop: " + stopString);
16+
for (const auto &stopString :
17+
Text::SmartString::split(stoplist, ',', true)) {
18+
if (auto &&parts =
19+
Text::SmartString::split(stopString, ' ', true);
20+
parts.size() == 2)
21+
if (auto pos = std::stod(parts[1]);
22+
std::isfinite(pos)
23+
&& (stops.empty() || pos >= stops.back().pos)) {
24+
stops.emplace_back(pos, Color::fromString(parts[0]));
25+
continue;
26+
}
27+
throw std::logic_error(
28+
"invalid gradient stop: " + stopString);
3229
}
3330
}
3431

src/base/gfx/colorpalette.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ ColorPalette::ColorPalette(const std::string &string)
2121
Text::SmartString::split(string, ' ', true, "()");
2222
colors.reserve(colorList.size());
2323
for (const auto &color : colorList) {
24-
colors.emplace_back(color);
24+
colors.emplace_back(Color::fromString(color));
2525
}
2626
}
2727

src/base/gfx/colortransform.cpp

Lines changed: 15 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -13,44 +13,31 @@
1313
namespace Gfx
1414
{
1515

16-
ColorTransform::ColorTransform(const std::string &code) : code(code)
16+
ColorTransform ColorTransform::fromString(const std::string &code)
1717
{
18-
Text::SmartString::trim(this->code);
19-
20-
if (code == "none") {
21-
*this = None();
22-
return;
23-
}
18+
if (Text::SmartString::trim_view(code) == "none") return None();
2419

2520
const Text::FuncString func(code);
2621

27-
if (func.isEmpty()) return;
22+
if (func.isEmpty()) return {};
2823

2924
if (func.getParams().size() != 1)
3025
throw std::logic_error(
3126
"invalid color transform parameter count");
3227

33-
if (func.getName() == "color") {
34-
auto color = Color(func.getParams().at(0));
35-
*this = OverrideColor(color);
36-
}
37-
else if (func.getName() == "lightness") {
38-
auto factor = std::stod(func.getParams().at(0));
39-
*this = Lightness(factor);
40-
}
41-
else if (func.getName() == "grayscale") {
42-
auto factor = std::stod(func.getParams().at(0));
43-
*this = Grayscale(factor);
44-
}
45-
else if (func.getName() == "opacity") {
46-
auto factor = std::stod(func.getParams().at(0));
47-
*this = Opacity(factor);
48-
}
49-
else
50-
throw std::logic_error("invalid color transform string");
28+
if (func.getName() == "color")
29+
return OverrideColor(
30+
Color::fromString(func.getParams().at(0)));
31+
if (func.getName() == "lightness")
32+
return Lightness(std::stod(func.getParams().at(0)));
33+
if (func.getName() == "grayscale")
34+
return Grayscale(std::stod(func.getParams().at(0)));
35+
if (func.getName() == "opacity")
36+
return Opacity(std::stod(func.getParams().at(0)));
37+
throw std::logic_error("invalid color transform string");
5138
}
5239

53-
ColorTransform ColorTransform::OverrideColor(Gfx::Color overrideColor)
40+
ColorTransform ColorTransform::OverrideColor(Color overrideColor)
5441
{
5542
return {[=](const Color &)
5643
{
@@ -124,7 +111,7 @@ ColorTransform ColorTransform::operator+(
124111
""};
125112
}
126113

127-
Gfx::Color ColorTransform::operator()(const Gfx::Color &color) const
114+
Color ColorTransform::operator()(const Color &color) const
128115
{
129116
return convert ? convert(color) : color;
130117
}

0 commit comments

Comments
 (0)