Skip to content

Commit

Permalink
Hopson97 code review
Browse files Browse the repository at this point in the history
Summary:
Modernize code by using C++17 features and unordered_map::emplace() rather
than unordered_map::insert(std::make_pair(...))

file_io:
* Remove fileExists() function

obd_parser:
* Replace calls to unordered_map::insert() with calls to

main:
* Replace calls to unordered_map::insert() with calls to
unorderd_map::emplace()
* Use std::filesystem::exists() instead of fileExists helper function
  • Loading branch information
MarkusG committed Feb 10, 2020
1 parent 060813b commit 20b4c1b
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 21 deletions.
6 changes: 0 additions & 6 deletions src/common/common/file_io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,3 @@ std::string loadFileContents(const std::string& path)
stream << inFile.rdbuf();
return stream.str();
}

bool fileExists(const std::string& path)
{
std::ifstream inFile(path);
return !!inFile;
}
2 changes: 1 addition & 1 deletion src/common/common/obd_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ std::unordered_map<std::string, std::string> parseObdData(const std::string& obd

while (contents >> key) {
contents >> value;
data.insert(std::make_pair(key, value));
data.emplace(key, value);
}

return data;
Expand Down
28 changes: 14 additions & 14 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ struct Config {
*/
void loadFromConfigFile(Config& config)
{
if (fileExists("client.obd")) {
if (std::filesystem::exists("client.obd")) {
auto clientData = parseObdData(loadFileContents("client.obd"));

config.client.fullScreen = std::stoi(clientData["fullscreen"]);
Expand All @@ -68,17 +68,17 @@ void loadFromConfigFile(Config& config)
}
else {
std::unordered_map<std::string, std::string> clientData;
clientData.insert(std::make_pair("fullscreen", config.client.fullScreen ? "1" : "0"));
clientData.insert(std::make_pair("window_width", std::to_string(config.client.windowWidth)));
clientData.insert(std::make_pair("window_height", std::to_string(config.client.windowHeight)));
clientData.insert(std::make_pair("cap_fps", config.client.isFpsCapped ? "1" : "0"));
clientData.insert(std::make_pair("shouldShowInstructions", config.client.shouldShowInstructions ? "1" : "0"));
clientData.insert(std::make_pair("fps_limit", std::to_string(config.client.fpsLimit)));
clientData.insert(std::make_pair("fov", std::to_string(config.client.fov)));
clientData.insert(std::make_pair("fps_limit", std::to_string(config.client.fpsLimit)));
clientData.insert(std::make_pair("skin", config.client.skinName));
clientData.insert(std::make_pair("texture_pack", config.client.texturePack));
clientData.insert(std::make_pair("server_ip", config.client.serverIp));
clientData.emplace("fullscreen", config.client.fullScreen ? "1" : "0");
clientData.emplace("window_width", std::to_string(config.client.windowWidth));
clientData.emplace("window_height", std::to_string(config.client.windowHeight));
clientData.emplace("cap_fps", config.client.isFpsCapped ? "1" : "0");
clientData.emplace("shouldShowInstructions", config.client.shouldShowInstructions ? "1" : "0");
clientData.emplace("fps_limit", std::to_string(config.client.fpsLimit));
clientData.emplace("fov", std::to_string(config.client.fov));
clientData.emplace("fps_limit", std::to_string(config.client.fpsLimit));
clientData.emplace("skin", config.client.skinName);
clientData.emplace("texture_pack", config.client.texturePack);
clientData.emplace("server_ip", config.client.serverIp);

std::string clientDataSerialized = serializeObdData(clientData);
std::ofstream clientFile("client.obd");
Expand All @@ -88,13 +88,13 @@ void loadFromConfigFile(Config& config)
clientFile << clientDataSerialized << std::endl;
}

if (fileExists("server.obd")) {
if (std::filesystem::exists("server.obd")) {
auto serverData = parseObdData(loadFileContents("server.obd"));
config.server.worldSize = std::stoi(serverData["world_size"]);
}
else {
std::unordered_map<std::string, std::string> serverData;
serverData.insert(std::make_pair("world_size", std::to_string(config.server.worldSize)));
serverData.emplace("world_size", std::to_string(config.server.worldSize));

std::string serverDataSerialized = serializeObdData(serverData);
std::ofstream serverFile("server.obd");
Expand Down

0 comments on commit 20b4c1b

Please sign in to comment.