Compare commits
No commits in common. "c45bd6137c11a1886965d278e52f7b3de723ae1e" and "6dbd4c7cedbd058e9072f71b03bebb5120b16cce" have entirely different histories.
c45bd6137c
...
6dbd4c7ced
@ -1,12 +1,8 @@
|
||||
#pragma once
|
||||
|
||||
#include <concepts>
|
||||
#include <cstddef>
|
||||
#include <ranges>
|
||||
|
||||
#include <memory>
|
||||
#include <type_traits>
|
||||
|
||||
namespace gz::util {
|
||||
/// Satisfied when T is in PackTypes
|
||||
template<typename T, typename... PackTypes>
|
||||
@ -27,19 +23,6 @@ namespace gz::util {
|
||||
template<typename T, typename ValueType>
|
||||
concept ContiguousRange = std::ranges::contiguous_range<T> and std::same_as<std::ranges::range_value_t<T>, ValueType>;
|
||||
|
||||
/// ObjectType accessible via *t and convertible to bool
|
||||
template<typename T, typename ObjectType>
|
||||
concept IsPointerToType = requires (T t/*, std::ptrdiff_t idx*/) {
|
||||
/* { t.operator*() } -> std::same_as<std::add_lvalue_reference_t<ObjectType>>; */
|
||||
/* { t.operator->() } -> std::same_as<std::add_pointer_t<ObjectType>>; */
|
||||
/* { t.operator[](idx) } -> std::same_as<std::add_lvalue_reference_t<ObjectType>>; */
|
||||
{ *t } -> std::same_as<std::add_lvalue_reference_t<ObjectType>>;
|
||||
/* { t-> } -> std::same_as<std::add_lvalue_reference_t<ObjectType>>; */
|
||||
{ static_cast<bool>(t) };
|
||||
};
|
||||
static_assert(IsPointerToType<std::shared_ptr<int>, int>, "");
|
||||
static_assert(IsPointerToType<int*, int>, "");
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
@ -113,7 +113,7 @@ namespace gz {
|
||||
* - in SettingsManager::set: depends on throwExceptionWhenNewValueNotAllowed
|
||||
* @see sm_validity
|
||||
*/
|
||||
util::unordered_string_map<SettingsManagerAllowedValues<CacheTypes...>> allowedValues;
|
||||
/* util::unordered_string_map<SettingsManagerAllowedValues<CacheTypes...>> allowedValues; */
|
||||
/**
|
||||
* @brief Wether to throw an exception when trying to set an invalid value
|
||||
*/
|
||||
@ -488,7 +488,7 @@ namespace gz {
|
||||
writeFileOnExit = createInfo.writeFileOnExit;
|
||||
throwExceptionWhenNewValueNotAllowed = createInfo.throwExceptionWhenNewValueNotAllowed;
|
||||
|
||||
allowedValues = std::move(createInfo.allowedValues);
|
||||
/* allowedValues = std::move(createInfo.allowedValues); */
|
||||
settings = std::move(createInfo.initialValues);
|
||||
|
||||
filepath = createInfo.filepath;
|
||||
@ -497,11 +497,11 @@ namespace gz {
|
||||
}
|
||||
|
||||
// erase invalid initalValues
|
||||
for (auto it = settings.begin(); it != settings.end(); it++) {
|
||||
if (!isValueAllowed<std::string>(it->first, it->second)) {
|
||||
it = settings.erase(it);
|
||||
}
|
||||
}
|
||||
/* for (auto it = settings.begin(); it != settings.end(); it++) { */
|
||||
/* if (!isValueAllowed<std::string>(it->first, it->second)) { */
|
||||
/* it = settings.erase(it); */
|
||||
/* } */
|
||||
/* } */
|
||||
|
||||
initCache<void, CacheTypes...>();
|
||||
}
|
||||
@ -541,12 +541,18 @@ namespace gz {
|
||||
template<StringConvertible... CacheTypes>
|
||||
template<util::IsInPack<CacheTypes...> T>
|
||||
const T& SettingsManager<CacheTypes...>::get(const std::string& key) {
|
||||
static_assert(util::IsInPack<T, CacheTypes...>, "Type T is not in parameter pack CacheTypes...");
|
||||
/* if (!isRegisteredType<T>()) { */
|
||||
/* throw InvalidType("Invalid type: '" + std::string(typeid(T).name()) + "'", "SettingsManager::get"); */
|
||||
/* } */
|
||||
if (!settings.contains(key)) {
|
||||
throw InvalidArgument("Invalid key: '" + key + "'", "SettingsManager::get");
|
||||
}
|
||||
// if not cached -> cache
|
||||
if (!settingsCache[typeid(T).name()].contains(key)) {
|
||||
try {
|
||||
/* settingsCache[typeid(T).name()][key].emplace(fromString<T>(settings[key])); */
|
||||
/* settingsCache[typeid(T).name()][key](fromString<T>(settings[key])); */
|
||||
settingsCache[typeid(T).name()][key] = std::variant<std::monostate, CacheTypes...>(fromString<T>(settings[key]));
|
||||
}
|
||||
catch (...) {
|
||||
@ -575,6 +581,9 @@ namespace gz {
|
||||
template<StringConvertible... CacheTypes>
|
||||
template<util::IsInPack<CacheTypes...> T>
|
||||
const T& SettingsManager<CacheTypes...>::getOr(const std::string& key, const T& fallback) requires std::copy_constructible<T> {
|
||||
/* if (!isRegisteredType<T>()) { */
|
||||
/* throw InvalidType("Invalid type: '" + std::string(typeid(T).name()) + "'", "SettingsManager::getOr"); */
|
||||
/* } */
|
||||
if (settings.contains(key)) {
|
||||
return get<T>(key);
|
||||
}
|
||||
@ -653,6 +662,9 @@ namespace gz {
|
||||
template<StringConvertible... CacheTypes>
|
||||
template<util::IsInPack<CacheTypes...> T>
|
||||
void SettingsManager<CacheTypes...>::set(const std::string& key, const T& value) {
|
||||
/* if (!isRegisteredType<T>()) { */
|
||||
/* throw InvalidType("Invalid type: '" + std::string(typeid(T).name()) + "'", "SettingsManager::set<" + std::string(typeid(T).name()) + ">"); */
|
||||
/* } */
|
||||
// convert to string
|
||||
std::string s;
|
||||
try {
|
||||
@ -699,6 +711,44 @@ namespace gz {
|
||||
//
|
||||
// ALLOWED VALUES
|
||||
//
|
||||
/* template<StringConvertible... CacheTypes> */
|
||||
/* template<IntegralInPack<std::string, CacheTypes...> T> */
|
||||
/* bool SettingsManager<CacheTypes...>::isValueAllowed(const std::string& key, const T& value) const noexcept { */
|
||||
/* if (!allowedValues.contains(key)) { */
|
||||
/* return true; */
|
||||
/* } */
|
||||
/* const std::vector<T>* av = nullptr; */
|
||||
/* try { */
|
||||
/* av = &std::get<std::vector<T>>(allowedValues.at(key).allowedValues); */
|
||||
/* } */
|
||||
/* catch (std::bad_variant_access&) { */
|
||||
/* return false; */
|
||||
/* } */
|
||||
|
||||
/* switch (allowedValues.at(key).type) { */
|
||||
/* case SM_LIST: { */
|
||||
/* for (auto it = av->begin(); it != av->end(); it++) { */
|
||||
/* if (*it == value) { */
|
||||
/* return true; */
|
||||
/* } */
|
||||
/* } */
|
||||
/* } */
|
||||
/* break; */
|
||||
/* case SM_RANGE: { */
|
||||
/* bool valid = true; */
|
||||
/* // value >= lowest */
|
||||
/* valid &= value >= av->at(0); */
|
||||
/* // value < highest */
|
||||
/* valid &= value < av->at(1); */
|
||||
/* // value == lowest + n * step */
|
||||
/* valid &= (value - av->at(0)) % av->at(2) == 0; */
|
||||
/* return valid; */
|
||||
/* } */
|
||||
/* break; */
|
||||
/* } // switch */
|
||||
/* return false; */
|
||||
/* } */
|
||||
|
||||
template<StringConvertible... CacheTypes>
|
||||
template<NumberInPack<std::string, CacheTypes...> T>
|
||||
bool SettingsManager<CacheTypes...>::isValueAllowed(const std::string& key, const T& value) const noexcept {
|
||||
@ -734,7 +784,6 @@ namespace gz {
|
||||
} // switch
|
||||
return false;
|
||||
}
|
||||
|
||||
template<StringConvertible... CacheTypes>
|
||||
template<NotNumberInPack<std::string, CacheTypes...> T>
|
||||
bool SettingsManager<CacheTypes...>::isValueAllowed(const std::string& key, const T& value) const noexcept {
|
||||
|
Loading…
Reference in New Issue
Block a user