ECC-1620: Refactor Unit::value()

This commit is contained in:
Eugen Betke 2023-09-20 15:16:55 +00:00
parent 0a1b650ba2
commit 249867cc4a
9 changed files with 48 additions and 37 deletions

View File

@ -690,11 +690,11 @@ static int pack_long_(grib_accessor* a, const long end_step_value, const long en
if ((err = grib_set_long_internal(grib_handle_of_accessor(a), self->time_range_value, time_range_opt.value<long>())) != GRIB_SUCCESS)
return err;
if ((err = grib_set_long_internal(grib_handle_of_accessor(a), self->time_range_unit, time_range_opt.unit().to_long())) != GRIB_SUCCESS)
if ((err = grib_set_long_internal(grib_handle_of_accessor(a), self->time_range_unit, time_range_opt.unit().value<long>())) != GRIB_SUCCESS)
return err;
if ((err = grib_set_long_internal(grib_handle_of_accessor(a), forecast_time_value_key, forecast_time_opt.value<long>())) != GRIB_SUCCESS)
return err;
if ((err = grib_set_long_internal(grib_handle_of_accessor(a), forecast_time_unit_key, forecast_time_opt.unit().to_long())) != GRIB_SUCCESS)
if ((err = grib_set_long_internal(grib_handle_of_accessor(a), forecast_time_unit_key, forecast_time_opt.unit().value<long>())) != GRIB_SUCCESS)
return err;
@ -765,7 +765,7 @@ static int pack_long(grib_accessor* a, const long* val, size_t* len)
return ret;
if (end_step_unit == 255)
end_step_unit = Unit{Unit::Value::HOUR}.to_long();
end_step_unit = Unit{Unit::Value::HOUR}.value<long>();
}
else {
end_step_unit = force_step_units;
@ -783,10 +783,10 @@ static int pack_string(grib_accessor* a, const char* val, size_t* len)
Step end_step = step_from_string(val);
end_step.optimize_unit();
if ((ret = grib_set_long_internal(h, "endStepUnit", end_step.unit().to_long())) != GRIB_SUCCESS)
if ((ret = grib_set_long_internal(h, "endStepUnit", end_step.unit().value<long>())) != GRIB_SUCCESS)
return ret;
if ((ret = pack_long_(a, end_step.value<long>(), end_step.unit().to_long())) != GRIB_SUCCESS)
if ((ret = pack_long_(a, end_step.value<long>(), end_step.unit().value<long>())) != GRIB_SUCCESS)
return ret;
return GRIB_SUCCESS;
}

View File

@ -237,13 +237,13 @@ static int pack_string_new(grib_accessor* a, const char* val, size_t* len)
std::tie(step_0, step_1) = find_common_units(steps[0].optimize_unit(), steps[1].optimize_unit());
}
if ((ret = grib_set_long_internal(h, "startStepUnit", step_0.unit().to_long())))
if ((ret = grib_set_long_internal(h, "startStepUnit", step_0.unit().value<long>())))
return ret;
if ((ret = set_step(h, "forecastTime" , "indicatorOfUnitOfTimeRange", step_0)) != GRIB_SUCCESS)
return ret;
if ((self->end_step != NULL) && (steps.size() > 1)) {
if ((ret = grib_set_long_internal(h, "endStepUnit", step_1.unit().to_long())))
if ((ret = grib_set_long_internal(h, "endStepUnit", step_1.unit().value<long>())))
return ret;
if ((ret = grib_set_long_internal(h, self->end_step, step_1.value<long>())))
return ret;

View File

@ -142,8 +142,8 @@ static size_t string_length(grib_accessor* a)
return 255;
}
static long staticStepUnits = Unit{Unit::Value::MISSING}.to_long();
static long staticForceStepUnits = Unit{Unit::Value::MISSING}.to_long();
static long staticStepUnits = Unit{Unit::Value::MISSING}.value<long>();
static long staticForceStepUnits = Unit{Unit::Value::MISSING}.value<long>();
static int pack_long(grib_accessor* a, const long* val, size_t* len)
{
@ -174,16 +174,16 @@ static int unpack_long(grib_accessor* a, long* val, size_t* len)
if (forecast_time_opt && time_range_opt) {
auto [step_a, step_b] = find_common_units(forecast_time_opt.value().optimize_unit(), (forecast_time_opt.value() + time_range_opt.value()).optimize_unit());
*val = step_a.unit().to_long();
*val = step_a.unit().value<long>();
}
else if (forecast_time_opt && !time_range_opt) {
*val = forecast_time_opt.value().optimize_unit().unit().to_long();
*val = forecast_time_opt.value().optimize_unit().unit().value<long>();
}
else if (!forecast_time_opt && time_range_opt) {
*val = time_range_opt.value().optimize_unit().unit().to_long();
*val = time_range_opt.value().optimize_unit().unit().value<long>();
}
else if (!forecast_time_opt && !time_range_opt) {
*val = Unit{Unit::Value::HOUR}.to_long();
*val = Unit{Unit::Value::HOUR}.value<long>();
}
return GRIB_SUCCESS;
@ -191,7 +191,7 @@ static int unpack_long(grib_accessor* a, long* val, size_t* len)
static int pack_string(grib_accessor* a, const char* val, size_t* len)
{
long unit = Unit{val}.to_long();
long unit = Unit{val}.value<long>();
pack_long(a, &unit, len);
return GRIB_SUCCESS;
}
@ -203,7 +203,7 @@ static int unpack_string(grib_accessor* a, char* val, size_t* len)
size_t unit_len = 0;
if ((ret = unpack_long(a, &unit, &unit_len)) != GRIB_SUCCESS)
return ret;
*len = snprintf(val, *len, "%s", Unit{unit}.to_string().c_str());
*len = snprintf(val, *len, "%s", Unit{unit}.value<std::string>().c_str());
return GRIB_SUCCESS;
}

View File

@ -192,7 +192,7 @@ static int unpack_long(grib_accessor* a, long* val, size_t* len)
Step step{forecast_time_value, forecast_time_unit};
if ((err = grib_set_long_internal(h, "startStepUnit", Unit{step_units}.to_long())) != GRIB_SUCCESS)
if ((err = grib_set_long_internal(h, "startStepUnit", Unit{step_units}.value<long>())) != GRIB_SUCCESS)
return err;
*val = step.value<long>(Unit{step_units});
@ -220,7 +220,7 @@ static int unpack_double(grib_accessor* a, double * val, size_t* len)
Step step{forecast_time_value, forecast_time_unit};
if ((err = grib_set_long_internal(h, "startStepUnit", Unit{step_units}.to_long())) != GRIB_SUCCESS)
if ((err = grib_set_long_internal(h, "startStepUnit", Unit{step_units}.value<long>())) != GRIB_SUCCESS)
return err;
*val = step.value<double>(Unit{step_units});
@ -318,7 +318,7 @@ int pack_long_new_(grib_accessor* a, const long start_step_value, const long sta
auto [sa, sb] = find_common_units(forecast_time.optimize_unit(), time_range.optimize_unit());
if ((err = set_step(h, self->forecast_time_value, self->forecast_time_unit, sa)) != GRIB_SUCCESS)
return err;
if ((err = grib_set_long_internal(h, "startStepUnit", forecast_time.unit().to_long())) != GRIB_SUCCESS)
if ((err = grib_set_long_internal(h, "startStepUnit", forecast_time.unit().value<long>())) != GRIB_SUCCESS)
return err;
if ((err = set_step(h, self->time_range_value, self->time_range_unit, sb)) != GRIB_SUCCESS)
return err;
@ -326,7 +326,7 @@ int pack_long_new_(grib_accessor* a, const long start_step_value, const long sta
}
forecast_time.optimize_unit();
if ((err = grib_set_long_internal(h, "startStepUnit", forecast_time.unit().to_long())) != GRIB_SUCCESS)
if ((err = grib_set_long_internal(h, "startStepUnit", forecast_time.unit().value<long>())) != GRIB_SUCCESS)
return err;
if ((err = set_step(h, self->forecast_time_value, self->forecast_time_unit, forecast_time)) != GRIB_SUCCESS)
return err;
@ -349,7 +349,7 @@ static int pack_long(grib_accessor* a, const long* val, size_t* len)
return ret;
if (start_step_unit == 255)
start_step_unit = Unit{Unit::Value::HOUR}.to_long();
start_step_unit = Unit{Unit::Value::HOUR}.value<long>();
}
else {
start_step_unit = force_step_units;
@ -369,7 +369,7 @@ static int pack_string(grib_accessor* a, const char* val, size_t* len)
Step step = step_from_string(val);
if ((ret = pack_long_new_(a, step.value<long>(), step.unit().to_long())) != GRIB_SUCCESS)
if ((ret = pack_long_new_(a, step.value<long>(), step.unit().value<long>())) != GRIB_SUCCESS)
return ret;
return GRIB_SUCCESS;

View File

@ -113,7 +113,7 @@ std::pair<Step, Step> find_common_units(const Step& startStep, const Step& endSt
}
else {
auto it = std::find_if(Unit::unit_order_.begin(), Unit::unit_order_.end(), [&](const auto& e) {
return e == a.unit().to_value() || e == b.unit().to_value();
return e == a.unit().value<Unit::Value>() || e == b.unit().value<Unit::Value>();
});
assert(it != Unit::unit_order_.end());
@ -147,7 +147,7 @@ void Step::init_long(long value, const Unit& unit)
void Step::init_double(double value, const Unit& unit)
{
long seconds = Unit::get_converter().unit_to_duration(unit.to_value());
long seconds = Unit::get_converter().unit_to_duration(unit.value<Unit::Value>());
init_long(static_cast<long>(value * seconds), Unit{Unit::Value::SECOND});
unit_ = unit;
}

View File

@ -68,7 +68,7 @@ public:
if (unit_ == Unit::Value::HOUR)
u = "";
else
u = unit_.to_string();
u = unit_.value<std::string>();
int err = snprintf(output, max_size, (format + "%s").c_str(), value<double>(), u.c_str());
if (err < 0 || err >= max_size) {
@ -88,7 +88,7 @@ private:
}
Seconds<long> seconds = to_seconds<long>(internal_value_, internal_unit_);
long multiplier = Unit::get_converter().unit_to_duration(unit_.to_value());
long multiplier = Unit::get_converter().unit_to_duration(unit_.value<Unit::Value>());
internal_value_ = seconds.count() / multiplier;
internal_unit_ = unit_;
@ -134,7 +134,7 @@ template <typename T> T Step::value(const Unit& unit) const {
template <typename T>
Seconds<T> to_seconds(long value, const Unit& unit) {
Seconds<T> seconds;
switch (unit.to_value()) {
switch (unit.value<Unit::Value>()) {
case Unit::Value::SECOND: seconds = Seconds<T>(value); break;
case Unit::Value::MINUTE: seconds = Minutes<T>(value); break;
case Unit::Value::MINUTES15: seconds = Minutes15<T>(value); break;
@ -150,7 +150,7 @@ Seconds<T> to_seconds(long value, const Unit& unit) {
case Unit::Value::YEARS30: seconds = Years30<T>(value); break;
case Unit::Value::CENTURY: seconds = Centuries<T>(value); break;
default:
std::string msg = "Unknown unit: " + unit.to_string();
std::string msg = "Unknown unit: " + unit.value<std::string>();
throw std::runtime_error(msg);
}
return seconds;
@ -159,8 +159,8 @@ Seconds<T> to_seconds(long value, const Unit& unit) {
template <typename T>
T from_seconds(Seconds<T> seconds, const Unit& unit) {
T value = 0;
switch (unit.to_value()) {
T value;
switch (unit.value<Unit::Value>()) {
case Unit::Value::SECOND: value = std::chrono::duration_cast<Seconds<T>>(seconds).count(); break;
case Unit::Value::MINUTE: value = std::chrono::duration_cast<Minutes<T>>(seconds).count(); break;
case Unit::Value::MINUTES15: value = std::chrono::duration_cast<Minutes15<T>>(seconds).count(); break;
@ -176,7 +176,7 @@ T from_seconds(Seconds<T> seconds, const Unit& unit) {
case Unit::Value::YEARS30: value = std::chrono::duration_cast<Years30<T>>(seconds).count(); break;
case Unit::Value::CENTURY: value = std::chrono::duration_cast<Centuries<T>>(seconds).count(); break;
default:
std::string msg = "Unknown unit: " + unit.to_string();
std::string msg = "Unknown unit: " + unit.value<std::string>();
throw std::runtime_error(msg);
}
return value;

View File

@ -27,3 +27,14 @@ std::vector<Unit::Value> Unit::complete_unit_order_ = {
Unit::Value::CENTURY
};
template <> long Unit::value<long>() const {
return map_.unit_to_long(internal_value_);
}
template <> Unit::Value Unit::value<Unit::Value>() const {
return internal_value_;
}
template <> std::string Unit::value<std::string>() const {
return map_.unit_to_name(internal_value_);
}

View File

@ -64,12 +64,12 @@ public:
return *this;
}
std::string to_string() const {
return map_.unit_to_name(internal_value_);
}
long to_long() const {return map_.unit_to_long(internal_value_);}
Value to_value() const {return internal_value_;}
//std::string to_string() const {
// return map_.unit_to_name(internal_value_);
//}
//long to_long() const {return map_.unit_to_long(internal_value_);}
//Value to_value() const {return internal_value_;}
template <typename T> T value() const;
static std::vector<Value> unit_order_;
static std::vector<Value> complete_unit_order_;

View File

@ -28,7 +28,7 @@ int set_step(grib_handle* h, const std::string& value_key, const std::string& un
//step_copy.optimize_unit();
if ((err = grib_set_long_internal(h, value_key.c_str(), step_copy.value<long>())) != GRIB_SUCCESS)
return err;
if ((err = grib_set_long_internal(h, unit_key.c_str(), step_copy.unit().to_long())) != GRIB_SUCCESS)
if ((err = grib_set_long_internal(h, unit_key.c_str(), step_copy.unit().value<long>())) != GRIB_SUCCESS)
return err;
return GRIB_SUCCESS;
}