Refactor mapToInt/mapToUint

This commit is contained in:
Benjamin Nauck
2025-04-01 08:02:42 +02:00
parent accaa30571
commit 2c78997339
2 changed files with 44 additions and 34 deletions

View File

@@ -292,34 +292,40 @@ public:
UnsignedValidator * mValidator{nullptr};
UIntSpinBoxPrivate() = default;
uint mapToUInt( int v ) const
unsigned mapToUInt( int v ) const
{
uint ui;
if ( v == std::numeric_limits<int>::min() ) {
using int_limits = std::numeric_limits<int>;
using uint_limits = std::numeric_limits<unsigned>;
unsigned ui;
if ( v == int_limits::min() ) {
ui = 0;
} else if ( v == std::numeric_limits<int>::max() ) {
ui = std::numeric_limits<unsigned>::max();
} else if ( v == int_limits::max() ) {
ui = uint_limits::max();
} else if ( v < 0 ) {
v -= std::numeric_limits<int>::min();
ui = static_cast<uint>(v);
v -= int_limits::min();
ui = static_cast<unsigned>(v);
} else {
ui = static_cast<uint>(v);
ui -= std::numeric_limits<int>::min();
ui = static_cast<unsigned>(v);
ui -= int_limits::min();
} return ui;
}
int mapToInt( uint v ) const
int mapToInt( unsigned v ) const
{
using int_limits = std::numeric_limits<int>;
using uint_limits = std::numeric_limits<unsigned>;
int in;
if ( v == std::numeric_limits<unsigned>::max() ) {
in = std::numeric_limits<int>::max();
if ( v == uint_limits::max() ) {
in = int_limits::max();
} else if ( v == 0 ) {
in = std::numeric_limits<int>::min();
} else if ( v > std::numeric_limits<int>::max() ) {
v += std::numeric_limits<int>::min();
in = int_limits::min();
} else if ( v > int_limits::max() ) {
v += int_limits::min();
in = static_cast<int>(v);
} else {
in = v;
in += std::numeric_limits<int>::min();
in += int_limits::min();
} return in;
}
};

View File

@@ -1520,43 +1520,47 @@ public:
UIntSpinBoxPrivate()
: mValidator(0)
{}
uint mapToUInt(int v) const
unsigned mapToUInt(int v) const
{
using limits = std::numeric_limits;
uint ui;
if (v == limits<int>::min()) {
using int_limits = std::numeric_limits<int>;
using uint_limits = std::numeric_limits<unsigned>;
unsigned ui;
if (v == int_limits::min()) {
ui = 0;
}
else if (v == limits<int>::max()) {
ui = limits<uint>::max();
else if (v == int_limits::max()) {
ui = uint_limits::max();
}
else if (v < 0) {
v -= limits<int>::min();
ui = static_cast<uint>(v);
v -= int_limits::min();
ui = static_cast<unsigned>(v);
}
else {
ui = static_cast<uint>(v);
ui -= limits<int>::min();
ui = static_cast<unsigned>(v);
ui -= int_limits::min();
}
return ui;
}
int mapToInt(uint v) const
int mapToInt(unsigned v) const
{
using limits = std::numeric_limits;
using int_limits = std::numeric_limits<int>;
using uint_limits = std::numeric_limits<unsigned>;
int in;
if (v == limits<uint>::max()) {
in = limits<int>::max();
if (v == uint_limits::max()) {
in = int_limits::max();
}
else if (v == 0) {
in = limits<int>::min();
in = int_limits::min();
}
else if (v > limits<int>::max()) {
v += limits<int>::min();
else if (v > int_limits::max()) {
v += int_limits::min();
in = static_cast<int>(v);
}
else {
in = v;
in += limits<int>::min();
in += int_limits::min();
}
return in;
}