Magic_Game/src/kiwano/utils/LocalStorage.h

164 lines
5.1 KiB
C
Raw Normal View History

2019-04-11 14:40:54 +08:00
// Copyright (c) 2016-2018 Kiwano - Nomango
2020-01-21 10:09:55 +08:00
//
// 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:
2020-01-21 10:09:55 +08:00
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
2020-01-21 10:09:55 +08:00
//
// 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.
#pragma once
2020-01-17 16:55:47 +08:00
#include <kiwano/core/Common.h>
2020-02-06 16:54:47 +08:00
#include <kiwano/core/ObjectBase.h>
2019-04-11 14:40:54 +08:00
namespace kiwano
{
2020-02-06 16:54:47 +08:00
KGE_DECLARE_SMART_PTR(LocalStorage);
2020-01-21 10:09:55 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 本地存储
/// @details LocalStorage是一个简易的持久化工具存放字符串-值)的键值对
/// 支持的数据类型包括 (bool | int | float | double | String)
/// 例如, 保存游戏最高分, 以便下次进行游戏时读取:
2020-01-21 10:09:55 +08:00
/// @code
2020-02-10 17:32:04 +08:00
/// LocalStorage data; // 创建数据对象
/// data.SaveInt("best-score", 20); // 保存最高分 20
/// int best = data.GetInt("best-score"); // 读取之前储存的最高分
2020-01-21 10:09:55 +08:00
/// @endcode
2020-02-06 16:54:47 +08:00
class KGE_API LocalStorage : public virtual ObjectBase
2020-01-21 10:09:55 +08:00
{
public:
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 构建本地存储对象
/// @param file_path 文件储存路径
/// @param field 字段名
2020-02-10 13:47:00 +08:00
LocalStorage(String const& file_path = "data.ini", String const& field = "defalut");
2020-01-21 10:09:55 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 获取文件储存路径
2020-01-21 10:09:55 +08:00
String const& GetFilePath() const;
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 设置文件储存路径
2020-01-21 10:09:55 +08:00
void SetFilePath(String const& file_path);
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 获取字段名
2020-01-21 10:09:55 +08:00
String const& GetFieldName() const;
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 设置字段名
2020-01-21 10:09:55 +08:00
void SetFieldName(String const& field);
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 判断键对应的数据是否存在
2020-01-21 10:09:55 +08:00
bool Exists(String const& key) const;
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 保存 int 类型的值
/// @param key 键
/// @param val 值
/// @return 操作是否成功
2020-01-21 10:09:55 +08:00
bool SaveInt(String const& key, int val) const;
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 保存 float 类型的值
/// @param key 键
/// @param val 值
/// @return 操作是否成功
2020-01-21 10:09:55 +08:00
bool SaveFloat(String const& key, float val) const;
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 保存 double 类型的值
/// @param key 键
/// @param val 值
/// @return 操作是否成功
2020-01-21 10:09:55 +08:00
bool SaveDouble(String const& key, double val) const;
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 保存 bool 类型的值
/// @param key 键
/// @param val 值
/// @return 操作是否成功
2020-01-21 10:09:55 +08:00
bool SaveBool(String const& key, bool val) const;
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 保存 String 类型的值
/// @param key 键
/// @param val 值
/// @return 操作是否成功
2020-01-21 10:09:55 +08:00
bool SaveString(String const& key, String const& val) const;
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 获取 int 类型的值
/// @param key 键
/// @param default_value 值不存在时返回的默认值
/// @return 值
2020-01-21 10:09:55 +08:00
int GetInt(String const& key, int default_value = 0) const;
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 获取 float 类型的值
/// @param key 键
/// @param default_value 值不存在时返回的默认值
/// @return 值
2020-01-21 10:09:55 +08:00
float GetFloat(String const& key, float default_value = 0.0f) const;
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 获取 double 类型的值
/// @param key 键
/// @param default_value 值不存在时返回的默认值
/// @return 值
2020-01-21 10:09:55 +08:00
double GetDouble(String const& key, double default_value = 0.0) const;
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 获取 bool 类型的值
/// @param key 键
/// @param default_value 值不存在时返回的默认值
/// @return 值
2020-01-21 10:09:55 +08:00
bool GetBool(String const& key, bool default_value = false) const;
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 获取 字符串 类型的值
/// @param key 键
/// @param default_value 值不存在时返回的默认值
/// @return 值
2020-01-21 10:09:55 +08:00
String GetString(String const& key, String const& default_value = String()) const;
private:
String file_path_;
String field_name_;
};
inline String const& LocalStorage::GetFilePath() const
{
return file_path_;
}
inline String const& LocalStorage::GetFieldName() const
{
return field_name_;
}
inline void LocalStorage::SetFilePath(String const& file_path)
{
file_path_ = file_path;
}
inline void LocalStorage::SetFieldName(String const& field_name)
{
field_name_ = field_name;
}
2020-01-21 10:09:55 +08:00
} // namespace kiwano