Address some cppcheck suggestions

I addressed the suggestions in files which I know well enough to feel
highly comfortable to edit them.

Thanks to @matthiaskrgr for the cppcheck run.
This commit is contained in:
Jyrki Vesterinen 2017-03-18 12:28:41 +02:00
parent 2c00677d4b
commit 3dfd421191
14 changed files with 76 additions and 76 deletions

View file

@ -476,18 +476,18 @@ void prob_matrix::initialize_row(unsigned plane, unsigned row, double row_prob,
}
}
double &prob_matrix::val(unsigned p, unsigned row, unsigned col)
double &prob_matrix::val(unsigned plane, unsigned row, unsigned col)
{
assert(row < rows_);
assert(col < cols_);
return plane_[p][row * cols_ + col];
return plane_[plane][row * cols_ + col];
}
const double &prob_matrix::val(unsigned p, unsigned row, unsigned col) const
const double &prob_matrix::val(unsigned plane, unsigned row, unsigned col) const
{
assert(row < rows_);
assert(col < cols_);
return plane_[p][row * cols_ + col];
return plane_[plane][row * cols_ + col];
}
// xfer, shift_cols and shift_rows use up most of our time. Careful!
@ -1153,7 +1153,7 @@ void probability_combat_matrix::receive_blow_b(double hit_chance)
int dst = a_slows_ ? src|2 : src;
// A is slow in planes 1 and 3.
unsigned damage = src & 1 ? a_slow_damage_ : a_damage_;
unsigned damage = (src & 1) ? a_slow_damage_ : a_damage_;
shift_cols(dst, src, damage, hit_chance, a_drain_constant_, a_drain_percent_);
}
@ -1173,7 +1173,7 @@ void probability_combat_matrix::receive_blow_a(double hit_chance)
int dst = b_slows_ ? src|1 : src;
// B is slow in planes 2 and 3.
unsigned damage = src & 2 ? b_slow_damage_ : b_damage_;
unsigned damage = (src & 2) ? b_slow_damage_ : b_damage_;
shift_rows(dst, src, damage, hit_chance, b_drain_constant_, b_drain_percent_);
}
@ -1256,7 +1256,7 @@ private:
unsigned int calc_blows_a(unsigned int a_hp) const;
unsigned int calc_blows_b(unsigned int b_hp) const;
static void divide_all_elements(std::vector<double>& vec, double divisor);
static void scale_probabilities(const std::vector<double>& source, std::vector<double>& target, double multiplier, unsigned int singular_hp);
static void scale_probabilities(const std::vector<double>& source, std::vector<double>& target, double divisor, unsigned int singular_hp);
};
monte_carlo_combat_matrix::monte_carlo_combat_matrix(unsigned int a_max_hp, unsigned int b_max_hp,
@ -1949,11 +1949,11 @@ void merge_slice_summary(std::vector<double> & dst, const std::vector<double> &
// Of course, one could be a woman. Or both.
// And either could be non-human, too.
// Um, ok, it was a stupid thing to say.
void combatant::fight(combatant &opp, bool levelup_considered)
void combatant::fight(combatant &opponent, bool levelup_considered)
{
// If defender has firststrike and we don't, reverse.
if (opp.u_.firststrike && !u_.firststrike) {
opp.fight(*this, levelup_considered);
if(opponent.u_.firststrike && !u_.firststrike) {
opponent.fight(*this, levelup_considered);
return;
}
@ -1961,15 +1961,15 @@ void combatant::fight(combatant &opp, bool levelup_considered)
printf("A:\n");
dump(u_);
printf("B:\n");
dump(opp.u_);
dump(opponent.u_);
#endif
#if 0
std::vector<double> prev = summary[0], opp_prev = opp.summary[0];
complex_fight(opp, 1);
std::vector<double> res = summary[0], opp_res = opp.summary[0];
std::vector<double> prev = summary[0], opp_prev = opponent.summary[0];
complex_fight(opponent, 1);
std::vector<double> res = summary[0], opp_res = opponent.summary[0];
summary[0] = prev;
opp.summary[0] = opp_prev;
opponent.summary[0] = opp_prev;
#endif
// The chance so far of not being hit this combat:
@ -1979,10 +1979,10 @@ void combatant::fight(combatant &opp, bool levelup_considered)
// If we've fought before and we have swarm, we might have to split the
// calculation by number of attacks.
const std::vector<combat_slice> split = split_summary(u_, summary);
const std::vector<combat_slice> opp_split = split_summary(opp.u_, opp.summary);
const std::vector<combat_slice> opp_split = split_summary(opponent.u_, opponent.summary);
bool use_monte_carlo_simulation = (
fight_complexity(split.size(), opp_split.size(), u_, opp.u_) >
fight_complexity(split.size(), opp_split.size(), u_, opponent.u_) >
MONTE_CARLO_SIMULATION_THRESHOLD &&
preferences::damage_prediction_allow_monte_carlo_simulation());
@ -1991,16 +1991,16 @@ void combatant::fight(combatant &opp, bool levelup_considered)
// A very complex fight. Use Monte Carlo simulation instead of exact
// probability calculations.
complex_fight(attack_prediction_mode::monte_carlo_simulation,
u_, opp.u_, u_.num_blows, opp.u_.num_blows,
summary, opp.summary, self_not_hit, opp_not_hit,
u_, opponent.u_, u_.num_blows, opponent.u_.num_blows,
summary, opponent.summary, self_not_hit, opp_not_hit,
levelup_considered,
split, opp_split, slowed, opp.slowed);
split, opp_split, slowed, opponent.slowed);
}
else if (split.size() == 1 && opp_split.size() == 1)
{
// No special treatment due to swarm is needed. Ignore the split.
do_fight(u_, opp.u_, u_.num_blows, opp.u_.num_blows,
summary, opp.summary, self_not_hit, opp_not_hit,
do_fight(u_, opponent.u_, u_.num_blows, opponent.u_.num_blows,
summary, opponent.summary, self_not_hit, opp_not_hit,
levelup_considered);
}
else
@ -2022,10 +2022,10 @@ void combatant::fight(combatant &opp, bool levelup_considered)
split[s].end_hp, split[s].prob);
init_slice_summary(sit_summary[1], summary[1], split[s].begin_hp,
split[s].end_hp, split[s].prob);
init_slice_summary(sit_opp_summary[0], opp.summary[0],
init_slice_summary(sit_opp_summary[0], opponent.summary[0],
opp_split[t].begin_hp, opp_split[t].end_hp,
opp_split[t].prob);
init_slice_summary(sit_opp_summary[1], opp.summary[1],
init_slice_summary(sit_opp_summary[1], opponent.summary[1],
opp_split[t].begin_hp, opp_split[t].end_hp,
opp_split[t].prob);
@ -2034,7 +2034,7 @@ void combatant::fight(combatant &opp, bool levelup_considered)
double sit_self_not_hit = sit_prob;
double sit_opp_not_hit = sit_prob;
do_fight(u_, opp.u_, split[s].strikes, opp_split[t].strikes,
do_fight(u_, opponent.u_, split[s].strikes, opp_split[t].strikes,
sit_summary, sit_opp_summary, sit_self_not_hit,
sit_opp_not_hit, levelup_considered);
@ -2050,22 +2050,22 @@ void combatant::fight(combatant &opp, bool levelup_considered)
// Swap in the results.
summary[0].swap(summary_result[0]);
summary[1].swap(summary_result[1]);
opp.summary[0].swap(opp_summary_result[0]);
opp.summary[1].swap(opp_summary_result[1]);
opponent.summary[0].swap(opp_summary_result[0]);
opponent.summary[1].swap(opp_summary_result[1]);
}
#if 0
assert(summary[0].size() == res.size());
assert(opp.summary[0].size() == opp_res.size());
assert(opponent.summary[0].size() == opp_res.size());
for (unsigned int i = 0; i < summary[0].size(); ++i) {
if (std::fabs(summary[0][i] - res[i]) > 0.000001) {
std::cerr << "Mismatch for " << i << " hp: " << summary[0][i] << " should have been " << res[i] << "\n";
assert(0);
}
}
for (unsigned int i = 0; i < opp.summary[0].size(); ++i) {
if (std::fabs(opp.summary[0][i] - opp_res[i])> 0.000001) {
std::cerr << "Mismatch for " << i << " hp: " << opp.summary[0][i] << " should have been " << opp_res[i] << "\n";
for (unsigned int i = 0; i < opponent.summary[0].size(); ++i) {
if (std::fabs(opponent.summary[0][i] - opp_res[i])> 0.000001) {
std::cerr << "Mismatch for " << i << " hp: " << opponent.summary[0][i] << " should have been " << opp_res[i] << "\n";
assert(0);
}
}
@ -2080,29 +2080,29 @@ void combatant::fight(combatant &opp, bool levelup_considered)
for (unsigned int i = 0; i < size; ++i)
hp_dist[i] = summary[0][i] + summary[1][i];
}
if (opp.summary[1].empty())
opp.hp_dist = opp.summary[0];
if(opponent.summary[1].empty())
opponent.hp_dist = opponent.summary[0];
else {
const unsigned size = opp.summary[0].size();
opp.hp_dist.resize(size);
const unsigned size = opponent.summary[0].size();
opponent.hp_dist.resize(size);
for (unsigned int i = 0; i < size; ++i)
opp.hp_dist[i] = opp.summary[0][i] + opp.summary[1][i];
opponent.hp_dist[i] = opponent.summary[0][i] + opponent.summary[1][i];
}
// Chance that we / they were touched this time.
double touched = 1.0 - self_not_hit;
double opp_touched = 1.0 - opp_not_hit;
if (opp.u_.poisons)
if(opponent.u_.poisons)
poisoned += (1 - poisoned) * touched;
if (u_.poisons)
opp.poisoned += (1 - opp.poisoned) * opp_touched;
opponent.poisoned += (1 - opponent.poisoned) * opp_touched;
if(!use_monte_carlo_simulation)
{
if(opp.u_.slows)
if(opponent.u_.slows)
slowed += (1 - slowed) * touched;
if(u_.slows)
opp.slowed += (1 - opp.slowed) * opp_touched;
opponent.slowed += (1 - opponent.slowed) * opp_touched;
}
else
{
@ -2111,11 +2111,11 @@ void combatant::fight(combatant &opp, bool levelup_considered)
* We need to recalculate it based on the HP distribution.
*/
slowed = std::accumulate(summary[1].begin(), summary[1].end(), 0.0);
opp.slowed = std::accumulate(opp.summary[1].begin(), opp.summary[1].end(), 0.0);
opponent.slowed = std::accumulate(opponent.summary[1].begin(), opponent.summary[1].end(), 0.0);
}
untouched *= self_not_hit;
opp.untouched *= opp_not_hit;
opponent.untouched *= opp_not_hit;
}
double combatant::average_hp(unsigned int healing) const

View file

@ -139,7 +139,7 @@ public:
dispatcher& dispatcher,
const ui_event event,
bool& handled,
bool& halt)
bool& halt) const
{
functor(dispatcher, event, handled, halt);
}
@ -183,7 +183,7 @@ bool dispatcher::fire(const ui_event event, widget& target)
class trigger_mouse
{
public:
trigger_mouse(const point& coordinate) : coordinate_(coordinate)
explicit trigger_mouse(const point& coordinate) : coordinate_(coordinate)
{
}
@ -295,7 +295,7 @@ public:
dispatcher& dispatcher,
const ui_event event,
bool& handled,
bool& halt)
bool& halt) const
{
functor(dispatcher, event, handled, halt, nullptr);
}
@ -315,7 +315,7 @@ bool dispatcher::fire(const ui_event event, widget& target, void*)
class trigger_message
{
public:
trigger_message(message& msg) : message_(msg)
explicit trigger_message(message& msg) : message_(msg)
{
}

View file

@ -27,7 +27,7 @@ namespace dialogs
class install_dependencies : public modal_dialog
{
public:
install_dependencies(const addons_list& addons)
explicit install_dependencies(const addons_list& addons)
: addons_(addons)
{}

View file

@ -104,7 +104,7 @@ namespace dialogs
namespace {
struct filter_transform
{
filter_transform(const std::vector<std::string>& filtertext) : filtertext_(filtertext) {}
explicit filter_transform(const std::vector<std::string>& filtertext) : filtertext_(filtertext) {}
bool operator()(const config& cfg) const
{
for(const auto& filter : filtertext_)

View file

@ -56,7 +56,7 @@ public:
void add_overlay_location(std::set<map_location>& locations);
private:
const image::locator& current_image() { return images_.get_current_frame(); }
const image::locator& current_image() const { return images_.get_current_frame(); }
animated<image::locator> images_;
@ -112,7 +112,7 @@ public:
* impl's of exposed functions
*/
halo_impl(display & screen) :
explicit halo_impl(display & screen) :
disp(&screen),
haloes(),
halo_id(1),

View file

@ -40,7 +40,7 @@ const int NO_HALO = 0;
class manager
{
public:
manager(display& disp);
manager(display& screen);
/**
* Add a haloing effect using 'image centered on (x,y).

View file

@ -141,7 +141,7 @@ template<
>
struct lexical_caster
{
To operator()(From value, boost::optional<To> fallback)
To operator()(From value, boost::optional<To> fallback) const
{
DEBUG_THROW("generic");
@ -173,7 +173,7 @@ struct lexical_caster<
typename std::remove_pointer<From>::type>::value >::type
>
{
std::string operator()(From value, boost::optional<std::string>)
std::string operator()(From value, boost::optional<std::string>) const
{
DEBUG_THROW("specialized - To std::string - From integral (pointer)");
@ -199,7 +199,7 @@ struct lexical_caster<
char*, const char*> , From>::value >::type
>
{
long long operator()(From value, boost::optional<long long> fallback)
long long operator()(From value, boost::optional<long long> fallback) const
{
DEBUG_THROW("specialized - To long long - From (const) char*");
@ -224,7 +224,7 @@ struct lexical_caster<
, std::string
>
{
long long operator()(const std::string& value, boost::optional<long long> fallback)
long long operator()(const std::string& value, boost::optional<long long> fallback) const
{
DEBUG_THROW("specialized - To long long - From std::string");
@ -256,7 +256,7 @@ struct lexical_caster<
char*, const char*> , From>::value >::type
>
{
To operator()(From value, boost::optional<To> fallback)
To operator()(From value, boost::optional<To> fallback) const
{
DEBUG_THROW("specialized - To signed - From (const) char*");
@ -280,7 +280,7 @@ struct lexical_caster<
, typename std::enable_if<std::is_integral<To>::value && std::is_signed<To>::value && !std::is_same<To, long long>::value >::type
>
{
To operator()(const std::string& value, boost::optional<To> fallback)
To operator()(const std::string& value, boost::optional<To> fallback) const
{
DEBUG_THROW("specialized - To signed - From std::string");
@ -315,7 +315,7 @@ struct lexical_caster<
char*, const char*> , From>::value >::type
>
{
To operator()(From value, boost::optional<To> fallback)
To operator()(From value, boost::optional<To> fallback) const
{
DEBUG_THROW("specialized - To floating point - From (const) char*");
@ -339,7 +339,7 @@ struct lexical_caster<
, typename std::enable_if<std::is_floating_point<To>::value >::type
>
{
To operator()(const std::string& value, boost::optional<To> fallback)
To operator()(const std::string& value, boost::optional<To> fallback) const
{
DEBUG_THROW("specialized - To floating point - From std::string");
@ -385,7 +385,7 @@ struct lexical_caster<
char*, const char*> , From>::value >::type
>
{
unsigned long long operator()(From value, boost::optional<unsigned long long> fallback)
unsigned long long operator()(From value, boost::optional<unsigned long long> fallback) const
{
DEBUG_THROW(
"specialized - To unsigned long long - From (const) char*");
@ -411,7 +411,7 @@ struct lexical_caster<
, std::string
>
{
unsigned long long operator()(const std::string& value, boost::optional<unsigned long long> fallback)
unsigned long long operator()(const std::string& value, boost::optional<unsigned long long> fallback) const
{
DEBUG_THROW("specialized - To unsigned long long - From std::string");
@ -443,7 +443,7 @@ struct lexical_caster<
char*, const char*> , From>::value >::type
>
{
To operator()(From value, boost::optional<To> fallback)
To operator()(From value, boost::optional<To> fallback) const
{
DEBUG_THROW("specialized - To unsigned - From (const) char*");
@ -467,7 +467,7 @@ struct lexical_caster<
, typename std::enable_if<std::is_unsigned<To>::value >::type
>
{
To operator()(const std::string& value, boost::optional<To> fallback)
To operator()(const std::string& value, boost::optional<To> fallback) const
{
DEBUG_THROW("specialized - To unsigned - From std::string");

View file

@ -205,7 +205,7 @@ play_controller::~play_controller()
struct throw_end_level
{
void operator()(const config&)
void operator()(const config&) const
{
throw_quit_game_exception();
}
@ -728,7 +728,7 @@ events::mouse_handler& play_controller::get_mouse_handler_base()
return mouse_handler_;
}
std::shared_ptr<wb::manager> play_controller::get_whiteboard()
std::shared_ptr<wb::manager> play_controller::get_whiteboard() const
{
return whiteboard_manager_;
}
@ -1026,7 +1026,7 @@ game_events::wml_event_pump& play_controller::pump()
return gamestate().events_manager_->pump();
}
int play_controller::get_ticks()
int play_controller::get_ticks() const
{
return ticks_;
}

View file

@ -195,7 +195,7 @@ public:
events::mouse_handler& get_mouse_handler_base() override;
events::menu_handler& get_menu_handler() { return menu_handler_; }
std::shared_ptr<wb::manager> get_whiteboard();
std::shared_ptr<wb::manager> get_whiteboard() const;
const mp_game_settings& get_mp_settings();
game_classification& get_classification();
int get_server_request_number() const { return gamestate().server_request_number_; }
@ -203,7 +203,7 @@ public:
game_events::wml_event_pump& pump();
int get_ticks();
int get_ticks() const;
virtual soundsource::manager* get_soundsource_man() override;
virtual plugins_context* get_plugins_context() override;
@ -233,7 +233,7 @@ public:
return level_["theme"].str();
}
virtual bool should_return_to_play_side()
virtual bool should_return_to_play_side() const
{
return is_regular_game_end();
}

View file

@ -683,7 +683,7 @@ void playsingle_controller::enable_replay(bool is_unit_test)
}
}
bool playsingle_controller::should_return_to_play_side()
bool playsingle_controller::should_return_to_play_side() const
{
if(player_type_changed_ || is_regular_game_end()) {
return true;

View file

@ -59,7 +59,7 @@ public:
bool get_player_type_changed() const { return player_type_changed_; }
void set_player_type_changed() { player_type_changed_ = true; }
virtual bool should_return_to_play_side() override;
virtual bool should_return_to_play_side() const override;
replay_controller * get_replay_controller() { return replay_.get(); }
bool is_replay() override { return get_replay_controller() != nullptr; }
void enable_replay(bool is_unit_test = false);

View file

@ -73,7 +73,7 @@ namespace random_new
return *def;
}
unsigned int rng::get_random_calls()
unsigned int rng::get_random_calls() const
{
return random_calls_;
}

View file

@ -37,7 +37,7 @@ namespace random_new
* Note that this may be different from the number of random calls to
* the underlying rng, and to the random_calls number in save files!
*/
unsigned int get_random_calls();
unsigned int get_random_calls() const;
/**
* This helper method provides a random int from the underlying generator,

View file

@ -68,7 +68,7 @@ private:
*
* This is required by TC modification
*/
void set_up_team_colors()
static void set_up_team_colors()
{
std::vector<std::string> tc;
@ -93,7 +93,7 @@ private:
paths_manager_.set_paths(cfg);
}
config create_color_range(const std::string& id,
static config create_color_range(const std::string& id,
const std::string& rgb,
const std::string& name)
{
@ -106,7 +106,7 @@ private:
return cfg;
}
config create_path_config(const std::string& path)
static config create_path_config(const std::string& path)
{
config cfg;