From df7a48708749345dd607f39902964ed59345f192 Mon Sep 17 00:00:00 2001 From: Nils Schubert Date: Thu, 19 Sep 2024 13:21:06 +0200 Subject: [PATCH] Added test case/example for usage with enum class Signed-off-by: Nils Schubert --- nautilus/include/nautilus/val_enum.hpp | 54 +++++++++++++++---- .../test/execution-tests/EnumFunction.hpp | 40 ++++++++++++++ 2 files changed, 85 insertions(+), 9 deletions(-) diff --git a/nautilus/include/nautilus/val_enum.hpp b/nautilus/include/nautilus/val_enum.hpp index bc04f360..1fb3c708 100644 --- a/nautilus/include/nautilus/val_enum.hpp +++ b/nautilus/include/nautilus/val_enum.hpp @@ -20,19 +20,55 @@ class val { using raw_type = underlying_type_t; using basic_type = raw_type; - val() : value() {} - #ifdef ENABLE_TRACING - val(val t) : state(t.state), value((T) details::getRawValue(t)) {} - val(val& t) : state(tracing::traceCopy(t.state)), value(t.value) {} - val(val&& t) : state(t.state), value(t.value) {} - val(T val) : state(tracing::traceConstant((underlying_type_t) val)), value(val) {} + template + requires std::is_enum_v && (!std::is_convertible_v>) + val(T val) : state(tracing::traceConstant(static_cast>(val))), value(static_cast>(val)) { + } + + template + requires std::is_enum_v && (!std::is_convertible_v>) + val(val& val) : state(tracing::traceConstant(static_cast>(val))), value(static_cast>(val)) { + } + val(val t) : state(t.state), value((T) details::getRawValue(t)) { + } + val(val& t) : state(tracing::traceCopy(t.state)), value(t.value) { + } + val(val&& t) : state(t.state), value(t.value) { + } + val(T val) : state(tracing::traceConstant((underlying_type_t) val)), value(val) { + } #else - val(val t) : value((T) details::getRawValue(t)) {} - val(val& t) : value(t.value) {} - val(T val) : value(val) {} + template + requires std::is_enum_v && (!std::is_convertible_v>) + val(T val) : value(static_cast>(val)) { + } + + template + requires std::is_enum_v && (!std::is_convertible_v>) + val(val& val) : value(static_cast>(val)) { + } + + val(val t) : value((T) details::getRawValue(t)) { + } + val(val& t) : value(t.value) { + } + val(T val) : value(val) { + } #endif + template + requires std::is_enum_v && (!std::is_convertible_v>) + bool operator==(const RHS& other) const { + return val(value) == val(static_cast>(other)); + } + + template + requires std::is_enum_v && (!std::is_convertible_v>) + bool operator!=(const RHS& other) const { + return val(value) != val(static_cast>(other)); + } + operator val() const { return value; } diff --git a/nautilus/test/execution-tests/EnumFunction.hpp b/nautilus/test/execution-tests/EnumFunction.hpp index c37e9bdb..738c7b1d 100644 --- a/nautilus/test/execution-tests/EnumFunction.hpp +++ b/nautilus/test/execution-tests/EnumFunction.hpp @@ -1,15 +1,22 @@ #pragma once #include +#include namespace nautilus { enum Color { BLUE, GREEN }; +enum class LogLevel : uint8_t { LOG_NONE = 1, LOG_FATAL_ERROR = 2, LOG_ERROR = 3, LOG_WARNING = 4, LOG_INFO = 5, LOG_DEBUG = 6, LOG_TRACE = 7 }; + val handleEnum(val enumVal) { return enumVal == Color::BLUE; } +val handleEnumLogLevel(val enumVal) { + return enumVal == LogLevel::LOG_DEBUG || enumVal == LogLevel::LOG_INFO; +} + val isEnum(val enumVal) { if (enumVal == Color::BLUE) { return 1; @@ -33,7 +40,40 @@ int32_t enumFunction(Color value) { } } +auto enumClassFunction(LogLevel level) { + switch (level) { + + case LogLevel::LOG_NONE: + return 42; + break; + case LogLevel::LOG_FATAL_ERROR: + return 42; + break; + case LogLevel::LOG_ERROR: + return 42; + break; + case LogLevel::LOG_WARNING: + return 42; + break; + case LogLevel::LOG_INFO: + return 42; + break; + case LogLevel::LOG_DEBUG: + return 42; + break; + case LogLevel::LOG_TRACE: + return 42; + break; + } + + return 43; +} + val callEnumFunction(val enumVal) { return invoke(enumFunction, enumVal); } + +val callEnumClassFunction(val enumClassVal) { + return invoke(enumClassFunction, enumClassVal); +} } // namespace nautilus \ No newline at end of file