Skip to content

Commit

Permalink
Merge pull request #2506 from JanVogelsang/refactoring-casts
Browse files Browse the repository at this point in the history
Modernize the codebase - convert C-style casts to static_casts
  • Loading branch information
jougs authored Aug 7, 2023
2 parents e8932d7 + 721837a commit d31b9a2
Show file tree
Hide file tree
Showing 18 changed files with 84 additions and 84 deletions.
2 changes: 1 addition & 1 deletion libnestutil/numerics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ const double numerics::nan = 0.0 / 0.0;
long
ld_round( double x )
{
return ( long ) std::floor( x + 0.5 );
return static_cast< long >( std::floor( x + 0.5 ) );
}

double
Expand Down
14 changes: 7 additions & 7 deletions libnestutil/stopwatch.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,12 @@ class Stopwatch

enum
{
MICROSEC = ( timeunit_t ) 1,
MILLISEC = MICROSEC * ( timeunit_t ) 1000,
SECONDS = MILLISEC * ( timeunit_t ) 1000,
MINUTES = SECONDS * ( timeunit_t ) 60,
HOURS = MINUTES * ( timeunit_t ) 60,
DAYS = HOURS * ( timeunit_t ) 24
MICROSEC = static_cast< timeunit_t >( 1 ),
MILLISEC = MICROSEC * static_cast< timeunit_t >( 1000 ),
SECONDS = MILLISEC * static_cast< timeunit_t >( 1000 ),
MINUTES = SECONDS * static_cast< timeunit_t >( 60 ),
HOURS = MINUTES * static_cast< timeunit_t >( 60 ),
DAYS = HOURS * static_cast< timeunit_t >( 24 )
};

static bool correct_timeunit( timeunit_t t );
Expand Down Expand Up @@ -222,7 +222,7 @@ nest::Stopwatch::elapsed_timestamp() const
return _end - _beg + _prev_elapsed;
}
#else
return ( timestamp_t ) 0;
return static_cast< timestamp_t >( 0 );
#endif
}

Expand Down
2 changes: 1 addition & 1 deletion nestkernel/layer_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ MaskedLayer< D >::check_mask_( Layer< D >& layer, bool allow_oversized )
for ( int i = 0; i < D; ++i )
{
oversize |= layer.get_periodic_mask()[ i ]
and ( grid_mask.get_lower_right()[ i ] - grid_mask.get_upper_left()[ i ] ) > ( int ) dims[ i ];
and ( grid_mask.get_lower_right()[ i ] - grid_mask.get_upper_left()[ i ] ) > static_cast< int >( dims[ i ] );
}
if ( oversize )
{
Expand Down
2 changes: 1 addition & 1 deletion nestkernel/model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ Model::set_threads_( size_t t )
void
Model::reserve_additional( size_t t, size_t n )
{
assert( ( size_t ) t < memory_.size() );
assert( t < memory_.size() );
memory_[ t ].reserve( n );
}

Expand Down
2 changes: 1 addition & 1 deletion nestkernel/model.h
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ class Model
inline Node*
Model::create( size_t t )
{
assert( ( size_t ) t < memory_.size() );
assert( t < memory_.size() );
Node* n = create_();
memory_[ t ].emplace_back( n );
return n;
Expand Down
2 changes: 1 addition & 1 deletion nestkernel/model_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ size_t
ModelManager::get_node_model_id( const Name name ) const
{
const Name model_name( name );
for ( int i = 0; i < ( int ) node_models_.size(); ++i )
for ( int i = 0; i < static_cast< int >( node_models_.size() ); ++i )
{
assert( node_models_[ i ] );
if ( model_name == node_models_[ i ]->get_name() )
Expand Down
24 changes: 12 additions & 12 deletions nestkernel/ring_buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,8 @@ RingBuffer::set_value( const long offs, const double v )
inline double
RingBuffer::get_value( const long offs )
{
assert( 0 <= offs and ( size_t ) offs < buffer_.size() );
assert( ( long ) offs < kernel().connection_manager.get_min_delay() );
assert( 0 <= offs and static_cast< size_t >( offs ) < buffer_.size() );
assert( offs < kernel().connection_manager.get_min_delay() );

// offs == 0 is beginning of slice, but we have to
// take modulo into account when indexing
Expand All @@ -183,8 +183,8 @@ RingBuffer::get_value( const long offs )
inline double
RingBuffer::get_value_wfr_update( const long offs )
{
assert( 0 <= offs and ( size_t ) offs < buffer_.size() );
assert( ( long ) offs < kernel().connection_manager.get_min_delay() );
assert( 0 <= offs and static_cast< size_t >( offs ) < buffer_.size() );
assert( offs < kernel().connection_manager.get_min_delay() );

// offs == 0 is beginning of slice, but we have to
// take modulo into account when indexing
Expand All @@ -198,7 +198,7 @@ RingBuffer::get_index_( const long d ) const
{
const long idx = kernel().event_delivery_manager.get_modulo( d );
assert( 0 <= idx );
assert( ( size_t ) idx < buffer_.size() );
assert( static_cast< size_t >( idx ) < buffer_.size() );
return idx;
}

Expand Down Expand Up @@ -258,15 +258,15 @@ class MultRBuffer
inline void
MultRBuffer::add_value( const long offs, const double v )
{
assert( 0 <= offs and ( size_t ) offs < buffer_.size() );
assert( 0 <= offs and static_cast< size_t >( offs ) < buffer_.size() );
buffer_[ get_index_( offs ) ] *= v;
}

inline double
MultRBuffer::get_value( const long offs )
{
assert( 0 <= offs and ( size_t ) offs < buffer_.size() );
assert( ( long ) offs < kernel().connection_manager.get_min_delay() );
assert( 0 <= offs and static_cast< size_t >( offs ) < buffer_.size() );
assert( offs < kernel().connection_manager.get_min_delay() );

// offs == 0 is beginning of slice, but we have to
// take modulo into account when indexing
Expand All @@ -280,7 +280,7 @@ inline size_t
MultRBuffer::get_index_( const long d ) const
{
const long idx = kernel().event_delivery_manager.get_modulo( d );
assert( 0 <= idx and ( size_t ) idx < buffer_.size() );
assert( 0 <= idx and static_cast< size_t >( idx ) < buffer_.size() );
return idx;
}

Expand Down Expand Up @@ -346,8 +346,8 @@ ListRingBuffer::append_value( const long offs, const double v )
inline std::list< double >&
ListRingBuffer::get_list( const long offs )
{
assert( 0 <= offs and ( size_t ) offs < buffer_.size() );
assert( ( long ) offs < kernel().connection_manager.get_min_delay() );
assert( 0 <= offs and static_cast< size_t >( offs ) < buffer_.size() );
assert( offs < kernel().connection_manager.get_min_delay() );

// offs == 0 is beginning of slice, but we have to
// take modulo into account when indexing
Expand All @@ -360,7 +360,7 @@ ListRingBuffer::get_index_( const long d ) const
{
const long idx = kernel().event_delivery_manager.get_modulo( d );
assert( 0 <= idx );
assert( ( size_t ) idx < buffer_.size() );
assert( static_cast< size_t >( idx ) < buffer_.size() );
return idx;
}

Expand Down
6 changes: 3 additions & 3 deletions nestkernel/simulation_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1080,7 +1080,7 @@ nest::SimulationManager::advance_time_()
to_do_ -= to_step_ - from_step_;

// advance clock, update modulos, slice counter only if slice completed
if ( ( long ) to_step_ == kernel().connection_manager.get_min_delay() )
if ( to_step_ == kernel().connection_manager.get_min_delay() )
{
clock_ += Time::step( kernel().connection_manager.get_min_delay() );
++slice_;
Expand All @@ -1094,7 +1094,7 @@ nest::SimulationManager::advance_time_()

long end_sim = from_step_ + to_do_;

if ( kernel().connection_manager.get_min_delay() < ( long ) end_sim )
if ( kernel().connection_manager.get_min_delay() < end_sim )
{
// update to end of time slice
to_step_ = kernel().connection_manager.get_min_delay();
Expand All @@ -1104,7 +1104,7 @@ nest::SimulationManager::advance_time_()
to_step_ = end_sim; // update to end of simulation time
}

assert( to_step_ - from_step_ <= ( long ) kernel().connection_manager.get_min_delay() );
assert( to_step_ - from_step_ <= kernel().connection_manager.get_min_delay() );
}

void
Expand Down
2 changes: 1 addition & 1 deletion nestkernel/slice_ring_buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ inline void
SliceRingBuffer::add_spike( const long rel_delivery, const long stamp, const double ps_offset, const double weight )
{
const long idx = kernel().event_delivery_manager.get_slice_modulo( rel_delivery );
assert( ( size_t ) idx < queue_.size() );
assert( static_cast< size_t >( idx ) < queue_.size() );
assert( ps_offset >= 0 );

queue_[ idx ].push_back( SpikeInfo( stamp, ps_offset, weight ) );
Expand Down
2 changes: 1 addition & 1 deletion sli/interpret.cc
Original file line number Diff line number Diff line change
Expand Up @@ -987,7 +987,7 @@ SLIInterpreter::stack_backtrace( int n )
{
for ( int p = n - 1; p >= 0; --p )
{
if ( ( size_t ) p > EStack.load() )
if ( static_cast< size_t >( p ) > EStack.load() )
{
continue;
}
Expand Down
2 changes: 1 addition & 1 deletion sli/scanner.cc
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ Scanner::Scanner( std::istream* is )

code.Group( alpha, "ABCDFGHIJKLMNOPQRSTUVWXYZ" );
code.Group( alpha, "abcdfghijklmopqrsuvwxyz" );
code.Range( alpha, ( char ) 161, ( char ) 255 );
code.Range( alpha, static_cast< char >( 161 ), static_cast< char >( 255 ) );
code[ '_' ] = alpha;
code.Group( alpha, "~`!@#$^&=|:;'<,>?\"" ); // according to PS

Expand Down
36 changes: 18 additions & 18 deletions sli/sliarray.cc
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,13 @@ SLIArrayModule::RangeFunction::execute( SLIInterpreter* i ) const
{
double d = ad->get( 0 );
ad->erase();
long n = ( long ) std::floor( d );
long n = static_cast< long >( std::floor( d ) );
if ( n > 0 )
{
ad->reserve( n );
for ( long j = 1; j <= n; ++j )
{
ad->push_back( ( double ) j );
ad->push_back( static_cast< double >( j ) );
}
}
i->EStack.pop();
Expand Down Expand Up @@ -262,7 +262,7 @@ SLIArrayModule::ArangeFunction::execute( SLIInterpreter* i ) const
else
{
double d = ad->get( 0 );
long n = ( long ) std::floor( d );
long n = static_cast< long >( std::floor( d ) );
if ( n < 0 )
{
i->raiseerror( "RangeCheck" );
Expand Down Expand Up @@ -1019,7 +1019,7 @@ SLIArrayModule::IMapFunction::execute( SLIInterpreter* i ) const
}
}

if ( ( size_t ) procc->get() < proclimit )
if ( static_cast< size_t >( procc->get() ) < proclimit )
{
/* we are still evaluating the procedure. */
i->EStack.push( proc->get( pos ) ); // get next command from the procedure
Expand All @@ -1046,7 +1046,7 @@ SLIArrayModule::IMapFunction::execute( SLIInterpreter* i ) const
} while ( true );
}
}
if ( ( size_t ) procc->get() >= proclimit )
if ( static_cast< size_t >( procc->get() ) >= proclimit )
{
( *procc ) = 0;
}
Expand Down Expand Up @@ -1157,7 +1157,7 @@ SLIArrayModule::IMap_ivFunction::execute( SLIInterpreter* i ) const
}
}

if ( ( size_t ) procc->get() < proclimit )
if ( static_cast< size_t >( procc->get() ) < proclimit )
{
/* we are still evaluating the procedure. */
i->EStack.push( proc->get( pos ) ); // get next command from the procedure
Expand All @@ -1184,7 +1184,7 @@ SLIArrayModule::IMap_ivFunction::execute( SLIInterpreter* i ) const
} while ( true );
}
}
if ( ( size_t ) procc->get() >= proclimit )
if ( static_cast< size_t >( procc->get() ) >= proclimit )
{
( *procc ) = 0;
}
Expand Down Expand Up @@ -1292,7 +1292,7 @@ SLIArrayModule::IMap_dvFunction::execute( SLIInterpreter* i ) const
}
}

if ( ( size_t ) procc->get() < proclimit )
if ( static_cast< size_t >( procc->get() ) < proclimit )
{
/* we are still evaluating the procedure. */
i->EStack.push( proc->get( pos ) ); // get next command from the procedure
Expand All @@ -1319,7 +1319,7 @@ SLIArrayModule::IMap_dvFunction::execute( SLIInterpreter* i ) const
} while ( true );
}
}
if ( ( size_t ) procc->get() >= proclimit )
if ( static_cast< size_t >( procc->get() ) >= proclimit )
{
( *procc ) = 0;
}
Expand Down Expand Up @@ -1522,7 +1522,7 @@ SLIArrayModule::IMapIndexedFunction::execute( SLIInterpreter* i ) const
}
}

if ( ( size_t ) procc->get() < proclimit )
if ( static_cast< size_t >( procc->get() ) < proclimit )
{
/* we are still evaluating the procedure. */
i->EStack.push( proc->get( pos ) ); // get next command from the procedure
Expand All @@ -1548,7 +1548,7 @@ SLIArrayModule::IMapIndexedFunction::execute( SLIInterpreter* i ) const
} while ( true );
}
}
if ( ( size_t ) procc->get() >= proclimit )
if ( static_cast< size_t >( procc->get() ) >= proclimit )
{
( *procc ) = 0;
}
Expand Down Expand Up @@ -1687,7 +1687,7 @@ SLIArrayModule::IMapThreadFunction::execute( SLIInterpreter* i ) const
}
}

if ( ( size_t ) proccountd->get() < proclimit )
if ( static_cast< size_t >( proccountd->get() ) < proclimit )
{
/* we are still evaluating the procedure. */
// get next command from the procedure
Expand Down Expand Up @@ -1715,7 +1715,7 @@ SLIArrayModule::IMapThreadFunction::execute( SLIInterpreter* i ) const
} while ( true );
}
}
if ( ( size_t ) proccountd->get() >= proclimit )
if ( static_cast< size_t >( proccountd->get() ) >= proclimit )
{
( *proccountd ) = 0;
}
Expand Down Expand Up @@ -1865,7 +1865,7 @@ SLIArrayModule::Put_a_a_tFunction::execute( SLIInterpreter* i ) const
return;
}

if ( j >= ( int ) source->size() )
if ( j >= static_cast< int >( source->size() ) )
{
i->message( SLIInterpreter::M_ERROR, "Put", "Index out of range." );
i->message( SLIInterpreter::M_ERROR, "Put", "Source array is unchanged." );
Expand Down Expand Up @@ -2756,10 +2756,10 @@ SLIArrayModule::GaborFunction::execute( SLIInterpreter* i ) const
result.reserve( nrow );

std::vector< double > col( ncol );
for ( size_t r = 0; r < ( size_t ) nrow; ++r )
for ( size_t r = 0; r < static_cast< size_t >( nrow ); ++r )
{
const double y = ymin + r * dy;
for ( size_t c = 0; c < ( size_t ) ncol; ++c )
for ( size_t c = 0; c < static_cast< size_t >( ncol ); ++c )
{
const double x = xmin + c * dx;
const double x1 = x * cos_phi - y * sin_phi;
Expand Down Expand Up @@ -2862,11 +2862,11 @@ SLIArrayModule::Gauss2dFunction::execute( SLIInterpreter* i ) const
result.reserve( nrow );

std::vector< double > col( ncol );
for ( size_t r = 0; r < ( size_t ) nrow; ++r )
for ( size_t r = 0; r < static_cast< size_t >( nrow ); ++r )
{
const double y = ymin + r * dy;
col.assign( ncol, 0.0 ); // clear contents
for ( size_t c = 0; c < ( size_t ) ncol; ++c )
for ( size_t c = 0; c < static_cast< size_t >( ncol ); ++c )
{
const double x = xmin + c * dx;
const double x1 = x * cos_phi - y * sin_phi;
Expand Down
10 changes: 5 additions & 5 deletions sli/slicontrol.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1550,11 +1550,11 @@ PclocksFunction::execute( SLIInterpreter* i ) const
return;
}

Token rtime( ( long ) realtime );
Token utime( ( long ) foo.tms_utime );
Token stime( ( long ) foo.tms_stime );
Token cutime( ( long ) foo.tms_cutime );
Token cstime( ( long ) foo.tms_cstime );
Token rtime( static_cast< long >( realtime ) );
Token utime( static_cast< long >( foo.tms_utime ) );
Token stime( static_cast< long >( foo.tms_stime ) );
Token cutime( static_cast< long >( foo.tms_cutime ) );
Token cstime( static_cast< long >( foo.tms_cstime ) );

ArrayDatum result;
result.push_back( rtime );
Expand Down
Loading

0 comments on commit d31b9a2

Please sign in to comment.