2024-02-07 16:14:47 +00:00
|
|
|
// This file Copyright © Mnemosyne LLC.
|
|
|
|
// It may be used under GPLv2 (SPDX: GPL-2.0-only), GPLv3 (SPDX: GPL-3.0-only),
|
|
|
|
// or any future license endorsed by Mnemosyne LLC.
|
|
|
|
// License text can be found in the licenses/ folder.
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2024-03-16 04:27:55 +00:00
|
|
|
#include <functional>
|
|
|
|
#include <typeindex>
|
|
|
|
#include <typeinfo>
|
|
|
|
#include <unordered_map>
|
2024-02-07 16:14:47 +00:00
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include "libtransmission/quark.h"
|
|
|
|
#include "libtransmission/variant.h"
|
|
|
|
|
|
|
|
namespace libtransmission
|
|
|
|
{
|
|
|
|
|
|
|
|
class Settings
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
void load(tr_variant const& src);
|
|
|
|
|
|
|
|
[[nodiscard]] tr_variant save() const;
|
|
|
|
|
|
|
|
protected:
|
2024-03-16 04:27:55 +00:00
|
|
|
Settings();
|
|
|
|
|
|
|
|
// convert from tr_variant to T
|
|
|
|
template<typename T>
|
|
|
|
using Load = bool (*)(tr_variant const& src, T* tgt);
|
|
|
|
|
|
|
|
// convert from T to tr_variant
|
|
|
|
template<typename T>
|
|
|
|
using Save = tr_variant (*)(T const& src);
|
|
|
|
|
|
|
|
template<typename T>
|
2024-03-30 17:56:48 +00:00
|
|
|
void add_type_handler(Load<T> load_handler, Save<T> save_handler)
|
2024-03-16 04:27:55 +00:00
|
|
|
{
|
|
|
|
auto const key = std::type_index(typeid(T*));
|
|
|
|
|
2024-03-30 17:56:48 +00:00
|
|
|
// wrap load_handler + save_handler with void* wrappers so that
|
2024-03-16 04:27:55 +00:00
|
|
|
// they can be stored in the save_ and load_ maps
|
2024-03-30 17:56:48 +00:00
|
|
|
load_.insert_or_assign(
|
|
|
|
key,
|
|
|
|
[load_handler](tr_variant const& src, void* tgt) { return load_handler(src, static_cast<T*>(tgt)); });
|
|
|
|
save_.insert_or_assign(key, [save_handler](void const* src) { return save_handler(*static_cast<T const*>(src)); });
|
2024-03-16 04:27:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct Field
|
|
|
|
{
|
|
|
|
template<typename T>
|
|
|
|
Field(tr_quark key_in, T* ptr_in)
|
|
|
|
: key{ key_in }
|
|
|
|
, type{ typeid(T*) }
|
|
|
|
, ptr{ ptr_in }
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
tr_quark key;
|
|
|
|
std::type_info const& type;
|
|
|
|
void* ptr;
|
|
|
|
};
|
|
|
|
|
|
|
|
using Fields = std::vector<Field>;
|
2024-02-07 16:14:47 +00:00
|
|
|
|
|
|
|
[[nodiscard]] virtual Fields fields() = 0;
|
2024-03-16 04:27:55 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
std::unordered_map<std::type_index, std::function<tr_variant(void const* src)>> save_;
|
|
|
|
std::unordered_map<std::type_index, std::function<bool(tr_variant const& src, void* tgt)>> load_;
|
2024-02-07 16:14:47 +00:00
|
|
|
};
|
|
|
|
} // namespace libtransmission
|