mirror of https://github.com/ecmwf/eccodes.git
ECC-1620: Refactoring of unit
This commit is contained in:
parent
b842df4fdf
commit
0a1b650ba2
|
@ -355,10 +355,10 @@ static int unpack_one_time_range_double_(grib_accessor* a, double *val , size_t*
|
|||
}
|
||||
}
|
||||
if (add_time_range) {
|
||||
*val = (start_step + time_range).value<double>(UnitType(step_units));
|
||||
*val = (start_step + time_range).value<double>(Unit(step_units));
|
||||
}
|
||||
else {
|
||||
*val = start_step.value<double>(UnitType(start_step_unit));
|
||||
*val = start_step.value<double>(Unit(start_step_unit));
|
||||
}
|
||||
|
||||
return GRIB_SUCCESS;
|
||||
|
@ -470,7 +470,7 @@ static int unpack_multiple_time_ranges_double_(grib_accessor* a, double* val, si
|
|||
long the_coded_time_range = arr_coded_time_range[i];
|
||||
|
||||
Step time_range{the_coded_unit, the_coded_time_range};
|
||||
*val = (start_step + time_range).value<double>(UnitType(step_units));
|
||||
*val = (start_step + time_range).value<double>(Unit(step_units));
|
||||
|
||||
return GRIB_SUCCESS;
|
||||
}
|
||||
|
@ -633,7 +633,7 @@ static int pack_long_(grib_accessor* a, const long end_step_value, const long en
|
|||
if (err != GRIB_SUCCESS)
|
||||
return err;
|
||||
|
||||
dstep = end_step.value<double>(UnitType{Unit::DAY});
|
||||
dstep = end_step.value<double>(Unit{Unit::Value::DAY});
|
||||
dend += dstep;
|
||||
|
||||
err = grib_julian_to_datetime(dend, &year_of_end_of_interval, &month_of_end_of_interval,
|
||||
|
@ -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 = UnitType{Unit::HOUR}.to_long();
|
||||
end_step_unit = Unit{Unit::Value::HOUR}.to_long();
|
||||
}
|
||||
else {
|
||||
end_step_unit = force_step_units;
|
||||
|
|
|
@ -142,8 +142,8 @@ static size_t string_length(grib_accessor* a)
|
|||
return 255;
|
||||
}
|
||||
|
||||
static long staticStepUnits = UnitType{Unit::MISSING}.to_long();
|
||||
static long staticForceStepUnits = UnitType{Unit::MISSING}.to_long();
|
||||
static long staticStepUnits = Unit{Unit::Value::MISSING}.to_long();
|
||||
static long staticForceStepUnits = Unit{Unit::Value::MISSING}.to_long();
|
||||
|
||||
static int pack_long(grib_accessor* a, const long* val, size_t* len)
|
||||
{
|
||||
|
@ -183,7 +183,7 @@ static int unpack_long(grib_accessor* a, long* val, size_t* len)
|
|||
*val = time_range_opt.value().optimize_unit().unit().to_long();
|
||||
}
|
||||
else if (!forecast_time_opt && !time_range_opt) {
|
||||
*val = UnitType{Unit::HOUR}.to_long();
|
||||
*val = Unit{Unit::Value::HOUR}.to_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 = UnitType{val}.to_long();
|
||||
long unit = Unit{val}.to_long();
|
||||
pack_long(a, &unit, len);
|
||||
return GRIB_SUCCESS;
|
||||
}
|
||||
|
@ -203,11 +203,10 @@ 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", UnitType{unit}.to_string().c_str());
|
||||
*len = snprintf(val, *len, "%s", Unit{unit}.to_string().c_str());
|
||||
return GRIB_SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
static int get_native_type(grib_accessor* a)
|
||||
{
|
||||
return GRIB_TYPE_LONG;
|
||||
|
|
|
@ -192,10 +192,10 @@ 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", UnitType{step_units}.to_long())) != GRIB_SUCCESS)
|
||||
if ((err = grib_set_long_internal(h, "startStepUnit", Unit{step_units}.to_long())) != GRIB_SUCCESS)
|
||||
return err;
|
||||
|
||||
*val = step.value<long>(UnitType{step_units});
|
||||
*val = step.value<long>(Unit{step_units});
|
||||
|
||||
return GRIB_SUCCESS;
|
||||
}
|
||||
|
@ -220,10 +220,10 @@ 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", UnitType{step_units}.to_long())) != GRIB_SUCCESS)
|
||||
if ((err = grib_set_long_internal(h, "startStepUnit", Unit{step_units}.to_long())) != GRIB_SUCCESS)
|
||||
return err;
|
||||
|
||||
*val = step.value<double>(UnitType{step_units});
|
||||
*val = step.value<double>(Unit{step_units});
|
||||
|
||||
return GRIB_SUCCESS;
|
||||
}
|
||||
|
@ -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 = UnitType{Unit::HOUR}.to_long();
|
||||
start_step_unit = Unit{Unit::Value::HOUR}.to_long();
|
||||
}
|
||||
else {
|
||||
start_step_unit = force_step_units;
|
||||
|
|
20
src/step.cc
20
src/step.cc
|
@ -22,7 +22,7 @@ Step step_from_string(std::string step)
|
|||
if (unit.size() == 0) {
|
||||
unit = "h";
|
||||
}
|
||||
Step ret{std::stod(value), UnitType{unit}};
|
||||
Step ret{std::stod(value), Unit{unit}};
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
|
@ -93,7 +93,7 @@ std::pair<Step, Step> find_common_units(const Step& startStep, const Step& endSt
|
|||
Step b = endStep;
|
||||
|
||||
if (a.internal_value_ == 0 && b.internal_value_ == 0) {
|
||||
UnitType unit = a.internal_unit_ > b.internal_unit_ ? a.internal_unit_ : b.internal_unit_;
|
||||
Unit unit = a.internal_unit_ > b.internal_unit_ ? a.internal_unit_ : b.internal_unit_;
|
||||
b.internal_unit_ = unit;
|
||||
b.unit_ = unit;
|
||||
a.internal_unit_ = unit;
|
||||
|
@ -112,11 +112,11 @@ std::pair<Step, Step> find_common_units(const Step& startStep, const Step& endSt
|
|||
b.recalculateValue();
|
||||
}
|
||||
else {
|
||||
auto it = std::find_if(UnitType::unit_order_.begin(), UnitType::unit_order_.end(), [&](const auto& e) {
|
||||
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();
|
||||
});
|
||||
|
||||
assert(it != UnitType::unit_order_.end());
|
||||
assert(it != Unit::unit_order_.end());
|
||||
|
||||
a.set_unit(*it);
|
||||
b.set_unit(*it);
|
||||
|
@ -137,7 +137,7 @@ void Step::sanity_check() const
|
|||
}
|
||||
|
||||
|
||||
void Step::init_long(long value, const UnitType& unit)
|
||||
void Step::init_long(long value, const Unit& unit)
|
||||
{
|
||||
internal_value_ = value;
|
||||
internal_unit_ = unit;
|
||||
|
@ -145,10 +145,10 @@ void Step::init_long(long value, const UnitType& unit)
|
|||
sanity_check();
|
||||
}
|
||||
|
||||
void Step::init_double(double value, const UnitType& unit)
|
||||
void Step::init_double(double value, const Unit& unit)
|
||||
{
|
||||
long seconds = UnitType::get_converter().unit_to_duration(unit.to_value());
|
||||
init_long(static_cast<long>(value * seconds), UnitType{Unit::SECOND});
|
||||
long seconds = Unit::get_converter().unit_to_duration(unit.to_value());
|
||||
init_long(static_cast<long>(value * seconds), Unit{Unit::Value::SECOND});
|
||||
unit_ = unit;
|
||||
}
|
||||
|
||||
|
@ -161,8 +161,8 @@ Step& Step::optimize_unit()
|
|||
unit_ = internal_unit_;
|
||||
Seconds<long> seconds = to_seconds<long>(internal_value_, internal_unit_);
|
||||
|
||||
for (auto it = UnitType::unit_order_.rbegin(); it != UnitType::unit_order_.rend(); ++it) {
|
||||
long multiplier = UnitType::get_converter().unit_to_duration(*it);
|
||||
for (auto it = Unit::unit_order_.rbegin(); it != Unit::unit_order_.rend(); ++it) {
|
||||
long multiplier = Unit::get_converter().unit_to_duration(*it);
|
||||
if (seconds.count() % multiplier == 0) {
|
||||
internal_value_ = seconds.count() / multiplier;
|
||||
internal_unit_ = *it;
|
||||
|
|
104
src/step.h
104
src/step.h
|
@ -18,28 +18,28 @@
|
|||
class Step {
|
||||
public:
|
||||
// Constructors
|
||||
Step() : internal_value_(0), internal_unit_(Unit::SECOND) {}
|
||||
Step() : internal_value_(0), internal_unit_(Unit::Value::SECOND) {}
|
||||
|
||||
Step(double value, const UnitType& unit) : internal_unit_{unit}, unit_{internal_unit_} {init_double(value, unit);}
|
||||
Step(double value, Unit unit) {init_double(value, UnitType{unit});}
|
||||
Step(double value, long unit) {init_double(value, UnitType{unit});}
|
||||
Step(double value, const std::string& unit) {init_double(value, UnitType{unit});}
|
||||
Step(double value, const Unit& unit) : internal_unit_{unit}, unit_{internal_unit_} {init_double(value, unit);}
|
||||
Step(double value, Unit::Value unit) {init_double(value, Unit{unit});}
|
||||
Step(double value, long unit) {init_double(value, Unit{unit});}
|
||||
Step(double value, const std::string& unit) {init_double(value, Unit{unit});}
|
||||
|
||||
Step(long value, const UnitType& unit) { init_long(value, unit);}
|
||||
Step(long value, Unit unit) {init_long(value, UnitType{unit});}
|
||||
Step(long value, long unit) {init_long(value, UnitType{unit});}
|
||||
Step(long value, const std::string& unit) {init_long(value, UnitType{unit});}
|
||||
Step(long value, const Unit& unit) { init_long(value, unit);}
|
||||
Step(long value, Unit::Value unit) {init_long(value, Unit{unit});}
|
||||
Step(long value, long unit) {init_long(value, Unit{unit});}
|
||||
Step(long value, const std::string& unit) {init_long(value, Unit{unit});}
|
||||
|
||||
// Getters
|
||||
template <typename T> T value() const;
|
||||
template <typename T> T value(const UnitType& unit) const;
|
||||
UnitType unit() const { return unit_; }
|
||||
template <typename T> T value(const Unit& unit) const;
|
||||
Unit unit() const { return unit_; }
|
||||
|
||||
// Setters
|
||||
Step& set_unit(const std::string& unit_name) {unit_ = UnitType{unit_name}; return *this;}
|
||||
Step& set_unit(long unit_code) {unit_ = UnitType{unit_code}; return *this;}
|
||||
Step& set_unit(const UnitType& new_unit) {unit_ = new_unit; return *this;}
|
||||
Step& set_unit(const Unit new_unit) {unit_ = new_unit; return *this;}
|
||||
Step& set_unit(const std::string& unit_name) {unit_ = Unit{unit_name}; return *this;}
|
||||
Step& set_unit(long unit_code) {unit_ = Unit{unit_code}; return *this;}
|
||||
Step& set_unit(const Unit& new_unit) {unit_ = new_unit; return *this;}
|
||||
Step& set_unit(const Unit::Value new_unit) {unit_ = new_unit; return *this;}
|
||||
|
||||
// Operators
|
||||
bool operator==(const Step& other) const;
|
||||
|
@ -65,7 +65,7 @@ public:
|
|||
char output[max_size];
|
||||
std::string u;
|
||||
|
||||
if (unit_ == Unit::HOUR)
|
||||
if (unit_ == Unit::Value::HOUR)
|
||||
u = "";
|
||||
else
|
||||
u = unit_.to_string();
|
||||
|
@ -78,8 +78,8 @@ public:
|
|||
}
|
||||
|
||||
private:
|
||||
void init_long(long value, const UnitType& unit);
|
||||
void init_double(double value, const UnitType& unit);
|
||||
void init_long(long value, const Unit& unit);
|
||||
void init_double(double value, const Unit& unit);
|
||||
void sanity_check() const;
|
||||
Step& recalculateValue() {
|
||||
if (internal_value_ == 0) {
|
||||
|
@ -88,7 +88,7 @@ private:
|
|||
}
|
||||
|
||||
Seconds<long> seconds = to_seconds<long>(internal_value_, internal_unit_);
|
||||
long multiplier = UnitType::get_converter().unit_to_duration(unit_.to_value());
|
||||
long multiplier = Unit::get_converter().unit_to_duration(unit_.to_value());
|
||||
internal_value_ = seconds.count() / multiplier;
|
||||
internal_unit_ = unit_;
|
||||
|
||||
|
@ -96,8 +96,8 @@ private:
|
|||
}
|
||||
|
||||
long internal_value_;
|
||||
UnitType internal_unit_;
|
||||
UnitType unit_;
|
||||
Unit internal_unit_;
|
||||
Unit unit_;
|
||||
};
|
||||
|
||||
|
||||
|
@ -118,7 +118,7 @@ template <typename T> T Step::value() const {
|
|||
return value;
|
||||
}
|
||||
|
||||
template <typename T> T Step::value(const UnitType& unit) const {
|
||||
template <typename T> T Step::value(const Unit& unit) const {
|
||||
if (internal_value_ == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
@ -132,23 +132,23 @@ template <typename T> T Step::value(const UnitType& unit) const {
|
|||
|
||||
|
||||
template <typename T>
|
||||
Seconds<T> to_seconds(long value, const UnitType& unit) {
|
||||
Seconds<T> to_seconds(long value, const Unit& unit) {
|
||||
Seconds<T> seconds;
|
||||
switch (unit.to_value()) {
|
||||
case Unit::SECOND: seconds = Seconds<T>(value); break;
|
||||
case Unit::MINUTE: seconds = Minutes<T>(value); break;
|
||||
case Unit::MINUTES15: seconds = Minutes15<T>(value); break;
|
||||
case Unit::MINUTES30: seconds = Minutes30<T>(value); break;
|
||||
case Unit::HOUR: seconds = Hours<T>(value); break;
|
||||
case Unit::HOURS3: seconds = Hours3<T>(value); break;
|
||||
case Unit::HOURS6: seconds = Hours6<T>(value); break;
|
||||
case Unit::HOURS12: seconds = Hours12<T>(value); break;
|
||||
case Unit::DAY: seconds = Days<T>(value); break;
|
||||
case Unit::MONTH: seconds = Months<T>(value); break;
|
||||
case Unit::YEAR: seconds = Years<T>(value); break;
|
||||
case Unit::YEARS10: seconds = Years10<T>(value); break;
|
||||
case Unit::YEARS30: seconds = Years30<T>(value); break;
|
||||
case Unit::CENTURY: seconds = Centuries<T>(value); break;
|
||||
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;
|
||||
case Unit::Value::MINUTES30: seconds = Minutes30<T>(value); break;
|
||||
case Unit::Value::HOUR: seconds = Hours<T>(value); break;
|
||||
case Unit::Value::HOURS3: seconds = Hours3<T>(value); break;
|
||||
case Unit::Value::HOURS6: seconds = Hours6<T>(value); break;
|
||||
case Unit::Value::HOURS12: seconds = Hours12<T>(value); break;
|
||||
case Unit::Value::DAY: seconds = Days<T>(value); break;
|
||||
case Unit::Value::MONTH: seconds = Months<T>(value); break;
|
||||
case Unit::Value::YEAR: seconds = Years<T>(value); break;
|
||||
case Unit::Value::YEARS10: seconds = Years10<T>(value); break;
|
||||
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();
|
||||
throw std::runtime_error(msg);
|
||||
|
@ -158,23 +158,23 @@ Seconds<T> to_seconds(long value, const UnitType& unit) {
|
|||
|
||||
|
||||
template <typename T>
|
||||
T from_seconds(Seconds<T> seconds, const UnitType& unit) {
|
||||
T from_seconds(Seconds<T> seconds, const Unit& unit) {
|
||||
T value = 0;
|
||||
switch (unit.to_value()) {
|
||||
case Unit::SECOND: value = std::chrono::duration_cast<Seconds<T>>(seconds).count(); break;
|
||||
case Unit::MINUTE: value = std::chrono::duration_cast<Minutes<T>>(seconds).count(); break;
|
||||
case Unit::MINUTES15: value = std::chrono::duration_cast<Minutes15<T>>(seconds).count(); break;
|
||||
case Unit::MINUTES30: value = std::chrono::duration_cast<Minutes30<T>>(seconds).count(); break;
|
||||
case Unit::HOUR: value = std::chrono::duration_cast<Hours<T>>(seconds).count(); break;
|
||||
case Unit::HOURS3: value = std::chrono::duration_cast<Hours3<T>>(seconds).count(); break;
|
||||
case Unit::HOURS6: value = std::chrono::duration_cast<Hours6<T>>(seconds).count(); break;
|
||||
case Unit::HOURS12: value = std::chrono::duration_cast<Hours12<T>>(seconds).count(); break;
|
||||
case Unit::DAY: value = std::chrono::duration_cast<Days<T>>(seconds).count(); break;
|
||||
case Unit::MONTH: value = std::chrono::duration_cast<Months<T>>(seconds).count(); break;
|
||||
case Unit::YEAR: value = std::chrono::duration_cast<Years<T>>(seconds).count(); break;
|
||||
case Unit::YEARS10: value = std::chrono::duration_cast<Years10<T>>(seconds).count(); break;
|
||||
case Unit::YEARS30: value = std::chrono::duration_cast<Years30<T>>(seconds).count(); break;
|
||||
case Unit::CENTURY: value = std::chrono::duration_cast<Centuries<T>>(seconds).count(); break;
|
||||
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;
|
||||
case Unit::Value::MINUTES30: value = std::chrono::duration_cast<Minutes30<T>>(seconds).count(); break;
|
||||
case Unit::Value::HOUR: value = std::chrono::duration_cast<Hours<T>>(seconds).count(); break;
|
||||
case Unit::Value::HOURS3: value = std::chrono::duration_cast<Hours3<T>>(seconds).count(); break;
|
||||
case Unit::Value::HOURS6: value = std::chrono::duration_cast<Hours6<T>>(seconds).count(); break;
|
||||
case Unit::Value::HOURS12: value = std::chrono::duration_cast<Hours12<T>>(seconds).count(); break;
|
||||
case Unit::Value::DAY: value = std::chrono::duration_cast<Days<T>>(seconds).count(); break;
|
||||
case Unit::Value::MONTH: value = std::chrono::duration_cast<Months<T>>(seconds).count(); break;
|
||||
case Unit::Value::YEAR: value = std::chrono::duration_cast<Years<T>>(seconds).count(); break;
|
||||
case Unit::Value::YEARS10: value = std::chrono::duration_cast<Years10<T>>(seconds).count(); break;
|
||||
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();
|
||||
throw std::runtime_error(msg);
|
||||
|
|
|
@ -1,29 +1,29 @@
|
|||
#include "step_unit.h"
|
||||
|
||||
UnitType::Map UnitType::map_{};
|
||||
Unit::Map Unit::map_{};
|
||||
|
||||
std::vector<Unit> UnitType::unit_order_ = {
|
||||
Unit::SECOND,
|
||||
Unit::MINUTE,
|
||||
Unit::HOUR,
|
||||
//Unit::DAY,
|
||||
std::vector<Unit::Value> Unit::unit_order_ = {
|
||||
Unit::Value::SECOND,
|
||||
Unit::Value::MINUTE,
|
||||
Unit::Value::HOUR,
|
||||
//Unit::Value::DAY,
|
||||
};
|
||||
|
||||
std::vector<Unit> UnitType::complete_unit_order_ = {
|
||||
Unit::MISSING ,
|
||||
Unit::SECOND ,
|
||||
Unit::MINUTE ,
|
||||
Unit::MINUTES15 ,
|
||||
Unit::MINUTES30 ,
|
||||
Unit::HOUR ,
|
||||
Unit::HOURS3 ,
|
||||
Unit::HOURS6 ,
|
||||
Unit::HOURS12 ,
|
||||
Unit::DAY ,
|
||||
Unit::MONTH ,
|
||||
Unit::YEAR ,
|
||||
Unit::YEARS10 ,
|
||||
Unit::YEARS30 ,
|
||||
Unit::CENTURY
|
||||
std::vector<Unit::Value> Unit::complete_unit_order_ = {
|
||||
Unit::Value::MISSING ,
|
||||
Unit::Value::SECOND ,
|
||||
Unit::Value::MINUTE ,
|
||||
Unit::Value::MINUTES15 ,
|
||||
Unit::Value::MINUTES30 ,
|
||||
Unit::Value::HOUR ,
|
||||
Unit::Value::HOURS3 ,
|
||||
Unit::Value::HOURS6 ,
|
||||
Unit::Value::HOURS12 ,
|
||||
Unit::Value::DAY ,
|
||||
Unit::Value::MONTH ,
|
||||
Unit::Value::YEAR ,
|
||||
Unit::Value::YEARS10 ,
|
||||
Unit::Value::YEARS30 ,
|
||||
Unit::Value::CENTURY
|
||||
};
|
||||
|
||||
|
|
127
src/step_unit.h
127
src/step_unit.h
|
@ -23,42 +23,43 @@ template <typename T> using Minutes30 = std::chrono::duration<T, std::ratio<1800
|
|||
template <typename T> using Missing = std::chrono::duration<T, std::ratio<0>>;
|
||||
|
||||
|
||||
enum class Unit {
|
||||
MINUTE = 0,
|
||||
HOUR = 1,
|
||||
DAY = 2,
|
||||
MONTH = 3,
|
||||
YEAR = 4,
|
||||
YEARS10 = 5,
|
||||
YEARS30 = 6,
|
||||
CENTURY = 7,
|
||||
HOURS3 = 10,
|
||||
HOURS6 = 11,
|
||||
HOURS12 = 12,
|
||||
SECOND = 13,
|
||||
MINUTES15 = 14,
|
||||
MINUTES30 = 15,
|
||||
MISSING = 255,
|
||||
};
|
||||
|
||||
class UnitType;
|
||||
template <typename T> Seconds<T> to_seconds(long value, const UnitType& unit);
|
||||
template <typename T> T from_seconds(Seconds<T> seconds, const UnitType& unit);
|
||||
class Unit;
|
||||
template <typename T> Seconds<T> to_seconds(long value, const Unit& unit);
|
||||
template <typename T> T from_seconds(Seconds<T> seconds, const Unit& unit);
|
||||
|
||||
class UnitType {
|
||||
class Unit {
|
||||
public:
|
||||
UnitType() : internal_value_(Unit::HOUR) {}
|
||||
explicit UnitType(Unit unit_value) : internal_value_(unit_value) {}
|
||||
explicit UnitType(const std::string& unit_value) {internal_value_ = map_.name_to_unit(unit_value);}
|
||||
explicit UnitType(long unit_value) {internal_value_ = map_.long_to_unit(unit_value);}
|
||||
enum class Value {
|
||||
MINUTE = 0,
|
||||
HOUR = 1,
|
||||
DAY = 2,
|
||||
MONTH = 3,
|
||||
YEAR = 4,
|
||||
YEARS10 = 5,
|
||||
YEARS30 = 6,
|
||||
CENTURY = 7,
|
||||
HOURS3 = 10,
|
||||
HOURS6 = 11,
|
||||
HOURS12 = 12,
|
||||
SECOND = 13,
|
||||
MINUTES15 = 14,
|
||||
MINUTES30 = 15,
|
||||
MISSING = 255,
|
||||
};
|
||||
|
||||
bool operator>(const UnitType& other) const {return map_.unit_to_duration(internal_value_) > map_.unit_to_duration(other.internal_value_);}
|
||||
bool operator==(const Unit value) const {return map_.unit_to_duration(internal_value_) == map_.unit_to_duration(value);}
|
||||
bool operator==(const UnitType& unit) const {return map_.unit_to_duration(internal_value_) == map_.unit_to_duration(unit.internal_value_);}
|
||||
bool operator!=(const UnitType& unit) const {return !(*this == unit);}
|
||||
bool operator!=(const Unit value) const {return !(*this == value);}
|
||||
Unit() : internal_value_(Value::HOUR) {}
|
||||
explicit Unit(Value unit_value) : internal_value_(unit_value) {}
|
||||
explicit Unit(const std::string& unit_value) {internal_value_ = map_.name_to_unit(unit_value);}
|
||||
explicit Unit(long unit_value) {internal_value_ = map_.long_to_unit(unit_value);}
|
||||
|
||||
UnitType& operator=(const Unit value) {
|
||||
bool operator>(const Unit& other) const {return map_.unit_to_duration(internal_value_) > map_.unit_to_duration(other.internal_value_);}
|
||||
bool operator==(const Value value) const {return map_.unit_to_duration(internal_value_) == map_.unit_to_duration(value);}
|
||||
bool operator==(const Unit& unit) const {return map_.unit_to_duration(internal_value_) == map_.unit_to_duration(unit.internal_value_);}
|
||||
bool operator!=(const Unit& unit) const {return !(*this == unit);}
|
||||
bool operator!=(const Value value) const {return !(*this == value);}
|
||||
|
||||
Unit& operator=(const Value value) {
|
||||
internal_value_ = value;
|
||||
return *this;
|
||||
}
|
||||
|
@ -68,9 +69,9 @@ public:
|
|||
}
|
||||
|
||||
long to_long() const {return map_.unit_to_long(internal_value_);}
|
||||
Unit to_value() const {return internal_value_;}
|
||||
static std::vector<Unit> unit_order_;
|
||||
static std::vector<Unit> complete_unit_order_;
|
||||
Value to_value() const {return internal_value_;}
|
||||
static std::vector<Value> unit_order_;
|
||||
static std::vector<Value> complete_unit_order_;
|
||||
|
||||
private:
|
||||
class Map {
|
||||
|
@ -92,54 +93,54 @@ private:
|
|||
}
|
||||
|
||||
// wmo_code <-> unit_name
|
||||
std::string unit_to_name(const Unit& unit_value) const {return value_to_name_.at(unit_value);}
|
||||
Unit name_to_unit(const std::string& name) const {return name_to_value_.at(name);}
|
||||
std::string unit_to_name(const Value& unit_value) const {return value_to_name_.at(unit_value);}
|
||||
Value name_to_unit(const std::string& name) const {return name_to_value_.at(name);}
|
||||
|
||||
// unit_value <-> duration
|
||||
long unit_to_duration(const Unit& unit_value) const {return value_to_duration_.at(unit_value);}
|
||||
Unit duration_to_unit(long duration) const {return duration_to_value_.at(duration);}
|
||||
long unit_to_duration(const Value& unit_value) const {return value_to_duration_.at(unit_value);}
|
||||
Value duration_to_unit(long duration) const {return duration_to_value_.at(duration);}
|
||||
|
||||
// wmo_code <-> unit_name
|
||||
long unit_to_long(const Unit& unit_value) const {return value_to_long_.at(unit_value);}
|
||||
Unit long_to_unit(long wmo_code) const {return long_to_value_.at(wmo_code);}
|
||||
long unit_to_long(const Value& unit_value) const {return value_to_long_.at(unit_value);}
|
||||
Value long_to_unit(long wmo_code) const {return long_to_value_.at(wmo_code);}
|
||||
|
||||
private:
|
||||
struct Entry {
|
||||
Unit unit_value;
|
||||
Value unit_value;
|
||||
std::string unit_name;
|
||||
long duration;
|
||||
};
|
||||
|
||||
const std::array<Entry, 15> tab_ = {
|
||||
Entry{Unit::MISSING , "MISSING" , 0},
|
||||
Entry{Unit::SECOND , "s" , 1},
|
||||
Entry{Unit::MINUTE , "m" , 60},
|
||||
Entry{Unit::MINUTES15 , "15m" , 900},
|
||||
Entry{Unit::MINUTES30 , "30m" , 1800},
|
||||
Entry{Unit::HOUR , "h" , 3600},
|
||||
Entry{Unit::HOURS3 , "3h" , 10800},
|
||||
Entry{Unit::HOURS6 , "6h" , 21600},
|
||||
Entry{Unit::HOURS12 , "12h" , 43200},
|
||||
Entry{Unit::DAY , "D" , 86400},
|
||||
Entry{Unit::MONTH , "M" , 2592000},
|
||||
Entry{Unit::YEAR , "Y" , 31536000},
|
||||
Entry{Unit::YEARS10 , "10Y" , 315360000},
|
||||
Entry{Unit::YEARS30 , "30Y" , 946080000},
|
||||
Entry{Unit::CENTURY , "C" , 3153600000},
|
||||
Entry{Value::MISSING , "MISSING" , 0},
|
||||
Entry{Value::SECOND , "s" , 1},
|
||||
Entry{Value::MINUTE , "m" , 60},
|
||||
Entry{Value::MINUTES15 , "15m" , 900},
|
||||
Entry{Value::MINUTES30 , "30m" , 1800},
|
||||
Entry{Value::HOUR , "h" , 3600},
|
||||
Entry{Value::HOURS3 , "3h" , 10800},
|
||||
Entry{Value::HOURS6 , "6h" , 21600},
|
||||
Entry{Value::HOURS12 , "12h" , 43200},
|
||||
Entry{Value::DAY , "D" , 86400},
|
||||
Entry{Value::MONTH , "M" , 2592000},
|
||||
Entry{Value::YEAR , "Y" , 31536000},
|
||||
Entry{Value::YEARS10 , "10Y" , 315360000},
|
||||
Entry{Value::YEARS30 , "30Y" , 946080000},
|
||||
Entry{Value::CENTURY , "C" , 3153600000},
|
||||
};
|
||||
|
||||
std::unordered_map<std::string, Unit> name_to_value_;
|
||||
std::unordered_map<Unit, std::string> value_to_name_;
|
||||
std::unordered_map<std::string, Value> name_to_value_;
|
||||
std::unordered_map<Value, std::string> value_to_name_;
|
||||
|
||||
std::unordered_map<Unit, long> value_to_long_;
|
||||
std::unordered_map<long, Unit> long_to_value_;
|
||||
std::unordered_map<Value, long> value_to_long_;
|
||||
std::unordered_map<long, Value> long_to_value_;
|
||||
|
||||
std::unordered_map<Unit, long> value_to_duration_;
|
||||
std::unordered_map<long, Unit> duration_to_value_;
|
||||
std::unordered_map<Value, long> value_to_duration_;
|
||||
std::unordered_map<long, Value> duration_to_value_;
|
||||
};
|
||||
|
||||
|
||||
Unit internal_value_;
|
||||
Value internal_value_;
|
||||
static Map map_;
|
||||
public:
|
||||
static Map& get_converter() {return map_;}
|
||||
|
|
Loading…
Reference in New Issue