test: add test for carton binstore
- rename serialize namespace to serdes. - fix some compile issue. - add test for carton binstore
This commit is contained in:
@@ -1,5 +1,8 @@
|
||||
#pragma once
|
||||
#include "binstore/types.hpp"
|
||||
#include "binstore/serdes.hpp"
|
||||
#include "binstore/setting.hpp"
|
||||
#include "binstore/configuration.hpp"
|
||||
#include "binstore/storage.hpp"
|
||||
|
||||
namespace yycc::carton::binstore {
|
||||
|
||||
}
|
||||
namespace yycc::carton::binstore {}
|
||||
|
||||
@@ -5,11 +5,13 @@
|
||||
#include <string>
|
||||
#include <limits>
|
||||
#include <concepts>
|
||||
#include <stdexcept>
|
||||
#include <cmath>
|
||||
#include <type_traits>
|
||||
|
||||
#define NS_YYCC_BINSTORE_TYPES ::yycc::carton::binstore::types
|
||||
|
||||
namespace yycc::carton::binstore::serializer {
|
||||
namespace yycc::carton::binstore::serdes {
|
||||
|
||||
// TODO: Support numeric list and string list SerDes.
|
||||
|
||||
@@ -120,6 +122,33 @@ namespace yycc::carton::binstore::serializer {
|
||||
NS_YYCC_BINSTORE_TYPES::ByteArray reset() const { return this->serialize(TDefault).value(); }
|
||||
};
|
||||
|
||||
template<typename T, auto TDefault = static_cast<T>(0)>
|
||||
requires std::is_enum_v<T>
|
||||
struct EnumSerDes {
|
||||
EnumSerDes() = default;
|
||||
YYCC_DEFAULT_COPY_MOVE(EnumSerDes)
|
||||
|
||||
using UnderlyingType = std::underlying_type_t<T>;
|
||||
using ValueType = T;
|
||||
|
||||
std::optional<NS_YYCC_BINSTORE_TYPES::ByteArray> serialize(const ValueType& value) const {
|
||||
return inner.serialize(static_cast<UnderlyingType>(value));
|
||||
}
|
||||
|
||||
std::optional<ValueType> deserialize(const NS_YYCC_BINSTORE_TYPES::ByteArray& ba) const {
|
||||
return inner.deserialize(ba).transform([](auto v) { return static_cast<ValueType>(v); });
|
||||
}
|
||||
|
||||
NS_YYCC_BINSTORE_TYPES::ByteArray reset() const { return inner.reset(); }
|
||||
|
||||
private:
|
||||
IntegralSerDes<UnderlyingType,
|
||||
std::numeric_limits<UnderlyingType>::min(),
|
||||
std::numeric_limits<UnderlyingType>::max(),
|
||||
static_cast<UnderlyingType>(TDefault)>
|
||||
inner;
|
||||
};
|
||||
|
||||
template<bool TDefault = false>
|
||||
struct BoolSerDes {
|
||||
BoolSerDes() = default;
|
||||
@@ -195,4 +224,4 @@ namespace yycc::carton::binstore::serializer {
|
||||
NS_YYCC_BINSTORE_TYPES::ByteArray reset() const { return this->serialize(u8"").value(); }
|
||||
};
|
||||
|
||||
} // namespace yycc::carton::binstore::serializer
|
||||
} // namespace yycc::carton::binstore::serdes
|
||||
@@ -6,7 +6,7 @@
|
||||
|
||||
#define TYPES ::yycc::carton::binstore::types
|
||||
#define CFG ::yycc::carton::binstore::configuration
|
||||
#define SERDES ::yycc::carton::binstore::serializer
|
||||
#define SERDES ::yycc::carton::binstore::serdes
|
||||
#define SAFECAST ::yycc::num::safe_cast
|
||||
|
||||
namespace yycc::carton::binstore::storage {
|
||||
@@ -129,7 +129,7 @@ namespace yycc::carton::binstore::storage {
|
||||
|
||||
TYPES::BinstoreResult<void> Storage::load(std::istream& s, LoadStrategy strategy) {
|
||||
// Before loading, we need clear all stored raw data first.
|
||||
this->raws.clear();
|
||||
this->clear();
|
||||
|
||||
// Read identifier
|
||||
TYPES::VersionIdentifier version;
|
||||
@@ -171,7 +171,7 @@ namespace yycc::carton::binstore::storage {
|
||||
auto token = token_finder.value();
|
||||
|
||||
// If there is duplicated entry, report error
|
||||
if (this->raws.contains(token)) std::unexpected(TYPES::BinstoreError::DuplicatedAssign);
|
||||
if (this->raws.contains(token)) return std::unexpected(TYPES::BinstoreError::DuplicatedAssign);
|
||||
// Otherwise insert it
|
||||
this->raws.emplace(token, std::move(ba));
|
||||
}
|
||||
@@ -212,6 +212,10 @@ namespace yycc::carton::binstore::storage {
|
||||
return {};
|
||||
}
|
||||
|
||||
void Storage::clear() {
|
||||
this->raws.clear();
|
||||
}
|
||||
|
||||
bool Storage::has_setting(TYPES::Token token) const {
|
||||
return this->cfg.get_settings().has_setting(token);
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
#include "../../macro/class_copy_move.hpp"
|
||||
#include "types.hpp"
|
||||
#include "configuration.hpp"
|
||||
#include "serializer.hpp"
|
||||
#include "serdes.hpp"
|
||||
#include <map>
|
||||
#include <optional>
|
||||
#include <utility>
|
||||
@@ -13,7 +13,7 @@
|
||||
|
||||
#define NS_YYCC_BINSTORE_TYPES ::yycc::carton::binstore::types
|
||||
#define NS_YYCC_BINSTORE_CFG ::yycc::carton::binstore::configuration
|
||||
#define NS_YYCC_BINSTORE_SERDES ::yycc::carton::binstore::serializer
|
||||
#define NS_YYCC_BINSTORE_SERDES ::yycc::carton::binstore::serdes
|
||||
|
||||
namespace yycc::carton::binstore::storage {
|
||||
|
||||
@@ -61,10 +61,15 @@ namespace yycc::carton::binstore::storage {
|
||||
YYCC_DEFAULT_COPY_MOVE(Storage)
|
||||
|
||||
public:
|
||||
NS_YYCC_BINSTORE_TYPES::BinstoreResult<void> load_from_file(const std::filesystem::path& fpath, LoadStrategy strategy);
|
||||
NS_YYCC_BINSTORE_TYPES::BinstoreResult<void> load(std::istream& s, LoadStrategy strategy);
|
||||
NS_YYCC_BINSTORE_TYPES::BinstoreResult<void> load_from_file(const std::filesystem::path& fpath, LoadStrategy strategy = LoadStrategy::MigrateOld);
|
||||
NS_YYCC_BINSTORE_TYPES::BinstoreResult<void> load(std::istream& s, LoadStrategy strategy = LoadStrategy::MigrateOld);
|
||||
NS_YYCC_BINSTORE_TYPES::BinstoreResult<void> save_into_file(const std::filesystem::path& fpath);
|
||||
NS_YYCC_BINSTORE_TYPES::BinstoreResult<void> save(std::ostream& s);
|
||||
/**
|
||||
* @brief Clear all raw data saved in internal cache.
|
||||
* @details This will cause every setting was set in default value when user fetching them.
|
||||
*/
|
||||
void clear();
|
||||
|
||||
private:
|
||||
/**
|
||||
@@ -129,7 +134,7 @@ namespace yycc::carton::binstore::storage {
|
||||
// Get raw value.
|
||||
const auto& ba = this->get_raw_value(token);
|
||||
// Try to deserialize it.
|
||||
auto value = serdes.deserialize(raw_value);
|
||||
auto value = serdes.deserialize(ba);
|
||||
// If the result is okey, return it.
|
||||
if (value.has_value()) {
|
||||
return value.value();
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
#include <string_view>
|
||||
#include <limits>
|
||||
#include <concepts>
|
||||
#include <stdexcept>
|
||||
#include <cmath>
|
||||
|
||||
#define NS_YYCC_NUM_PARSE ::yycc::num::parse
|
||||
|
||||
Reference in New Issue
Block a user