Skip to content

Commit ce0bd31

Browse files
committed
greatly simplify CAngle
1 parent 2a20ff5 commit ce0bd31

File tree

2 files changed

+6
-58
lines changed

2 files changed

+6
-58
lines changed

include/units/Angle.hpp

Lines changed: 5 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -41,39 +41,25 @@ template <> struct LookupName<Quantity<std::ratio<0>, std::ratio<0>, std::ratio<
4141
* because the constructor is private. However, you can do
4242
* Angle angle = 2_cDeg
4343
*/
44-
class CAngle : public Quantity<std::ratio<0>, std::ratio<0>, std::ratio<0>, std::ratio<0>, std::ratio<1>, std::ratio<0>,
45-
std::ratio<0>, std::ratio<0>> {
44+
class CAngle {
4645
// make string literals friends, so they have access to the constructor
4746
friend constexpr CAngle operator""_cRad(long double value);
4847
friend constexpr CAngle operator""_cRad(unsigned long long value);
4948
friend constexpr CAngle operator""_cDeg(long double value);
5049
friend constexpr CAngle operator""_cDeg(unsigned long long value);
5150
friend constexpr CAngle operator""_cRot(long double value);
5251
friend constexpr CAngle operator""_cRot(unsigned long long value);
53-
friend constexpr CAngle operator*(double multiple, CAngle quantity);
54-
friend constexpr CAngle operator*(CAngle quantity, double multiple);
55-
friend constexpr CAngle operator/(double multiple, CAngle quantity);
56-
friend constexpr CAngle operator/(CAngle quantity, double multiple);
5752
public:
5853
// make CAngle able to be implicitly converted to Angle
5954
constexpr operator Angle() const { return Angle(M_PI_2 - this->value); }
6055

61-
constexpr Angle operator-(Angle other) const { return Angle(*this) - other; }
56+
constexpr Angle operator-() const { return CAngle(-this->value); }
6257

63-
constexpr CAngle operator-() const { return CAngle(-this->value); }
64-
65-
constexpr Angle operator+(Angle other) const { return Angle(*this) + other; }
66-
67-
constexpr CAngle operator+() const { return CAngle(this->value); }
58+
constexpr Angle operator+() const { return CAngle(this->value); }
6859
private:
69-
// only allow construction through literals
70-
constexpr CAngle(double value)
71-
: Quantity<std::ratio<0>, std::ratio<0>, std::ratio<0>, std::ratio<0>, std::ratio<1>, std::ratio<0>,
72-
std::ratio<0>, std::ratio<0>>(value) {}
60+
const double value;
7361

74-
constexpr CAngle(Angle value)
75-
: Quantity<std::ratio<0>, std::ratio<0>, std::ratio<0>, std::ratio<0>, std::ratio<1>, std::ratio<0>,
76-
std::ratio<0>, std::ratio<0>>(value) {}
62+
constexpr CAngle(double value) : value(value) {}
7763
};
7864

7965
constexpr bool operator==(Angle lhs, CAngle rhs) { return lhs == Angle(rhs); }
@@ -83,27 +69,6 @@ inline std::ostream& operator<<(std::ostream& os, const Angle& quantity) {
8369
return os;
8470
}
8571

86-
constexpr Angle operator+(Angle lhs, CAngle rhs) { return lhs + Angle(rhs); }
87-
88-
constexpr Angle operator-(Angle lhs, CAngle rhs) { return lhs - Angle(rhs); }
89-
90-
constexpr CAngle operator*(double multiple, CAngle quantity) { return CAngle(multiple * quantity.internal()); }
91-
92-
constexpr CAngle operator*(CAngle quantity, double multiple) { return CAngle(multiple * quantity.internal()); }
93-
94-
constexpr CAngle operator/(CAngle quantity, double multiple) { return CAngle(quantity.internal() / multiple); }
95-
96-
namespace units {
97-
template <typename T>
98-
concept isAngle = std::same_as<T, CAngle> || std::same_as<T, Angle> ||
99-
std::same_as<T, Quantity<std::ratio<0>, std::ratio<0>, std::ratio<0>, std::ratio<0>, std::ratio<1>,
100-
std::ratio<0>, std::ratio<0>, std::ratio<0>>>;
101-
102-
template <isAngle Q, isAngle R, isAngle S> constexpr Angle clamp(Q lhs, R lo, S hi) {
103-
return Angle(std::clamp(lhs.internal(), lo.internal(), hi.internal()));
104-
}
105-
} // namespace units
106-
10772
constexpr Angle rad = Angle(1.0);
10873
constexpr Angle deg = Angle(M_PI / 180);
10974
constexpr Angle rot = Angle(M_TWOPI);

src/main.cpp

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,6 @@ void initialize() {
2121
std::ratio<0>, std::ratio<0>>(1.0);
2222
a.orientation += 2_rpm2;
2323
2_rpm2 -= a.orientation;
24-
to_cDeg(Quantity<std::ratio<0>, std::ratio<0>, std::ratio<0>, std::ratio<0>, std::ratio<1>, std::ratio<0>,
25-
std::ratio<0>, std::ratio<0>>(5.0) -
26-
a.theta() + 5_cDeg);
2724
Quantity<std::ratio<0>, std::ratio<0>, std::ratio<1>, std::ratio<0>, std::ratio<1>, std::ratio<0>, std::ratio<0>,
2825
std::ratio<0>>
2926
c = Multiplied<Angle, Time>();
@@ -32,9 +29,6 @@ void initialize() {
3229
Length z = toLinear<Angle>(y, 2_cm);
3330
static_assert(Angle(5.1) >= Quantity<std::ratio<0>, std::ratio<0>, std::ratio<0>, std::ratio<0>, std::ratio<1>,
3431
std::ratio<0>, std::ratio<0>, std::ratio<0>>(5.0));
35-
units::clamp(2_cDeg, a.theta(),
36-
Quantity<std::ratio<0>, std::ratio<0>, std::ratio<0>, std::ratio<0>, std::ratio<1>, std::ratio<0>,
37-
std::ratio<0>, std::ratio<0>>(5.0));
3832
units::max(10_celsius, Quantity<std::ratio<0>, std::ratio<0>, std::ratio<0>, std::ratio<0>, std::ratio<0>,
3933
std::ratio<1>, std::ratio<0>, std::ratio<0>>(1.0));
4034
// check Vector3D overloads
@@ -54,18 +48,7 @@ void initialize() {
5448
void angleTests() {
5549
static_assert(+15_cDeg == 75_stDeg);
5650
static_assert(to_stDeg(-15_cDeg) == to_stDeg(105_stDeg));
57-
static_assert(r2i(to_stDeg(2 * 15_cDeg)) == r2i(to_stDeg(60_stDeg)));
51+
static_assert(r2i(to_stDeg(30_cDeg)) == r2i(to_stDeg(60_stDeg)));
5852
static_assert(r2i(to_stDeg(+0_cDeg)) == r2i(to_stDeg(90_stDeg)));
59-
static_assert(90_stDeg == +0_cDeg);
6053
Angle a = 2_cDeg;
61-
Angle b = 2_cDeg + 2_stDeg;
62-
Angle c = 2_stDeg - 2_cDeg;
63-
Angle d = 2_cDeg + Quantity<std::ratio<0>, std::ratio<0>, std::ratio<0>, std::ratio<0>, std::ratio<1>,
64-
std::ratio<0>, std::ratio<0>, std::ratio<0>>(5.0);
65-
Angle e = Quantity<std::ratio<0>, std::ratio<0>, std::ratio<0>, std::ratio<0>, std::ratio<1>, std::ratio<0>,
66-
std::ratio<0>, std::ratio<0>>(5.0) +
67-
2_cDeg;
68-
units::clamp(2_cDeg, 2_stDeg,
69-
Quantity<std::ratio<0>, std::ratio<0>, std::ratio<0>, std::ratio<0>, std::ratio<1>, std::ratio<0>,
70-
std::ratio<0>, std::ratio<0>>(5.0));
7154
}

0 commit comments

Comments
 (0)