diff options
author | hop311 <hop3114@gmail.com> | 2023-10-28 11:39:08 +0200 |
---|---|---|
committer | hop311 <hop3114@gmail.com> | 2023-10-29 20:42:47 +0100 |
commit | 164264b047921dbe1567d2af183e1cffb200a8cb (patch) | |
tree | 21c3c81f65ac3259db4808ebe9fd32a94ca993af /src/openvic-simulation/types | |
parent | d8ec90f07342876e9331819bd3cc372050f78248 (diff) |
Astyle formatting (with manual cleanup)
Diffstat (limited to 'src/openvic-simulation/types')
-rw-r--r-- | src/openvic-simulation/types/Colour.hpp | 4 | ||||
-rw-r--r-- | src/openvic-simulation/types/Date.cpp | 179 | ||||
-rw-r--r-- | src/openvic-simulation/types/IdentifierRegistry.cpp | 4 | ||||
-rw-r--r-- | src/openvic-simulation/types/IdentifierRegistry.hpp | 33 | ||||
-rw-r--r-- | src/openvic-simulation/types/Vector.cpp | 1 | ||||
-rw-r--r-- | src/openvic-simulation/types/fixed_point/FixedPoint.hpp | 28 |
6 files changed, 181 insertions, 68 deletions
diff --git a/src/openvic-simulation/types/Colour.hpp b/src/openvic-simulation/types/Colour.hpp index 15c574f..e516d5b 100644 --- a/src/openvic-simulation/types/Colour.hpp +++ b/src/openvic-simulation/types/Colour.hpp @@ -15,8 +15,8 @@ namespace OpenVic { * When colour_t is used in a purely graphical context, NULL_COLOUR * should be allowed. */ - static constexpr colour_t NULL_COLOUR = 0, FULL_COLOUR = 0xFF, - MAX_COLOUR_RGB = 0xFFFFFF, MAX_COLOUR_ARGB = 0xFFFFFFFF; + static constexpr colour_t NULL_COLOUR = 0, FULL_COLOUR = 0xFF; + static constexpr colour_t MAX_COLOUR_RGB = 0xFFFFFF, MAX_COLOUR_ARGB = 0xFFFFFFFF; constexpr colour_t float_to_colour_byte(float f, float min = 0.0f, float max = 1.0f) { return static_cast<colour_t>(std::clamp(min + f * (max - min), min, max) * 255.0f); diff --git a/src/openvic-simulation/types/Date.cpp b/src/openvic-simulation/types/Date.cpp index 6e21dfc..68d0941 100644 --- a/src/openvic-simulation/types/Date.cpp +++ b/src/openvic-simulation/types/Date.cpp @@ -12,20 +12,40 @@ using namespace OpenVic; Timespan::Timespan(day_t value) : days { value } {} -bool Timespan::operator<(Timespan other) const { return days < other.days; }; -bool Timespan::operator>(Timespan other) const { return days > other.days; }; -bool Timespan::operator<=(Timespan other) const { return days <= other.days; }; -bool Timespan::operator>=(Timespan other) const { return days >= other.days; }; -bool Timespan::operator==(Timespan other) const { return days == other.days; }; -bool Timespan::operator!=(Timespan other) const { return days != other.days; }; +bool Timespan::operator<(Timespan other) const { + return days < other.days; +}; +bool Timespan::operator>(Timespan other) const { + return days > other.days; +}; +bool Timespan::operator<=(Timespan other) const { + return days <= other.days; +}; +bool Timespan::operator>=(Timespan other) const { + return days >= other.days; +}; +bool Timespan::operator==(Timespan other) const { + return days == other.days; +}; +bool Timespan::operator!=(Timespan other) const { + return days != other.days; +}; -Timespan Timespan::operator+(Timespan other) const { return days + other.days; } +Timespan Timespan::operator+(Timespan other) const { + return days + other.days; +} -Timespan Timespan::operator-(Timespan other) const { return days - other.days; } +Timespan Timespan::operator-(Timespan other) const { + return days - other.days; +} -Timespan Timespan::operator*(day_t factor) const { return days * factor; } +Timespan Timespan::operator*(day_t factor) const { + return days * factor; +} -Timespan Timespan::operator/(day_t factor) const { return days / factor; } +Timespan Timespan::operator/(day_t factor) const { + return days / factor; +} Timespan& Timespan::operator+=(Timespan other) { days += other.days; @@ -65,16 +85,16 @@ Timespan::operator std::string() const { } Timespan Timespan::fromYears(day_t num) { - return num * Date::DAYS_IN_YEAR; + return num * Date::DAYS_IN_YEAR; } Timespan Timespan::fromMonths(day_t num) { - return (num / Date::MONTHS_IN_YEAR) * Date::DAYS_IN_YEAR + - Date::DAYS_UP_TO_MONTH[num % Date::MONTHS_IN_YEAR]; + return (num / Date::MONTHS_IN_YEAR) * Date::DAYS_IN_YEAR + + Date::DAYS_UP_TO_MONTH[num % Date::MONTHS_IN_YEAR]; } Timespan Timespan::fromDays(day_t num) { - return num; + return num; } std::ostream& OpenVic::operator<<(std::ostream& out, Timespan const& timespan) { @@ -132,16 +152,32 @@ Date::day_t Date::getDay() const { return (static_cast<Timespan::day_t>(timespan) % DAYS_IN_YEAR) - DAYS_UP_TO_MONTH[getMonth() - 1] + 1; } -bool Date::operator<(Date other) const { return timespan < other.timespan; }; -bool Date::operator>(Date other) const { return timespan > other.timespan; }; -bool Date::operator<=(Date other) const { return timespan <= other.timespan; }; -bool Date::operator>=(Date other) const { return timespan >= other.timespan; }; -bool Date::operator==(Date other) const { return timespan == other.timespan; }; -bool Date::operator!=(Date other) const { return timespan != other.timespan; }; +bool Date::operator<(Date other) const { + return timespan < other.timespan; +}; +bool Date::operator>(Date other) const { + return timespan > other.timespan; +}; +bool Date::operator<=(Date other) const { + return timespan <= other.timespan; +}; +bool Date::operator>=(Date other) const { + return timespan >= other.timespan; +}; +bool Date::operator==(Date other) const { + return timespan == other.timespan; +}; +bool Date::operator!=(Date other) const { + return timespan != other.timespan; +}; -Date Date::operator+(Timespan other) const { return timespan + other; } +Date Date::operator+(Timespan other) const { + return timespan + other; +} -Timespan Date::operator-(Date other) const { return timespan - other.timespan; } +Timespan Date::operator-(Date other) const { + return timespan - other.timespan; +} Date& Date::operator+=(Timespan other) { timespan += other; @@ -175,20 +211,28 @@ Date::operator std::string() const { } std::ostream& OpenVic::operator<<(std::ostream& out, Date const& date) { - return out << static_cast<int>(date.getYear()) << Date::SEPARATOR_CHARACTER << static_cast<int>(date.getMonth()) << Date::SEPARATOR_CHARACTER << static_cast<int>(date.getDay()); + return out << static_cast<int>(date.getYear()) << Date::SEPARATOR_CHARACTER + << static_cast<int>(date.getMonth()) << Date::SEPARATOR_CHARACTER << static_cast<int>(date.getDay()); } // Parsed from string of the form YYYY.MM.DD Date Date::from_string(char const* const str, char const* const end, bool* successful, bool quiet) { - if (successful != nullptr) *successful = true; + if (successful != nullptr) { + *successful = true; + } year_t year = 0; month_t month = 1; day_t day = 1; if (str == nullptr || end <= str) { - if (!quiet) Logger::error("Invalid string start/end pointers: ", static_cast<void const*>(str), " - ", static_cast<void const*>(end)); - if (successful != nullptr) *successful = false; + if (!quiet) { + Logger::error("Invalid string start/end pointers: ", static_cast<void const*>(str), + " - ", static_cast<void const*>(end)); + } + if (successful != nullptr) { + *successful = false; + } return { year, month, day }; } @@ -196,16 +240,25 @@ Date Date::from_string(char const* const str, char const* const end, bool* succe while (std::isdigit(*year_end) && ++year_end < end); if (year_end <= str) { - if (!quiet) Logger::error("Failed to find year digits in date: ", std::string_view { str, static_cast<size_t>(end - str) }); - if (successful != nullptr) *successful = false; + if (!quiet) { + Logger::error("Failed to find year digits in date: ", + std::string_view { str, static_cast<size_t>(end - str) }); + } + if (successful != nullptr) { + *successful = false; + } return { year, month, day }; } bool sub_successful = false; uint64_t val = StringUtils::string_to_uint64(str, year_end, &sub_successful, 10); if (!sub_successful || val > std::numeric_limits<year_t>::max()) { - if (!quiet) Logger::error("Failed to read year: ", std::string_view { str, static_cast<size_t>(end - str) }); - if (successful != nullptr) *successful = false; + if (!quiet) { + Logger::error("Failed to read year: ", std::string_view { str, static_cast<size_t>(end - str) }); + } + if (successful != nullptr) { + *successful = false; + } return { year, month, day }; } year = val; @@ -217,14 +270,24 @@ Date Date::from_string(char const* const str, char const* const end, bool* succe while (std::isdigit(*month_end) && ++month_end < end); } if (month_start >= month_end) { - if (!quiet) Logger::error("Failed to find month digits in date: ", std::string_view { str, static_cast<size_t>(end - str) }); - if (successful != nullptr) *successful = false; + if (!quiet) { + Logger::error("Failed to find month digits in date: ", + std::string_view { str, static_cast<size_t>(end - str) }); + } + if (successful != nullptr) { + *successful = false; + } } else { sub_successful = false; val = StringUtils::string_to_uint64(month_start, month_end, &sub_successful, 10); if (!sub_successful || val < 1 || val > MONTHS_IN_YEAR) { - if (!quiet) Logger::error("Failed to read month: ", std::string_view { str, static_cast<size_t>(end - str) }); - if (successful != nullptr) *successful = false; + if (!quiet) { + Logger::error("Failed to read month: ", + std::string_view { str, static_cast<size_t>(end - str) }); + } + if (successful != nullptr) { + *successful = false; + } } else { month = val; if (month_end < end) { @@ -235,32 +298,60 @@ Date Date::from_string(char const* const str, char const* const end, bool* succe while (std::isdigit(*day_end) && ++day_end < end); } if (day_start >= day_end) { - if (!quiet) Logger::error("Failed to find day digits in date: ", std::string_view { str, static_cast<size_t>(end - str) }); - if (successful != nullptr) *successful = false; + if (!quiet) { + Logger::error("Failed to find day digits in date: ", + std::string_view { str, static_cast<size_t>(end - str) }); + } + if (successful != nullptr) { + *successful = false; + } } else { sub_successful = false; val = StringUtils::string_to_uint64(day_start, day_end, &sub_successful); if (!sub_successful || val < 1 || val > DAYS_IN_MONTH[month - 1]) { - if (!quiet) Logger::error("Failed to read day: ", std::string_view { str, static_cast<size_t>(end - str) }); - if (successful != nullptr) *successful = false; + if (!quiet) { + Logger::error("Failed to read day: ", + std::string_view { str, static_cast<size_t>(end - str) }); + } + if (successful != nullptr) { + *successful = false; + } } else { day = val; if (day_end < end) { - if (!quiet) Logger::error("Unexpected string \"", std::string_view { day_end, static_cast<size_t>(end - day_end) }, "\" at the end of date ", std::string_view { str, static_cast<size_t>(end - str) }); - if (successful != nullptr) *successful = false; + if (!quiet) { + Logger::error( + "Unexpected string \"", + std::string_view { day_end, static_cast<size_t>(end - day_end) }, + "\" at the end of date ", + std::string_view { str, static_cast<size_t>(end - str) }); + } + if (successful != nullptr) { + *successful = false; + } } } } } else { - if (!quiet) Logger::error("Unexpected character \"", *month_end, "\" in month of date ", std::string_view { str, static_cast<size_t>(end - str) }); - if (successful != nullptr) *successful = false; + if (!quiet) { + Logger::error("Unexpected character \"", *month_end, "\" in month of date ", + std::string_view { str, static_cast<size_t>(end - str) }); + } + if (successful != nullptr) { + *successful = false; + } } } } } } else { - if (!quiet) Logger::error("Unexpected character \"", *year_end, "\" in year of date ", std::string_view { str, static_cast<size_t>(end - str) }); - if (successful != nullptr) *successful = false; + if (!quiet) { + Logger::error("Unexpected character \"", *year_end, "\" in year of date ", + std::string_view { str, static_cast<size_t>(end - str) }); + } + if (successful != nullptr) { + *successful = false; + } } } return { year, month, day }; diff --git a/src/openvic-simulation/types/IdentifierRegistry.cpp b/src/openvic-simulation/types/IdentifierRegistry.cpp index f92750b..1bc3747 100644 --- a/src/openvic-simulation/types/IdentifierRegistry.cpp +++ b/src/openvic-simulation/types/IdentifierRegistry.cpp @@ -25,7 +25,9 @@ HasColour::HasColour(colour_t const new_colour, bool can_be_null, bool can_have_ assert((can_be_null || colour != NULL_COLOUR) && colour <= (!can_have_alpha ? MAX_COLOUR_RGB : MAX_COLOUR_ARGB)); } -colour_t HasColour::get_colour() const { return colour; } +colour_t HasColour::get_colour() const { + return colour; +} std::string HasColour::colour_to_hex_string() const { return OpenVic::colour_to_hex_string(colour); diff --git a/src/openvic-simulation/types/IdentifierRegistry.hpp b/src/openvic-simulation/types/IdentifierRegistry.hpp index f53d78b..0c5c5ab 100644 --- a/src/openvic-simulation/types/IdentifierRegistry.hpp +++ b/src/openvic-simulation/types/IdentifierRegistry.hpp @@ -45,10 +45,8 @@ namespace OpenVic { #define HASID_PROPERTY(NAME) \ const NAME; \ -\ public: \ auto get_##NAME() const->decltype(get_property(NAME)) { return get_property(NAME); } \ -\ private: }; @@ -80,7 +78,8 @@ private: */ class HasIdentifierAndColour : public HasIdentifier, public HasColour { protected: - HasIdentifierAndColour(std::string_view new_identifier, const colour_t new_colour, bool can_be_null, bool can_have_alpha); + HasIdentifierAndColour(std::string_view new_identifier, const colour_t new_colour, + bool can_be_null, bool can_have_alpha); public: HasIdentifierAndColour(HasIdentifierAndColour const&) = delete; @@ -97,9 +96,8 @@ private: constexpr auto pred = [](typename decimal_map_t<T>::value_type a, typename decimal_map_t<T>::value_type b) -> bool { return a.second < b.second; }; - const typename decimal_map_t<T>::const_iterator result = std::max_element( - map.begin(), map.end(), pred - ); + const typename decimal_map_t<T>::const_iterator result = + std::max_element(map.begin(), map.end(), pred); if (result != map.end()) { return *result; } else { @@ -111,11 +109,13 @@ private: /* Callbacks for trying to add duplicate keys via UniqueKeyRegistry::add_item */ static bool duplicate_fail_callback(std::string_view registry_name, std::string_view duplicate_identifier) { - Logger::error("Failure adding item to the ", registry_name, " registry - an item with the identifier \"", duplicate_identifier, "\" already exists!"); + Logger::error("Failure adding item to the ", registry_name, " registry - an item with the identifier \"", + duplicate_identifier, "\" already exists!"); return false; } static bool duplicate_warning_callback(std::string_view registry_name, std::string_view duplicate_identifier) { - Logger::warning("Warning adding item to the ", registry_name, " registry - an item with the identifier \"", duplicate_identifier, "\" already exists!"); + Logger::warning("Warning adding item to the ", registry_name, " registry - an item with the identifier \"", + duplicate_identifier, "\" already exists!"); return true; } static bool duplicate_ignore_callback(std::string_view registry_name, std::string_view duplicate_identifier) { @@ -141,14 +141,16 @@ private: using value_type = _Type; using storage_type = _Storage; - UniqueKeyRegistry(std::string_view new_name, bool new_log_lock = true, _GetIdentifier new_GetIdentifier = {}, _GetPointer new_GetPointer = {}) + UniqueKeyRegistry(std::string_view new_name, bool new_log_lock = true, + _GetIdentifier new_GetIdentifier = {}, _GetPointer new_GetPointer = {}) : name { new_name }, log_lock { new_log_lock }, GetIdentifier { new_GetIdentifier }, GetPointer { new_GetPointer } {} std::string_view get_name() const { return name; } - bool add_item(storage_type&& item, NodeTools::callback_t<std::string_view, std::string_view> duplicate_callback = duplicate_fail_callback) { + bool add_item(storage_type&& item, + NodeTools::callback_t<std::string_view, std::string_view> duplicate_callback = duplicate_fail_callback) { if (locked) { Logger::error("Cannot add item to the ", name, " registry - locked!"); return false; @@ -156,7 +158,9 @@ private: const std::string_view new_identifier = GetIdentifier(GetPointer(item)); if (duplicate_callback && duplicate_callback.target<bool(std::string_view, std::string_view)>() == duplicate_ignore_callback) { - if (has_identifier(new_identifier)) return true; + if (has_identifier(new_identifier)) { + return true; + } } else { value_type const* old_item = get_item_by_identifier(new_identifier); if (old_item != nullptr) { @@ -173,7 +177,9 @@ private: Logger::error("Failed to lock ", name, " registry - already locked!"); } else { locked = true; - if (log_lock) Logger::info("Locked ", name, " registry after registering ", size(), " items"); + if (log_lock) { + Logger::info("Locked ", name, " registry after registering ", size(), " items"); + } } } @@ -256,7 +262,8 @@ private: return identifiers; } - NodeTools::node_callback_t expect_item_decimal_map(NodeTools::callback_t<decimal_map_t<value_type const*>&&> callback) const { + NodeTools::node_callback_t expect_item_decimal_map( + NodeTools::callback_t<decimal_map_t<value_type const*>&&> callback) const { return [this, callback](ast::NodeCPtr node) -> bool { decimal_map_t<value_type const*> map; bool ret = expect_item_dictionary([&map](value_type const& key, ast::NodeCPtr value) -> bool { diff --git a/src/openvic-simulation/types/Vector.cpp b/src/openvic-simulation/types/Vector.cpp index 10d2dd2..5065084 100644 --- a/src/openvic-simulation/types/Vector.cpp +++ b/src/openvic-simulation/types/Vector.cpp @@ -8,7 +8,6 @@ constexpr vec2_t<T>::vec2_t(T new_val) : x { new_val }, y { new_val } {} template<typename T> constexpr vec2_t<T>::vec2_t(T new_x, T new_y) : x { new_x }, y { new_y } {} - template<typename T> constexpr vec2_t<T> vec2_t<T>::abs() const { return { }; diff --git a/src/openvic-simulation/types/fixed_point/FixedPoint.hpp b/src/openvic-simulation/types/fixed_point/FixedPoint.hpp index 649d3f6..16e400f 100644 --- a/src/openvic-simulation/types/fixed_point/FixedPoint.hpp +++ b/src/openvic-simulation/types/fixed_point/FixedPoint.hpp @@ -253,7 +253,9 @@ namespace OpenVic { } val += err; } - if (val.is_negative()) stream << "-"; + if (val.is_negative()) { + stream << "-"; + } val = val.abs(); stream << val.to_int64_t() << "."; val = val.get_frac(); @@ -283,7 +285,9 @@ namespace OpenVic { // Deterministic static constexpr fixed_point_t parse(char const* str, char const* const end, bool* successful = nullptr) { - if (successful != nullptr) *successful = false; + if (successful != nullptr) { + *successful = false; + } if (str == nullptr || str >= end) { return _0(); @@ -294,7 +298,9 @@ namespace OpenVic { if (*str == '-') { negative = true; ++str; - if (str == end) return _0(); + if (str == end) { + return _0(); + } } char const* dot_pointer = str; @@ -306,20 +312,26 @@ namespace OpenVic { } fixed_point_t result = _0(); - if (successful != nullptr) *successful = true; + if (successful != nullptr) { + *successful = true; + } if (dot_pointer != str) { // Non-empty integer part bool int_successful = false; result += parse_integer(str, dot_pointer, &int_successful); - if (!int_successful && successful != nullptr) *successful = false; + if (!int_successful && successful != nullptr) { + *successful = false; + } } if (dot_pointer + 1 < end) { // Non-empty fractional part bool frac_successful = false; result += parse_fraction(dot_pointer + 1, end, &frac_successful); - if (!frac_successful && successful != nullptr) *successful = false; + if (!frac_successful && successful != nullptr) { + *successful = false; + } } return negative ? -result : result; @@ -599,7 +611,9 @@ namespace OpenVic { static constexpr fixed_point_t parse_fraction(char const* str, char const* end, bool* successful) { char const* const read_end = str + PRECISION; - if (read_end < end) end = read_end; + if (read_end < end) { + end = read_end; + } uint64_t parsed_value = StringUtils::string_to_uint64(str, end, successful, 10); while (end++ < read_end) { parsed_value *= 10; |