From af61a7c65004de8f4fe59e4d0f24c51318d8fe0c Mon Sep 17 00:00:00 2001 From: Eli Yukelzon Date: Sun, 31 Jul 2011 15:08:25 +0300 Subject: [PATCH] added support for optional default values in CCUserDefault getters --- cocos2dx/include/CCUserDefault.h | 12 ++++++------ cocos2dx/support/CCUserDefault.cpp | 22 +++++++++++----------- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/cocos2dx/include/CCUserDefault.h b/cocos2dx/include/CCUserDefault.h index 59bb034bbc..c4b5d5fc89 100644 --- a/cocos2dx/include/CCUserDefault.h +++ b/cocos2dx/include/CCUserDefault.h @@ -38,18 +38,18 @@ public: ~CCUserDefault(); // get value methods - bool getBoolForKey(const char* pKey); - int getIntegerForKey(const char* pKey); - float getFloatForKey(const char* pKey); - double getDoubleForKey(const char* pKey); - std::string getStringForKey(const char* pKey); + bool getBoolForKey(const char* pKey, bool defaultValue = false); + int getIntegerForKey(const char* pKey, int defaultValue = 0); + float getFloatForKey(const char* pKey, float defaultValue=0.0f); + double getDoubleForKey(const char* pKey, double defaultValue=0.0); + std::string getStringForKey(const char* pKey, const std::string & defaultValue = ""); // set value methods void setBoolForKey(const char* pKey, bool value); void setIntegerForKey(const char* pKey, int value); void setFloatForKey(const char* pKey, float 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 void purgeSharedUserDefault(); diff --git a/cocos2dx/support/CCUserDefault.cpp b/cocos2dx/support/CCUserDefault.cpp index 8423abca3c..d49c255130 100644 --- a/cocos2dx/support/CCUserDefault.cpp +++ b/cocos2dx/support/CCUserDefault.cpp @@ -148,10 +148,10 @@ void CCUserDefault::purgeSharedUserDefault() m_spUserDefault = NULL; } -bool CCUserDefault::getBoolForKey(const char* pKey) +bool CCUserDefault::getBoolForKey(const char* pKey, bool defaultValue) { const char* value = getValueForKey(pKey); - bool ret = false; + bool ret = defaultValue; if (value) { @@ -164,10 +164,10 @@ bool CCUserDefault::getBoolForKey(const char* pKey) return ret; } -int CCUserDefault::getIntegerForKey(const char* pKey) +int CCUserDefault::getIntegerForKey(const char* pKey, int defaultValue) { const char* value = getValueForKey(pKey); - int ret = 0; + int ret = defaultValue; if (value) { @@ -178,17 +178,17 @@ int CCUserDefault::getIntegerForKey(const char* pKey) 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; } -double CCUserDefault::getDoubleForKey(const char* pKey) +double CCUserDefault::getDoubleForKey(const char* pKey, double defaultValue) { const char* value = getValueForKey(pKey); - double ret = 0.0; + double ret = defaultValue; if (value) { @@ -199,10 +199,10 @@ double CCUserDefault::getDoubleForKey(const char* pKey) return ret; } -string CCUserDefault::getStringForKey(const char* pKey) +string CCUserDefault::getStringForKey(const char* pKey, const std::string & defaultValue) { const char* value = getValueForKey(pKey); - string ret(""); + string ret = defaultValue; if (value) { @@ -264,7 +264,7 @@ void CCUserDefault::setDoubleForKey(const char* pKey, double value) setValueForKey(pKey, tmp); } -void CCUserDefault::setStringForKey(const char* pKey, std::string value) +void CCUserDefault::setStringForKey(const char* pKey, const std::string & value) { // check key if (! pKey)