Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Pedro Alves <palves@redhat.com>
To: gdb-patches@sourceware.org
Subject: [PATCH 1/3] enum_flags: Use C++11 std::underlying_type
Date: Fri, 04 Nov 2016 03:23:00 -0000	[thread overview]
Message-ID: <1478229738-24469-2-git-send-email-palves@redhat.com> (raw)
In-Reply-To: <1478229738-24469-1-git-send-email-palves@redhat.com>

Now that we can require C++11, we can use std::underlying_type instead
of rolling our own.

gdb/ChangeLog:
yyyy-mm-dd  Pedro Alves  <palves@redhat.com>

	* common/enum-flags.h: Include <type_traits>.
	(integer_for_size, enum_underlying_type): Delete.
	(class enum_flags): Use std::underlying_type.
---
 gdb/common/enum-flags.h | 24 +++---------------------
 1 file changed, 3 insertions(+), 21 deletions(-)

diff --git a/gdb/common/enum-flags.h b/gdb/common/enum-flags.h
index 9b8c952..ff48067 100644
--- a/gdb/common/enum-flags.h
+++ b/gdb/common/enum-flags.h
@@ -51,6 +51,8 @@
 
 #ifdef __cplusplus
 
+#include <type_traits>
+
 /* Traits type used to prevent the global operator overloads from
    instantiating for non-flag enums.  */
 template<typename T> struct enum_flags_type {};
@@ -66,32 +68,12 @@ template<typename T> struct enum_flags_type {};
     typedef enum_flags<enum_type> type;			\
   }
 
-/* Until we can rely on std::underlying type being universally
-   available (C++11), roll our own for enums.  */
-template<int size, bool sign> class integer_for_size { typedef void type; };
-template<> struct integer_for_size<1, 0> { typedef uint8_t type; };
-template<> struct integer_for_size<2, 0> { typedef uint16_t type; };
-template<> struct integer_for_size<4, 0> { typedef uint32_t type; };
-template<> struct integer_for_size<8, 0> { typedef uint64_t type; };
-template<> struct integer_for_size<1, 1> { typedef int8_t type; };
-template<> struct integer_for_size<2, 1> { typedef int16_t type; };
-template<> struct integer_for_size<4, 1> { typedef int32_t type; };
-template<> struct integer_for_size<8, 1> { typedef int64_t type; };
-
-template<typename T>
-struct enum_underlying_type
-{
-  typedef typename
-    integer_for_size<sizeof (T), static_cast<bool>(T (-1) < T (0))>::type
-    type;
-};
-
 template <typename E>
 class enum_flags
 {
 public:
   typedef E enum_type;
-  typedef typename enum_underlying_type<enum_type>::type underlying_type;
+  typedef typename std::underlying_type<enum_type>::type underlying_type;
 
 private:
   /* Private type used to support initializing flag types with zero:
-- 
2.5.5


  parent reply	other threads:[~2016-11-04  3:23 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-11-04  3:23 [PATCH 0/3] enum_flags: Fix problems and add comprehensive unit tests Pedro Alves
2016-11-04  3:23 ` [PATCH 2/3] " Pedro Alves
2016-11-05  2:47   ` Simon Marchi
2016-11-05  3:05   ` Simon Marchi
2016-11-04  3:23 ` [PATCH 3/3] enum_flags: Fix ternary operator and remove implicit convertion to raw enum Pedro Alves
2016-11-04  3:23 ` Pedro Alves [this message]
2016-11-05  2:28   ` [PATCH 1/3] enum_flags: Use C++11 std::underlying_type Simon Marchi
2016-11-07 13:33 ` [PATCH 0/3] enum_flags: Fix problems and add comprehensive unit tests Luis Machado

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1478229738-24469-2-git-send-email-palves@redhat.com \
    --to=palves@redhat.com \
    --cc=gdb-patches@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox