Skip to content

Commit 5746b84

Browse files
committed
Remove "*this = " expressions
1 parent bf65b6d commit 5746b84

File tree

13 files changed

+82
-88
lines changed

13 files changed

+82
-88
lines changed

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/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: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -22,55 +22,51 @@ 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, false); !f.isEmpty()) {
5048
using Conv::parse;
5149

5250
if (f.getName() == "rgb") {
5351
auto ps = f.getParams();
5452
if (ps.size() != 3)
5553
throw std::logic_error("invalid color string");
56-
*this = RGBA(parse<uint32_t>(ps.at(0)),
54+
return RGBA(parse<uint32_t>(ps.at(0)),
5755
parse<uint32_t>(ps.at(1)),
5856
parse<uint32_t>(ps.at(2)));
5957
}
60-
else if (f.getName() == "rgba") {
58+
if (f.getName() == "rgba") {
6159
auto ps = f.getParams();
6260
if (ps.size() != 4)
6361
throw std::logic_error("invalid color string");
64-
*this = RGBA(parse<uint32_t>(ps.at(0)),
62+
return RGBA(parse<uint32_t>(ps.at(0)),
6563
parse<uint32_t>(ps.at(1)),
6664
parse<uint32_t>(ps.at(2)),
6765
static_cast<uint8_t>(parse<double>(ps.at(3)) * 255));
6866
}
69-
else
70-
throw std::logic_error("invalid color string");
71-
}
72-
else
7367
throw std::logic_error("invalid color string");
68+
}
69+
throw std::logic_error("invalid color string");
7470
}
7571

7672
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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ ColorGradient::ColorGradient(const std::string &stoplist)
2323
auto parts = Text::SmartString::split(stopString, ' ', true);
2424
if (parts.size() == 2) {
2525
pos = std::stod(parts[1]);
26-
stops.emplace_back(pos, Color(parts[0]));
26+
stops.emplace_back(pos, Color::fromString(parts[0]));
2727
}
2828
else
2929
throw std::logic_error(

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: 17 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -13,44 +13,32 @@
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);
18+
const Text::FuncString func(code, false);
1919

20-
if (code == "none") {
21-
*this = None();
22-
return;
23-
}
20+
if (func.getName() == "none" && func.getParams().empty())
21+
return None();
2422

25-
const Text::FuncString func(code);
26-
27-
if (func.isEmpty()) return;
23+
if (func.isEmpty()) return {};
2824

2925
if (func.getParams().size() != 1)
3026
throw std::logic_error(
3127
"invalid color transform parameter count");
3228

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");
29+
if (func.getName() == "color")
30+
return OverrideColor(
31+
Color::fromString(func.getParams().at(0)));
32+
if (func.getName() == "lightness")
33+
return Lightness(std::stod(func.getParams().at(0)));
34+
if (func.getName() == "grayscale")
35+
return Grayscale(std::stod(func.getParams().at(0)));
36+
if (func.getName() == "opacity")
37+
return Opacity(std::stod(func.getParams().at(0)));
38+
throw std::logic_error("invalid color transform string");
5139
}
5240

53-
ColorTransform ColorTransform::OverrideColor(Gfx::Color overrideColor)
41+
ColorTransform ColorTransform::OverrideColor(Color overrideColor)
5442
{
5543
return {[=](const Color &)
5644
{
@@ -124,7 +112,7 @@ ColorTransform ColorTransform::operator+(
124112
""};
125113
}
126114

127-
Gfx::Color ColorTransform::operator()(const Gfx::Color &color) const
115+
Color ColorTransform::operator()(const Color &color) const
128116
{
129117
return convert ? convert(color) : color;
130118
}

src/base/gfx/colortransform.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,25 @@ namespace Gfx
1111
class ColorTransform
1212
{
1313
public:
14-
explicit ColorTransform(const std::string &code = std::string());
14+
ColorTransform() = default;
15+
static ColorTransform fromString(const std::string &);
1516
explicit operator std::string() const;
1617

17-
static ColorTransform OverrideColor(Gfx::Color overrideColor);
18+
static ColorTransform OverrideColor(Color overrideColor);
1819
static ColorTransform Grayscale(double factor);
1920
static ColorTransform Lightness(double factor);
2021
static ColorTransform Opacity(double factor);
2122
static ColorTransform None();
2223

23-
Gfx::Color operator()(const Gfx::Color &color) const;
24+
Color operator()(const Color &color) const;
2425

2526
ColorTransform operator*(double value) const;
2627
ColorTransform operator+(const ColorTransform &other) const;
2728

2829
bool operator==(const ColorTransform &other) const;
2930

3031
private:
31-
using Convert = std::function<Gfx::Color(const Gfx::Color &)>;
32+
using Convert = std::function<Color(const Color &)>;
3233
std::string code;
3334
Convert convert;
3435

src/base/gfx/font.cpp

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,12 @@
99
namespace Gfx
1010
{
1111

12-
Font::Weight::Weight(const std::string &str)
12+
Font::Weight Font::Weight::fromString(const std::string &str)
1313
{
14-
if (str == "normal")
15-
*this = Normal();
16-
else if (str == "bold")
17-
*this = Bold();
18-
else {
19-
value = static_cast<int>(Conv::parse<double>(str));
20-
}
14+
if (str == "normal") return Normal();
15+
if (str == "bold") return Bold();
16+
17+
return Weight{static_cast<int>(Conv::parse<double>(str))};
2118
}
2219

2320
Font::Weight::operator std::string() const

src/base/gfx/font.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class Font
1717
static Weight Bold() { return Weight{700}; }
1818
Weight() : value(Normal().value) {}
1919
explicit Weight(int value) : value(value) {}
20-
explicit Weight(const std::string &str);
20+
static Weight fromString(const std::string &str);
2121
explicit operator int() const { return value; };
2222
explicit operator std::string() const;
2323
Weight operator*(double factor) const;

0 commit comments

Comments
 (0)