Use const ref in Settings Class

master
Unknown 2019-02-22 13:52:27 +01:00
parent d9101ee13d
commit 38ef37c48e
2 changed files with 11 additions and 11 deletions

View File

@ -2,14 +2,14 @@
#include <iostream> #include <iostream>
#include "Settings.h" #include "Settings.h"
static std::string trim(const std::string s) static std::string trim(const std::string &s)
{ {
size_t start = s.find_first_not_of(" \t\r\n"); size_t start = s.find_first_not_of(" \t\r\n");
size_t end = s.find_last_not_of(" \t\r\n"); size_t end = s.find_last_not_of(" \t\r\n");
return s.substr(start, (end-start+1)); return s.substr(start, (end-start+1));
} }
Settings::Settings(std::string filename, std::string msgName) Settings::Settings(const std::string &filename, const std::string &msgName)
: m_filename(filename), : m_filename(filename),
m_messageName(msgName) m_messageName(msgName)
{ {
@ -25,7 +25,7 @@ Settings::~Settings(void)
m_file.close(); m_file.close();
} }
std::string Settings::get(std::string key) std::string Settings::get(const std::string &key)
{ {
std::string value; std::string value;
if (getGeneric(key, &value)) if (getGeneric(key, &value))
@ -34,7 +34,7 @@ std::string Settings::get(std::string key)
throw std::runtime_error(std::string("Failed to read key '") + key + "' from file '" + m_messageName + "'"); throw std::runtime_error(std::string("Failed to read key '") + key + "' from file '" + m_messageName + "'");
} }
std::string Settings::get(std::string key, std::string defaultValue) std::string Settings::get(const std::string &key, const std::string &defaultValue)
{ {
std::string value; std::string value;
if (getGeneric(key, &value)) if (getGeneric(key, &value))
@ -43,7 +43,7 @@ std::string Settings::get(std::string key, std::string defaultValue)
return defaultValue; return defaultValue;
} }
bool Settings::getGeneric(std::string key, std::string *pvalue) bool Settings::getGeneric(const std::string &key, std::string *pvalue)
{ {
std::string line; std::string line;
int linenr = 0; int linenr = 0;

View File

@ -8,18 +8,18 @@
class Settings { class Settings {
public: public:
Settings(std::string filename, std::string msgName = ""); Settings(const std::string &filename, const std::string &msgName = "");
~Settings(void); ~Settings(void);
std::string get(std::string key); std::string get(const std::string &key);
std::string get(std::string key, std::string defaultValue); std::string get(const std::string &key, const std::string &defaultValue);
bool check(std::string key) { return getGeneric(key); } bool check(const std::string &key) { return getGeneric(key); }
bool check(std::string key, std::string &value) { return getGeneric(key, &value); } bool check(const std::string &key, std::string &value) { return getGeneric(key, &value); }
private: private:
std::string m_filename; std::string m_filename;
std::string m_messageName; std::string m_messageName;
std::ifstream m_file; std::ifstream m_file;
bool getGeneric(std::string key, std::string *pvalue = NULL); bool getGeneric(const std::string &key, std::string *pvalue = nullptr);
}; };
#endif // _SETTINGS_H_ #endif // _SETTINGS_H_