Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Define xbt::type_identity_t (similar to C++20's std::type_identity_t).
[simgrid.git] / include / xbt / utility.hpp
index be9d287..0345806 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (c) 2016-2020. The SimGrid Team.
+/* Copyright (c) 2016-2022. The SimGrid Team.
  * All rights reserved.                                                     */
 
 /* This program is free software; you can redistribute it and/or modify it
@@ -7,12 +7,41 @@
 #ifndef XBT_UTILITY_HPP
 #define XBT_UTILITY_HPP
 
-#include <tuple>
+#include <array>
 #include <functional>
+#include <tuple>
+#include <type_traits>
+#include <xbt/base.h>
+
+/** @brief Helper macro to declare enum class
+ *
+ * Declares an enum class EnumType, and a function "const char* to_c_str(EnumType)" to retrieve a C-string description
+ * for each value.
+ */
+#define XBT_DECLARE_ENUM_CLASS(EnumType, ...)                                                                          \
+  enum class EnumType;                                                                                                 \
+  static constexpr char const* to_c_str(EnumType value)                                                                \
+  {                                                                                                                    \
+    constexpr std::array<const char*, _XBT_COUNT_ARGS(__VA_ARGS__)> names{{_XBT_STRINGIFY_ARGS(__VA_ARGS__)}};         \
+    return names.at(static_cast<int>(value));                                                                          \
+  }                                                                                                                    \
+  enum class EnumType { __VA_ARGS__ } /* defined here to handle trailing semicolon */
 
 namespace simgrid {
 namespace xbt {
 
+/** @brief Replacement for C++20's std::type_identity_t
+ */
+#if __cplusplus >= 201806L // __cpp_lib_type_identity
+template <class T> using type_identity_t = typename std::type_identity_t<T>;
+#else
+template <class T> struct type_identity {
+  using type = T;
+};
+
+template <class T> using type_identity_t = typename type_identity<T>::type;
+#endif
+
 /** @brief A hash which works with more stuff
  *
  *  It can hash pairs: the standard hash currently doesn't include this.