Use const ref in Settings Class
parent
d9101ee13d
commit
38ef37c48e
|
@ -2,14 +2,14 @@
|
|||
#include <iostream>
|
||||
#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 end = s.find_last_not_of(" \t\r\n");
|
||||
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_messageName(msgName)
|
||||
{
|
||||
|
@ -25,7 +25,7 @@ Settings::~Settings(void)
|
|||
m_file.close();
|
||||
}
|
||||
|
||||
std::string Settings::get(std::string key)
|
||||
std::string Settings::get(const std::string &key)
|
||||
{
|
||||
std::string 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 + "'");
|
||||
}
|
||||
|
||||
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;
|
||||
if (getGeneric(key, &value))
|
||||
|
@ -43,7 +43,7 @@ std::string Settings::get(std::string key, std::string 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;
|
||||
int linenr = 0;
|
||||
|
|
|
@ -8,18 +8,18 @@
|
|||
|
||||
class Settings {
|
||||
public:
|
||||
Settings(std::string filename, std::string msgName = "");
|
||||
Settings(const std::string &filename, const std::string &msgName = "");
|
||||
~Settings(void);
|
||||
std::string get(std::string key);
|
||||
std::string get(std::string key, std::string defaultValue);
|
||||
bool check(std::string key) { return getGeneric(key); }
|
||||
bool check(std::string key, std::string &value) { return getGeneric(key, &value); }
|
||||
std::string get(const std::string &key);
|
||||
std::string get(const std::string &key, const std::string &defaultValue);
|
||||
bool check(const std::string &key) { return getGeneric(key); }
|
||||
bool check(const std::string &key, std::string &value) { return getGeneric(key, &value); }
|
||||
private:
|
||||
std::string m_filename;
|
||||
std::string m_messageName;
|
||||
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_
|
||||
|
|
Loading…
Reference in New Issue