Pass strings by constref when possible

These instances were found by cppcheck.
Thanks matthiaskrgr for running it.
This commit is contained in:
Celtic Minstrel 2017-03-16 17:07:04 -04:00
parent c04e4a4a39
commit f815c2272a
25 changed files with 32 additions and 32 deletions

View file

@ -163,7 +163,7 @@ void carryover_info::remove_side(const std::string& id) {
struct save_id_equals
{
save_id_equals(std::string val) : value (val) {}
save_id_equals(const std::string& val) : value (val) {}
bool operator () (carryover& v2)
{
return value == v2.get_save_id();
@ -250,7 +250,7 @@ const config carryover_info::to_config()
return cfg;
}
carryover* carryover_info::get_side(std::string save_id){
carryover* carryover_info::get_side(const std::string& save_id){
for(carryover& side : carryover_sides_) {
if(side.get_save_id() == save_id){
return &side;

View file

@ -65,7 +65,7 @@ public:
/// @param from_snapshot true if cfg is a [snapshot], false if cfg is [carryover_sides(_start)]
explicit carryover_info(const config& cfg, bool from_snapshot = false);
carryover* get_side(std::string save_id);
carryover* get_side(const std::string& save_id);
std::vector<carryover>& get_all_sides();
void add_side(const config& cfg);
void add_side(const team& t, const int gold, const bool add);

View file

@ -82,7 +82,7 @@ std::string get_addons_dir();
* maximum 1000 files then start always giving 999
*/
std::string get_next_filename(const std::string& name, const std::string& extension);
void set_user_config_dir(std::string path);
void set_user_config_dir(const std::string& path);
void set_user_data_dir(std::string path);
std::string get_user_config_dir();

View file

@ -594,7 +594,7 @@ static void set_user_config_path(path newconfig)
}
}
void set_user_config_dir(std::string newconfigdir)
void set_user_config_dir(const std::string& newconfigdir)
{
set_user_config_path(newconfigdir);
}

View file

@ -21,7 +21,7 @@
namespace font {
struct error : public game::error {
error(std::string str = "Font initialization failed") : game::error(str) {}
error(const std::string& str = "Font initialization failed") : game::error(str) {}
};
} // end namespace font

View file

@ -33,7 +33,7 @@ namespace savegame { struct load_game_metadata; }
struct jump_to_campaign_info
{
public:
jump_to_campaign_info(bool jump,int difficulty, std::string campaign_id,std::string scenario_id)
jump_to_campaign_info(bool jump,int difficulty, const std::string& campaign_id,const std::string& scenario_id)
: jump_(jump)
, difficulty_(difficulty)
, campaign_id_(campaign_id)

View file

@ -18,7 +18,7 @@
namespace events{
generic_event::generic_event(std::string name) :
generic_event::generic_event(const std::string& name) :
name_(name),
observers_(),
change_handler_(false),

View file

@ -43,7 +43,7 @@ This is the class that notifies the observers and maintains a list of them.
*/
class generic_event{
public:
generic_event(std::string name);
generic_event(const std::string& name);
virtual ~generic_event() {}
virtual bool attach_handler(observer* obs);

View file

@ -191,7 +191,7 @@ public:
{
}
stuff_list_adder stuff_list_entry(tree_view_node* parent, const std::string defn)
stuff_list_adder stuff_list_entry(tree_view_node* parent, const std::string& defn)
{
return stuff_list_adder(parent ? *parent : stuff_list_->get_root_node(), defn);
}

View file

@ -223,7 +223,7 @@ public:
} catch (...) { std::cerr << "Swallowed an exception when trying to write lua command line history\n";}
}
#endif
void add_to_history (std::string str) {
void add_to_history (const std::string& str) {
prefix_ = "";
(void) str;
#ifdef HAVE_HISTORY

View file

@ -445,7 +445,7 @@ void save_hotkeys(config& cfg)
}
}
std::string get_names(std::string id)
std::string get_names(const std::string& id)
{
// Names are used in places like the hot-key preferences menu
std::vector<std::string> names;

View file

@ -421,7 +421,7 @@ void clear_hotkeys();
* "ctrl+l" or "n" or "mouse 1". The comman separated string is of the form "ctrl+l,n,mouse 1".
* @return The comma separated string of hotkey names.
*/
std::string get_names(std::string id);
std::string get_names(const std::string& id);
/**
* Save the non-default hotkeys to the config.

View file

@ -431,7 +431,7 @@ private:
class scale_modification : public modification
{
public:
scale_modification(int width, int height, std::string fn, bool use_nn)
scale_modification(int width, int height, const std::string& fn, bool use_nn)
: w_(width), h_(height), nn_(use_nn), fn_(fn)
{}
virtual surface operator()(const surface& src) const;
@ -461,7 +461,7 @@ protected:
class scale_exact_modification : public scale_modification
{
public:
scale_exact_modification(int width, int height, std::string fn, bool use_nn)
scale_exact_modification(int width, int height, const std::string& fn, bool use_nn)
: scale_modification(width, height, fn, use_nn)
{}
virtual std::pair<int,int> calculate_size(const surface& src) const;
@ -474,7 +474,7 @@ public:
class scale_into_modification : public scale_modification
{
public:
scale_into_modification(int width, int height, std::string fn, bool use_nn)
scale_into_modification(int width, int height, const std::string& fn, bool use_nn)
: scale_modification(width, height, fn, use_nn)
{}
virtual std::pair<int,int> calculate_size(const surface& src) const;

View file

@ -33,17 +33,17 @@ namespace mp_ui_alerts {
namespace {
bool lobby_pref(std::string id)
bool lobby_pref(const std::string& id)
{
return preferences::get(id + "_lobby", get_def_pref_lobby(id));
}
bool sound_pref(std::string id)
bool sound_pref(const std::string& id)
{
return preferences::get(id + "_sound", get_def_pref_sound(id));
}
bool notif_pref(std::string id)
bool notif_pref(const std::string& id)
{
return preferences::get(id + "_notif", get_def_pref_notif(id));
}

View file

@ -85,7 +85,7 @@ using attack_ptr_in = boost::intrusive_ptr<typename utils::const_clone<attack_ty
// Note that these two templates are designed on the assumption that T is either unit or unit_type
template<typename T>
auto find_attack(T* u, std::string id) -> attack_ptr_in<T>
auto find_attack(T* u, const std::string& id) -> attack_ptr_in<T>
{
auto attacks = u->attacks();
for(auto at = attacks.begin(); at != attacks.end(); ++at) {

View file

@ -1349,7 +1349,7 @@ std::istream *preprocess_file(const std::string& fname, preproc_map *defines)
}
void preprocess_resource(const std::string& res_name, preproc_map *defines_map,
bool write_cfg, bool write_plain_cfg,std::string target_directory)
bool write_cfg, bool write_plain_cfg,const std::string& target_directory)
{
if (filesystem::is_directory(res_name))
{

View file

@ -75,6 +75,6 @@ std::ostream& operator<<(std::ostream& stream, const preproc_map::value_type& de
std::istream *preprocess_file(const std::string& fname, preproc_map *defines = nullptr);
void preprocess_resource(const std::string& res_name, preproc_map *defines_map,
bool write_cfg=false, bool write_plain_cfg=false, std::string target_directory="");
bool write_cfg=false, bool write_plain_cfg=false, const std::string& target_directory="");
#endif

View file

@ -502,7 +502,7 @@ static void si_string_impl_stream_write(std::stringstream &ss, double input) {
ss.precision(oldprec);
}
std::string si_string(double input, bool base2, std::string unit) {
std::string si_string(double input, bool base2, const std::string& unit) {
const double multiplier = base2 ? 1024 : 1000;
typedef std::array<std::string, 9> strings9;

View file

@ -281,7 +281,7 @@ inline std::string signed_percent(int val) {return signed_value(val) + "%";}
*
* There are no default values because they would not be translatable.
*/
std::string si_string(double input, bool base2, std::string unit);
std::string si_string(double input, bool base2, const std::string& unit);
/**
* Try to complete the last word of 'text' with the 'wordlist'.

View file

@ -222,7 +222,7 @@ config mp_sync::get_user_choice(const std::string &name, const mp_sync::user_cho
return retv[side];
}
user_choice_manager::user_choice_manager(const std::string &name, const mp_sync::user_choice &uch, std::set<int> sides)
user_choice_manager::user_choice_manager(const std::string &name, const mp_sync::user_choice &uch, const std::set<int>& sides)
: required_(sides)
, res_()
, local_choice_(0)

View file

@ -89,7 +89,7 @@ class user_choice_manager : events::pump_monitor
const std::string& tagname_;
const int current_side_;
// private constructor, this object is only constructed by user_choice_manager::get_user_choice_internal
user_choice_manager(const std::string &name, const mp_sync::user_choice &uch, std::set<int> sides);
user_choice_manager(const std::string &name, const mp_sync::user_choice &uch, const std::set<int>& sides);
~user_choice_manager() {}
void search_in_replay();
public:

View file

@ -781,7 +781,7 @@ void theme::add_object(const config& cfg)
}
}
void theme::remove_object(std::string id){
void theme::remove_object(const std::string& id){
for(std::vector<theme::panel>::iterator p = panels_.begin(); p != panels_.end(); ++p) {
if (p->get_id() == id){
panels_.erase(p);
@ -873,7 +873,7 @@ void theme::modify(const config &cfg)
}
}
theme::object& theme::find_element(std::string id){
theme::object& theme::find_element(const std::string& id){
static theme::object empty_object;
theme::object* res = &empty_object;
for (std::vector<theme::panel>::iterator p = panels_.begin(); p != panels_.end(); ++p){

View file

@ -294,9 +294,9 @@ public:
events::generic_event& theme_reset_event() { return theme_reset_event_; }
private:
theme::object& find_element(std::string id);
theme::object& find_element(const std::string& id);
void add_object(const config& cfg);
void remove_object(std::string id);
void remove_object(const std::string& id);
void set_object_location(theme::object& element, std::string rect_str, std::string ref_id);
//notify observers that the theme has been rebuilt completely

View file

@ -572,7 +572,7 @@ void unit_die(const map_location& loc, unit& loser,
void unit_attack(display * disp, game_board & board,
const map_location& a, const map_location& b, int damage,
const attack_type& attack, const attack_type* secondary_attack,
int swing,std::string hit_text,int drain_amount,std::string att_text, const std::vector<std::string>* extra_hit_sounds)
int swing,const std::string& hit_text,int drain_amount,const std::string& att_text, const std::vector<std::string>* extra_hit_sounds)
{
if(!disp ||disp->video().update_locked() || disp->video().faked() ||
(disp->fogged(a) && disp->fogged(b)) || preferences::show_combat() == false) {

View file

@ -120,7 +120,7 @@ void unit_sheath_weapon( const map_location& loc, unit* u=nullptr, const attack_
void unit_attack(display * disp, game_board & board, //TODO: Would be nice if this could be purely a display function and defer damage dealing to its caller
const map_location& a, const map_location& b, int damage,
const attack_type& attack, const attack_type* secondary_attack,
int swing, std::string hit_text, int drain_amount, std::string att_text, const std::vector<std::string>* extra_hit_sounds=nullptr);
int swing, const std::string& hit_text, int drain_amount, const std::string& att_text, const std::vector<std::string>* extra_hit_sounds=nullptr);
void unit_recruited(const map_location& loc,