1
0

feat: swap the default value with min/max value in binstore serde.

considering setting default value is common that min/max value,
move default value template argument ahead of min/max template argument,
because C++ can not skip template argument like Rust `_` does.
This commit is contained in:
2025-12-16 21:26:49 +08:00
parent 8a604ee813
commit e864b0115e
2 changed files with 8 additions and 12 deletions

View File

@@ -46,9 +46,9 @@ namespace yycc::carton::binstore::serdes {
using SerDesValueType = T::ValueType; using SerDesValueType = T::ValueType;
template<std::integral T, template<std::integral T,
auto TDefault = static_cast<T>(0),
auto TMin = std::numeric_limits<T>::min(), auto TMin = std::numeric_limits<T>::min(),
auto TMax = std::numeric_limits<T>::max(), auto TMax = std::numeric_limits<T>::max()>
auto TDefault = static_cast<T>(0)>
struct IntegralSerDes { struct IntegralSerDes {
IntegralSerDes() = default; IntegralSerDes() = default;
YYCC_DEFAULT_COPY_MOVE(IntegralSerDes) YYCC_DEFAULT_COPY_MOVE(IntegralSerDes)
@@ -81,9 +81,9 @@ namespace yycc::carton::binstore::serdes {
}; };
template<std::floating_point T, template<std::floating_point T,
auto TDefault = static_cast<T>(0),
auto TMin = std::numeric_limits<T>::lowest(), auto TMin = std::numeric_limits<T>::lowest(),
auto TMax = std::numeric_limits<T>::max(), auto TMax = std::numeric_limits<T>::max()>
auto TDefault = static_cast<T>(0)>
struct FloatingPointSerDes { struct FloatingPointSerDes {
FloatingPointSerDes() { FloatingPointSerDes() {
// TODO: Remove this and make it "= default" when 3 common STL make std::isfinite become constexpr. // TODO: Remove this and make it "= default" when 3 common STL make std::isfinite become constexpr.
@@ -122,7 +122,7 @@ namespace yycc::carton::binstore::serdes {
NS_YYCC_BINSTORE_TYPES::ByteArray reset() const { return this->serialize(TDefault).value(); } NS_YYCC_BINSTORE_TYPES::ByteArray reset() const { return this->serialize(TDefault).value(); }
}; };
template<typename T, auto TDefault = static_cast<T>(0)> template<typename T, T TDefault>
requires std::is_enum_v<T> requires std::is_enum_v<T>
struct EnumSerDes { struct EnumSerDes {
EnumSerDes() = default; EnumSerDes() = default;
@@ -142,11 +142,7 @@ namespace yycc::carton::binstore::serdes {
NS_YYCC_BINSTORE_TYPES::ByteArray reset() const { return inner.reset(); } NS_YYCC_BINSTORE_TYPES::ByteArray reset() const { return inner.reset(); }
private: private:
IntegralSerDes<UnderlyingType, IntegralSerDes<UnderlyingType, static_cast<UnderlyingType>(TDefault)> inner;
std::numeric_limits<UnderlyingType>::min(),
std::numeric_limits<UnderlyingType>::max(),
static_cast<UnderlyingType>(TDefault)>
inner;
}; };
template<bool TDefault = false> template<bool TDefault = false>

View File

@@ -78,9 +78,9 @@ namespace yycctest::carton::binstore {
Token string_setting; Token string_setting;
using StringSettingSerDes = serdes::StringSerDes; using StringSettingSerDes = serdes::StringSerDes;
Token bool_setting; Token bool_setting;
using BoolSettingSerDes = serdes::BoolSerDes<>; using BoolSettingSerDes = serdes::BoolSerDes<false>;
Token clamped_float_setting; Token clamped_float_setting;
using ClampedFloatSettingSerDes = serdes::FloatingPointSerDes<float, -1.0f, 1.0f>; using ClampedFloatSettingSerDes = serdes::FloatingPointSerDes<float, 0.0f, -1.0f, 1.0f>;
Token enum_setting; Token enum_setting;
using EnumSettingSerDes = serdes::EnumSerDes<TestEnum, TestEnum::Test1>; using EnumSettingSerDes = serdes::EnumSerDes<TestEnum, TestEnum::Test1>;