base: Fix enums checkpointing
authorGiacomo Travaglini <giacomo.travaglini@arm.com>
Thu, 7 Feb 2019 07:34:56 +0000 (07:34 +0000)
committerGiacomo Travaglini <giacomo.travaglini@arm.com>
Mon, 18 Feb 2019 09:57:33 +0000 (09:57 +0000)
Creating an extra version of string to number converters (__to_number)
in base/str.hh; it will be used by enums only when unserializing
them.  The reason not to have a single helper for both enums and
integers is that std::numeric_limits trait is not specialized for enums.
We fix this by using the std::underlying_type trait.

Change-Id: I819e35c0df8c094de7b7a6390152964fa47d513d
Signed-off-by: Giacomo Travaglini <giacomo.travaglini@arm.com>
Reviewed-by: Ciro Santilli <ciro.santilli@arm.com>
Signed-off-by: Giacomo Travaglini <giacomo.travaglini@arm.com>
Reviewed-on: https://gem5-review.googlesource.com/c/16382
Reviewed-by: Daniel Carvalho <odanrc@yahoo.com.br>
Reviewed-by: Andreas Sandberg <andreas.sandberg@arm.com>
Maintainer: Jason Lowe-Power <jason@lowepower.com>

src/base/str.hh

index 61022bd26eda96d88c6c976f8e1e47540f4c68ff..1ea18b70de430280f670c62a44d3bdc164370868 100644 (file)
@@ -40,6 +40,7 @@
 #include <locale>
 #include <stdexcept>
 #include <string>
+#include <type_traits>
 #include <vector>
 
 #include "base/logging.hh"
@@ -108,8 +109,7 @@ tokenize(std::vector<std::string> &vector, const std::string &s,
  *       integeral type, as well as enums and floating-point types.
  */
 template <class T>
-typename std::enable_if<(std::is_integral<T>::value ||
-                        std::is_enum<T>::value) &&
+typename std::enable_if<std::is_integral<T>::value &&
                         std::is_signed<T>::value, T>::type
 __to_number(const std::string &value)
 {
@@ -121,8 +121,7 @@ __to_number(const std::string &value)
 }
 
 template <class T>
-typename std::enable_if<(std::is_integral<T>::value ||
-                        std::is_enum<T>::value) &&
+typename std::enable_if<std::is_integral<T>::value &&
                         !std::is_signed<T>::value, T>::type
 __to_number(const std::string &value)
 {
@@ -133,6 +132,14 @@ __to_number(const std::string &value)
     return static_cast<T>(r);
 }
 
+template <class T>
+typename std::enable_if<std::is_enum<T>::value, T>::type
+__to_number(const std::string &value)
+{
+    auto r = __to_number<typename std::underlying_type<T>::type>(value);
+    return static_cast<T>(r);
+}
+
 template <class T>
 typename std::enable_if<std::is_floating_point<T>::value, T>::type
 __to_number(const std::string &value)