序言
在开发的过程中,我们会遇到这样的一些需求:
- 用户注册完账号(如:UserAccount),在下次登录的时候,能够默认显示该登录的账户
- 用户设置了禁止音效、禁止背景音乐等内容,在下次登录的时候,希望能够保持设置
于是,在开发的过程中,我们将这一类需求,抽象成:
本地数据存储功能
Cocos2dx+Lua引擎中的UserDefault类,就为我们实现了其中一类数据的存储。
UserDefault 类详解
下面,将围绕2点进行展开:
- UserDefault类的功能;
- UserDefault类的定义与实现;
功能
-
UserDefault 类在本地(指手机writable path下)创建一个
UserDefault.xml
文件,来作为存储的数据库。 -
UserDefault.xml
可以存储以下的数据类型:布尔类型、整型、单精度浮点型、双精度浮点型、字符串类型、Data类型。 - UserDefault 提供了字符串为关键字的写入和读取的API接口。
类定义与实现
类的定义
头文件UserDefault.h如下:
#ifndef __SUPPORT_CCUSERDEFAULT_H__
#define __SUPPORT_CCUSERDEFAULT_H__
#include "platform/CCPlatformMacros.h"
#include <string>
#include "base/CCData.h"
/**
* @addtogroup base
* @{
*/
NS_CC_BEGIN
/**
* UserDefault 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 UserDefault
{
public:
// get value methods
/**
* 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);
/**
* 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.
* @js NA
*/
virtual bool getBoolForKey(const char* key, bool defaultValue);
/**
* 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);
/**
* 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.
* @js NA
*/
virtual int getIntegerForKey(const char* key, int defaultValue);
/**
* 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);
/**
* 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.
* @js NA
*/
virtual float getFloatForKey(const char* key, float defaultValue);
/**
* 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);
/**
* 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.
* @js NA
*/
virtual double getDoubleForKey(const char* key, double defaultValue);
/**
* 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);
/**
* 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.
* @js NA
*/
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.
* @param key The key to get value.
* @return Data value of the key.
* @js NA
*/
Data getDataForKey(const char* key);
/**
* 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.
* @js NA
*/
virtual Data getDataForKey(const char* key, const Data& defaultValue);
// set value methods
/**
* Set bool value by key.
* @param key The key to set.
* @param value A bool value to set to the key.
* @js NA
*/
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
*/
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
*/
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
*/
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
*/
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
*/
virtual void setDataForKey(const char* key, const Data& value);
/**
* You should invoke this function to save values set by setXXXForKey().
* @js NA
*/
virtual void flush();
/**
* delete any value by key,
* @param key The key to delete value.
* @js NA
*/
virtual void deleteValueForKey(const char* key);
/** Returns the singleton.
* @js NA
* @lua NA
*/
static UserDefault* getInstance();
/**
* @js NA
*/
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.
*
* @warning It will delete previous delegate
*/
static void setDelegate(UserDefault *delegate);
/** @deprecated Use getInstace() instead.
* @js NA
* @lua NA
*/
CC_DEPRECATED_ATTRIBUTE static UserDefault* sharedUserDefault();
/**@deprecated Use destroyInstance() instead.
* @js NA
*/
CC_DEPRECATED_ATTRIBUTE static void purgeSharedUserDefault();
/** All supported platforms other iOS & Android use xml file to save values. This function is return the file path of the xml path.
* @js NA
*/
static const std::string& getXMLFilePath();
/** All supported platforms other iOS & Android and CC_PLATFORM_WINRT use xml file to save values. This function checks whether the xml file exists or not.
* @return True if the xml file exists, false if not.
* @js NA
*/
static bool isXMLFileExist();
protected:
UserDefault();
virtual ~UserDefault();
private:
static bool createXMLFile();
static void initXMLFilePath();
static UserDefault* _userDefault;
static std::string _filePath;
static bool _isFilePathInitialized;
};
NS_CC_END
// end of base group
/** @} */
#endif // __SUPPORT_CCUSERDEFAULT_H__
为了方便起见,我们可以查看类视图。
UserDefault.png
其中,方法名左侧有一个图标,它有三种不同的样式:
Public样式(图标右下角为空)
Public
Protected样式(图标右下角为星号)
Protected
Private样式(图标右下角为锁)
Private
字段的左侧图标,同理。
类的字段
类的Static字段初始化
UserDefault* UserDefault::_userDefault = nullptr;
string UserDefault::_filePath = string("");
bool UserDefault::_isFilePathInitialized = false;
- _userDefault : UserDefault是一个单例,类的实例存储在_userDefault字段中
-
_filePath : 记录
UserDefault.xml
文件所在路径(此处初始化为可读写路径/UserDefault.xml) - _isFilePathInitialized:标记_filePath是否被初始化
类的方法
类的方法,其作用,在UserDefault中说的非常明白,这里不做重复的阐述。
只做一些补充:
- 构造函数和析构函数放空,没有业务逻辑,被声明为Protected,可以被子类继承,进行功能扩展。
类的实现:
- 类的实现过程中,构建了特定结构的xml表:
<?xml version="1.0" encoding="UTF-8"?>
<userDefaultRoot>
<Key1>Value1</Key1>
<Key2>Value2</Key1>
<Key3>Value3</Key1>
</userDefaultRoot>
- 类中提供了6类数据结构的读取和写入接口,其实现,都是利用了tinyxml2类来操作规定了存储结构的xml文件,对返回的数据做类型转换。
tolua
在lua_cocos2dx_auto.cpp
文件中,注册了lua端调用此类的接口
int lua_register_cocos2dx_UserDefault(lua_State* tolua_S)
{
tolua_usertype(tolua_S,"cc.UserDefault");
tolua_cclass(tolua_S,"UserDefault","cc.UserDefault","",nullptr);
tolua_beginmodule(tolua_S,"UserDefault");
tolua_function(tolua_S,"setIntegerForKey",lua_cocos2dx_UserDefault_setIntegerForKey);
tolua_function(tolua_S,"deleteValueForKey",lua_cocos2dx_UserDefault_deleteValueForKey);
tolua_function(tolua_S,"getFloatForKey",lua_cocos2dx_UserDefault_getFloatForKey);
tolua_function(tolua_S,"getBoolForKey",lua_cocos2dx_UserDefault_getBoolForKey);
tolua_function(tolua_S,"setDoubleForKey",lua_cocos2dx_UserDefault_setDoubleForKey);
tolua_function(tolua_S,"setFloatForKey",lua_cocos2dx_UserDefault_setFloatForKey);
tolua_function(tolua_S,"getStringForKey",lua_cocos2dx_UserDefault_getStringForKey);
tolua_function(tolua_S,"setStringForKey",lua_cocos2dx_UserDefault_setStringForKey);
tolua_function(tolua_S,"flush",lua_cocos2dx_UserDefault_flush);
tolua_function(tolua_S,"getIntegerForKey",lua_cocos2dx_UserDefault_getIntegerForKey);
tolua_function(tolua_S,"getDoubleForKey",lua_cocos2dx_UserDefault_getDoubleForKey);
tolua_function(tolua_S,"setBoolForKey",lua_cocos2dx_UserDefault_setBoolForKey);
tolua_function(tolua_S,"destroyInstance", lua_cocos2dx_UserDefault_destroyInstance);
tolua_function(tolua_S,"getXMLFilePath", lua_cocos2dx_UserDefault_getXMLFilePath);
tolua_function(tolua_S,"isXMLFileExist", lua_cocos2dx_UserDefault_isXMLFileExist);
tolua_endmodule(tolua_S);
std::string typeName = typeid(cocos2d::UserDefault).name();
g_luaType[typeName] = "cc.UserDefault";
g_typeCast["UserDefault"] = "cc.UserDefault";
return 1;
}
总结
在通读这个类后,我们完成了UserDefault类的源代码阅读。
网友评论