diff options
author | hop311 <hop3114@gmail.com> | 2023-10-29 15:11:10 +0100 |
---|---|---|
committer | hop311 <hop3114@gmail.com> | 2023-10-29 21:08:08 +0100 |
commit | 1b5e43fa7750cc4025d32f18390593cbce3ba842 (patch) | |
tree | d37fcb69766ec029ea4e3e2816c419f9d7e05f7c /src/openvic-simulation/types | |
parent | 164264b047921dbe1567d2af183e1cffb200a8cb (diff) |
Clang-format formatting (with manual cleanup)
Diffstat (limited to 'src/openvic-simulation/types')
-rw-r--r-- | src/openvic-simulation/types/Date.cpp | 70 | ||||
-rw-r--r-- | src/openvic-simulation/types/IdentifierRegistry.cpp | 10 | ||||
-rw-r--r-- | src/openvic-simulation/types/IdentifierRegistry.hpp | 92 | ||||
-rw-r--r-- | src/openvic-simulation/types/Vector.cpp | 2 | ||||
-rw-r--r-- | src/openvic-simulation/types/fixed_point/FixedPoint.hpp | 2 |
5 files changed, 110 insertions, 66 deletions
diff --git a/src/openvic-simulation/types/Date.cpp b/src/openvic-simulation/types/Date.cpp index 68d0941..8fbb859 100644 --- a/src/openvic-simulation/types/Date.cpp +++ b/src/openvic-simulation/types/Date.cpp @@ -89,8 +89,7 @@ Timespan Timespan::fromYears(day_t num) { } 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) { @@ -112,8 +111,11 @@ Timespan::day_t const* Date::DAYS_UP_TO_MONTH = generate_days_up_to_month(); Timespan::day_t const* Date::generate_days_up_to_month() { static Timespan::day_t days_up_to_month[MONTHS_IN_YEAR]; Timespan::day_t days = 0; - for (int month = 0; month < MONTHS_IN_YEAR; - days_up_to_month[month] = days, days += DAYS_IN_MONTH[month++]); + int month = 0; + while (month < MONTHS_IN_YEAR) { + days_up_to_month[month] = days; + days += DAYS_IN_MONTH[month++]; + } assert(days == DAYS_IN_YEAR); return days_up_to_month; } @@ -123,9 +125,11 @@ Date::month_t const* Date::MONTH_FROM_DAY_IN_YEAR = generate_month_from_day_in_y Date::month_t const* Date::generate_month_from_day_in_year() { static month_t month_from_day_in_year[DAYS_IN_YEAR]; Timespan::day_t days_left = 0; - for (int day = 0, month = 0; day < DAYS_IN_YEAR; - days_left = (days_left > 0 ? days_left : DAYS_IN_MONTH[month++]) - 1, - month_from_day_in_year[day++] = month); + int day = 0, month = 0; + while (day < DAYS_IN_YEAR) { + days_left = (days_left > 0 ? days_left : DAYS_IN_MONTH[month++]) - 1; + month_from_day_in_year[day++] = month; + } assert(days_left == 0); assert(month_from_day_in_year[DAYS_IN_YEAR - 1] == MONTHS_IN_YEAR); return month_from_day_in_year; @@ -211,8 +215,8 @@ 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 @@ -227,8 +231,9 @@ Date Date::from_string(char const* const str, char const* const end, bool* succe if (str == nullptr || end <= str) { if (!quiet) { - Logger::error("Invalid string start/end pointers: ", static_cast<void const*>(str), - " - ", static_cast<void const*>(end)); + Logger::error( + "Invalid string start/end pointers: ", static_cast<void const*>(str), " - ", static_cast<void const*>(end) + ); } if (successful != nullptr) { *successful = false; @@ -237,12 +242,11 @@ Date Date::from_string(char const* const str, char const* const end, bool* succe } char const* year_end = str; - while (std::isdigit(*year_end) && ++year_end < end); + 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) }); + Logger::error("Failed to find year digits in date: ", std::string_view { str, static_cast<size_t>(end - str) }); } if (successful != nullptr) { *successful = false; @@ -267,12 +271,13 @@ Date Date::from_string(char const* const str, char const* const end, bool* succe char const* const month_start = year_end + 1; char const* month_end = month_start; if (month_start < end) { - while (std::isdigit(*month_end) && ++month_end < end); + 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) }); + Logger::error( + "Failed to find month digits in date: ", std::string_view { str, static_cast<size_t>(end - str) } + ); } if (successful != nullptr) { *successful = false; @@ -282,8 +287,7 @@ Date Date::from_string(char const* const str, char const* const end, bool* succe 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) }); + Logger::error("Failed to read month: ", std::string_view { str, static_cast<size_t>(end - str) }); } if (successful != nullptr) { *successful = false; @@ -295,12 +299,14 @@ Date Date::from_string(char const* const str, char const* const end, bool* succe char const* const day_start = month_end + 1; char const* day_end = day_start; if (day_start < end) { - while (std::isdigit(*day_end) && ++day_end < end); + 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) }); + Logger::error( + "Failed to find day digits in date: ", + std::string_view { str, static_cast<size_t>(end - str) } + ); } if (successful != nullptr) { *successful = false; @@ -310,8 +316,9 @@ Date Date::from_string(char const* const str, char const* const end, bool* succe 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) }); + Logger::error( + "Failed to read day: ", std::string_view { str, static_cast<size_t>(end - str) } + ); } if (successful != nullptr) { *successful = false; @@ -324,7 +331,8 @@ Date Date::from_string(char const* const str, char const* const end, bool* succe "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) }); + std::string_view { str, static_cast<size_t>(end - str) } + ); } if (successful != nullptr) { *successful = false; @@ -334,8 +342,10 @@ Date Date::from_string(char const* const str, char const* const end, bool* succe } } else { if (!quiet) { - Logger::error("Unexpected character \"", *month_end, "\" in month of date ", - std::string_view { str, static_cast<size_t>(end - str) }); + 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; @@ -346,8 +356,10 @@ Date Date::from_string(char const* const str, char const* const end, bool* succe } } else { if (!quiet) { - Logger::error("Unexpected character \"", *year_end, "\" in year of date ", - std::string_view { str, static_cast<size_t>(end - str) }); + 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; diff --git a/src/openvic-simulation/types/IdentifierRegistry.cpp b/src/openvic-simulation/types/IdentifierRegistry.cpp index 1bc3747..faddd75 100644 --- a/src/openvic-simulation/types/IdentifierRegistry.cpp +++ b/src/openvic-simulation/types/IdentifierRegistry.cpp @@ -4,8 +4,7 @@ using namespace OpenVic; -HasIdentifier::HasIdentifier(std::string_view new_identifier) - : identifier { new_identifier } { +HasIdentifier::HasIdentifier(std::string_view new_identifier) : identifier { new_identifier } { assert(!identifier.empty()); } @@ -33,7 +32,6 @@ std::string HasColour::colour_to_hex_string() const { return OpenVic::colour_to_hex_string(colour); } -HasIdentifierAndColour::HasIdentifierAndColour(std::string_view new_identifier, - const colour_t new_colour, bool can_be_null, bool can_have_alpha) - : HasIdentifier { new_identifier }, - HasColour { new_colour, can_be_null, can_have_alpha } {} +HasIdentifierAndColour::HasIdentifierAndColour( + std::string_view new_identifier, const colour_t new_colour, bool can_be_null, bool can_have_alpha +) : HasIdentifier { new_identifier }, HasColour { new_colour, can_be_null, can_have_alpha } {} diff --git a/src/openvic-simulation/types/IdentifierRegistry.hpp b/src/openvic-simulation/types/IdentifierRegistry.hpp index 0c5c5ab..bcb8e33 100644 --- a/src/openvic-simulation/types/IdentifierRegistry.hpp +++ b/src/openvic-simulation/types/IdentifierRegistry.hpp @@ -9,8 +9,12 @@ #include "openvic-simulation/utility/Logger.hpp" #define REF_GETTERS(var) \ - constexpr decltype(var)& get_##var() { return var; } \ - constexpr decltype(var) const& get_##var() const { return var; } + constexpr decltype(var)& get_##var() { \ + return var; \ + } \ + constexpr decltype(var) const& get_##var() const { \ + return var; \ + } namespace OpenVic { /* @@ -46,7 +50,9 @@ namespace OpenVic { #define HASID_PROPERTY(NAME) \ const NAME; \ public: \ - auto get_##NAME() const->decltype(get_property(NAME)) { return get_property(NAME); } \ + auto get_##NAME() const->decltype(get_property(NAME)) { \ + return get_property(NAME); \ + } \ private: }; @@ -78,8 +84,9 @@ 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; @@ -96,8 +103,7 @@ 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 { @@ -109,13 +115,17 @@ 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,16 +151,19 @@ 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 = {}) - : name { new_name }, log_lock { new_log_lock }, 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; @@ -212,7 +225,9 @@ private: #define GETTERS \ value_type _const* get_item_by_identifier(std::string_view identifier) _const { \ const typename decltype(identifier_index_map)::const_iterator it = identifier_index_map.find(identifier); \ - if (it != identifier_index_map.end()) return GetPointer(items[it->second]); \ + if (it != identifier_index_map.end()) { \ + return GetPointer(items[it->second]); \ + } \ return nullptr; \ } \ value_type _const* get_item_by_index(size_t index) _const { \ @@ -221,7 +236,9 @@ private: NodeTools::callback_t<std::string_view> expect_item_str(NodeTools::callback_t<value_type _const&> callback) _const { \ return [this, callback](std::string_view identifier) -> bool { \ value_type _const* item = get_item_by_identifier(identifier); \ - if (item != nullptr) return callback(*item); \ + if (item != nullptr) { \ + return callback(*item); \ + } \ Logger::error("Invalid ", name, ": ", identifier); \ return false; \ }; \ @@ -229,7 +246,8 @@ private: NodeTools::node_callback_t expect_item_identifier(NodeTools::callback_t<value_type _const&> callback) _const { \ return NodeTools::expect_identifier(expect_item_str(callback)); \ } \ - NodeTools::node_callback_t expect_item_dictionary(NodeTools::callback_t<value_type _const&, ast::NodeCPtr> callback) _const { \ + NodeTools::node_callback_t expect_item_dictionary(NodeTools::callback_t<value_type _const&, ast::NodeCPtr> callback) \ + _const { \ return NodeTools::expect_dictionary([this, callback](std::string_view key, ast::NodeCPtr value) -> bool { \ return expect_item_str(std::bind(callback, std::placeholders::_1, value))(key); \ }); \ @@ -262,8 +280,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 { @@ -321,8 +339,12 @@ private: using IdentifierInstanceRegistry = InstanceRegistry<_Type, _get_identifier<_Type>>; #define IDENTIFIER_REGISTRY_ACCESSORS_CUSTOM_PLURAL(singular, plural) \ - void lock_##plural() { plural.lock(); } \ - bool plural##_are_locked() const { return plural.is_locked(); } \ + void lock_##plural() { \ + plural.lock(); \ + } \ + bool plural##_are_locked() const { \ + return plural.is_locked(); \ + } \ decltype(plural)::value_type const* get_##singular##_by_identifier(std::string_view identifier) const { \ return plural.get_item_by_identifier(identifier); \ } \ @@ -338,16 +360,24 @@ private: std::vector<std::string_view> get_##singular##_identifiers() const { \ return plural.get_item_identifiers(); \ } \ - NodeTools::callback_t<std::string_view> expect_##singular##_str(NodeTools::callback_t<decltype(plural)::value_type const&> callback) const { \ + NodeTools::callback_t<std::string_view> expect_##singular##_str( \ + NodeTools::callback_t<decltype(plural)::value_type const&> callback \ + ) const { \ return plural.expect_item_str(callback); \ } \ - NodeTools::node_callback_t expect_##singular##_identifier(NodeTools::callback_t<decltype(plural)::value_type const&> callback) const { \ + NodeTools::node_callback_t expect_##singular##_identifier( \ + NodeTools::callback_t<decltype(plural)::value_type const&> callback \ + ) const { \ return plural.expect_item_identifier(callback); \ } \ - NodeTools::node_callback_t expect_##singular##_dictionary(NodeTools::callback_t<decltype(plural)::value_type const&, ast::NodeCPtr> callback) const { \ + NodeTools::node_callback_t expect_##singular##_dictionary( \ + NodeTools::callback_t<decltype(plural)::value_type const&, ast::NodeCPtr> callback \ + ) const { \ return plural.expect_item_dictionary(callback); \ } \ - NodeTools::node_callback_t expect_##singular##_decimal_map(NodeTools::callback_t<decimal_map_t<decltype(plural)::value_type const*>&&> callback) const { \ + NodeTools::node_callback_t expect_##singular##_decimal_map( \ + NodeTools::callback_t<decimal_map_t<decltype(plural)::value_type const*>&&> callback \ + ) const { \ return plural.expect_item_decimal_map(callback); \ } @@ -355,13 +385,17 @@ private: decltype(plural)::value_type* get_##singular##_by_identifier(std::string_view identifier) { \ return plural.get_item_by_identifier(identifier); \ } \ - NodeTools::callback_t<std::string_view> expect_##singular##_str(NodeTools::callback_t<decltype(plural)::value_type&> callback) { \ + NodeTools::callback_t<std::string_view> expect_##singular##_str( \ + NodeTools::callback_t<decltype(plural)::value_type&> callback \ + ) { \ return plural.expect_item_str(callback); \ } \ NodeTools::node_callback_t expect_##singular##_identifier(NodeTools::callback_t<decltype(plural)::value_type&> callback) { \ return plural.expect_item_identifier(callback); \ } \ - NodeTools::node_callback_t expect_##singular##_dictionary(NodeTools::callback_t<decltype(plural)::value_type&, ast::NodeCPtr> callback) { \ + NodeTools::node_callback_t expect_##singular##_dictionary( \ + NodeTools::callback_t<decltype(plural)::value_type&, ast::NodeCPtr> callback \ + ) { \ return plural.expect_item_dictionary(callback); \ } diff --git a/src/openvic-simulation/types/Vector.cpp b/src/openvic-simulation/types/Vector.cpp index 5065084..28f30bf 100644 --- a/src/openvic-simulation/types/Vector.cpp +++ b/src/openvic-simulation/types/Vector.cpp @@ -10,7 +10,7 @@ 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 { }; + return {}; } template<typename T> diff --git a/src/openvic-simulation/types/fixed_point/FixedPoint.hpp b/src/openvic-simulation/types/fixed_point/FixedPoint.hpp index 16e400f..4bf5716 100644 --- a/src/openvic-simulation/types/fixed_point/FixedPoint.hpp +++ b/src/openvic-simulation/types/fixed_point/FixedPoint.hpp @@ -304,7 +304,7 @@ namespace OpenVic { } char const* dot_pointer = str; - while (*dot_pointer != '.' && ++dot_pointer != end); + while (*dot_pointer != '.' && ++dot_pointer != end) {} if (dot_pointer == str && dot_pointer + 1 == end) { // Invalid: ".", "+." or "-." |