feat: finish binstore setting
This commit is contained in:
@ -184,4 +184,5 @@ namespace yycc::carton::binstore::serializer {
|
||||
|
||||
NS_YYCC_BINSTORE_TYPES::ByteArray reset() const { return this->serialize(u8"").value(); }
|
||||
};
|
||||
|
||||
} // namespace yycc::carton::binstore::serializer
|
||||
|
||||
@ -1,3 +1,89 @@
|
||||
#pragma once
|
||||
#include "setting.hpp"
|
||||
#include "../../patch/format.hpp"
|
||||
#include <stdexcept>
|
||||
|
||||
namespace yycc::carton::binstore::setting {}
|
||||
#define TYPES ::yycc::carton::binstore::types
|
||||
#define FORMAT ::yycc::patch::format
|
||||
|
||||
namespace yycc::carton::binstore::setting {
|
||||
|
||||
#pragma region Setting Class
|
||||
|
||||
Setting::Setting(const std::u8string_view& name) : name(name) {
|
||||
if (name.empty()) {
|
||||
throw std::logic_error("the name of setting should not be empty");
|
||||
}
|
||||
}
|
||||
|
||||
Setting::~Setting() {}
|
||||
|
||||
std::u8string_view Setting::get_name() const {
|
||||
return this->name;
|
||||
}
|
||||
|
||||
#pragma endregion
|
||||
|
||||
#pragma region RegisteredSetting Class
|
||||
|
||||
RegisteredSetting::RegisteredSetting(TYPES::Token token, Setting&& setting) : token(token), setting(std::move(setting)) {}
|
||||
|
||||
RegisteredSetting::~RegisteredSetting() {}
|
||||
|
||||
TYPES::Token RegisteredSetting::get_token() const {
|
||||
return this->token;
|
||||
}
|
||||
|
||||
const Setting& RegisteredSetting::get_setting() const {
|
||||
return this->setting;
|
||||
}
|
||||
|
||||
#pragma endregion
|
||||
|
||||
#pragma region SettingCollection Class
|
||||
|
||||
SettingCollection::SettingCollection() : names(), settings() {}
|
||||
|
||||
SettingCollection::~SettingCollection() {}
|
||||
|
||||
TYPES::Token SettingCollection::add_setting(Setting&& setting) {
|
||||
auto token = this->settings.size();
|
||||
|
||||
std::u8string name(setting.get_name());
|
||||
auto [_, ok] = this->names.try_emplace(name, token);
|
||||
if (!ok) {
|
||||
throw std::logic_error(FORMAT::format("duplicated setting name {}", name));
|
||||
}
|
||||
|
||||
this->settings.emplace_back(RegisteredSetting(token, std::move(setting)));
|
||||
return token;
|
||||
}
|
||||
|
||||
std::optional<TYPES::Token> SettingCollection::find_name(const std::u8string_view& name) const {
|
||||
auto finder = this->names.find(std::u8string(name));
|
||||
if (finder == this->names.end()) return std::nullopt;
|
||||
else return finder->second;
|
||||
}
|
||||
|
||||
bool SettingCollection::has_setting(TYPES::Token token) const {
|
||||
return token < this->settings.size();
|
||||
}
|
||||
|
||||
const Setting& SettingCollection::get_setting(TYPES::Token token) const {
|
||||
return this->settings.at(token).get_setting();
|
||||
}
|
||||
|
||||
const std::vector<RegisteredSetting>& SettingCollection::all_settings() const {
|
||||
return this->settings;
|
||||
}
|
||||
|
||||
size_t SettingCollection::length() const {
|
||||
return this->settings.size();
|
||||
}
|
||||
|
||||
bool SettingCollection::empty() const {
|
||||
return this->settings.empty();
|
||||
}
|
||||
|
||||
#pragma endregion
|
||||
|
||||
} // namespace yycc::carton::binstore::setting
|
||||
@ -1,3 +1,64 @@
|
||||
#pragma once
|
||||
#include "../../macro/class_copy_move.hpp"
|
||||
#include "types.hpp"
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <vector>
|
||||
#include <map>
|
||||
#include <optional>
|
||||
|
||||
namespace yycc::carton::binstore::setting {}
|
||||
#define NS_YYCC_BINSTORE_TYPES ::yycc::carton::binstore::types
|
||||
|
||||
namespace yycc::carton::binstore::setting {
|
||||
|
||||
class Setting {
|
||||
public:
|
||||
Setting(const std::u8string_view& name);
|
||||
~Setting();
|
||||
YYCC_DEFAULT_COPY_MOVE(Setting)
|
||||
|
||||
public:
|
||||
std::u8string_view get_name() const;
|
||||
|
||||
private:
|
||||
std::u8string name;
|
||||
};
|
||||
|
||||
class RegisteredSetting {
|
||||
public:
|
||||
RegisteredSetting(NS_YYCC_BINSTORE_TYPES::Token token, Setting&& setting);
|
||||
~RegisteredSetting();
|
||||
YYCC_DEFAULT_COPY_MOVE(RegisteredSetting)
|
||||
|
||||
public:
|
||||
NS_YYCC_BINSTORE_TYPES::Token get_token() const;
|
||||
const Setting& get_setting() const;
|
||||
|
||||
private:
|
||||
NS_YYCC_BINSTORE_TYPES::Token token;
|
||||
Setting setting;
|
||||
};
|
||||
|
||||
class SettingCollection {
|
||||
public:
|
||||
SettingCollection();
|
||||
~SettingCollection();
|
||||
YYCC_DEFAULT_COPY_MOVE(SettingCollection)
|
||||
|
||||
public:
|
||||
NS_YYCC_BINSTORE_TYPES::Token add_setting(Setting&& setting);
|
||||
std::optional<NS_YYCC_BINSTORE_TYPES::Token> find_name(const std::u8string_view& name) const;
|
||||
bool has_setting(NS_YYCC_BINSTORE_TYPES::Token token) const;
|
||||
const Setting& get_setting(NS_YYCC_BINSTORE_TYPES::Token token) const;
|
||||
const std::vector<RegisteredSetting>& all_settings() const;
|
||||
size_t length() const;
|
||||
bool empty() const;
|
||||
|
||||
private:
|
||||
std::map<std::u8string, NS_YYCC_BINSTORE_TYPES::Token> names;
|
||||
std::vector<RegisteredSetting> settings;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#undef NS_YYCC_BINSTORE_TYPES
|
||||
|
||||
@ -15,6 +15,19 @@ namespace yycc::carton::binstore::types {
|
||||
template<typename T>
|
||||
using BinstoreResult = std::expected<T, BinstoreError>;
|
||||
|
||||
/**
|
||||
* @brief An unique token type.
|
||||
* @details
|
||||
* When outside code registering an setting in collection
|
||||
* there must be a token returned by manager.
|
||||
* When outside code want to visit this registered item again,
|
||||
* they should provide this token returned when registering.
|
||||
*
|
||||
* Its value actually is the index of its stored vector.
|
||||
* So this type is an alias to vector size type.
|
||||
*/
|
||||
using Token = size_t;
|
||||
|
||||
/// @brief The raw data of setting.
|
||||
class ByteArray {
|
||||
public:
|
||||
|
||||
Reference in New Issue
Block a user