Skip to content

Commit 38f0cc9

Browse files
refactor: Introduce bridge representations for integer and float types to enhance type handling
Signed-off-by: FrozenlemonTee <1115306170@qq.com>
1 parent 2510fc1 commit 38f0cc9

1 file changed

Lines changed: 147 additions & 0 deletions

File tree

tests/basic/support/conversion_box_types.hpp

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,80 @@ struct FloatBox {
1616
double value;
1717
};
1818

19+
struct IntBridgeRep {
20+
int value;
21+
22+
friend constexpr auto operator+(IntBridgeRep lhs,
23+
IntBridgeRep rhs) noexcept -> IntBridgeRep {
24+
return IntBridgeRep{lhs.value + rhs.value};
25+
}
26+
27+
friend constexpr auto operator-(IntBridgeRep lhs,
28+
IntBridgeRep rhs) noexcept -> IntBridgeRep {
29+
return IntBridgeRep{lhs.value - rhs.value};
30+
}
31+
32+
friend constexpr auto operator*(IntBridgeRep lhs,
33+
IntBridgeRep rhs) noexcept -> IntBridgeRep {
34+
return IntBridgeRep{lhs.value * rhs.value};
35+
}
36+
37+
friend constexpr auto operator/(IntBridgeRep lhs,
38+
IntBridgeRep rhs) noexcept -> IntBridgeRep {
39+
return IntBridgeRep{lhs.value / rhs.value};
40+
}
41+
42+
friend constexpr auto operator==(IntBridgeRep lhs,
43+
IntBridgeRep rhs) noexcept -> bool {
44+
return lhs.value == rhs.value;
45+
}
46+
47+
constexpr explicit operator int() const noexcept { return value; }
48+
};
49+
50+
struct FloatBridgeRep {
51+
double value;
52+
53+
friend constexpr auto operator+(FloatBridgeRep lhs,
54+
FloatBridgeRep rhs) noexcept
55+
-> FloatBridgeRep {
56+
return FloatBridgeRep{lhs.value + rhs.value};
57+
}
58+
59+
friend constexpr auto operator-(FloatBridgeRep lhs,
60+
FloatBridgeRep rhs) noexcept
61+
-> FloatBridgeRep {
62+
return FloatBridgeRep{lhs.value - rhs.value};
63+
}
64+
65+
friend constexpr auto operator*(FloatBridgeRep lhs,
66+
FloatBridgeRep rhs) noexcept
67+
-> FloatBridgeRep {
68+
return FloatBridgeRep{lhs.value * rhs.value};
69+
}
70+
71+
friend constexpr auto operator/(FloatBridgeRep lhs,
72+
FloatBridgeRep rhs) noexcept
73+
-> FloatBridgeRep {
74+
return FloatBridgeRep{lhs.value / rhs.value};
75+
}
76+
77+
friend constexpr auto operator==(FloatBridgeRep lhs,
78+
FloatBridgeRep rhs) noexcept -> bool {
79+
return lhs.value == rhs.value;
80+
}
81+
82+
constexpr explicit operator double() const noexcept { return value; }
83+
};
84+
85+
struct BridgedIntBox {
86+
IntBridgeRep value;
87+
};
88+
89+
struct BridgedFloatBox {
90+
FloatBridgeRep value;
91+
};
92+
1993
} // namespace mcpplibs::primitives::test_support::conversion
2094

2195
template <>
@@ -78,3 +152,76 @@ struct mcpplibs::primitives::underlying::traits<
78152

79153
static constexpr auto is_valid_rep(rep_type) noexcept -> bool { return true; }
80154
};
155+
156+
template <>
157+
struct mcpplibs::primitives::underlying::traits<
158+
mcpplibs::primitives::test_support::conversion::BridgedIntBox> {
159+
using value_type =
160+
mcpplibs::primitives::test_support::conversion::BridgedIntBox;
161+
using rep_type =
162+
mcpplibs::primitives::test_support::conversion::IntBridgeRep;
163+
164+
static constexpr bool enabled = true;
165+
static constexpr auto kind = mcpplibs::primitives::underlying::category::integer;
166+
167+
static constexpr auto to_rep(value_type value) noexcept -> rep_type {
168+
return value.value;
169+
}
170+
171+
static constexpr auto from_rep(rep_type value) noexcept -> value_type {
172+
return value_type{value};
173+
}
174+
175+
static constexpr auto is_valid_rep(rep_type) noexcept -> bool { return true; }
176+
};
177+
178+
template <>
179+
struct mcpplibs::primitives::underlying::traits<
180+
mcpplibs::primitives::test_support::conversion::BridgedFloatBox> {
181+
using value_type =
182+
mcpplibs::primitives::test_support::conversion::BridgedFloatBox;
183+
using rep_type =
184+
mcpplibs::primitives::test_support::conversion::FloatBridgeRep;
185+
186+
static constexpr bool enabled = true;
187+
static constexpr auto kind =
188+
mcpplibs::primitives::underlying::category::floating;
189+
190+
static constexpr auto to_rep(value_type value) noexcept -> rep_type {
191+
return value.value;
192+
}
193+
194+
static constexpr auto from_rep(rep_type value) noexcept -> value_type {
195+
return value_type{value};
196+
}
197+
198+
static constexpr auto is_valid_rep(rep_type) noexcept -> bool { return true; }
199+
};
200+
201+
template <>
202+
struct mcpplibs::primitives::underlying::common_rep_traits<
203+
mcpplibs::primitives::test_support::conversion::IntBridgeRep, int> {
204+
using type = int;
205+
static constexpr bool enabled = true;
206+
};
207+
208+
template <>
209+
struct mcpplibs::primitives::underlying::common_rep_traits<
210+
int, mcpplibs::primitives::test_support::conversion::IntBridgeRep> {
211+
using type = int;
212+
static constexpr bool enabled = true;
213+
};
214+
215+
template <>
216+
struct mcpplibs::primitives::underlying::common_rep_traits<
217+
mcpplibs::primitives::test_support::conversion::FloatBridgeRep, double> {
218+
using type = double;
219+
static constexpr bool enabled = true;
220+
};
221+
222+
template <>
223+
struct mcpplibs::primitives::underlying::common_rep_traits<
224+
double, mcpplibs::primitives::test_support::conversion::FloatBridgeRep> {
225+
using type = double;
226+
static constexpr bool enabled = true;
227+
};

0 commit comments

Comments
 (0)