Make getters of the Settings class const

Also removed 2 unused functions.
master
Craig Robbins 2014-09-11 22:49:25 +10:00 committed by ShadowNinja
parent a020d1b653
commit b97c9c6577
1 changed files with 37 additions and 80 deletions

View File

@ -443,107 +443,62 @@ public:
m_defaults[name] = value; m_defaults[name] = value;
} }
bool exists(std::string name) bool exists(std::string name) const
{ {
JMutexAutoLock lock(m_mutex); JMutexAutoLock lock(m_mutex);
return (m_settings.find(name) != m_settings.end() || m_defaults.find(name) != m_defaults.end()); return m_settings.find(name) != m_settings.end()
|| m_defaults.find(name) != m_defaults.end();
} }
std::string get(std::string name) std::string get(std::string name) const
{ {
JMutexAutoLock lock(m_mutex); JMutexAutoLock lock(m_mutex);
std::map<std::string, std::string>::iterator n; std::map<std::string, std::string>::const_iterator n;
n = m_settings.find(name); if ((n = m_settings.find(name)) == m_settings.end())
if(n == m_settings.end()) if ((n = m_defaults.find(name)) == m_defaults.end())
{
n = m_defaults.find(name);
if(n == m_defaults.end())
{
throw SettingNotFoundException(("Setting [" + name + "] not found ").c_str()); throw SettingNotFoundException(("Setting [" + name + "] not found ").c_str());
}
}
return n->second; return n->second;
} }
//////////// Get setting //////////// Get setting
bool getBool(std::string name) bool getBool(std::string name) const
{ {
return is_yes(get(name)); return is_yes(get(name));
} }
bool getFlag(std::string name) bool getFlag(std::string name) const
{
try
{ {
try {
return getBool(name); return getBool(name);
} } catch(SettingNotFoundException &e) {
catch(SettingNotFoundException &e)
{
return false; return false;
} }
} }
// Asks if empty float getFloat(std::string name) const
bool getBoolAsk(std::string name, std::string question, bool def)
{
// If it is in settings
if(exists(name))
return getBool(name);
std::string s;
char templine[10];
std::cout<<question<<" [y/N]: ";
std::cin.getline(templine, 10);
s = templine;
if(s == "")
return def;
return is_yes(s);
}
float getFloat(std::string name)
{ {
return stof(get(name)); return stof(get(name));
} }
u16 getU16(std::string name) u16 getU16(std::string name) const
{ {
return stoi(get(name), 0, 65535); return stoi(get(name), 0, 65535);
} }
u16 getU16Ask(std::string name, std::string question, u16 def) s16 getS16(std::string name) const
{
// If it is in settings
if(exists(name))
return getU16(name);
std::string s;
char templine[10];
std::cout<<question<<" ["<<def<<"]: ";
std::cin.getline(templine, 10);
s = templine;
if(s == "")
return def;
return stoi(s, 0, 65535);
}
s16 getS16(std::string name)
{ {
return stoi(get(name), -32768, 32767); return stoi(get(name), -32768, 32767);
} }
s32 getS32(std::string name) s32 getS32(std::string name) const
{ {
return stoi(get(name)); return stoi(get(name));
} }
v3f getV3F(std::string name) v3f getV3F(std::string name) const
{ {
v3f value; v3f value;
Strfnd f(get(name)); Strfnd f(get(name));
@ -554,7 +509,7 @@ public:
return value; return value;
} }
v2f getV2F(std::string name) v2f getV2F(std::string name) const
{ {
v2f value; v2f value;
Strfnd f(get(name)); Strfnd f(get(name));
@ -564,7 +519,7 @@ public:
return value; return value;
} }
u64 getU64(std::string name) u64 getU64(std::string name) const
{ {
u64 value = 0; u64 value = 0;
std::string s = get(name); std::string s = get(name);
@ -573,16 +528,18 @@ public:
return value; return value;
} }
u32 getFlagStr(std::string name, FlagDesc *flagdesc, u32 *flagmask) u32 getFlagStr(std::string name, FlagDesc *flagdesc, u32 *flagmask) const
{ {
std::string val = get(name); std::string val = get(name);
return (std::isdigit(val[0])) ? stoi(val) : return std::isdigit(val[0])
readFlagString(val, flagdesc, flagmask); ? stoi(val)
: readFlagString(val, flagdesc, flagmask);
} }
// N.B. if getStruct() is used to read a non-POD aggregate type, // N.B. if getStruct() is used to read a non-POD aggregate type,
// the behavior is undefined. // the behavior is undefined.
bool getStruct(std::string name, std::string format, void *out, size_t olen) bool getStruct(std::string name, std::string format,
void *out, size_t olen) const
{ {
std::string valstr; std::string valstr;
@ -599,7 +556,7 @@ public:
} }
//////////// Try to get value, no exception thrown //////////// Try to get value, no exception thrown
bool getNoEx(std::string name, std::string &val) bool getNoEx(std::string name, std::string &val) const
{ {
try { try {
val = get(name); val = get(name);
@ -612,7 +569,7 @@ public:
// N.B. getFlagStrNoEx() does not set val, but merely modifies it. Thus, // N.B. getFlagStrNoEx() does not set val, but merely modifies it. Thus,
// val must be initialized before using getFlagStrNoEx(). The intention of // val must be initialized before using getFlagStrNoEx(). The intention of
// this is to simplify modifying a flags field from a default value. // this is to simplify modifying a flags field from a default value.
bool getFlagStrNoEx(std::string name, u32 &val, FlagDesc *flagdesc) bool getFlagStrNoEx(std::string name, u32 &val, FlagDesc *flagdesc) const
{ {
try { try {
u32 flags, flagmask; u32 flags, flagmask;
@ -628,7 +585,7 @@ public:
} }
} }
bool getFloatNoEx(std::string name, float &val) bool getFloatNoEx(std::string name, float &val) const
{ {
try { try {
val = getFloat(name); val = getFloat(name);
@ -638,7 +595,7 @@ public:
} }
} }
bool getU16NoEx(std::string name, int &val) bool getU16NoEx(std::string name, int &val) const
{ {
try { try {
val = getU16(name); val = getU16(name);
@ -648,7 +605,7 @@ public:
} }
} }
bool getU16NoEx(std::string name, u16 &val) bool getU16NoEx(std::string name, u16 &val) const
{ {
try { try {
val = getU16(name); val = getU16(name);
@ -658,7 +615,7 @@ public:
} }
} }
bool getS16NoEx(std::string name, int &val) bool getS16NoEx(std::string name, int &val) const
{ {
try { try {
val = getU16(name); val = getU16(name);
@ -668,7 +625,7 @@ public:
} }
} }
bool getS16NoEx(std::string name, s16 &val) bool getS16NoEx(std::string name, s16 &val) const
{ {
try { try {
val = getS16(name); val = getS16(name);
@ -678,7 +635,7 @@ public:
} }
} }
bool getS32NoEx(std::string name, s32 &val) bool getS32NoEx(std::string name, s32 &val) const
{ {
try { try {
val = getS32(name); val = getS32(name);
@ -688,7 +645,7 @@ public:
} }
} }
bool getV3FNoEx(std::string name, v3f &val) bool getV3FNoEx(std::string name, v3f &val) const
{ {
try { try {
val = getV3F(name); val = getV3F(name);
@ -698,7 +655,7 @@ public:
} }
} }
bool getV2FNoEx(std::string name, v2f &val) bool getV2FNoEx(std::string name, v2f &val) const
{ {
try { try {
val = getV2F(name); val = getV2F(name);
@ -708,7 +665,7 @@ public:
} }
} }
bool getU64NoEx(std::string name, u64 &val) bool getU64NoEx(std::string name, u64 &val) const
{ {
try { try {
val = getU64(name); val = getU64(name);
@ -852,7 +809,7 @@ private:
std::map<std::string, std::string> m_settings; std::map<std::string, std::string> m_settings;
std::map<std::string, std::string> m_defaults; std::map<std::string, std::string> m_defaults;
// All methods that access m_settings/m_defaults directly should lock this. // All methods that access m_settings/m_defaults directly should lock this.
JMutex m_mutex; mutable JMutex m_mutex;
}; };
#endif #endif