From d95c3c9da75018e3e959e5493ebd9c520e00bf7a Mon Sep 17 00:00:00 2001 From: hop311 Date: Thu, 12 Oct 2023 23:43:10 +0100 Subject: Changed return types to std::string_view --- src/openvic-simulation/economy/Building.cpp | 2 +- src/openvic-simulation/economy/Building.hpp | 2 +- src/openvic-simulation/military/Unit.cpp | 14 +++++++------- src/openvic-simulation/military/Unit.hpp | 14 +++++++------- src/openvic-simulation/politics/Government.cpp | 2 +- src/openvic-simulation/politics/Government.hpp | 2 +- src/openvic-simulation/pop/Culture.cpp | 2 +- src/openvic-simulation/pop/Culture.hpp | 2 +- src/openvic-simulation/types/IdentifierRegistry.cpp | 2 +- src/openvic-simulation/types/IdentifierRegistry.hpp | 20 ++++++++++++++------ 10 files changed, 35 insertions(+), 27 deletions(-) diff --git a/src/openvic-simulation/economy/Building.cpp b/src/openvic-simulation/economy/Building.cpp index d27c91a..23bd04c 100644 --- a/src/openvic-simulation/economy/Building.cpp +++ b/src/openvic-simulation/economy/Building.cpp @@ -17,7 +17,7 @@ BuildingType const& Building::get_type() const { return type; } -std::string const& Building::get_on_completion() const { +std::string_view Building::get_on_completion() const { return on_completion; } diff --git a/src/openvic-simulation/economy/Building.hpp b/src/openvic-simulation/economy/Building.hpp index 3f74c3d..3d1e24b 100644 --- a/src/openvic-simulation/economy/Building.hpp +++ b/src/openvic-simulation/economy/Building.hpp @@ -70,7 +70,7 @@ namespace OpenVic { Building(Building&&) = default; BuildingType const& get_type() const; - std::string const& get_on_completion() const; + std::string_view get_on_completion() const; fixed_point_t get_completion_size() const; level_t get_max_level() const; std::map const& get_goods_cost() const; diff --git a/src/openvic-simulation/military/Unit.cpp b/src/openvic-simulation/military/Unit.cpp index 3da24da..63ece91 100644 --- a/src/openvic-simulation/military/Unit.cpp +++ b/src/openvic-simulation/military/Unit.cpp @@ -27,7 +27,7 @@ Unit::type_t Unit::get_type() const { return type; } -std::string const& Unit::get_sprite() const { +std::string_view Unit::get_sprite() const { return sprite; } @@ -35,7 +35,7 @@ bool Unit::is_active() const { return active; } -std::string const& Unit::get_unit_type() const { +std::string_view Unit::get_unit_type() const { return unit_type; } @@ -88,15 +88,15 @@ bool LandUnit::get_primary_culture() const { return primary_culture; } -std::string const& LandUnit::get_sprite_override() const { +std::string_view LandUnit::get_sprite_override() const { return sprite_override; } -std::string const& LandUnit::get_sprite_mount() const { +std::string_view LandUnit::get_sprite_mount() const { return sprite_mount; } -std::string const& LandUnit::get_sprite_mount_attach_node() const { +std::string_view LandUnit::get_sprite_mount_attach_node() const { return sprite_mount_attach_node; } @@ -146,11 +146,11 @@ bool NavalUnit::is_transport() const { return transport; } -std::string const& NavalUnit::get_move_sound() const { +std::string_view NavalUnit::get_move_sound() const { return move_sound; } -std::string const& NavalUnit::get_select_sound() const { +std::string_view NavalUnit::get_select_sound() const { return select_sound; } diff --git a/src/openvic-simulation/military/Unit.hpp b/src/openvic-simulation/military/Unit.hpp index 65e6259..de30763 100644 --- a/src/openvic-simulation/military/Unit.hpp +++ b/src/openvic-simulation/military/Unit.hpp @@ -57,9 +57,9 @@ namespace OpenVic { icon_t get_icon() const; type_t get_type() const; - std::string const& get_sprite() const; + std::string_view get_sprite() const; bool is_active() const; - std::string const& get_unit_type() const; + std::string_view get_unit_type() const; bool has_floating_flag() const; uint32_t get_priority() const; @@ -94,9 +94,9 @@ namespace OpenVic { LandUnit(LandUnit&&) = default; bool get_primary_culture() const; - std::string const& get_sprite_override() const; - std::string const& get_sprite_mount() const; - std::string const& get_sprite_mount_attach_node() const; + std::string_view get_sprite_override() const; + std::string_view get_sprite_mount() const; + std::string_view get_sprite_mount_attach_node() const; fixed_point_t get_reconnaissance() const; fixed_point_t get_attack() const; @@ -138,8 +138,8 @@ namespace OpenVic { bool can_sail() const; bool is_transport() const; bool is_capital() const; - std::string const& get_move_sound() const; - std::string const& get_select_sound() const; + std::string_view get_move_sound() const; + std::string_view get_select_sound() const; fixed_point_t get_colonial_points() const; bool can_build_overseas() const; uint32_t get_min_port_level() const; diff --git a/src/openvic-simulation/politics/Government.cpp b/src/openvic-simulation/politics/Government.cpp index 1a48806..16b3bc4 100644 --- a/src/openvic-simulation/politics/Government.cpp +++ b/src/openvic-simulation/politics/Government.cpp @@ -30,7 +30,7 @@ Timespan GovernmentType::get_term_duration() const { return term_duration; } -std::string const& GovernmentType::get_flag_type() const { +std::string_view GovernmentType::get_flag_type() const { return flag_type_identifier; } diff --git a/src/openvic-simulation/politics/Government.hpp b/src/openvic-simulation/politics/Government.hpp index c72dae6..ba8496f 100644 --- a/src/openvic-simulation/politics/Government.hpp +++ b/src/openvic-simulation/politics/Government.hpp @@ -24,7 +24,7 @@ namespace OpenVic { bool holds_elections() const; bool can_appoint_ruling_party() const; Timespan get_term_duration() const; - std::string const& get_flag_type() const; + std::string_view get_flag_type() const; }; struct GovernmentTypeManager { diff --git a/src/openvic-simulation/pop/Culture.cpp b/src/openvic-simulation/pop/Culture.cpp index 261ad4c..e3ba5dd 100644 --- a/src/openvic-simulation/pop/Culture.cpp +++ b/src/openvic-simulation/pop/Culture.cpp @@ -15,7 +15,7 @@ CultureGroup::CultureGroup(std::string_view new_identifier, std::string_view new unit_graphical_culture_type { new_unit_graphical_culture_type }, is_overseas { new_is_overseas } {} -std::string const& CultureGroup::get_leader() const { +std::string_view CultureGroup::get_leader() const { return leader; } diff --git a/src/openvic-simulation/pop/Culture.hpp b/src/openvic-simulation/pop/Culture.hpp index b6b4952..d36a90b 100644 --- a/src/openvic-simulation/pop/Culture.hpp +++ b/src/openvic-simulation/pop/Culture.hpp @@ -31,7 +31,7 @@ namespace OpenVic { public: CultureGroup(CultureGroup&&) = default; - std::string const& get_leader() const; + std::string_view get_leader() const; GraphicalCultureType const& get_unit_graphical_culture_type() const; bool get_is_overseas() const; }; diff --git a/src/openvic-simulation/types/IdentifierRegistry.cpp b/src/openvic-simulation/types/IdentifierRegistry.cpp index d6afd3a..8e93cb1 100644 --- a/src/openvic-simulation/types/IdentifierRegistry.cpp +++ b/src/openvic-simulation/types/IdentifierRegistry.cpp @@ -9,7 +9,7 @@ HasIdentifier::HasIdentifier(std::string_view new_identifier) assert(!identifier.empty()); } -std::string const& HasIdentifier::get_identifier() const { +std::string_view HasIdentifier::get_identifier() const { return identifier; } diff --git a/src/openvic-simulation/types/IdentifierRegistry.hpp b/src/openvic-simulation/types/IdentifierRegistry.hpp index aea5728..3ba7fc4 100644 --- a/src/openvic-simulation/types/IdentifierRegistry.hpp +++ b/src/openvic-simulation/types/IdentifierRegistry.hpp @@ -28,7 +28,7 @@ namespace OpenVic { HasIdentifier& operator=(HasIdentifier const&) = delete; HasIdentifier& operator=(HasIdentifier&&) = delete; - std::string const& get_identifier() const; + std::string_view get_identifier() const; }; std::ostream& operator<<(std::ostream& stream, HasIdentifier const& obj); @@ -73,7 +73,7 @@ namespace OpenVic { distribution_t::value_type get_largest_item(distribution_t const& dist); template - using get_identifier_func_t = std::string const&(T::*)(void) const; + using get_identifier_func_t = std::string_view(T::*)(void) const; template _Type, get_identifier_func_t<_Base> get_identifier, typename _Storage, _Type* (*get_ptr)(_Storage&), _Type const* (*get_cptr)(_Storage const&)> @@ -92,7 +92,7 @@ namespace OpenVic { UniqueKeyRegistry(std::string_view new_name, bool new_log_lock = true) : name { new_name }, log_lock { new_log_lock } {} - std::string const& get_name() const { + std::string_view get_name() const { return name; } @@ -102,7 +102,7 @@ namespace OpenVic { return false; } value_type const* new_item = (*get_cptr)(item); - std::string const& new_identifier = (new_item->*get_identifier)(); + const std::string_view new_identifier = (new_item->*get_identifier)(); value_type const* old_item = get_item_by_identifier(new_identifier); if (old_item != nullptr) { #define DUPLICATE_MESSAGE "Cannot add item to the ", name, " registry - an item with the identifier \"", new_identifier, "\" already exists!" @@ -115,7 +115,7 @@ namespace OpenVic { } #undef DUPLICATE_MESSAGE } - identifier_index_map[new_identifier] = items.size(); + identifier_index_map.emplace(new_identifier, items.size()); items.push_back(std::move(item)); return true; } @@ -249,8 +249,16 @@ namespace OpenVic { } }; + template + [[nodiscard]] inline constexpr T* _addressof(T& v) noexcept { + return std::addressof(v); + } + + template + const T* _addressof(const T&&) = delete; + template _Type, get_identifier_func_t<_Base> get_identifier> - using ValueRegistry = UniqueKeyRegistry<_Base, _Type, get_identifier, _Type, std::addressof<_Type>, std::addressof>; + using ValueRegistry = UniqueKeyRegistry<_Base, _Type, get_identifier, _Type, _addressof<_Type>, _addressof>; template constexpr _Type* get_ptr(std::unique_ptr<_Type>& storage) { -- cgit v1.2.3-56-ga3b1