2011-05-06 15:31:51 +08:00
|
|
|
/****************************************************************************
|
2012-09-24 21:22:20 +08:00
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
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__
|
|
|
|
|
2012-06-19 13:50:11 +08:00
|
|
|
#include "platform/CCPlatformMacros.h"
|
2012-04-19 14:35:52 +08:00
|
|
|
#include <string>
|
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
2012-06-20 18:09:11 +08:00
|
|
|
/**
|
|
|
|
* @addtogroup data_storage
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
/**
|
|
|
|
* CCUserDefault acts as a tiny database. You can save and get base type values by it.
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
class CC_DLL CCUserDefault
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
~CCUserDefault();
|
|
|
|
|
|
|
|
// get value methods
|
|
|
|
|
|
|
|
/**
|
|
|
|
@brief Get bool value by key, if the key doesn't exist, a default value will return.
|
|
|
|
You can set the default value, or it is false.
|
|
|
|
*/
|
2012-11-16 14:11:21 +08:00
|
|
|
bool getBoolForKey(const char* pKey);
|
|
|
|
bool getBoolForKey(const char* pKey, bool defaultValue);
|
2012-04-19 14:35:52 +08:00
|
|
|
/**
|
|
|
|
@brief Get integer value by key, if the key doesn't exist, a default value will return.
|
|
|
|
You can set the default value, or it is 0.
|
|
|
|
*/
|
2012-11-16 14:11:21 +08:00
|
|
|
int getIntegerForKey(const char* pKey);
|
|
|
|
int getIntegerForKey(const char* pKey, int defaultValue);
|
2012-04-19 14:35:52 +08:00
|
|
|
/**
|
|
|
|
@brief Get float value by key, if the key doesn't exist, a default value will return.
|
|
|
|
You can set the default value, or it is 0.0f.
|
|
|
|
*/
|
2012-11-16 14:11:21 +08:00
|
|
|
float getFloatForKey(const char* pKey);
|
|
|
|
float getFloatForKey(const char* pKey, float defaultValue);
|
2012-04-19 14:35:52 +08:00
|
|
|
/**
|
|
|
|
@brief Get double value by key, if the key doesn't exist, a default value will return.
|
|
|
|
You can set the default value, or it is 0.0.
|
|
|
|
*/
|
2012-11-16 14:11:21 +08:00
|
|
|
double getDoubleForKey(const char* pKey);
|
|
|
|
double getDoubleForKey(const char* pKey, double defaultValue);
|
2012-04-19 14:35:52 +08:00
|
|
|
/**
|
|
|
|
@brief Get string value by key, if the key doesn't exist, a default value will return.
|
|
|
|
You can set the default value, or it is "".
|
|
|
|
*/
|
2012-11-16 14:11:21 +08:00
|
|
|
std::string getStringForKey(const char* pKey);
|
|
|
|
std::string getStringForKey(const char* pKey, const std::string & defaultValue);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// set value methods
|
|
|
|
|
|
|
|
/**
|
|
|
|
@brief Set bool value by key.
|
|
|
|
*/
|
|
|
|
void setBoolForKey(const char* pKey, bool value);
|
|
|
|
/**
|
|
|
|
@brief Set integer value by key.
|
|
|
|
*/
|
|
|
|
void setIntegerForKey(const char* pKey, int value);
|
|
|
|
/**
|
|
|
|
@brief Set float value by key.
|
|
|
|
*/
|
|
|
|
void setFloatForKey(const char* pKey, float value);
|
|
|
|
/**
|
|
|
|
@brief Set double value by key.
|
|
|
|
*/
|
|
|
|
void setDoubleForKey(const char* pKey, double value);
|
|
|
|
/**
|
|
|
|
@brief Set string value by key.
|
|
|
|
*/
|
|
|
|
void setStringForKey(const char* pKey, const std::string & value);
|
|
|
|
/**
|
|
|
|
@brief Save content to xml file
|
|
|
|
*/
|
|
|
|
void flush();
|
|
|
|
|
|
|
|
static CCUserDefault* sharedUserDefault();
|
|
|
|
static void purgeSharedUserDefault();
|
|
|
|
const static std::string& getXMLFilePath();
|
|
|
|
|
|
|
|
private:
|
|
|
|
CCUserDefault();
|
|
|
|
static bool createXMLFile();
|
|
|
|
static bool isXMLFileExist();
|
|
|
|
static void initXMLFilePath();
|
|
|
|
|
|
|
|
static CCUserDefault* m_spUserDefault;
|
|
|
|
static std::string m_sFilePath;
|
|
|
|
static bool m_sbIsFilePathInitialized;
|
|
|
|
};
|
|
|
|
|
2012-06-20 18:09:11 +08:00
|
|
|
// end of data_storage group
|
|
|
|
/// @}
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
NS_CC_END
|
|
|
|
|
|
|
|
#endif // __SUPPORT_CCUSERDEFAULT_H__
|