2011-05-06 15:31:51 +08:00
|
|
|
/****************************************************************************
|
2012-09-24 21:22:20 +08:00
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
2014-01-07 11:25:07 +08:00
|
|
|
Copyright (c) 2013-2014 Chukong Technologies Inc.
|
2011-05-06 15:31:51 +08:00
|
|
|
|
|
|
|
http://www.cocos2d-x.org
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
2012-04-19 14:35:52 +08:00
|
|
|
****************************************************************************/
|
|
|
|
#ifndef __SUPPORT_CCUSERDEFAULT_H__
|
|
|
|
#define __SUPPORT_CCUSERDEFAULT_H__
|
|
|
|
|
2014-09-10 08:17:07 +08:00
|
|
|
#include "platform/CCPlatformMacros.h"
|
2012-04-19 14:35:52 +08:00
|
|
|
#include <string>
|
2014-04-27 01:35:57 +08:00
|
|
|
#include "base/CCData.h"
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2012-06-20 18:09:11 +08:00
|
|
|
/**
|
2015-03-24 11:15:40 +08:00
|
|
|
* @addtogroup base
|
2012-06-20 18:09:11 +08:00
|
|
|
* @{
|
|
|
|
*/
|
2015-03-27 12:01:20 +08:00
|
|
|
NS_CC_BEGIN
|
|
|
|
|
2012-06-20 18:09:11 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
/**
|
2013-06-20 14:13:12 +08:00
|
|
|
* UserDefault acts as a tiny database. You can save and get base type values by it.
|
2012-04-19 14:35:52 +08:00
|
|
|
* For example, setBoolForKey("played", true) will add a bool value true into the database.
|
|
|
|
* Its key is "played". You can get the value of the key by getBoolForKey("played").
|
|
|
|
*
|
|
|
|
* It supports the following base types:
|
|
|
|
* bool, int, float, double, string
|
|
|
|
*/
|
2013-06-20 14:13:12 +08:00
|
|
|
class CC_DLL UserDefault
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
// get value methods
|
|
|
|
|
|
|
|
/**
|
2015-03-18 21:24:31 +08:00
|
|
|
* Get bool value by key, if the key doesn't exist, will return false.
|
|
|
|
* You can set the default value, or it is false.
|
|
|
|
* @param key The key to get value.
|
|
|
|
* @return Bool value by `key`.
|
|
|
|
* @js NA
|
|
|
|
*/
|
|
|
|
bool getBoolForKey(const char* key);
|
|
|
|
|
2013-09-13 11:41:20 +08:00
|
|
|
/**
|
2015-03-18 21:24:31 +08:00
|
|
|
* Get bool value by key, if the key doesn't exist, will return passed default value.
|
|
|
|
* @param key The key to get value.
|
|
|
|
* @param defaultValue The default value to return if the key doesn't exist.
|
2013-09-13 11:41:20 +08:00
|
|
|
* @js NA
|
|
|
|
*/
|
2015-03-18 21:24:31 +08:00
|
|
|
bool getBoolForKey(const char* key, bool defaultValue);
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
/**
|
2015-03-18 21:24:31 +08:00
|
|
|
* Get integer value by key, if the key doesn't exist, will return 0.
|
|
|
|
* You can set the default value, or it is 0.
|
|
|
|
* @param key The key to get value.
|
|
|
|
* @return Integer value of the key.
|
|
|
|
* @js NA
|
|
|
|
*/
|
|
|
|
int getIntegerForKey(const char* key);
|
|
|
|
|
2013-09-13 11:41:20 +08:00
|
|
|
/**
|
2015-03-18 21:24:31 +08:00
|
|
|
* Get bool value by key, if the key doesn't exist, will return passed default value.
|
|
|
|
* @param key The key to get value.
|
|
|
|
* @param defaultValue The default value to return if the key doesn't exist.
|
|
|
|
* @return Integer value of the key.
|
2013-09-13 11:41:20 +08:00
|
|
|
* @js NA
|
|
|
|
*/
|
2015-03-18 21:24:31 +08:00
|
|
|
int getIntegerForKey(const char* key, int defaultValue);
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
/**
|
2015-03-18 21:24:31 +08:00
|
|
|
* Get float value by key, if the key doesn't exist, will return 0.0.
|
|
|
|
* @param key The key to get value.
|
|
|
|
* @return Float value of the key.
|
|
|
|
* @js NA
|
|
|
|
*/
|
|
|
|
float getFloatForKey(const char* key);
|
|
|
|
|
2013-09-13 11:41:20 +08:00
|
|
|
/**
|
2015-03-18 21:24:31 +08:00
|
|
|
* Get float value by key, if the key doesn't exist, will return passed default value.
|
|
|
|
* @param key The key to get value.
|
|
|
|
* @param defaultValue The default value to return if the key doesn't exist.
|
|
|
|
* @return Float value of the key.
|
2013-09-13 11:41:20 +08:00
|
|
|
* @js NA
|
|
|
|
*/
|
2015-03-18 21:24:31 +08:00
|
|
|
float getFloatForKey(const char* key, float defaultValue);
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
/**
|
2015-03-18 21:24:31 +08:00
|
|
|
* Get double value by key, if the key doesn't exist, will return 0.0.
|
|
|
|
* @param key The key to get value.
|
|
|
|
* @return Double value of the key.
|
|
|
|
* @js NA
|
|
|
|
*/
|
|
|
|
double getDoubleForKey(const char* key);
|
|
|
|
|
2013-09-13 11:41:20 +08:00
|
|
|
/**
|
2015-03-18 21:24:31 +08:00
|
|
|
* Get double value by key, if the key doesn't exist, will return passed default value.
|
|
|
|
* @param key The key to get value.
|
|
|
|
* @param defaultValue The default value to return if the key doesn't exist.
|
|
|
|
* @return Double value of the key.
|
2013-09-13 11:41:20 +08:00
|
|
|
* @js NA
|
|
|
|
*/
|
2015-03-18 21:24:31 +08:00
|
|
|
double getDoubleForKey(const char* key, double defaultValue);
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
/**
|
2015-03-18 21:24:31 +08:00
|
|
|
* Get string value by key, if the key doesn't exist, will return an empty string.
|
|
|
|
* @param key The key to get value.
|
|
|
|
* @return String value of the key.
|
|
|
|
* @js NA
|
|
|
|
*/
|
|
|
|
std::string getStringForKey(const char* key);
|
|
|
|
|
2013-09-13 11:41:20 +08:00
|
|
|
/**
|
2015-03-18 21:24:31 +08:00
|
|
|
* Get string value by key, if the key doesn't exist, will return passed default value.
|
|
|
|
* @param key The key to get value.
|
|
|
|
* @param defaultValue The default value to return if the key doesn't exist.
|
|
|
|
* @return String value of the key.
|
2013-09-13 11:41:20 +08:00
|
|
|
* @js NA
|
|
|
|
*/
|
2015-03-18 21:24:31 +08:00
|
|
|
std::string getStringForKey(const char* key, const std::string & defaultValue);
|
|
|
|
|
2013-05-29 08:06:41 +08:00
|
|
|
/**
|
2015-03-18 21:24:31 +08:00
|
|
|
* Get Data value by key, if the key doesn't exist, will return an empty Data.
|
|
|
|
* @param key The key to get value.
|
|
|
|
* @return Data value of the key.
|
2013-09-13 11:41:20 +08:00
|
|
|
* @js NA
|
2013-05-29 08:06:41 +08:00
|
|
|
*/
|
2015-03-18 21:24:31 +08:00
|
|
|
Data getDataForKey(const char* key);
|
|
|
|
|
2013-09-13 11:41:20 +08:00
|
|
|
/**
|
2015-03-18 21:24:31 +08:00
|
|
|
* Get Data value by key, if the key doesn't exist, will return an empty Data.
|
|
|
|
* @param key The key to get value.
|
|
|
|
* @param defaultValue The default value to return if the key doesn't exist.
|
|
|
|
* @return Data value of the key.
|
2013-09-13 11:41:20 +08:00
|
|
|
* @js NA
|
|
|
|
*/
|
2015-03-18 21:24:31 +08:00
|
|
|
Data getDataForKey(const char* key, const Data& defaultValue);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// set value methods
|
|
|
|
|
|
|
|
/**
|
2015-03-18 21:24:31 +08:00
|
|
|
* Set bool value by key.
|
|
|
|
* @param key The key to set.
|
|
|
|
* @param value A bool value to set to the key.
|
2013-09-13 11:41:20 +08:00
|
|
|
* @js NA
|
|
|
|
*/
|
2015-03-18 21:24:31 +08:00
|
|
|
void setBoolForKey(const char* key, bool value);
|
2012-04-19 14:35:52 +08:00
|
|
|
/**
|
2015-03-18 21:24:31 +08:00
|
|
|
* Set integer value by key.
|
|
|
|
* @param key The key to set.
|
|
|
|
* @param value A integer value to set to the key.
|
2013-09-13 11:41:20 +08:00
|
|
|
* @js NA
|
|
|
|
*/
|
2015-03-18 21:24:31 +08:00
|
|
|
void setIntegerForKey(const char* key, int value);
|
2012-04-19 14:35:52 +08:00
|
|
|
/**
|
2015-03-18 21:24:31 +08:00
|
|
|
* Set float value by key.
|
|
|
|
* @param key The key to set.
|
|
|
|
* @param value A float value to set to the key.
|
2013-09-13 11:41:20 +08:00
|
|
|
* @js NA
|
|
|
|
*/
|
2015-03-18 21:24:31 +08:00
|
|
|
void setFloatForKey(const char* key, float value);
|
2012-04-19 14:35:52 +08:00
|
|
|
/**
|
2015-03-18 21:24:31 +08:00
|
|
|
* Set double value by key.
|
|
|
|
* @param key The key to set.
|
|
|
|
* @param value A double value to set to the key.
|
2013-09-13 11:41:20 +08:00
|
|
|
* @js NA
|
|
|
|
*/
|
2015-03-18 21:24:31 +08:00
|
|
|
void setDoubleForKey(const char* key, double value);
|
2012-04-19 14:35:52 +08:00
|
|
|
/**
|
2015-03-18 21:24:31 +08:00
|
|
|
* Set string value by key.
|
|
|
|
* @param key The key to set.
|
|
|
|
* @param value A string value to set to the key.
|
2013-09-13 11:41:20 +08:00
|
|
|
* @js NA
|
|
|
|
*/
|
2015-03-18 21:24:31 +08:00
|
|
|
void setStringForKey(const char* key, const std::string & value);
|
2013-05-29 08:06:41 +08:00
|
|
|
/**
|
2015-03-18 21:24:31 +08:00
|
|
|
* Set Data value by key.
|
|
|
|
* @param key The key to set.
|
|
|
|
* @param value A Data value to set to the key.
|
2013-09-13 11:41:20 +08:00
|
|
|
* @js NA
|
2013-05-29 08:06:41 +08:00
|
|
|
*/
|
2015-03-18 21:24:31 +08:00
|
|
|
void setDataForKey(const char* key, const Data& value);
|
2012-04-19 14:35:52 +08:00
|
|
|
/**
|
2015-03-18 21:24:31 +08:00
|
|
|
* You should invoke this function to save values set by setXXXForKey().
|
2013-09-13 11:41:20 +08:00
|
|
|
* @js NA
|
2012-04-19 14:35:52 +08:00
|
|
|
*/
|
|
|
|
void flush();
|
|
|
|
|
2015-03-18 21:24:31 +08:00
|
|
|
/** Returns the singleton.
|
2013-09-13 11:41:20 +08:00
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
|
|
|
*/
|
2013-07-12 06:24:23 +08:00
|
|
|
static UserDefault* getInstance();
|
2013-09-13 11:41:20 +08:00
|
|
|
/**
|
|
|
|
* @js NA
|
|
|
|
*/
|
2013-07-12 06:24:23 +08:00
|
|
|
static void destroyInstance();
|
|
|
|
|
2015-03-18 21:24:31 +08:00
|
|
|
/** @deprecated Use getInstace() instead.
|
2013-09-13 11:41:20 +08:00
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
|
|
|
*/
|
2013-07-12 06:24:23 +08:00
|
|
|
CC_DEPRECATED_ATTRIBUTE static UserDefault* sharedUserDefault();
|
2015-03-18 21:24:31 +08:00
|
|
|
/**@deprecated Use destroyInstance() instead.
|
2013-09-13 11:41:20 +08:00
|
|
|
* @js NA
|
|
|
|
*/
|
2013-07-12 06:24:23 +08:00
|
|
|
CC_DEPRECATED_ATTRIBUTE static void purgeSharedUserDefault();
|
2015-03-18 21:24:31 +08:00
|
|
|
/** All supported platforms other iOS & Android use xml file to save values. This function is return the file path of the xml path.
|
2013-09-13 11:41:20 +08:00
|
|
|
* @js NA
|
|
|
|
*/
|
2014-07-10 16:50:25 +08:00
|
|
|
static const std::string& getXMLFilePath();
|
2015-03-18 21:24:31 +08:00
|
|
|
/** All supported platforms other iOS & Android use xml file to save values. This function checks whether the xml file exists or not.
|
|
|
|
* @return True if the xml file exists, flase if not.
|
2013-09-13 11:41:20 +08:00
|
|
|
* @js NA
|
|
|
|
*/
|
2013-04-08 21:35:43 +08:00
|
|
|
static bool isXMLFileExist();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
private:
|
2013-06-20 14:13:12 +08:00
|
|
|
UserDefault();
|
2013-12-20 14:06:24 +08:00
|
|
|
~UserDefault();
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
static bool createXMLFile();
|
|
|
|
static void initXMLFilePath();
|
|
|
|
|
2013-07-12 06:24:23 +08:00
|
|
|
static UserDefault* _userDefault;
|
2013-06-15 14:03:30 +08:00
|
|
|
static std::string _filePath;
|
2013-07-12 06:24:23 +08:00
|
|
|
static bool _isFilePathInitialized;
|
2012-04-19 14:35:52 +08:00
|
|
|
};
|
|
|
|
|
2012-06-20 18:09:11 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
NS_CC_END
|
2015-03-27 12:01:20 +08:00
|
|
|
// end of base group
|
|
|
|
/** @} */
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
#endif // __SUPPORT_CCUSERDEFAULT_H__
|