added support for optional default values in CCUserDefault getters

This commit is contained in:
Eli Yukelzon 2011-07-31 15:08:25 +03:00
parent 9c5fa51e6b
commit af61a7c650
2 changed files with 17 additions and 17 deletions

View File

@ -38,18 +38,18 @@ public:
~CCUserDefault(); ~CCUserDefault();
// get value methods // get value methods
bool getBoolForKey(const char* pKey); bool getBoolForKey(const char* pKey, bool defaultValue = false);
int getIntegerForKey(const char* pKey); int getIntegerForKey(const char* pKey, int defaultValue = 0);
float getFloatForKey(const char* pKey); float getFloatForKey(const char* pKey, float defaultValue=0.0f);
double getDoubleForKey(const char* pKey); double getDoubleForKey(const char* pKey, double defaultValue=0.0);
std::string getStringForKey(const char* pKey); std::string getStringForKey(const char* pKey, const std::string & defaultValue = "");
// set value methods // set value methods
void setBoolForKey(const char* pKey, bool value); void setBoolForKey(const char* pKey, bool value);
void setIntegerForKey(const char* pKey, int value); void setIntegerForKey(const char* pKey, int value);
void setFloatForKey(const char* pKey, float value); void setFloatForKey(const char* pKey, float value);
void setDoubleForKey(const char* pKey, double value); void setDoubleForKey(const char* pKey, double value);
void setStringForKey(const char* pKey, std::string value); void setStringForKey(const char* pKey, const std::string & value);
static CCUserDefault* sharedUserDefault(); static CCUserDefault* sharedUserDefault();
static void purgeSharedUserDefault(); static void purgeSharedUserDefault();

View File

@ -148,10 +148,10 @@ void CCUserDefault::purgeSharedUserDefault()
m_spUserDefault = NULL; m_spUserDefault = NULL;
} }
bool CCUserDefault::getBoolForKey(const char* pKey) bool CCUserDefault::getBoolForKey(const char* pKey, bool defaultValue)
{ {
const char* value = getValueForKey(pKey); const char* value = getValueForKey(pKey);
bool ret = false; bool ret = defaultValue;
if (value) if (value)
{ {
@ -164,10 +164,10 @@ bool CCUserDefault::getBoolForKey(const char* pKey)
return ret; return ret;
} }
int CCUserDefault::getIntegerForKey(const char* pKey) int CCUserDefault::getIntegerForKey(const char* pKey, int defaultValue)
{ {
const char* value = getValueForKey(pKey); const char* value = getValueForKey(pKey);
int ret = 0; int ret = defaultValue;
if (value) if (value)
{ {
@ -178,17 +178,17 @@ int CCUserDefault::getIntegerForKey(const char* pKey)
return ret; return ret;
} }
float CCUserDefault::getFloatForKey(const char* pKey) float CCUserDefault::getFloatForKey(const char* pKey, float defaultValue)
{ {
float ret = (float)getDoubleForKey(pKey); float ret = (float)getDoubleForKey(pKey, (double)defaultValue);
return ret; return ret;
} }
double CCUserDefault::getDoubleForKey(const char* pKey) double CCUserDefault::getDoubleForKey(const char* pKey, double defaultValue)
{ {
const char* value = getValueForKey(pKey); const char* value = getValueForKey(pKey);
double ret = 0.0; double ret = defaultValue;
if (value) if (value)
{ {
@ -199,10 +199,10 @@ double CCUserDefault::getDoubleForKey(const char* pKey)
return ret; return ret;
} }
string CCUserDefault::getStringForKey(const char* pKey) string CCUserDefault::getStringForKey(const char* pKey, const std::string & defaultValue)
{ {
const char* value = getValueForKey(pKey); const char* value = getValueForKey(pKey);
string ret(""); string ret = defaultValue;
if (value) if (value)
{ {
@ -264,7 +264,7 @@ void CCUserDefault::setDoubleForKey(const char* pKey, double value)
setValueForKey(pKey, tmp); setValueForKey(pKey, tmp);
} }
void CCUserDefault::setStringForKey(const char* pKey, std::string value) void CCUserDefault::setStringForKey(const char* pKey, const std::string & value)
{ {
// check key // check key
if (! pKey) if (! pKey)