UserDefault::setDelegate

UserDefault very slow, each call need read / write and parse xml file.
Delegate makes it possible to use other variants.
This commit is contained in:
Vladimir Perminov 2015-04-19 22:07:48 +03:00
parent 425b2b62e1
commit 87d0d4f677
4 changed files with 57 additions and 63 deletions

View File

@ -475,13 +475,12 @@ UserDefault* UserDefault::sharedUserDefault()
}
UserDefault* UserDefault::getInstance()
{
#ifdef KEEP_COMPATABILITY
initXMLFilePath();
#endif
{
if (! _userDefault)
{
#ifdef KEEP_COMPATABILITY
initXMLFilePath();
#endif
_userDefault = new (std::nothrow) UserDefault();
}
@ -490,16 +489,7 @@ UserDefault* UserDefault::getInstance()
bool UserDefault::isXMLFileExist()
{
FILE *fp = fopen(_filePath.c_str(), "r");
bool bRet = false;
if (fp)
{
bRet = true;
fclose(fp);
}
return bRet;
return FileUtils::getInstance()->isFileExist(_filePath);
}
void UserDefault::initXMLFilePath()

View File

@ -475,13 +475,12 @@ void UserDefault::setDataForKey(const char* pKey, const Data& value) {
}
UserDefault* UserDefault::getInstance()
{
#ifdef KEEP_COMPATABILITY
initXMLFilePath();
#endif
{
if (! _userDefault)
{
#ifdef KEEP_COMPATABILITY
initXMLFilePath();
#endif
_userDefault = new (std::nothrow) UserDefault();
}
@ -507,16 +506,7 @@ void UserDefault::purgeSharedUserDefault()
bool UserDefault::isXMLFileExist()
{
FILE *fp = fopen(_filePath.c_str(), "r");
bool bRet = false;
if (fp)
{
bRet = true;
fclose(fp);
}
return bRet;
return FileUtils::getInstance()->isFileExist(_filePath);
}
void UserDefault::initXMLFilePath()

View File

@ -406,17 +406,17 @@ void UserDefault::setDataForKey(const char* pKey, const Data& value) {
UserDefault* UserDefault::getInstance()
{
initXMLFilePath();
// only create xml file one time
// the file exists after the program exit
if ((! isXMLFileExist()) && (! createXMLFile()))
if (!_userDefault)
{
return nullptr;
}
initXMLFilePath();
// only create xml file one time
// the file exists after the program exit
if ((!isXMLFileExist()) && (!createXMLFile()))
{
return nullptr;
}
if (! _userDefault)
{
_userDefault = new (std::nothrow) UserDefault();
}
@ -428,6 +428,15 @@ void UserDefault::destroyInstance()
CC_SAFE_DELETE(_userDefault);
}
void UserDefault::setDelegate(UserDefault *delegate)
{
if (_userDefault)
delete _userDefault;
_userDefault = delegate;
}
// FIXME:: deprecated
UserDefault* UserDefault::sharedUserDefault()
{
@ -442,16 +451,7 @@ void UserDefault::purgeSharedUserDefault()
bool UserDefault::isXMLFileExist()
{
FILE *fp = fopen(_filePath.c_str(), "r");
bool bRet = false;
if (fp)
{
bRet = true;
fclose(fp);
}
return bRet;
return FileUtils::getInstance()->isFileExist(_filePath);
}
void UserDefault::initXMLFilePath()

View File

@ -64,7 +64,7 @@ public:
* @param defaultValue The default value to return if the key doesn't exist.
* @js NA
*/
bool getBoolForKey(const char* key, bool defaultValue);
virtual bool getBoolForKey(const char* key, bool defaultValue);
/**
* Get integer value by key, if the key doesn't exist, will return 0.
@ -82,7 +82,7 @@ public:
* @return Integer value of the key.
* @js NA
*/
int getIntegerForKey(const char* key, int defaultValue);
virtual int getIntegerForKey(const char* key, int defaultValue);
/**
* Get float value by key, if the key doesn't exist, will return 0.0.
@ -99,7 +99,7 @@ public:
* @return Float value of the key.
* @js NA
*/
float getFloatForKey(const char* key, float defaultValue);
virtual float getFloatForKey(const char* key, float defaultValue);
/**
* Get double value by key, if the key doesn't exist, will return 0.0.
@ -116,7 +116,7 @@ public:
* @return Double value of the key.
* @js NA
*/
double getDoubleForKey(const char* key, double defaultValue);
virtual double getDoubleForKey(const char* key, double defaultValue);
/**
* Get string value by key, if the key doesn't exist, will return an empty string.
@ -133,7 +133,7 @@ public:
* @return String value of the key.
* @js NA
*/
std::string getStringForKey(const char* key, const std::string & defaultValue);
virtual std::string getStringForKey(const char* key, const std::string & defaultValue);
/**
* Get Data value by key, if the key doesn't exist, will return an empty Data.
@ -150,7 +150,7 @@ public:
* @return Data value of the key.
* @js NA
*/
Data getDataForKey(const char* key, const Data& defaultValue);
virtual Data getDataForKey(const char* key, const Data& defaultValue);
// set value methods
@ -160,47 +160,47 @@ public:
* @param value A bool value to set to the key.
* @js NA
*/
void setBoolForKey(const char* key, bool value);
virtual void setBoolForKey(const char* key, bool value);
/**
* Set integer value by key.
* @param key The key to set.
* @param value A integer value to set to the key.
* @js NA
*/
void setIntegerForKey(const char* key, int value);
virtual void setIntegerForKey(const char* key, int value);
/**
* Set float value by key.
* @param key The key to set.
* @param value A float value to set to the key.
* @js NA
*/
void setFloatForKey(const char* key, float value);
virtual void setFloatForKey(const char* key, float value);
/**
* Set double value by key.
* @param key The key to set.
* @param value A double value to set to the key.
* @js NA
*/
void setDoubleForKey(const char* key, double value);
virtual void setDoubleForKey(const char* key, double value);
/**
* Set string value by key.
* @param key The key to set.
* @param value A string value to set to the key.
* @js NA
*/
void setStringForKey(const char* key, const std::string & value);
virtual void setStringForKey(const char* key, const std::string & value);
/**
* Set Data value by key.
* @param key The key to set.
* @param value A Data value to set to the key.
* @js NA
*/
void setDataForKey(const char* key, const Data& value);
virtual void setDataForKey(const char* key, const Data& value);
/**
* You should invoke this function to save values set by setXXXForKey().
* @js NA
*/
void flush();
virtual void flush();
/** Returns the singleton.
* @js NA
@ -212,6 +212,18 @@ public:
*/
static void destroyInstance();
/**
* You can inherit from platform dependent implementation of UserDefault, such as UserDefaultAndroid,
* and use this function to set delegate, then UserDefault will invoke delegate's implementation.
* For example, your store native data base or other format store.
*
* If you don't want to system default implementation after setting delegate, you can just pass nullptr
* to this function.
*
* @warm It will delete previous delegate
*/
static void setDelegate(UserDefault *delegate);
/** @deprecated Use getInstace() instead.
* @js NA
* @lua NA
@ -231,10 +243,12 @@ public:
*/
static bool isXMLFileExist();
private:
protected:
UserDefault();
~UserDefault();
private:
static bool createXMLFile();
static void initXMLFilePath();