Skip to content

Commit

Permalink
Merge pull request #36227 from BevapDin/ywh
Browse files Browse the repository at this point in the history
Replace usage of `JsonArray::has_more` with range-based loops.
  • Loading branch information
ZhilkinSerg authored Dec 19, 2019
2 parents 8d25275 + f5efe43 commit fc868d0
Show file tree
Hide file tree
Showing 47 changed files with 392 additions and 546 deletions.
45 changes: 18 additions & 27 deletions src/artifact.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1165,9 +1165,8 @@ void it_artifact_tool::deserialize( const JsonObject &jo )
// Assumption, perhaps dangerous, that we won't wind up with m1 and m2 and
// a materials array in our serialized objects at the same time.
if( jo.has_array( "materials" ) ) {
JsonArray jarr = jo.get_array( "materials" );
for( size_t i = 0; i < jarr.size(); ++i ) {
materials.push_back( material_id( jarr.get_string( i ) ) );
for( const std::string &id : jo.get_array( "materials" ) ) {
materials.push_back( material_id( id ) );
}
}
volume = jo.get_int( "volume" ) * units::legacy_volume_factor;
Expand All @@ -1185,9 +1184,8 @@ void it_artifact_tool::deserialize( const JsonObject &jo )

// Artifacts in older saves store ammo as string.
if( jo.has_array( "ammo" ) ) {
JsonArray atypes = jo.get_array( "ammo" );
for( size_t i = 0; i < atypes.size(); ++i ) {
tool->ammo_id.insert( ammotype( atypes.get_string( i ) ) );
for( const std::string &id : jo.get_array( "ammo" ) ) {
tool->ammo_id.insert( ammotype( id ) );
}
} else if( jo.has_string( "ammo" ) ) {
tool->ammo_id.insert( ammotype( jo.get_string( "ammo" ) ) );
Expand All @@ -1209,36 +1207,31 @@ void it_artifact_tool::deserialize( const JsonObject &jo )
artifact->charge_req = ACR_NULL;
}

JsonArray ja = jo.get_array( "effects_wielded" );
while( ja.has_more() ) {
artifact->effects_wielded.push_back( static_cast<art_effect_passive>( ja.next_int() ) );
for( const int entry : jo.get_array( "effects_wielded" ) ) {
artifact->effects_wielded.push_back( static_cast<art_effect_passive>( entry ) );
}

ja = jo.get_array( "effects_activated" );
while( ja.has_more() ) {
artifact->effects_activated.push_back( static_cast<art_effect_active>( ja.next_int() ) );
for( const int entry : jo.get_array( "effects_activated" ) ) {
artifact->effects_activated.push_back( static_cast<art_effect_active>( entry ) );
}

ja = jo.get_array( "effects_carried" );
while( ja.has_more() ) {
artifact->effects_carried.push_back( static_cast<art_effect_passive>( ja.next_int() ) );
for( const int entry : jo.get_array( "effects_carried" ) ) {
artifact->effects_carried.push_back( static_cast<art_effect_passive>( entry ) );
}

//Generate any missing dream data (due to e.g. old save)
if( !jo.has_array( "dream_unmet" ) ) {
artifact->dream_msg_unmet = artifact_dream_data[static_cast<int>( artifact->charge_req )].msg_unmet;
} else {
ja = jo.get_array( "dream_unmet" );
while( ja.has_more() ) {
artifact->dream_msg_unmet.push_back( ja.next_string() );
for( const std::string &line : jo.get_array( "dream_unmet" ) ) {
artifact->dream_msg_unmet.push_back( line );
}
}
if( !jo.has_array( "dream_met" ) ) {
artifact->dream_msg_met = artifact_dream_data[static_cast<int>( artifact->charge_req )].msg_met;
} else {
ja = jo.get_array( "dream_met" );
while( ja.has_more() ) {
artifact->dream_msg_met.push_back( ja.next_string() );
for( const std::string &line : jo.get_array( "dream_met" ) ) {
artifact->dream_msg_met.push_back( line );
}
}
if( jo.has_int( "dream_freq_unmet" ) ) {
Expand Down Expand Up @@ -1280,9 +1273,8 @@ void it_artifact_armor::deserialize( const JsonObject &jo )
// Assumption, perhaps dangerous, that we won't wind up with m1 and m2 and
// a materials array in our serialized objects at the same time.
if( jo.has_array( "materials" ) ) {
JsonArray jarr = jo.get_array( "materials" );
for( size_t i = 0; i < jarr.size(); ++i ) {
materials.push_back( material_id( jarr.get_string( i ) ) );
for( const std::string &id : jo.get_array( "materials" ) ) {
materials.push_back( material_id( id ) );
}
}
volume = jo.get_int( "volume" ) * units::legacy_volume_factor;
Expand All @@ -1303,9 +1295,8 @@ void it_artifact_armor::deserialize( const JsonObject &jo )
armor->storage = jo.get_int( "storage" ) * units::legacy_volume_factor;
armor->power_armor = jo.get_bool( "power_armor" );

JsonArray ja = jo.get_array( "effects_worn" );
while( ja.has_more() ) {
artifact->effects_worn.push_back( static_cast<art_effect_passive>( ja.next_int() ) );
for( const int entry : jo.get_array( "effects_worn" ) ) {
artifact->effects_worn.push_back( static_cast<art_effect_passive>( entry ) );
}
}

Expand Down
21 changes: 6 additions & 15 deletions src/bionics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2308,30 +2308,22 @@ void load_bionic( const JsonObject &jsobj )
jsobj.read( "fuel_options", new_bionic.fuel_opts );
jsobj.read( "fuel_capacity", new_bionic.fuel_capacity );

JsonArray jsr = jsobj.get_array( "stat_bonus" );
while( jsr.has_more() ) {
JsonArray ja = jsr.next_array();
for( JsonArray ja : jsobj.get_array( "stat_bonus" ) ) {
new_bionic.stat_bonus.emplace( io::string_to_enum<Character::stat>( ja.get_string( 0 ) ),
ja.get_int( 1 ) );
}

JsonArray jsar = jsobj.get_array( "encumbrance" );
while( jsar.has_more() ) {
JsonArray ja = jsar.next_array();
for( JsonArray ja : jsobj.get_array( "encumbrance" ) ) {
new_bionic.encumbrance.emplace( get_body_part_token( ja.get_string( 0 ) ),
ja.get_int( 1 ) );
}

JsonArray jsarr = jsobj.get_array( "occupied_bodyparts" );
while( jsarr.has_more() ) {
JsonArray ja = jsarr.next_array();
for( JsonArray ja : jsobj.get_array( "occupied_bodyparts" ) ) {
new_bionic.occupied_bodyparts.emplace( get_body_part_token( ja.get_string( 0 ) ),
ja.get_int( 1 ) );
}

JsonArray json_arr = jsobj.get_array( "env_protec" );
while( json_arr.has_more() ) {
JsonArray ja = json_arr.next_array();
for( JsonArray ja : jsobj.get_array( "env_protec" ) ) {
new_bionic.env_protec.emplace( get_body_part_token( ja.get_string( 0 ) ),
ja.get_int( 1 ) );
}
Expand Down Expand Up @@ -2529,9 +2521,8 @@ void bionic::deserialize( JsonIn &jsin )
auto_start_threshold = jo.get_float( "auto_start_threshold" );
}
if( jo.has_array( "bionic_tags" ) ) {
JsonArray jsar = jo.get_array( "bionic_tags" );
while( jsar.has_more() ) {
bionic_tags.insert( jsar.next_string() );
for( const std::string &line : jo.get_array( "bionic_tags" ) ) {
bionic_tags.insert( line );
}
}

Expand Down
37 changes: 11 additions & 26 deletions src/cata_tiles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -542,9 +542,7 @@ void tileset_loader::load( const std::string &tileset_id, const bool precheck )
config.throw_error( "\"tile_info\" missing" );
}

JsonArray info = config.get_array( "tile_info" );
while( info.has_more() ) {
JsonObject curr_info = info.next_object();
for( const JsonObject &curr_info : config.get_array( "tile_info" ) ) {
ts.tile_height = curr_info.get_int( "height" );
ts.tile_width = curr_info.get_int( "width" );
tile_iso = curr_info.get_bool( "iso", false );
Expand Down Expand Up @@ -581,9 +579,7 @@ void tileset_loader::load( const std::string &tileset_id, const bool precheck )

int num_in_file = 1;
if( mod_config_json.test_array() ) {
JsonArray mod_config_array = mod_config_json.get_array();
while( mod_config_array.has_more() ) {
JsonObject mod_config = mod_config_array.next_object();
for( const JsonObject &mod_config : mod_config_json.get_array() ) {
if( mod_config.get_string( "type" ) == "mod_tileset" ) {
if( num_in_file == mts.num_in_file() ) {
load_internal( mod_config, tileset_root, img_path );
Expand Down Expand Up @@ -628,9 +624,7 @@ void tileset_loader::load_internal( const JsonObject &config, const std::string
// new system, several entries
// When loading multiple tileset images this defines where
// the tiles from the most recently loaded image start from.
JsonArray tiles_new = config.get_array( "tiles-new" );
while( tiles_new.has_more() ) {
JsonObject tile_part_def = tiles_new.next_object();
for( const JsonObject &tile_part_def : config.get_array( "tiles-new" ) ) {
const std::string tileset_image_path = tileset_root + '/' + tile_part_def.get_string( "file" );
R = -1;
G = -1;
Expand Down Expand Up @@ -728,9 +722,7 @@ void tileset_loader::load_ascii( const JsonObject &config )
if( !config.has_member( "ascii" ) ) {
config.throw_error( "\"ascii\" section missing" );
}
JsonArray ascii = config.get_array( "ascii" );
while( ascii.has_more() ) {
JsonObject entry = ascii.next_object();
for( const JsonObject &entry : config.get_array( "ascii" ) ) {
load_ascii_set( entry );
}
}
Expand Down Expand Up @@ -850,10 +842,7 @@ void tileset_loader::load_tilejson_from_file( const JsonObject &config )
config.throw_error( "\"tiles\" section missing" );
}

JsonArray tiles = config.get_array( "tiles" );
while( tiles.has_more() ) {
JsonObject entry = tiles.next_object();

for( const JsonObject &entry : config.get_array( "tiles" ) ) {
std::vector<std::string> ids;
if( entry.has_string( "id" ) ) {
ids.push_back( entry.get_string( "id" ) );
Expand All @@ -868,9 +857,7 @@ void tileset_loader::load_tilejson_from_file( const JsonObject &config )
int t_h3d = entry.get_int( "height_3d", 0 );
if( t_multi ) {
// fetch additional tiles
JsonArray subentries = entry.get_array( "additional_tiles" );
while( subentries.has_more() ) {
JsonObject subentry = subentries.next_object();
for( const JsonObject &subentry : entry.get_array( "additional_tiles" ) ) {
const std::string s_id = subentry.get_string( "id" );
const std::string m_id = t_id + "_" + s_id;
tile_type &curr_subtile = load_tile( subentry, m_id );
Expand Down Expand Up @@ -921,8 +908,8 @@ void tileset_loader::load_tile_spritelists( const JsonObject &entry,
// create one variation, populate sprite_ids with list of ints
if( g_array.test_int() ) {
std::vector<int> v;
while( g_array.has_more() ) {
const int sprite_id = g_array.next_int() + sprite_id_offset;
for( const int entry : g_array ) {
const int sprite_id = entry + sprite_id_offset;
if( sprite_id >= 0 ) {
v.push_back( sprite_id );
}
Expand All @@ -932,9 +919,8 @@ void tileset_loader::load_tile_spritelists( const JsonObject &entry,
// object elements of array indicates variations
// create one variation per object
else if( g_array.test_object() ) {
while( g_array.has_more() ) {
for( const JsonObject &vo : g_array ) {
std::vector<int> v;
JsonObject vo = g_array.next_object();
int weight = vo.get_int( "weight" );
// negative weight is invalid
if( weight < 0 ) {
Expand All @@ -949,9 +935,8 @@ void tileset_loader::load_tile_spritelists( const JsonObject &entry,
}
// array sprite means rotations
else if( vo.has_array( "sprite" ) ) {
JsonArray sprites = vo.get_array( "sprite" );
while( sprites.has_more() ) {
const int sprite_id = sprites.next_int() + sprite_id_offset;
for( const int entry : vo.get_array( "sprite" ) ) {
const int sprite_id = entry + sprite_id_offset;
if( sprite_id >= 0 && sprite_id < size ) {
v.push_back( sprite_id );
} else {
Expand Down
11 changes: 4 additions & 7 deletions src/clothing_mod.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,24 +64,21 @@ void clothing_mod::load( const JsonObject &jo, const std::string & )
mandatory( jo, was_loaded, "destroy_prompt", destroy_prompt );
optional( jo, was_loaded, "restricted", restricted, false );

JsonArray jarr = jo.get_array( "mod_value" );
while( jarr.has_more() ) {
JsonObject mv_jo = jarr.next_object();
for( const JsonObject &mv_jo : jo.get_array( "mod_value" ) ) {
mod_value mv;
std::string temp_str;
mandatory( mv_jo, was_loaded, "type", temp_str );
mv.type = io::string_to_enum<clothing_mod_type>( temp_str );
mandatory( mv_jo, was_loaded, "value", mv.value );
optional( mv_jo, was_loaded, "round_up", mv.round_up );
JsonArray jarr_prop = mv_jo.get_array( "proportion" );
while( jarr_prop.has_more() ) {
std::string str = jarr_prop.next_string();
for( const JsonValue &entry : mv_jo.get_array( "proportion" ) ) {
const std::string &str = entry.get_string();
if( str == "thickness" ) {
mv.thickness_propotion = true;
} else if( str == "coverage" ) {
mv.coverage_propotion = true;
} else {
jarr_prop.throw_error( R"(Invalid value, valid are: "coverage" and "thickness")" );
entry.throw_error( R"(Invalid value, valid are: "coverage" and "thickness")" );
}
}
mod_values.push_back( mv );
Expand Down
13 changes: 5 additions & 8 deletions src/condition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -894,17 +894,14 @@ conditional_t<T>::conditional_t( const JsonObject &jo )
bool found_sub_member = false;
const auto parse_array = []( const JsonObject & jo, const std::string & type ) {
std::vector<conditional_t> conditionals;
JsonArray ja = jo.get_array( type );
while( ja.has_more() ) {
if( ja.test_string() ) {
conditional_t<T> type_condition( ja.next_string() );
for( const JsonValue &entry : jo.get_array( type ) ) {
if( entry.test_string() ) {
conditional_t<T> type_condition( entry.get_string() );
conditionals.emplace_back( type_condition );
} else if( ja.test_object() ) {
JsonObject cond = ja.next_object();
} else {
JsonObject cond = entry.get_object();
conditional_t<T> type_condition( cond );
conditionals.emplace_back( type_condition );
} else {
ja.skip_value();
}
}
return conditionals;
Expand Down
12 changes: 3 additions & 9 deletions src/construction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1344,9 +1344,7 @@ void load_construction( const JsonObject &jo )

con.description = jo.get_string( "description" );
if( jo.has_member( "required_skills" ) ) {
auto sk = jo.get_array( "required_skills" );
while( sk.has_more() ) {
auto arr = sk.next_array();
for( JsonArray arr : jo.get_array( "required_skills" ) ) {
con.required_skills[skill_id( arr.get_string( 0 ) )] = arr.get_int( 1 );
}
} else {
Expand All @@ -1371,10 +1369,7 @@ void load_construction( const JsonObject &jo )
if( jo.has_string( "using" ) ) {
con.reqs_using = { { requirement_id( jo.get_string( "using" ) ), 1} };
} else if( jo.has_array( "using" ) ) {
auto arr = jo.get_array( "using" );

while( arr.has_more() ) {
auto cur = arr.next_array();
for( JsonArray cur : jo.get_array( "using" ) ) {
con.reqs_using.emplace_back( requirement_id( cur.get_string( 0 ) ), cur.get_int( 1 ) );
}
}
Expand Down Expand Up @@ -1403,8 +1398,7 @@ void load_construction( const JsonObject &jo )
con.post_flags = jo.get_tags( "post_flags" );

if( jo.has_member( "byproducts" ) ) {
JsonIn &stream = *jo.get_raw( "byproducts" );
con.byproduct_item_group = item_group::load_item_group( stream, "collection" );
con.byproduct_item_group = item_group::load_item_group( jo.get_member( "byproducts" ), "collection" );
}

static const std::map<std::string, std::function<bool( const tripoint & )>> pre_special_map = {{
Expand Down
6 changes: 2 additions & 4 deletions src/crafting_gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,8 @@ void load_recipe_category( const JsonObject &jsobj )

const std::string cat_name = get_cat_unprefixed( category );

craft_subcat_list[category] = std::vector<std::string>();
JsonArray subcats = jsobj.get_array( "recipe_subcategories" );
while( subcats.has_more() ) {
const std::string subcat_id = subcats.next_string();
craft_subcat_list[category].clear();
for( const std::string &subcat_id : jsobj.get_array( "recipe_subcategories" ) ) {
if( subcat_id.find( "CSC_" + cat_name + "_" ) != 0 && subcat_id != "CSC_ALL" ) {
jsobj.throw_error( "Crafting sub-category id has to be prefixed with CSC_<category_name>_" );
}
Expand Down
12 changes: 4 additions & 8 deletions src/damage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,7 @@ void damage_instance::deserialize( JsonIn &jsin )
JsonObject jo = jsin.get_object();
damage_units = load_damage_instance( jo ).damage_units;
} else if( jsin.test_array() ) {
JsonArray ja = jsin.get_array();
damage_units = load_damage_instance( ja ).damage_units;
damage_units = load_damage_instance( jsin.get_array() ).damage_units;
} else {
jsin.error( "Expected object or array for damage_instance" );
}
Expand Down Expand Up @@ -304,9 +303,7 @@ damage_instance load_damage_instance( const JsonObject &jo )
{
damage_instance di;
if( jo.has_array( "values" ) ) {
JsonArray jarr = jo.get_array( "values" );
while( jarr.has_more() ) {
JsonObject curr = jarr.next_object();
for( const JsonObject &curr : jo.get_array( "values" ) ) {
di.damage_units.push_back( load_damage_unit( curr ) );
}
} else if( jo.has_string( "damage_type" ) ) {
Expand All @@ -316,11 +313,10 @@ damage_instance load_damage_instance( const JsonObject &jo )
return di;
}

damage_instance load_damage_instance( JsonArray &jarr )
damage_instance load_damage_instance( const JsonArray &jarr )
{
damage_instance di;
while( jarr.has_more() ) {
JsonObject curr = jarr.next_object();
for( const JsonObject &curr : jarr ) {
di.damage_units.push_back( load_damage_unit( curr ) );
}

Expand Down
2 changes: 1 addition & 1 deletion src/damage.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ std::string name_by_dt( const damage_type &dt );
const skill_id &skill_by_dt( damage_type dt );

damage_instance load_damage_instance( const JsonObject &jo );
damage_instance load_damage_instance( JsonArray &jarr );
damage_instance load_damage_instance( const JsonArray &jarr );

resistances load_resistances_instance( const JsonObject &jo );

Expand Down
Loading

0 comments on commit fc868d0

Please sign in to comment.