Skip to content

Commit

Permalink
Modernize source code: last part (#6285)
Browse files Browse the repository at this point in the history
* Modernize source code: last par

* Use empty when needed
* Use emplace_back instead of push_back when needed
* For range-based loops
* Initializers fixes
* constructors, destructors default
* c++ C stl includes
  • Loading branch information
nerzhul committed Aug 20, 2017
1 parent 50669cd commit 1c1c97c
Show file tree
Hide file tree
Showing 72 changed files with 445 additions and 583 deletions.
2 changes: 1 addition & 1 deletion src/camera.h
Expand Up @@ -35,7 +35,7 @@ struct Nametag {
Nametag(scene::ISceneNode *a_parent_node,
const std::string &a_nametag_text,
const video::SColor &a_nametag_color,
const v3f a_nametag_pos):
const v3f &a_nametag_pos):
parent_node(a_parent_node),
nametag_text(a_nametag_text),
nametag_color(a_nametag_color),
Expand Down
4 changes: 2 additions & 2 deletions src/chat.cpp
Expand Up @@ -365,8 +365,8 @@ s32 ChatBuffer::getBottomScrollPos() const
s32 rows = (s32) m_rows;
if (rows == 0)
return 0;
else
return formatted_count - rows;

return formatted_count - rows;
}


Expand Down
2 changes: 1 addition & 1 deletion src/database-dummy.h
Expand Up @@ -35,7 +35,7 @@ class Database_Dummy : public MapDatabase, public PlayerDatabase
void savePlayer(RemotePlayer *player) {}
bool loadPlayer(RemotePlayer *player, PlayerSAO *sao) { return true; }
bool removePlayer(const std::string &name) { return true; }
void listPlayers(std::vector<std::string> &) {}
void listPlayers(std::vector<std::string> &res) {}

void beginSave() {}
void endSave() {}
Expand Down
4 changes: 2 additions & 2 deletions src/emerge.h
Expand Up @@ -29,10 +29,10 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#define BLOCK_EMERGE_ALLOW_GEN (1 << 0)
#define BLOCK_EMERGE_FORCE_QUEUE (1 << 1)

#define EMERGE_DBG_OUT(x) do { \
#define EMERGE_DBG_OUT(x) { \
if (enable_mapgen_debug_info) \
infostream << "EmergeThread: " x << std::endl; \
} while (0)
}

class EmergeThread;
class INodeDefManager;
Expand Down
6 changes: 3 additions & 3 deletions src/event.h
Expand Up @@ -22,7 +22,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
class MtEvent
{
public:
virtual ~MtEvent() {};
virtual ~MtEvent() = default;
//virtual MtEvent* clone(){ return new IEvent; }
virtual const char* getType() const = 0;

Expand All @@ -49,7 +49,7 @@ class SimpleTriggerEvent: public MtEvent
class MtEventReceiver
{
public:
virtual ~MtEventReceiver(){};
virtual ~MtEventReceiver() = default;
virtual void onEvent(MtEvent *e) = 0;
};

Expand All @@ -58,7 +58,7 @@ typedef void (*event_receive_func)(MtEvent *e, void *data);
class MtEventManager
{
public:
virtual ~MtEventManager(){};
virtual ~MtEventManager() = default;
virtual void put(MtEvent *e) = 0;
virtual void reg(const char *type, event_receive_func f, void *data) = 0;
// If data==NULL, every occurence of f is deregistered.
Expand Down
19 changes: 8 additions & 11 deletions src/event_manager.h
Expand Up @@ -43,17 +43,15 @@ class EventManager: public MtEventManager
std::map<std::string, Dest> m_dest;

public:
~EventManager()
{
}
~EventManager() = default;

void put(MtEvent *e)
{
std::map<std::string, Dest>::iterator i = m_dest.find(e->getType());
if(i != m_dest.end()){
std::list<FuncSpec> &funcs = i->second.funcs;
for(std::list<FuncSpec>::iterator i = funcs.begin();
i != funcs.end(); ++i){
(*(i->f))(e, i->d);
for (FuncSpec &func : funcs) {
(*(func.f))(e, func.d);
}
}
delete e;
Expand All @@ -62,11 +60,11 @@ class EventManager: public MtEventManager
{
std::map<std::string, Dest>::iterator i = m_dest.find(type);
if(i != m_dest.end()){
i->second.funcs.push_back(FuncSpec(f, data));
i->second.funcs.emplace_back(f, data);
} else{
std::list<FuncSpec> funcs;
Dest dest;
dest.funcs.push_back(FuncSpec(f, data));
dest.funcs.emplace_back(f, data);
m_dest[type] = dest;
}
}
Expand All @@ -86,9 +84,8 @@ class EventManager: public MtEventManager
}
}
} else{
for(std::map<std::string, Dest>::iterator
i = m_dest.begin(); i != m_dest.end(); ++i){
std::list<FuncSpec> &funcs = i->second.funcs;
for (auto &dest : m_dest) {
std::list<FuncSpec> &funcs = dest.second.funcs;
std::list<FuncSpec>::iterator j = funcs.begin();
while(j != funcs.end()){
bool remove = (j->f == f && (!data || j->d == data));
Expand Down
3 changes: 2 additions & 1 deletion src/exceptions.h
Expand Up @@ -27,7 +27,8 @@ class BaseException : public std::exception
{
public:
BaseException(const std::string &s) throw(): m_s(s) {}
~BaseException() throw() {}
~BaseException() throw() = default;

virtual const char * what() const throw()
{
return m_s.c_str();
Expand Down
86 changes: 36 additions & 50 deletions src/game.cpp
Expand Up @@ -69,8 +69,6 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "sound_openal.h"
#endif

extern Settings *g_settings;
extern Profiler *g_profiler;

/*
Text input system
Expand Down Expand Up @@ -248,7 +246,7 @@ void update_profiler_gui(gui::IGUIStaticText *guitext_profiler, FontEngine *fe,
setStaticText(guitext_profiler, text.c_str());
guitext_profiler->setVisible(true);

s32 w = fe->getTextWidth(text.c_str());
s32 w = fe->getTextWidth(text);

if (w < 400)
w = 400;
Expand Down Expand Up @@ -291,11 +289,9 @@ class ProfilerGraph
};
std::deque<Piece> m_log;
public:
u32 m_log_max_size;
u32 m_log_max_size = 200;

ProfilerGraph():
m_log_max_size(200)
{}
ProfilerGraph() = default;

void put(const Profiler::GraphValues &values)
{
Expand All @@ -314,14 +310,10 @@ class ProfilerGraph
// to be the same for each call to prevent flickering
std::map<std::string, Meta> m_meta;

for (std::deque<Piece>::const_iterator k = m_log.begin();
k != m_log.end(); ++k) {
const Piece &piece = *k;

for (Profiler::GraphValues::const_iterator i = piece.values.begin();
i != piece.values.end(); ++i) {
const std::string &id = i->first;
const float &value = i->second;
for (const Piece &piece : m_log) {
for (const auto &i : piece.values) {
const std::string &id = i.first;
const float &value = i.second;
std::map<std::string, Meta>::iterator j = m_meta.find(id);

if (j == m_meta.end()) {
Expand Down Expand Up @@ -349,9 +341,8 @@ class ProfilerGraph
sizeof(usable_colors) / sizeof(*usable_colors);
u32 next_color_i = 0;

for (std::map<std::string, Meta>::iterator i = m_meta.begin();
i != m_meta.end(); ++i) {
Meta &meta = i->second;
for (auto &i : m_meta) {
Meta &meta = i.second;
video::SColor color(255, 200, 200, 200);

if (next_color_i < usable_colors_count)
Expand Down Expand Up @@ -401,9 +392,7 @@ class ProfilerGraph
float lastscaledvalue = 0.0;
bool lastscaledvalue_exists = false;

for (std::deque<Piece>::const_iterator j = m_log.begin();
j != m_log.end(); ++j) {
const Piece &piece = *j;
for (const Piece &piece : m_log) {
float value = 0;
bool value_exists = false;
Profiler::GraphValues::const_iterator k =
Expand Down Expand Up @@ -763,8 +752,8 @@ class GameGlobalShaderConstantSetterFactory : public IShaderConstantSetterFactor

void setSky(Sky *sky) {
m_sky = sky;
for (size_t i = 0; i < created_nosky.size(); ++i) {
created_nosky[i]->setSky(m_sky);
for (GameGlobalShaderConstantSetter *ggscs : created_nosky) {
ggscs->setSky(m_sky);
}
created_nosky.clear();
}
Expand Down Expand Up @@ -793,7 +782,7 @@ bool nodePlacementPrediction(Client &client, const ItemDefinition &playeritem_de
if (!is_valid_position)
return false;

if (prediction != "" && !nodedef->get(node).rightclickable) {
if (!prediction.empty() && !nodedef->get(node).rightclickable) {
verbosestream << "Node placement prediction for "
<< playeritem_def.name << " is "
<< prediction << std::endl;
Expand Down Expand Up @@ -978,7 +967,7 @@ static void updateChat(Client &client, f32 dtime, bool show_debug,
}

// Get new messages from client
std::wstring message = L"";
std::wstring message;
while (client.getChatMessage(message)) {
chat_backend.addUnparsedMessage(message);
}
Expand Down Expand Up @@ -1116,8 +1105,8 @@ void KeyCache::populate()
if (handler) {
// First clear all keys, then re-add the ones we listen for
handler->dontListenForKeys();
for (size_t i = 0; i < KeyType::INTERNAL_ENUM_COUNT; i++) {
handler->listenForKey(key[i]);
for (const KeyPress &k : key) {
handler->listenForKey(k);
}
handler->listenForKey(EscapeKey);
handler->listenForKey(CancelKey);
Expand Down Expand Up @@ -1384,7 +1373,7 @@ class Game {

GameOnDemandSoundFetcher soundfetcher; // useful when testing
ISoundManager *sound;
bool sound_is_dummy;
bool sound_is_dummy = false;
SoundMaker *soundmaker;

ChatBackend *chat_backend;
Expand Down Expand Up @@ -1464,9 +1453,9 @@ class Game {
f32 m_cache_cam_smoothing;
f32 m_cache_fog_start;

bool m_invert_mouse;
bool m_first_loop_after_window_activation;
bool m_camera_offset_changed;
bool m_invert_mouse = false;
bool m_first_loop_after_window_activation = false;
bool m_camera_offset_changed = false;

#ifdef __ANDROID__
bool m_cache_hold_aux1;
Expand All @@ -1482,7 +1471,6 @@ Game::Game() :
itemdef_manager(NULL),
nodedef_manager(NULL),
sound(NULL),
sound_is_dummy(false),
soundmaker(NULL),
chat_backend(NULL),
current_formspec(NULL),
Expand All @@ -1496,10 +1484,7 @@ Game::Game() :
sky(NULL),
local_inventory(NULL),
hud(NULL),
mapper(NULL),
m_invert_mouse(false),
m_first_loop_after_window_activation(false),
m_camera_offset_changed(false)
mapper(NULL)
{
g_settings->registerChangedCallback("doubletap_jump",
&settingChangedCallback, this);
Expand Down Expand Up @@ -1806,7 +1791,7 @@ bool Game::init(
return false;

// Create a server if not connecting to an existing one
if (*address == "") {
if (address->empty()) {
if (!createSingleplayerServer(map_dir, gamespec, port, address))
return false;
}
Expand Down Expand Up @@ -2168,7 +2153,7 @@ bool Game::connectToServer(const std::string &playername,

wait_time += dtime;
// Only time out if we aren't waiting for the server we started
if ((*address != "") && (wait_time > 10)) {
if ((!address->empty()) && (wait_time > 10)) {
bool sent_old_init = g_settings->getFlag("send_pre_v25_init");
// If no pre v25 init was sent, and no answer was received,
// but the low level connection could be established
Expand Down Expand Up @@ -3175,8 +3160,9 @@ void Game::processClientEvents(CameraOrientation *cam)
break;

case CE_SHOW_FORMSPEC:
if (*(event.show_formspec.formspec) == "") {
if (current_formspec && ( *(event.show_formspec.formname) == "" || *(event.show_formspec.formname) == cur_formname) ){
if (event.show_formspec.formspec->empty()) {
if (current_formspec && (event.show_formspec.formname->empty()
|| *(event.show_formspec.formname) == cur_formname)) {
current_formspec->quitMenu();
}
} else {
Expand Down Expand Up @@ -3713,7 +3699,7 @@ PointedThing Game::updatePointedThing(
}

// Update selection mesh light level and vertex colors
if (selectionboxes->size() > 0) {
if (!selectionboxes->empty()) {
v3f pf = hud->getSelectionPos();
v3s16 p = floatToInt(pf, BS);

Expand All @@ -3722,8 +3708,8 @@ PointedThing Game::updatePointedThing(
u16 node_light = getInteriorLight(n, -1, nodedef);
u16 light_level = node_light;

for (u8 i = 0; i < 6; i++) {
n = map.getNodeNoEx(p + g_6dirs[i]);
for (const v3s16 &dir : g_6dirs) {
n = map.getNodeNoEx(p + dir);
node_light = getInteriorLight(n, -1, nodedef);
if (node_light > light_level)
light_level = node_light;
Expand Down Expand Up @@ -3797,7 +3783,7 @@ void Game::handlePointingAtNode(const PointedThing &pointed,
runData.repeat_rightclick_timer = 0;
infostream << "Ground right-clicked" << std::endl;

if (meta && meta->getString("formspec") != "" && !random_input
if (meta && !meta->getString("formspec").empty() && !random_input
&& !isKeyDown(KeyType::SNEAK)) {
// Report right click to server
if (nodedef_manager->get(map.getNodeNoEx(nodepos)).rightclickable) {
Expand Down Expand Up @@ -3842,7 +3828,7 @@ void Game::handlePointingAtNode(const PointedThing &pointed,
soundmaker->m_player_rightpunch_sound =
SimpleSoundSpec();

if (playeritem_def.node_placement_prediction == "" ||
if (playeritem_def.node_placement_prediction.empty() ||
nodedef_manager->get(map.getNodeNoEx(nodepos)).rightclickable) {
client->interact(3, pointed); // Report to server
} else {
Expand All @@ -3862,7 +3848,7 @@ void Game::handlePointingAtObject(const PointedThing &pointed, const ItemStack &
utf8_to_wide(runData.selected_object->infoText()));

if (show_debug) {
if (infotext != L"") {
if (!infotext.empty()) {
infotext += L"\n";
}
infotext += unescape_enriched(utf8_to_wide(
Expand Down Expand Up @@ -3973,7 +3959,7 @@ void Game::handleDigging(const PointedThing &pointed, const v3s16 &nodepos,

if (sound_dig.exists() && params.diggable) {
if (sound_dig.name == "__group") {
if (params.main_group != "") {
if (!params.main_group.empty()) {
soundmaker->m_player_leftpunch_sound.gain = 0.5;
soundmaker->m_player_leftpunch_sound.name =
std::string("default_dig_") +
Expand Down Expand Up @@ -4649,7 +4635,7 @@ void Game::showPauseMenu()
static const std::string mode = strgettext("- Mode: ");
if (!simple_singleplayer_mode) {
Address serverAddress = client->getServerAddress();
if (address != "") {
if (!address.empty()) {
os << mode << strgettext("Remote server") << "\n"
<< strgettext("- Address: ") << address;
} else {
Expand All @@ -4659,7 +4645,7 @@ void Game::showPauseMenu()
} else {
os << mode << strgettext("Singleplayer") << "\n";
}
if (simple_singleplayer_mode || address == "") {
if (simple_singleplayer_mode || address.empty()) {
static const std::string on = strgettext("On");
static const std::string off = strgettext("Off");
const std::string &damage = g_settings->getBool("enable_damage") ? on : off;
Expand All @@ -4673,7 +4659,7 @@ void Game::showPauseMenu()
<< strgettext("- Public: ") << announced << "\n";
std::string server_name = g_settings->get("server_name");
str_formspec_escape(server_name);
if (announced == on && server_name != "")
if (announced == on && !server_name.empty())
os << strgettext("- Server Name: ") << server_name;

}
Expand Down

0 comments on commit 1c1c97c

Please sign in to comment.