Skip to content
Snippets Groups Projects

algo: Allow reading enums as strings from YAML.

Merged Felix Weiglhofer requested to merge fweig/cbmroot:algo-enums into master
Files
6
+ 11
1
@@ -13,6 +13,7 @@
#include "BaseTypes.h"
#include "Prelude.h"
#include "Property.h"
#include "util/EnumDict.h"
namespace cbm::algo::config
{
@@ -28,10 +29,14 @@ namespace cbm::algo::config
{
using Type = std::remove_cv_t<std::remove_reference_t<T>>;
static_assert(!IsEnum<T> || detail::EnumHasDict_v<T>, "Enum must have a dictionary to be deserializable");
// TODO: error handling
if constexpr (IsBaseType<Type>) { return node.as<Type>(); }
else if constexpr (IsEnum<Type>) {
return static_cast<Type>(node.as<std::underlying_type_t<Type>>());
std::optional<T> maybet = FromString<T>(node.as<std::string>());
if (!maybet) { throw std::runtime_error(fmt::format("Invalid enum value: {}", node.as<std::string>())); }
return *maybet;
}
else if constexpr (IsVector<Type>) {
Type vector;
@@ -117,7 +122,12 @@ namespace cbm::algo::config
template<typename T>
void DoDump(const T& object, YAML::Emitter& ss, std::optional<YAML::EMITTER_MANIP> formatEntries = {})
{
static_assert(!IsEnum<T> || detail::EnumHasDict_v<T>, "Enum must have a dictionary");
if constexpr (IsBaseType<T>) { ss << object; }
else if constexpr (IsEnum<T>) {
ss << ToString<T>(object);
}
else if constexpr (IsVector<T> || IsArray<T>) {
ss << YAML::BeginSeq;
for (const auto& element : object) {
Loading