Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add new entry in Release_Notes.
[simgrid.git] / include / xbt / utility.hpp
index cab7266..c35be72 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (c) 2016-2021. The SimGrid Team.
+/* Copyright (c) 2016-2023. The SimGrid Team.
  * All rights reserved.                                                     */
 
 /* This program is free software; you can redistribute it and/or modify it
@@ -10,6 +10,7 @@
 #include <array>
 #include <functional>
 #include <tuple>
+#include <type_traits>
 #include <xbt/base.h>
 
 /** @brief Helper macro to declare enum class
   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[static_cast<int>(value)];                                                                             \
+    return names.at(static_cast<int>(value));                                                                          \
+  }                                                                                                                    \
+  static constexpr bool is_valid_##EnumType(int raw_value)                                                             \
+  {                                                                                                                    \
+    return raw_value >= 0 && raw_value < _XBT_COUNT_ARGS(__VA_ARGS__);                                                 \
   }                                                                                                                    \
   enum class EnumType { __VA_ARGS__ } /* defined here to handle trailing semicolon */
 
-namespace simgrid {
-namespace xbt {
+namespace simgrid::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
  *
@@ -63,6 +79,5 @@ template <class List, class Elem> inline void intrusive_erase(List& list, Elem&
   list.erase(list.iterator_to(elem));
 }
 
-}
-}
+} // namespace simgrid::xbt
 #endif