From 9af2fed46f110b01987bc300d8c7685ddd199ab9 Mon Sep 17 00:00:00 2001 From: Patrick Urbanke Date: Sun, 31 Mar 2024 14:36:59 +0200 Subject: [PATCH] Allow empty Literals --- include/rfl/Literal.hpp | 45 +++++++++++++++++++++++------------------ 1 file changed, 25 insertions(+), 20 deletions(-) diff --git a/include/rfl/Literal.hpp b/include/rfl/Literal.hpp index e772a1b7..bc99fa1f 100644 --- a/include/rfl/Literal.hpp +++ b/include/rfl/Literal.hpp @@ -47,7 +47,7 @@ class Literal { /// A single-field literal is special because it /// can also have a default constructor. template > + typename = std::enable_if_t> Literal() : value_(0) {} ~Literal() = default; @@ -81,7 +81,11 @@ class Literal { } /// Determines whether the literal contains the string. - static bool contains(const std::string& _str) { return has_value(_str); } + static bool contains(const std::string& _str) { + bool found = false; + has_value(_str, &found); + return found; + } /// Determines whether the literal contains the string at compile time. template @@ -280,37 +284,38 @@ class Literal { /// Finds the value of a string literal at compile time. template static constexpr int find_value_of() { - using FieldType = typename std::tuple_element<_i, FieldsType>::type; - if constexpr (FieldType::field_ == _name) { - return _i; - } else if constexpr (_i + 1 < num_fields_) { - return find_value_of<_name, _i + 1>(); - } else { + if constexpr (_i == num_fields_) { return -1; + } else { + using FieldType = typename std::tuple_element<_i, FieldsType>::type; + if constexpr (FieldType::field_ == _name) { + return _i; + } else { + return find_value_of<_name, _i + 1>(); + } } } /// Whether the literal contains this string. template - static bool has_value(const std::string& _str) { - using FieldType = typename std::tuple_element<_i, FieldsType>::type; - if (FieldType::field_.str() == _str) { - return true; - } - if constexpr (_i + 1 == num_fields_) { - return false; + static void has_value(const std::string& _str, bool* _found) { + if constexpr (_i == num_fields_) { + *_found = false; + return; } else { - return has_value<_i + 1>(_str); + using FieldType = typename std::tuple_element<_i, FieldsType>::type; + if (FieldType::field_.str() == _str) { + *_found = true; + return; + } + return has_value<_i + 1>(_str, _found); } } - static_assert(sizeof...(fields_) > 0, - "There must be at least one field in a Literal."); - static_assert(sizeof...(fields_) <= std::numeric_limits::max(), "Too many fields."); - static_assert(!has_duplicates(), + static_assert(sizeof...(fields_) <= 1 || !has_duplicates(), "Duplicate strings are not allowed in a Literal."); private: