Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make int_id::operator int explicit #37387

Merged
merged 1 commit into from
Jan 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/cata_tiles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2341,7 +2341,7 @@ bool cata_tiles::draw_furniture( const tripoint &p, const lit_level ll, int &hei
};
int subtile = 0;
int rotation = 0;
get_tile_values( f, neighborhood, subtile, rotation );
get_tile_values( f.to_i(), neighborhood, subtile, rotation );
const std::string &fname = f.id().str();
if( g->m.check_seen_cache( p ) ) {
g->u.memorize_tile( g->m.getabs( p ), fname, subtile, rotation );
Expand Down Expand Up @@ -2371,7 +2371,7 @@ bool cata_tiles::draw_furniture( const tripoint &p, const lit_level ll, int &hei
};
int subtile = 0;
int rotation = 0;
get_tile_values( f2, neighborhood, subtile, rotation );
get_tile_values( f2.to_i(), neighborhood, subtile, rotation );
const std::string &fname = f2.id().str();
// tile overrides are never memorized
// tile overrides are always shown with full visibility
Expand Down Expand Up @@ -2415,7 +2415,7 @@ bool cata_tiles::draw_trap( const tripoint &p, const lit_level ll, int &height_3
};
int subtile = 0;
int rotation = 0;
get_tile_values( tr, neighborhood, subtile, rotation );
get_tile_values( tr.to_i(), neighborhood, subtile, rotation );
const std::string trname = tr.id().str();
if( g->m.check_seen_cache( p ) ) {
g->u.memorize_tile( g->m.getabs( p ), trname, subtile, rotation );
Expand Down Expand Up @@ -2445,7 +2445,7 @@ bool cata_tiles::draw_trap( const tripoint &p, const lit_level ll, int &height_3
};
int subtile = 0;
int rotation = 0;
get_tile_values( tr2, neighborhood, subtile, rotation );
get_tile_values( tr2.to_i(), neighborhood, subtile, rotation );
const std::string &trname = tr2.id().str();
// tile overrides are never memorized
// tile overrides are always shown with full visibility
Expand Down Expand Up @@ -2503,7 +2503,7 @@ bool cata_tiles::draw_field_or_item( const tripoint &p, const lit_level ll, int

int subtile = 0;
int rotation = 0;
get_tile_values( fld, neighborhood, subtile, rotation );
get_tile_values( fld.to_i(), neighborhood, subtile, rotation );

ret_draw_field = draw_from_id_string( fld.id().str(), C_FIELD, empty_string, p, subtile,
rotation, lit, nv );
Expand Down
18 changes: 9 additions & 9 deletions src/editmap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ void editmap_hilight::draw( editmap &em, bool update )
char t_sym = terrain.symbol();
nc_color t_col = terrain.color();

if( g->m.furn( p ) > 0 ) {
if( g->m.furn( p ).to_i() > 0 ) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We really need a is_null function in int_id.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I fully agree, but that should perhaps come in a separate PR.

const furn_t &furniture_type = g->m.furn( p ).obj();
t_sym = furniture_type.symbol();
t_col = furniture_type.color();
Expand Down Expand Up @@ -568,7 +568,7 @@ void editmap::update_view_with_help( const std::string &txt, const std::string &
terrain_type.movecost
);
off++; // 2
if( g->m.furn( target ) > 0 ) {
if( g->m.furn( target ).to_i() > 0 ) {
mvwputch( w_info, point( 2, off ), furniture_type.color(), furniture_type.symbol() );
mvwprintw( w_info, point( 4, off ), _( "%d: %s; movecost %d movestr %d" ),
g->m.furn( target ).to_i(),
Expand Down Expand Up @@ -1011,22 +1011,22 @@ void editmap::update_fmenu_entry( uilist &fmenu, field &field, const field_type_
if( fld != nullptr ) {
field_intensity = fld->get_field_intensity();
}
fmenu.entries[idx].txt = ftype.get_name( field_intensity - 1 );
fmenu.entries[idx.to_i()].txt = ftype.get_name( field_intensity - 1 );
if( fld != nullptr ) {
fmenu.entries[idx].txt += " " + std::string( field_intensity, '*' );
fmenu.entries[idx.to_i()].txt += " " + std::string( field_intensity, '*' );
}
fmenu.entries[idx].text_color = fld != nullptr ? c_cyan : fmenu.text_color;
fmenu.entries[idx].extratxt.color = ftype.get_color( field_intensity - 1 );
fmenu.entries[idx].extratxt.txt = ftype.get_symbol( field_intensity - 1 );
fmenu.entries[idx.to_i()].text_color = fld != nullptr ? c_cyan : fmenu.text_color;
fmenu.entries[idx.to_i()].extratxt.color = ftype.get_color( field_intensity - 1 );
fmenu.entries[idx.to_i()].extratxt.txt = ftype.get_symbol( field_intensity - 1 );
}

void editmap::setup_fmenu( uilist &fmenu )
{
fmenu.entries.clear();
for( int i = 0; i < static_cast<int>( field_type::count() ); i++ ) {
const field_type_id fid = static_cast<field_type_id>( i );
fmenu.addentry( fid, true, -2, "" );
fmenu.entries[fid].extratxt.left = 1;
fmenu.addentry( fid.to_i(), true, -2, "" );
fmenu.entries[fid.to_i()].extratxt.left = 1;
update_fmenu_entry( fmenu, g->m.get_field( target ), fid );
}
if( sel_field >= 0 ) {
Expand Down
10 changes: 5 additions & 5 deletions src/generic_factory.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ class generic_factory

bool find_id( const string_id<T> &id, int_id<T> &result ) const {
result = id.get_cid();
if( is_valid( result ) && list[result].id == id ) {
if( is_valid( result ) && list[result.to_i()].id == id ) {
return true;
}
const auto iter = map.find( id );
Expand Down Expand Up @@ -266,7 +266,7 @@ class generic_factory
T &insert( const T &obj ) {
const auto iter = map.find( obj.id );
if( iter != map.end() ) {
T &result = list[iter->second];
T &result = list[iter->second.to_i()];
result = obj;
result.id.set_cid( iter->second );
return result;
Expand Down Expand Up @@ -341,7 +341,7 @@ class generic_factory
debugmsg( "invalid %s id \"%d\"", type_name, id.to_i() );
return dummy_obj;
}
return list[id];
return list[id.to_i()];
}
/**
* Returns the object with the given id.
Expand All @@ -356,14 +356,14 @@ class generic_factory
debugmsg( "invalid %s id \"%s\"", type_name, id.c_str() );
return dummy_obj;
}
return list[i_id];
return list[i_id.to_i()];
}
/**
* Checks whether the factory contains an object with the given id.
* This function can be used to implement @ref int_id::is_valid().
*/
bool is_valid( const int_id<T> &id ) const {
return static_cast<size_t>( id ) < list.size();
return static_cast<size_t>( id.to_i() ) < list.size();
}
/**
* Checks whether the factory contains an object with the given id.
Expand Down
9 changes: 6 additions & 3 deletions src/int_id.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class int_id
}

/**
* Prevent accidental construction from other int ids due to the non-explicit `operator int()`.
* Prevent accidental construction from other int ids.
*/
template < typename S, typename std::enable_if_t < !std::is_same<S, T>::value, int > = 0 >
int_id( const int_id<S> &id ) = delete;
Expand Down Expand Up @@ -86,12 +86,15 @@ class int_id
}
/**
* Conversion to int as with the @ref to_i function.
* It is non-explicit to allow using int_id implicitly as indices etc
*/
operator int() const {
explicit operator int() const {
return _id;
}

explicit operator bool() const {
return _id != 0;
}

// Those are optional, you need to implement them on your own if you want to use them.
// If you don't implement them, but use them, you'll get a linker error.
const string_id<T> &id() const;
Expand Down
14 changes: 7 additions & 7 deletions src/map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1505,14 +1505,14 @@ bool map::ter_set( const tripoint &p, const ter_id &new_terrain )

// HACK: Hack around ledges in traplocs or else it gets NASTY in z-level mode
if( old_t.trap != tr_null && old_t.trap != tr_ledge ) {
auto &traps = traplocs[old_t.trap];
auto &traps = traplocs[old_t.trap.to_i()];
const auto iter = std::find( traps.begin(), traps.end(), p );
if( iter != traps.end() ) {
traps.erase( iter );
}
}
if( new_t.trap != tr_null && new_t.trap != tr_ledge ) {
traplocs[new_t.trap].push_back( p );
traplocs[new_t.trap.to_i()].push_back( p );
}

if( old_t.transparent != new_t.transparent ) {
Expand Down Expand Up @@ -5015,7 +5015,7 @@ void map::trap_set( const tripoint &p, const trap_id &type )

current_submap->set_trap( l, type );
if( type != tr_null ) {
traplocs[type].push_back( p );
traplocs[type.to_i()].push_back( p );
}
}

Expand Down Expand Up @@ -5093,7 +5093,7 @@ void map::remove_trap( const tripoint &p )
}

current_submap->set_trap( l, tr_null );
auto &traps = traplocs[tid];
auto &traps = traplocs[tid.to_i()];
const auto iter = std::find( traps.begin(), traps.end(), p );
if( iter != traps.end() ) {
traps.erase( iter );
Expand Down Expand Up @@ -7034,11 +7034,11 @@ void map::actualize( const tripoint &grid )

const auto trap_here = tmpsub->get_trap( p );
if( trap_here != tr_null ) {
traplocs[trap_here].push_back( pnt );
traplocs[trap_here.to_i()].push_back( pnt );
}
const ter_t &ter = tmpsub->get_ter( p ).obj();
if( ter.trap != tr_null && ter.trap != tr_ledge ) {
traplocs[ter.trap].push_back( pnt );
traplocs[ter.trap.to_i()].push_back( pnt );
}

if( do_funnels ) {
Expand Down Expand Up @@ -7353,7 +7353,7 @@ const std::vector<tripoint> &map::get_furn_field_locations() const

const std::vector<tripoint> &map::trap_locations( const trap_id &type ) const
{
return traplocs[type];
return traplocs[type.to_i()];
}

bool map::inbounds( const tripoint &p ) const
Expand Down
15 changes: 11 additions & 4 deletions src/mapgen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -646,7 +646,7 @@ void mapgen_function_json_base::setup_setmap( const JsonArray &parray )
set_mapgen_defer( pjo, "id", "no such terrain" );
return;
}
tmp_i.val = tid.id();
tmp_i.val = tid.id().to_i();
}
break;
case JMAPGEN_SETMAP_FURN: {
Expand All @@ -656,7 +656,7 @@ void mapgen_function_json_base::setup_setmap( const JsonArray &parray )
set_mapgen_defer( pjo, "id", "no such furniture" );
return;
}
tmp_i.val = fid.id();
tmp_i.val = fid.id().to_i();
}
break;
case JMAPGEN_SETMAP_TRAP: {
Expand Down Expand Up @@ -4539,8 +4539,11 @@ void map::draw_temple( mapgendata &dat )
int x = rng( SEEX - 1, SEEX + 2 ), y = 2;
std::vector<point> path; // Path, from end to start
while( x < SEEX - 1 || x > SEEX + 2 || y < SEEY * 2 - 2 ) {
static const std::vector<ter_id> terrains = {
t_floor_red, t_floor_green, t_floor_blue,
};
path.push_back( point( x, y ) );
ter_set( point( x, y ), ter_id( rng( t_floor_red, t_floor_blue ) ) );
ter_set( point( x, y ), random_entry( terrains ) );
if( y == SEEY * 2 - 2 ) {
if( x < SEEX - 1 ) {
x++;
Expand Down Expand Up @@ -4592,7 +4595,11 @@ void map::draw_temple( mapgendata &dat )
for( int j = 2; j <= SEEY * 2 - 2; j++ ) {
mtrap_set( this, i, j, tr_temple_toggle );
if( ter( point( i, j ) ) == t_rock_floor ) {
ter_set( point( i, j ), ter_id( rng( t_rock_red, t_floor_blue ) ) );
static const std::vector<ter_id> terrains = {
t_rock_red, t_rock_green, t_rock_blue,
t_floor_red, t_floor_green, t_floor_blue,
};
ter_set( point( i, j ), random_entry( terrains ) );
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/mapgen_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3556,7 +3556,7 @@ static void stairs_debug_log( const map *const m, const std::string &msg, const
<< " tripoint: " << p
<< " terrain: " << p_ter.name()
<< " movecost: " << p_ter.movecost
<< " furniture: " << m->furn( p )
<< " furniture: " << m->furn( p ).to_i()
<< " indoors: " << p_ter.has_flag( "INDOORS" )
<< " flat: " << p_ter.has_flag( "FLAT" )
;
Expand Down
10 changes: 5 additions & 5 deletions src/monfaction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ static void apply_base_faction( mfaction_id base, mfaction_id faction_id )
{
for( const auto &pair : base.obj().attitude_map ) {
// Fill in values set in base faction, but not in derived one
auto &faction = faction_list[faction_id];
auto &faction = faction_list[faction_id.to_i()];
if( faction.attitude_map.count( pair.first ) == 0 ) {
faction.attitude_map.insert( pair );
}
Expand Down Expand Up @@ -140,7 +140,7 @@ void monfactions::finalize()
}

// Point parent to children
if( faction.base_faction >= 0 ) {
if( faction.base_faction.to_i() >= 0 ) {
child_map.insert( std::make_pair( faction.base_faction, faction.loadid ) );
}

Expand All @@ -159,7 +159,7 @@ void monfactions::finalize()
// If more than one root exists, use the first one.
const auto root = queue.front();
for( auto &faction : faction_list ) {
if( faction.base_faction < 0 ) {
if( faction.base_faction.to_i() < 0 ) {
faction.base_faction = root;
// If it is the (new) root, connecting it to own parent (self) would create a cycle.
// So only try to connect it to the parent if it isn't own parent.
Expand Down Expand Up @@ -193,7 +193,7 @@ void monfactions::finalize()
for( auto &fac : unloaded ) {
names.append( fac.id().str() );
names.append( " " );
auto &the_faction = faction_list[fac];
auto &the_faction = faction_list[fac.to_i()];
the_faction.base_faction = root;
}

Expand Down Expand Up @@ -240,7 +240,7 @@ void monfactions::load_monster_faction( const JsonObject &jo )
std::string base_faction = jo.get_string( "base_faction", "" );
mfaction_id base_id = get_or_add_faction( mfaction_str_id( base_faction ) );
// Don't get the reference until here (to avoid vector reallocation messing it up)
monfaction &faction = faction_list[cur_id];
monfaction &faction = faction_list[cur_id.to_i()];
faction.base_faction = base_id;

add_to_attitude_map( by_mood, faction.attitude_map, MFA_BY_MOOD );
Expand Down
4 changes: 2 additions & 2 deletions src/newcharacter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2162,7 +2162,7 @@ tab_direction set_description( const catacurses::window &w, avatar &you, const b
int offset = 0;
for( const auto &loc : start_location::get_all() ) {
if( g->scen->allowed_start( loc.ident() ) ) {
uilist_entry entry( loc.ident().get_cid(), true, -1, loc.name() );
uilist_entry entry( loc.ident().get_cid().to_i(), true, -1, loc.name() );

select_location.entries.emplace_back( entry );

Expand Down Expand Up @@ -2399,7 +2399,7 @@ tab_direction set_description( const catacurses::window &w, avatar &you, const b
select_location.query();
if( select_location.ret >= 0 ) {
for( const auto &loc : start_location::get_all() ) {
if( loc.ident().get_cid() == select_location.ret ) {
if( loc.ident().get_cid().to_i() == select_location.ret ) {
you.start_location = loc.ident();
break;
}
Expand Down
2 changes: 1 addition & 1 deletion src/overmap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -972,7 +972,7 @@ void overmap_special::finalize()

void overmap_special::check() const
{
std::set<int> invalid_terrains;
std::set<oter_id> invalid_terrains;
std::set<tripoint> points;

for( const auto &elem : terrains ) {
Expand Down
2 changes: 1 addition & 1 deletion src/overmap_connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ const overmap_connection::subtype *overmap_connection::pick_subtype_for(
return nullptr;
}

const size_t cache_index = ground;
const size_t cache_index = ground.to_i();
assert( cache_index < cached_subtypes.size() );

if( cached_subtypes[cache_index] ) {
Expand Down
2 changes: 1 addition & 1 deletion src/overmap_ui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1235,7 +1235,7 @@ static void place_ter_or_special( tripoint &curs, const tripoint &orig, const bo
colorize( string_from_color( oter.get_color( true ) ), oter.get_color( true ) ),
colorize( oter.get_name(), oter.get_color() ),
colorize( oter.id.str(), c_white ) );
pmenu.addentry( oter.id.id(), true, 0, entry_text );
pmenu.addentry( oter.id.id().to_i(), true, 0, entry_text );
}
} else {
pmenu.title = _( "Select special to place:" );
Expand Down