Magic_Game/src/kiwano/utils/LocalStorage.h

131 lines
3.2 KiB
C
Raw Normal View History

2019-04-11 14:40:54 +08:00
// Copyright (c) 2016-2018 Kiwano - Nomango
//
// 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.
#pragma once
2019-10-11 21:55:29 +08:00
#include <kiwano/macros.h>
#include <kiwano/core/core.h>
2019-04-11 14:40:54 +08:00
namespace kiwano
{
//
2019-10-11 21:12:29 +08:00
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݴ<EFBFBD>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD>
// LocalStorage <20><><EFBFBD><EFBFBD><EFBFBD>ڱ<EFBFBD><DAB1>ش<EFBFBD>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD>, ֧<>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ͱ<EFBFBD><CDB0><EFBFBD> (bool | int | float | double | String)
// <20><><EFBFBD>ݶ<EFBFBD><DDB6><EFBFBD><EFBFBD><EFBFBD> key-value (<28><>-ֵ) <20>ķ<EFBFBD>ʽ<EFBFBD><CABD>ȡ
// <20><><EFBFBD><EFBFBD>, <20><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD>Ϸ<EFBFBD><CFB7><EFBFBD>߷<EFBFBD>, <20>Ա<EFBFBD><D4B1>´ν<C2B4><CEBD><EFBFBD><EFBFBD><EFBFBD>Ϸʱ<CFB7><CAB1>ȡ:
2019-10-11 21:12:29 +08:00
// LocalStorage data; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݶ<EFBFBD><DDB6><EFBFBD>
// data.SaveInt(L"best-score", 20); // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>߷<EFBFBD> 20
// int best = data.GetInt(L"best-score"); // <20><>ȡ֮ǰ<D6AE><C7B0><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>߷<EFBFBD>
//
2019-10-11 21:12:29 +08:00
class KGE_API LocalStorage
{
public:
2019-10-11 21:12:29 +08:00
LocalStorage(
String const& file_path = L"./data.ini", // <20>ļ<EFBFBD>·<EFBFBD><C2B7>
String const& field = L"defalut" // <20>ֶ<EFBFBD><D6B6><EFBFBD>
);
// <20><>ȡ<EFBFBD><C8A1><EFBFBD>ݴ<EFBFBD><DDB4><EFBFBD>·<EFBFBD><C2B7>
inline String const& GetFilePath() const { return file_path_; }
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݴ<EFBFBD><DDB4><EFBFBD>·<EFBFBD><C2B7>
void SetFilePath(
String const& file_path
);
// <20><>ȡ<EFBFBD>ֶ<EFBFBD><D6B6><EFBFBD>
inline String const& GetFieldName() const { return field_name_; }
// <20><><EFBFBD><EFBFBD><EFBFBD>ֶ<EFBFBD><D6B6><EFBFBD>
void SetFieldName(
String const& field
);
// <20>ж<EFBFBD><D0B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD>
bool Exists(
String const& key
) const;
2019-09-29 22:23:13 +08:00
// <20><><EFBFBD><EFBFBD> int <20><><EFBFBD>͵<EFBFBD>ֵ
bool SaveInt(
String const& key,
2019-09-29 22:23:13 +08:00
int val
) const;
// <20><><EFBFBD><EFBFBD> float <20><><EFBFBD>͵<EFBFBD>ֵ
bool SaveFloat(
String const& key,
2019-09-29 22:23:13 +08:00
float val
) const;
2019-09-29 22:23:13 +08:00
// <20><><EFBFBD><EFBFBD> double <20><><EFBFBD>͵<EFBFBD>ֵ
bool SaveDouble(
String const& key,
2019-09-29 22:23:13 +08:00
double val
) const;
// <20><><EFBFBD><EFBFBD> bool <20><><EFBFBD>͵<EFBFBD>ֵ
bool SaveBool(
String const& key,
bool val
) const;
// <20><><EFBFBD><EFBFBD> String <20><><EFBFBD>͵<EFBFBD>ֵ
bool SaveString(
String const& key,
String const& val
) const;
2019-09-29 22:23:13 +08:00
// <20><>ȡ int <20><><EFBFBD>͵<EFBFBD>ֵ
int GetInt(
String const& key,
2019-09-29 22:23:13 +08:00
int default_value = 0
) const;
// <20><>ȡ float <20><><EFBFBD>͵<EFBFBD>ֵ
2019-09-29 22:23:13 +08:00
float GetFloat(
String const& key,
2019-09-29 22:23:13 +08:00
float default_value = 0.0f
) const;
2019-09-29 22:23:13 +08:00
// <20><>ȡ double <20><><EFBFBD>͵<EFBFBD>ֵ
double GetDouble(
String const& key,
2019-09-29 22:23:13 +08:00
double default_value = 0.0
) const;
// <20><>ȡ bool <20><><EFBFBD>͵<EFBFBD>ֵ
bool GetBool(
String const& key,
bool default_value = false
) const;
// <20><>ȡ <20>ַ<EFBFBD><D6B7><EFBFBD> <20><><EFBFBD>͵<EFBFBD>ֵ
String GetString(
String const& key,
2019-08-18 22:49:44 +08:00
String const& default_value = String()
) const;
protected:
String file_path_;
String field_name_;
};
}