Skip to content

Commit

Permalink
Use cleaner format for formatter() when possible
Browse files Browse the repository at this point in the history
  • Loading branch information
Vultraz committed Aug 5, 2016
1 parent 7154a58 commit 6130c66
Show file tree
Hide file tree
Showing 11 changed files with 47 additions and 47 deletions.
4 changes: 2 additions & 2 deletions src/addon/manager_ui.cpp
Expand Up @@ -204,9 +204,9 @@ addon_op_result do_resolve_addon_dependencies(CVideo& v, addons_client& client,
//

const std::string sep(1, COLUMN_SEPARATOR);
const std::string& header = (formatter() << HEADING_PREFIX << sep <<
const std::string& header = formatter() << HEADING_PREFIX << sep <<
_("Name") << sep << _("Version") << sep << _("Author") << sep <<
_("Size") << sep << _("Type")).str();
_("Size") << sep << _("Type");

This comment has been minimized.

Copy link
@cbeck88

cbeck88 Aug 5, 2016

Member

I think it would be better to change the type of header to const std::string and get rid of the reference here. It's not technically a dangling reference, but the rule that makes this okay is fairly obscure.


std::vector<std::string> options(1, header);
std::vector<int> sort_sizes;
Expand Down
2 changes: 1 addition & 1 deletion src/build_info.cpp
Expand Up @@ -52,7 +52,7 @@ const version_table_manager versions;
#if 0
std::string format_version(unsigned a, unsigned b, unsigned c)
{
return (formatter() << a << '.' << b << '.' << c).str();
return formatter() << a << '.' << b << '.' << c;
}
#endif

Expand Down
4 changes: 2 additions & 2 deletions src/desktop/version.cpp
Expand Up @@ -259,10 +259,10 @@ std::string os_version()

version += " (";
// Add internal version numbers.
version += (formatter()
version += formatter()
<< v.dwMajorVersion << '.'
<< v.dwMinorVersion << '.'
<< v.dwBuildNumber).str();
<< v.dwBuildNumber;
version += ")";

return base + " " + version;
Expand Down
32 changes: 16 additions & 16 deletions src/formula/variant.cpp
Expand Up @@ -404,9 +404,9 @@ variant variant::operator[](const variant& v) const
}
return operator[](v.as_int());
} else {
throw type_error((formatter() << "type error: "
throw type_error(formatter() << "type error: "
<< " expected a list or a map but found " << type_string()
<< " (" << to_debug_string() << ")").str());
<< " (" << to_debug_string() << ")");
}
}

Expand Down Expand Up @@ -481,9 +481,9 @@ size_t variant::num_elements() const
assert(map_);
return map_->size();
} else {
throw type_error((formatter() << "type error: "
throw type_error(formatter() << "type error: "
<< " expected a list or a map but found " << type_string()
<< " (" << to_debug_string() << ")").str());
<< " (" << to_debug_string() << ")");
}
}

Expand Down Expand Up @@ -516,9 +516,9 @@ int variant::as_decimal() const
} else if( type_ == TYPE_NULL) {
return 0;
} else {
throw type_error((formatter() << "type error: "
throw type_error(formatter() << "type error: "
<< " expected integer or decimal but found " << type_string()
<< " (" << to_debug_string() << ")").str());
<< " (" << to_debug_string() << ")");
}
}

Expand Down Expand Up @@ -640,7 +640,7 @@ variant variant::operator/(const variant& v) const
int denominator = v.as_decimal();

if(denominator == 0) {
throw type_error((formatter() << "divide by zero error").str());
throw type_error(formatter() << "divide by zero error");
}

long long long_int = as_decimal();
Expand All @@ -662,7 +662,7 @@ variant variant::operator/(const variant& v) const
const int numerator = as_int();
const int denominator = v.as_int();
if(denominator == 0) {
throw type_error((formatter() << "divide by zero error").str());;
throw type_error(formatter() << "divide by zero error");
}

return variant(numerator/denominator);
Expand All @@ -674,15 +674,15 @@ variant variant::operator%(const variant& v) const
const int numerator = as_decimal();
const int denominator = v.as_decimal();
if(denominator == 0) {
throw type_error((formatter() << "divide by zero error").str());
throw type_error(formatter() << "divide by zero error");
}

return variant(numerator%denominator, DECIMAL_VARIANT);
} else {
const int numerator = as_int();
const int denominator = v.as_int();
if(denominator == 0) {
throw type_error((formatter() << "divide by zero error").str());
throw type_error(formatter() << "divide by zero error");
}

return variant(numerator%denominator);
Expand Down Expand Up @@ -935,11 +935,11 @@ variant variant::concatenate(const variant& v) const
std::string res = as_string() + v.as_string();
return variant( res );
} else {
throw type_error((formatter() << "type error: expected two "
throw type_error(formatter() << "type error: expected two "
<< " lists or two maps but found " << type_string()
<< " (" << to_debug_string() << ")"
<< " and " << v.type_string()
<< " (" << v.to_debug_string() << ")").str());
<< " (" << v.to_debug_string() << ")");
}
}

Expand All @@ -962,11 +962,11 @@ variant variant::build_range(const variant& v) const {

bool variant::contains(const variant& v) const {
if(type_ != TYPE_LIST && type_ != TYPE_MAP) {
throw type_error((formatter() << "type error: expected "
throw type_error(formatter() << "type error: expected "
<< variant_type_to_string(TYPE_LIST) << " or "
<< variant_type_to_string(TYPE_MAP) << " but found "
<< variant_type_to_string(type_)
<< " (" << to_debug_string() << ")").str());
<< " (" << to_debug_string() << ")");
}

if(type_ == TYPE_LIST) {
Expand All @@ -981,9 +981,9 @@ bool variant::contains(const variant& v) const {
void variant::must_be(variant::TYPE t) const
{
if(type_ != t) {
throw type_error((formatter() << "type error: " << " expected "
throw type_error(formatter() << "type error: " << " expected "
<< variant_type_to_string(t) << " but found " << type_string()
<< " (" << to_debug_string() << ")").str());
<< " (" << to_debug_string() << ")");
}
}

Expand Down
28 changes: 14 additions & 14 deletions src/gui/core/canvas.cpp
Expand Up @@ -905,24 +905,24 @@ void tcircle::draw(surface& canvas,
VALIDATE_WITH_DEV_MESSAGE(
static_cast<int>(x - radius) >= 0,
_("Circle doesn't fit on canvas."),
(formatter() << "x = " << x << ", radius = " << radius).str());
formatter() << "x = " << x << ", radius = " << radius);

VALIDATE_WITH_DEV_MESSAGE(
static_cast<int>(y - radius) >= 0,
_("Circle doesn't fit on canvas."),
(formatter() << "y = " << y << ", radius = " << radius).str());
formatter() << "y = " << y << ", radius = " << radius);

VALIDATE_WITH_DEV_MESSAGE(
static_cast<int>(x + radius) < canvas->w,
_("Circle doesn't fit on canvas."),
(formatter() << "x = " << x << ", radius = " << radius
<< "', canvas width = " << canvas->w << ".").str());
formatter() << "x = " << x << ", radius = " << radius
<< "', canvas width = " << canvas->w << ".");

VALIDATE_WITH_DEV_MESSAGE(
static_cast<int>(y + radius) < canvas->h,
_("Circle doesn't fit on canvas."),
(formatter() << "y = " << y << ", radius = " << radius
<< "', canvas height = " << canvas->h << ".").str());
formatter() << "y = " << y << ", radius = " << radius
<< "', canvas height = " << canvas->h << ".");

// lock the surface
surface_lock locker(canvas);
Expand Down Expand Up @@ -1106,33 +1106,33 @@ void timage::draw(surface& canvas,
unsigned w = w_(local_variables);
VALIDATE_WITH_DEV_MESSAGE(static_cast<int>(w) >= 0,
_("Image doesn't fit on canvas."),
(formatter() << "Image '" << name
formatter() << "Image '" << name
<< "', w = " << static_cast<int>(w)
<< ".").str());
<< ".");

unsigned h = h_(local_variables);
VALIDATE_WITH_DEV_MESSAGE(static_cast<int>(h) >= 0,
_("Image doesn't fit on canvas."),
(formatter() << "Image '" << name
formatter() << "Image '" << name
<< "', h = " << static_cast<int>(h)
<< ".").str());
<< ".");

local_variables.add("image_width", variant(w ? w : image_->w));
local_variables.add("image_height", variant(h ? h : image_->h));

const unsigned x = x_(local_variables);
VALIDATE_WITH_DEV_MESSAGE(static_cast<int>(x) >= 0,
_("Image doesn't fit on canvas."),
(formatter() << "Image '" << name
formatter() << "Image '" << name
<< "', x = " << static_cast<int>(x)
<< ".").str());
<< ".");

const unsigned y = y_(local_variables);
VALIDATE_WITH_DEV_MESSAGE(static_cast<int>(y) >= 0,
_("Image doesn't fit on canvas."),
(formatter() << "Image '" << name
formatter() << "Image '" << name
<< "', y = " << static_cast<int>(y)
<< ".").str());
<< ".");

// Copy the data to local variables to avoid overwriting the originals.
SDL_Rect src_clip = src_clip_;
Expand Down
4 changes: 2 additions & 2 deletions src/gui/widgets/control.cpp
Expand Up @@ -664,8 +664,8 @@ tbuilder_control::tbuilder_control(const config& cfg)
VALIDATE_WITH_DEV_MESSAGE(
help.empty() || !tooltip.empty(),
_("Found a widget with a helptip and without a tooltip."),
(formatter() << "id '" << id << "' label '" << label
<< "' helptip '" << help << "'.").str());
formatter() << "id '" << id << "' label '" << label
<< "' helptip '" << help << "'.");


DBG_GUI_P << "Window builder: found control with id '" << id
Expand Down
4 changes: 2 additions & 2 deletions src/gui/widgets/debug.cpp
Expand Up @@ -56,7 +56,7 @@ std::string get_child_id(const std::string& parent_id,
// strings. No idea why so switched to using the good old lexical_cast
// instead.

// return (formatter() << parent_id << "_C_" << row << '_' << col).c_str();
// return formatter() << parent_id << "_C_" << row << '_' << col;
std::string result = parent_id + "_C_" + std::to_string(row)
+ '_' + std::to_string(col);

Expand Down Expand Up @@ -91,7 +91,7 @@ std::string get_base_filename()
static unsigned counter = 0;
++counter;

return (formatter() << buf << '_' << counter << '_').str();
return formatter() << buf << '_' << counter << '_';
}
/***** ***** ***** ***** FLAGS ***** ***** ***** *****/

Expand Down
2 changes: 1 addition & 1 deletion src/gui/widgets/slider.cpp
Expand Up @@ -147,7 +147,7 @@ t_string tslider::get_value_label() const
return maximum_value_label_;
}

return t_string((formatter() << get_value()).str());
return t_string(formatter() << get_value());

This comment has been minimized.

Copy link
@CelticMinstrel

CelticMinstrel Aug 5, 2016

Member

Why not use std::to_string instead?

This comment has been minimized.

Copy link
@CelticMinstrel

CelticMinstrel Aug 5, 2016

Member

(Though, if it's a number, I also wonder why it needs to be a t_string...)

}

void tslider::child_callback_positioner_moved()
Expand Down
8 changes: 4 additions & 4 deletions src/mt_rng.cpp
Expand Up @@ -64,9 +64,9 @@ uint32_t mt_rng::get_next_random()
{
uint32_t result = mt_();
++random_calls_;
DBG_RND << (formatter() << "pulled user random " << result
DBG_RND << "pulled user random " << result
<< " for call " << random_calls_
<< " with seed " << std::hex << random_seed_).str() << std::endl;
<< " with seed " << std::hex << random_seed_ << std::endl;

return result;
}
Expand All @@ -81,8 +81,8 @@ void mt_rng::seed_random(const uint32_t seed, const unsigned int call_count)
random_seed_ = seed;
mt_.seed(random_seed_);
discard(call_count); //mt_.discard(call_count);
DBG_RND << (formatter() << "Seeded random with " << std::hex << random_seed_ << " with "
<< random_calls_ << " calls.").str() << std::endl;
DBG_RND << "Seeded random with " << std::hex << random_seed_ << " with "
<< random_calls_ << " calls." << std::endl;
}

void mt_rng::seed_random(const std::string & seed_str, const unsigned int call_count)
Expand Down
4 changes: 2 additions & 2 deletions src/savegame.cpp
Expand Up @@ -639,8 +639,8 @@ ingame_savegame::ingame_savegame(saved_game &gamestate,

void ingame_savegame::create_filename()
{
set_filename((formatter() << gamestate().classification().label
<< " " << _("Turn") << " " << gamestate().get_starting_pos()["turn_at"]).str());
set_filename(formatter() << gamestate().classification().label
<< " " << _("Turn") << " " << gamestate().get_starting_pos()["turn_at"]);
}

void ingame_savegame::write_game(config_writer &out) {
Expand Down
2 changes: 1 addition & 1 deletion src/scripting/game_lua_kernel.cpp
Expand Up @@ -3545,7 +3545,7 @@ int game_lua_kernel::intf_modify_side(lua_State *L)
*/
int game_lua_kernel::intf_get_sides(lua_State* L)
{
LOG_LUA << "intf_get_sides called: this = " << (formatter() << std::hex << this).str() << " myname = " << my_name() << std::endl;
LOG_LUA << "intf_get_sides called: this = " << std::hex << this << " myname = " << my_name() << std::endl;
std::vector<int> sides;
const vconfig ssf = luaW_checkvconfig(L, 1, true);
if(ssf.null()){
Expand Down

0 comments on commit 6130c66

Please sign in to comment.