Keberus said:
Code:
void room_index::wipe_coord_resets( short map, short x, short y )
{
reset_data *pReset;
list < reset_data * >::iterator rst;
for( rst = resets.begin( ); rst != resets.end( ); )
{
pReset = *rst;
++rst;
if( (pReset->arg4 == map) && ( pReset->arg5 == x ) && ( pReset->arg6 == y ) )
{
resets.remove( pReset );
delete_reset( pReset );
}
}
}
Looks like a perfectly reasonable function. Generally speaking you might want to consider using algorithms rather than writing explicit loops, because it's less error prone, often more efficient and can make the code look clear and straight forward (and simply because I like them

). I'll first write a suggested version, then offer some brief explanation of what's going on.
Code:
#include <functional>
// Predicate function object
class CoordResetDeleter : public std::unary_function< reset_data*, bool >
{
public:
CoordResetDeleter( int m, int x, int y ) : _map( m ), _x( x ), _y( y ) { }
bool operator()( reset_data *pReset ) const
{
if( pReset->arg4 == _map && pReset->arg5 == _x && pReset->arg6 == _y )
{
delete pReset;
return true;
}
return false;
}
private:
int _map;
int _x;
int _y;
};
// room_index member function
void room_index::wipe_coord_resets( short map, short x, short y )
{
resets.remove_if( CoordResetDeleter( map, x, y ) );
}
The list.remove_if member function calls the predicate function in its argument for each element in the list (or operator() in the case of function objects). In this case we pass the conditions (map, x and y) to the constructor. If we didn't need to store these variables then we could have used a function instead of a function object.
We inherit from the std::unary_function template, because it creates some typedefS and such that are needed when using it with some STL algorithms, so it's generally a good habit to do it. You'll usually need to include the <functional> header for this.
My choice of name (CoordResetDeleter) is probably not the best. Try finding something that makes the line resets.remove_if( condition_is_fullfilled ) crystal clear to a human reader.
You can often reuse such predicate function classes in many different contexts, so it's definitely worth your time looking into it.
Oh, and a standard disclaimer, I've not tested the above code, may contain typos, etc etc.
Edit: of course with this solution you don't actually need the room_index::wipe_coord_resets member function, you can just call room->resets.remove_if directly in the client code. Unless the resets list is private, that is.
.........................
SWR2 Refactor - The improved SWR 2.0 codebase.