From 7ac8496bfa3fc2053799e0d15d38fbaee65b4c8c Mon Sep 17 00:00:00 2001
From: Olivier BICHLER <olivier.bichler@cea.fr>
Date: Tue, 4 Mar 2025 10:31:37 +0100
Subject: [PATCH] Added auto format_as() generation with EnumStrings

---
 include/aidge/data/DataFormat.hpp         |  4 ----
 include/aidge/data/DataType.hpp           |  6 ------
 include/aidge/data/Elts.hpp               |  4 ----
 include/aidge/data/Interpolation.hpp      | 25 +++++++++++++++++++++++
 include/aidge/operator/BitShift.hpp       |  8 ++++++++
 include/aidge/operator/DepthToSpace.hpp   |  8 ++++++++
 include/aidge/operator/GridSample.hpp     | 16 +++++++++++++++
 include/aidge/utils/Log.hpp               | 10 ---------
 include/aidge/utils/logger/EnumString.hpp |  7 +++++++
 9 files changed, 64 insertions(+), 24 deletions(-)

diff --git a/include/aidge/data/DataFormat.hpp b/include/aidge/data/DataFormat.hpp
index 4b3b949f0..4df854a1e 100644
--- a/include/aidge/data/DataFormat.hpp
+++ b/include/aidge/data/DataFormat.hpp
@@ -97,8 +97,4 @@ constexpr const char* const EnumStrings<Aidge::DataFormat>::data[] = {
 };
 }
 
-namespace Aidge {
-inline auto format_as(DataFormat df) { return EnumStrings<DataFormat>::data[static_cast<int>(df)]; }
-} // namespace Aidge
-
 #endif /* AIDGE_CORE_DATA_DATAFORMAT_H_ */
diff --git a/include/aidge/data/DataType.hpp b/include/aidge/data/DataType.hpp
index 41984f199..9963534d2 100644
--- a/include/aidge/data/DataType.hpp
+++ b/include/aidge/data/DataType.hpp
@@ -180,10 +180,4 @@ const char* const EnumStrings<Aidge::DataType>::data[]
        "UInt5", "UInt6", "UInt7", "UInt8", "UInt16", "UInt32", "UInt64", "Any"};
 }
 
-namespace Aidge {
-inline auto format_as(DataType dt) {
-    return EnumStrings<DataType>::data[static_cast<int>(dt)];
-}
-} // namespace Aidge
-
 #endif /* AIDGE_CORE_DATA_DATATYPE_H_ */
diff --git a/include/aidge/data/Elts.hpp b/include/aidge/data/Elts.hpp
index b2df11968..4bfca706e 100644
--- a/include/aidge/data/Elts.hpp
+++ b/include/aidge/data/Elts.hpp
@@ -118,8 +118,4 @@ const char* const EnumStrings<Aidge::Elts_t::EltType>::data[]
     = {"Data", "Token", "Undef"};
 }
 
-namespace Aidge {
-inline auto format_as(Elts_t::EltType elt) { return EnumStrings<Aidge::Elts_t::EltType>::data[static_cast<int>(elt)]; }
-}
-
 #endif /* AIDGE_ELTS_H_ */
diff --git a/include/aidge/data/Interpolation.hpp b/include/aidge/data/Interpolation.hpp
index d1af3e831..b9776ab6c 100644
--- a/include/aidge/data/Interpolation.hpp
+++ b/include/aidge/data/Interpolation.hpp
@@ -148,4 +148,29 @@ class Interpolation {
 };
 } // namespace Aidge
 
+/**
+ * @brief EnumStrings specialization for Interpolation::CoordinateTransformation.
+ */
+template <>
+const char* const EnumStrings<Aidge::Interpolation::CoordinateTransformation>::data[] = {
+    "HalfPixel",
+    "HalfPixelSymmetric",
+    "PytorchHalfPixel",
+    "AlignCorners",
+    "Asymmetric"
+};
+
+/**
+ * @brief EnumStrings specialization for Interpolation::Mode.
+ */
+template <>
+const char* const EnumStrings<Aidge::Interpolation::Mode>::data[] = {
+    "Cubic",
+    "Linear",
+    "RoundPreferFloor",
+    "RoundPreferCeil",
+    "Floor",
+    "Ceil"
+};
+
 #endif
diff --git a/include/aidge/operator/BitShift.hpp b/include/aidge/operator/BitShift.hpp
index afe074586..8ae64ede5 100644
--- a/include/aidge/operator/BitShift.hpp
+++ b/include/aidge/operator/BitShift.hpp
@@ -193,4 +193,12 @@ inline std::shared_ptr<Node> BitShift(const BitShift_Op::BitShiftDirection direc
 
 #undef LIST_BITSHIFT_ATTR
 
+/**
+ * @brief EnumStrings specialization for BitShift_Op::BitShiftDirection.
+ */
+template <>
+const char* const EnumStrings<Aidge::BitShift_Op::BitShiftDirection>::data[] = {
+    "left", "right"
+};
+
 #endif /* AIDGE_CORE_OPERATOR_BITSHIFT_H_ */
diff --git a/include/aidge/operator/DepthToSpace.hpp b/include/aidge/operator/DepthToSpace.hpp
index 7bf6ffdf3..0c79076e8 100644
--- a/include/aidge/operator/DepthToSpace.hpp
+++ b/include/aidge/operator/DepthToSpace.hpp
@@ -209,4 +209,12 @@ std::shared_ptr<Node> DepthToSpace(const std::uint32_t blockSize,
 
 #undef LIST_DEPTHTOSPACE_ATTR
 
+/**
+ * @brief EnumStrings specialization for DepthToSpace_Op::Mode.
+ */
+template <>
+const char* const EnumStrings<Aidge::DepthToSpace_Op::Mode>::data[] = {
+    "DCR", "CRD"
+};
+
 #endif //AIDGE_CORE_OPERATOR_DEPTHTOSPACE_H_
diff --git a/include/aidge/operator/GridSample.hpp b/include/aidge/operator/GridSample.hpp
index 2388cd0c1..348b7b20b 100644
--- a/include/aidge/operator/GridSample.hpp
+++ b/include/aidge/operator/GridSample.hpp
@@ -223,4 +223,20 @@ std::shared_ptr<Node> GridSample(
 
 #undef LIST_GRIDSAMPLE_ATTR
 
+/**
+ * @brief EnumStrings specialization for GridSample_Op::Mode.
+ */
+template <>
+const char* const EnumStrings<Aidge::GridSample_Op::Mode>::data[] = {
+    "Linear", "Nearest", "Cubic"
+};
+
+/**
+ * @brief EnumStrings specialization for GridSample_Op::PaddingMode.
+ */
+template <>
+const char* const EnumStrings<Aidge::GridSample_Op::PaddingMode>::data[] = {
+    "Zeros", "Border", "Reflection"
+};
+
 #endif /* AIDGE_CORE_OPERATOR_GRIDSAMPLE_H_ */
diff --git a/include/aidge/utils/Log.hpp b/include/aidge/utils/Log.hpp
index 394d65906..d2d06fa5f 100644
--- a/include/aidge/utils/Log.hpp
+++ b/include/aidge/utils/Log.hpp
@@ -260,16 +260,6 @@ private:
 
 } // namespace Aidge
 
-// Formatter specialization for Log::Level
-template <>
-struct fmt::formatter<Aidge::Log::Level> : formatter<const char*> {
-    template <typename FormatContext>
-    auto format(const Aidge::Log::Level& level, FormatContext& ctx) const {
-        const char* name = EnumStrings<Aidge::Log::Level>::data[static_cast<int>(level)];
-        return formatter<const char*>::format(name, ctx);
-    }
-};
-
 namespace {
 template <>
 const char* const EnumStrings<Aidge::Log::Level>::data[] = {
diff --git a/include/aidge/utils/logger/EnumString.hpp b/include/aidge/utils/logger/EnumString.hpp
index 6091d8bf5..cb491a183 100644
--- a/include/aidge/utils/logger/EnumString.hpp
+++ b/include/aidge/utils/logger/EnumString.hpp
@@ -12,6 +12,8 @@
 #ifndef AIDGE_CORE_UTILS_LOGGER_ENUMSTRING_H_
 #define AIDGE_CORE_UTILS_LOGGER_ENUMSTRING_H_
 
+#include <fmt/format.h>
+
 namespace {
 // This is the type that will hold all the strings. Each enumerate type will
 // declare its own specialization.
@@ -20,4 +22,9 @@ template <typename T> struct EnumStrings {
 };
 }
 
+namespace Aidge {
+template <class T, typename std::enable_if<std::is_enum<T>::value>::type* = nullptr>
+inline auto format_as(T elt) { return EnumStrings<T>::data[static_cast<int>(elt)]; }
+}
+
 #endif /* AIDGE_CORE_UTILS_LOGGER_ENUMSTRING_H_ */
-- 
GitLab