Skip to content

Commit e9dec36

Browse files
committed
Change to CRTP
1 parent 829793d commit e9dec36

File tree

6 files changed

+43
-58
lines changed

6 files changed

+43
-58
lines changed

src/base/anim/easinggradient.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#include "base/math/segmentedfunc.h"
1010
#include "base/math/segmentedfunc.tpp"
1111

12-
template struct Math::SegmentedFunction<double>;
12+
template struct Math::SegmentedFunction<double, Anim::EasingGradient>;
1313

1414
namespace Anim
1515
{

src/base/anim/easinggradient.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
namespace Anim
1010
{
1111

12-
class EasingGradient : protected Math::SegmentedFunction<double>
12+
class EasingGradient :
13+
protected Math::SegmentedFunction<double, EasingGradient>
1314
{
1415
public:
1516
friend struct SegmentedFunction;

src/base/gfx/colorgradient.cpp

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,8 @@
1010

1111
#include "color.h"
1212

13-
template struct Math::SegmentedFunction<Gfx::Color>;
14-
template Gfx::ColorGradient
15-
Math::SegmentedFunction<Gfx::Color>::operator*<Gfx::ColorGradient>(
16-
this const Gfx::ColorGradient &,
17-
double value);
18-
template Gfx::ColorGradient
19-
Math::SegmentedFunction<Gfx::Color>::operator+<Gfx::ColorGradient>(
20-
this const Gfx::ColorGradient &,
21-
const Gfx::ColorGradient &);
13+
template struct Math::SegmentedFunction<Gfx::Color,
14+
Gfx::ColorGradient>;
2215

2316
namespace Gfx
2417
{

src/base/gfx/colorgradient.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
namespace Gfx
1111
{
1212

13-
struct ColorGradient : Math::SegmentedFunction<Color>
13+
struct ColorGradient : Math::SegmentedFunction<Color, ColorGradient>
1414
{
1515
static ColorGradient HeatMap5Color();
1616
static ColorGradient HeatMap7Color();

src/base/math/segmentedfunc.h

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
namespace Math
77
{
88

9-
template <typename T> struct SegmentedFunction
9+
template <typename T, class CRTP> struct SegmentedFunction
1010
{
1111
struct Stop
1212
{
@@ -26,11 +26,41 @@ template <typename T> struct SegmentedFunction
2626
stops(std::move(stops))
2727
{}
2828

29-
template <class Self>
30-
[[nodiscard]] Self operator*(this const Self &, double);
29+
[[nodiscard]] friend CRTP operator*(const CRTP &self,
30+
double value)
31+
{
32+
auto res = self;
33+
for (auto &stop : res.stops) stop.value = stop.value * value;
34+
return res;
35+
}
36+
37+
[[nodiscard]] friend CRTP operator+(const CRTP &self,
38+
const CRTP &other)
39+
{
40+
CRTP res;
41+
auto &stops = self.stops;
3142

32-
template <class Self>
33-
[[nodiscard]] Self operator+(this const Self &, const Self &);
43+
for (auto it0 = stops.begin(), it1 = other.stops.begin();
44+
it0 != stops.end() || it1 != other.stops.end();) {
45+
if (it1 == other.stops.end() || it0->pos < it1->pos) {
46+
res.stops.emplace_back(it0->pos,
47+
it0->value + other(it0->pos));
48+
++it0;
49+
}
50+
else if (it0 == stops.end() || it1->pos < it0->pos) {
51+
res.stops.emplace_back(it1->pos,
52+
it1->value + self(it1->pos));
53+
++it1;
54+
}
55+
else {
56+
res.stops.emplace_back(it0->pos,
57+
it0->value + it1->value);
58+
++it1;
59+
++it0;
60+
}
61+
}
62+
return res;
63+
}
3464

3565
[[nodiscard]] bool operator==(
3666
const SegmentedFunction &other) const = default;

src/base/math/segmentedfunc.tpp

Lines changed: 2 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88

99
#include "segmentedfunc.h"
1010

11-
template <typename T>
12-
T Math::SegmentedFunction<T>::operator()(double pos) const
11+
template <typename T, class CRTP>
12+
T Math::SegmentedFunction<T, CRTP>::operator()(double pos) const
1313
{
1414
if (stops.empty()) return T();
1515
if (stops.size() == 1 || pos < stops.front().pos)
@@ -28,43 +28,4 @@ T Math::SegmentedFunction<T>::operator()(double pos) const
2828
return stops.back().value;
2929
}
3030

31-
template <typename T>
32-
template <class Self>
33-
Self Math::SegmentedFunction<T>::operator*(this const Self &self,
34-
double value)
35-
{
36-
auto res = self;
37-
for (auto &stop : res.stops) stop.value = stop.value * value;
38-
return res;
39-
}
40-
41-
template <typename T>
42-
template <class Self>
43-
Self Math::SegmentedFunction<T>::operator+(this const Self &self,
44-
const Self &other)
45-
{
46-
Self res;
47-
auto &stops = self.stops;
48-
49-
for (auto it0 = stops.begin(), it1 = other.stops.begin();
50-
it0 != stops.end() || it1 != other.stops.end();) {
51-
if (it1 == other.stops.end() || it0->pos < it1->pos) {
52-
res.stops.emplace_back(it0->pos,
53-
it0->value + other(it0->pos));
54-
++it0;
55-
}
56-
else if (it0 == stops.end() || it1->pos < it0->pos) {
57-
res.stops.emplace_back(it1->pos,
58-
it1->value + self(it1->pos));
59-
++it1;
60-
}
61-
else {
62-
res.stops.emplace_back(it0->pos, it0->value + it1->value);
63-
++it1;
64-
++it0;
65-
}
66-
}
67-
return res;
68-
}
69-
7031
#endif

0 commit comments

Comments
 (0)