From eb7b37ee713ad5a904797cd253877d7988d0b412 Mon Sep 17 00:00:00 2001 From: anothersimulacrum Date: Thu, 13 Jun 2024 23:48:48 +0000 Subject: [PATCH 1/2] mod_tracker::assign_src uses std::string_view There's no reason for it to use std::string & --- src/bionics.cpp | 2 +- src/bionics.h | 2 +- src/effect_on_condition.cpp | 2 +- src/effect_on_condition.h | 2 +- src/martialarts.cpp | 4 ++-- src/martialarts.h | 4 ++-- src/mod_tracker.h | 2 +- src/mutation.h | 2 +- src/mutation_data.cpp | 2 +- 9 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/bionics.cpp b/src/bionics.cpp index 048dd6dce8362..6aeef60d3f1c0 100644 --- a/src/bionics.cpp +++ b/src/bionics.cpp @@ -306,7 +306,7 @@ static social_modifiers load_bionic_social_mods( const JsonObject &jo ) return ret; } -void bionic_data::load( const JsonObject &jsobj, const std::string &src ) +void bionic_data::load( const JsonObject &jsobj, const std::string_view src ) { mandatory( jsobj, was_loaded, "id", id ); diff --git a/src/bionics.h b/src/bionics.h index fe4fb51c9b634..8718ee7bf054f 100644 --- a/src/bionics.h +++ b/src/bionics.h @@ -184,7 +184,7 @@ struct bionic_data { itype_id itype() const; - void load( const JsonObject &obj, const std::string &src ); + void load( const JsonObject &obj, std::string_view src ); void finalize(); static void load_bionic( const JsonObject &jo, const std::string &src ); static void finalize_bionic(); diff --git a/src/effect_on_condition.cpp b/src/effect_on_condition.cpp index 970303a79b3e4..276caba9baac0 100644 --- a/src/effect_on_condition.cpp +++ b/src/effect_on_condition.cpp @@ -117,7 +117,7 @@ void effect_on_condition::load( const JsonObject &jo, const std::string_view ) } effect_on_condition_id effect_on_conditions::load_inline_eoc( const JsonValue &jv, - const std::string &src ) + std::string_view src ) { if( jv.test_string() ) { return effect_on_condition_id( jv.get_string() ); diff --git a/src/effect_on_condition.h b/src/effect_on_condition.h index eb0208d9f538b..9055eb6928a71 100644 --- a/src/effect_on_condition.h +++ b/src/effect_on_condition.h @@ -96,7 +96,7 @@ void load_new_character( Character &you ); /** Load any new eocs that don't exist in the save. */ void load_existing_character( Character &you ); /** Loads an inline eoc */ -effect_on_condition_id load_inline_eoc( const JsonValue &jv, const std::string &src ); +effect_on_condition_id load_inline_eoc( const JsonValue &jv, std::string_view src ); /** queue an eoc to happen in the future */ void queue_effect_on_condition( time_duration duration, effect_on_condition_id eoc, Character &you, const std::unordered_map &context ); diff --git a/src/martialarts.cpp b/src/martialarts.cpp index 3caf4fdb94f02..38053ad16cfac 100644 --- a/src/martialarts.cpp +++ b/src/martialarts.cpp @@ -261,7 +261,7 @@ void ma_requirements::load( const JsonObject &jo, const std::string_view ) optional( jo, was_loaded, "weapon_damage_requirements", min_damage, ma_weapon_damage_reader {} ); } -void ma_technique::load( const JsonObject &jo, const std::string &src ) +void ma_technique::load( const JsonObject &jo, const std::string_view src ) { mandatory( jo, was_loaded, "name", name ); optional( jo, was_loaded, "description", description, translation() ); @@ -405,7 +405,7 @@ class ma_buff_reader : public generic_typed_reader } }; -void martialart::load( const JsonObject &jo, const std::string &src ) +void martialart::load( const JsonObject &jo, const std::string_view src ) { mandatory( jo, was_loaded, "name", name ); mandatory( jo, was_loaded, "description", description ); diff --git a/src/martialarts.h b/src/martialarts.h index 8b98a41383c83..30de2c062e733 100644 --- a/src/martialarts.h +++ b/src/martialarts.h @@ -163,7 +163,7 @@ class ma_technique public: ma_technique(); - void load( const JsonObject &jo, const std::string &src ); + void load( const JsonObject &jo, std::string_view src ); static void verify_ma_techniques(); void check() const; @@ -326,7 +326,7 @@ class martialart public: martialart(); - void load( const JsonObject &jo, const std::string &src ); + void load( const JsonObject &jo, std::string_view src ); void remove_all_buffs( Character &u ) const; diff --git a/src/mod_tracker.h b/src/mod_tracker.h index e051f29edf0a7..7c970fac7f8fd 100644 --- a/src/mod_tracker.h +++ b/src/mod_tracker.h @@ -47,7 +47,7 @@ struct has_src_member().src.emplace_b /** If those conditions are satisfied, keep track of where this item has been modified */ template::value > * = nullptr> - static void assign_src( T &def, const std::string &src ) { + static void assign_src( T &def, const std::string_view src ) { // We need to make sure we're keeping where this entity has been loaded // If the id this was last loaded with is not this one, discard the history and start again if( !def.src.empty() && def.src.back().first != def.id ) { diff --git a/src/mutation.h b/src/mutation.h index a4573809bf5e7..28cc070a4bb48 100644 --- a/src/mutation.h +++ b/src/mutation.h @@ -400,7 +400,7 @@ struct mutation_branch { // For init.cpp: reset (clear) the mutation data static void reset_all(); // For init.cpp: load mutation data from json - void load( const JsonObject &jo, const std::string &src ); + void load( const JsonObject &jo, std::string_view src ); static void load_trait( const JsonObject &jo, const std::string &src ); // For init.cpp: check internal consistency (valid ids etc.) of all mutations static void check_consistency(); diff --git a/src/mutation_data.cpp b/src/mutation_data.cpp index f36f66375bf47..799a4c39438d2 100644 --- a/src/mutation_data.cpp +++ b/src/mutation_data.cpp @@ -299,7 +299,7 @@ void mutation_variant::deserialize( const JsonObject &jo ) load( jo ); } -void mutation_branch::load( const JsonObject &jo, const std::string &src ) +void mutation_branch::load( const JsonObject &jo, const std::string_view src ) { mandatory( jo, was_loaded, "name", raw_name ); mandatory( jo, was_loaded, "description", raw_desc ); From 179e09bc312077067ddaccc53987e5f19d152e7d Mon Sep 17 00:00:00 2001 From: anothersimulacrum Date: Thu, 13 Jun 2024 23:48:29 +0000 Subject: [PATCH 2/2] Deduplicate src assigning code This is copy/pasted from the function, so just call the function. --- src/item_factory.cpp | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/item_factory.cpp b/src/item_factory.cpp index 26973b9e1b071..5f75230722fcb 100644 --- a/src/item_factory.cpp +++ b/src/item_factory.cpp @@ -4471,10 +4471,7 @@ void Item_factory::load_basic_info( const JsonObject &jo, itype &def, const std: check_and_create_magazine_pockets( def ); add_special_pockets( def ); - if( !def.src.empty() && def.src.back().first != def.id ) { - def.src.clear(); - } - def.src.emplace_back( def.id, mod_id( src ) ); + mod_tracker::assign_src( def, src ); if( def.magazines.empty() ) { migrate_mag_from_pockets( def );