Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ECC-1771: Fix step units initialisation in static library #197

Merged
merged 1 commit into from
Feb 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions src/step_unit.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@

namespace eccodes {

Unit::Map Unit::map_{};

std::vector<Unit::Value> Unit::grib_selected_units = {
Unit::Value::SECOND,
Unit::Value::MINUTE,
Expand All @@ -39,15 +37,15 @@ std::vector<Unit::Value> Unit::complete_unit_order_ = {
};

template <> long Unit::value<long>() const {
return map_.unit_to_long(internal_value_);
return get_converter().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_);
return get_converter().unit_to_name(internal_value_);
}

} // namespace eccodes
17 changes: 9 additions & 8 deletions src/step_unit.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,23 +68,23 @@ class Unit {

explicit Unit(const std::string& unit_value) {
try {
internal_value_ = map_.name_to_unit(unit_value);
internal_value_ = get_converter().name_to_unit(unit_value);
} catch (std::exception& e) {
throw std::runtime_error(std::string{"Unit not found "} + e.what());
}
}

explicit Unit(long unit_value) {
try {
internal_value_ = map_.long_to_unit(unit_value);
internal_value_ = get_converter().long_to_unit(unit_value);
} catch (std::exception& e) {
throw std::runtime_error(std::string{"Unit not found "} + e.what());
}
}

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& other) const {return get_converter().unit_to_duration(internal_value_) > get_converter().unit_to_duration(other.internal_value_);}
bool operator==(const Value value) const {return get_converter().unit_to_duration(internal_value_) == get_converter().unit_to_duration(value);}
bool operator==(const Unit& unit) const {return get_converter().unit_to_duration(internal_value_) == get_converter().unit_to_duration(unit.internal_value_);}
bool operator!=(const Unit& unit) const {return !(*this == unit);}
bool operator!=(const Value value) const {return !(*this == value);}

Expand Down Expand Up @@ -177,13 +177,14 @@ class Unit {


Value internal_value_;
static Map map_;
public:
static Map& get_converter() {return map_;}
static Map& get_converter() {
static Map map_;
return map_;
}
};



template <typename T>
Seconds<T> to_seconds(long value, const Unit& unit) {
Seconds<T> seconds;
Expand Down
Loading