Add Flags<T> class as a type-safe way of storing OR-combinations of enums.
authorbmeurer@chromium.org <bmeurer@chromium.org>
Wed, 27 Aug 2014 12:16:36 +0000 (12:16 +0000)
committerbmeurer@chromium.org <bmeurer@chromium.org>
Wed, 27 Aug 2014 12:16:36 +0000 (12:16 +0000)
TEST=base-unittests
R=svenpanne@chromium.org

Review URL: https://codereview.chromium.org/510773002

git-svn-id: https://v8.googlecode.com/svn/branches/bleeding_edge@23453 ce2b1a6d-e550-0410-aec6-3dcde31c8c00

BUILD.gn
src/base/flags.h [new file with mode: 0644]
test/base-unittests/base-unittests.gyp
test/base-unittests/flags-unittest.cc [new file with mode: 0644]
tools/gyp/v8.gyp

index 5d5ed17..26c9946 100644 (file)
--- a/BUILD.gn
+++ b/BUILD.gn
@@ -1165,6 +1165,7 @@ source_set("v8_libbase") {
     "src/base/build_config.h",
     "src/base/cpu.cc",
     "src/base/cpu.h",
+    "src/base/flags.h",
     "src/base/lazy-instance.h",
     "src/base/logging.cc",
     "src/base/logging.h",
diff --git a/src/base/flags.h b/src/base/flags.h
new file mode 100644 (file)
index 0000000..6a2c57f
--- /dev/null
@@ -0,0 +1,120 @@
+// Copyright 2014 the V8 project authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef V8_BASE_FLAGS_H_
+#define V8_BASE_FLAGS_H_
+
+#include "include/v8config.h"
+
+namespace v8 {
+namespace base {
+
+// The Flags class provides a type-safe way of storing OR-combinations of enum
+// values. The Flags<T> class is a template class, where T is an enum type.
+//
+// The traditional C++ approach for storing OR-combinations of enum values is to
+// use an int or unsigned int variable. The inconvenience with this approach is
+// that there's no type checking at all; any enum value can be OR'd with any
+// other enum value and passed on to a function that takes an int or unsigned
+// int.
+template <typename T>
+class Flags V8_FINAL {
+ public:
+  typedef T flag_type;
+  typedef int mask_type;
+
+  Flags() : mask_(0) {}
+  Flags(flag_type flag) : mask_(flag) {}  // NOLINT(runtime/explicit)
+  explicit Flags(mask_type mask) : mask_(mask) {}
+
+  Flags& operator&=(const Flags& flags) {
+    mask_ &= flags.mask_;
+    return *this;
+  }
+  Flags& operator|=(const Flags& flags) {
+    mask_ |= flags.mask_;
+    return *this;
+  }
+  Flags& operator^=(const Flags& flags) {
+    mask_ ^= flags.mask_;
+    return *this;
+  }
+
+  Flags operator&(const Flags& flags) const { return Flags(*this) &= flags; }
+  Flags operator|(const Flags& flags) const { return Flags(*this) |= flags; }
+  Flags operator^(const Flags& flags) const { return Flags(*this) ^= flags; }
+
+  Flags& operator&=(flag_type flag) { return operator&=(Flags(flag)); }
+  Flags& operator|=(flag_type flag) { return operator|=(Flags(flag)); }
+  Flags& operator^=(flag_type flag) { return operator^=(Flags(flag)); }
+
+  Flags operator&(flag_type flag) const { return operator&(Flags(flag)); }
+  Flags operator|(flag_type flag) const { return operator|(Flags(flag)); }
+  Flags operator^(flag_type flag) const { return operator^(Flags(flag)); }
+
+  Flags operator~() const { return Flags(~mask_); }
+
+  operator mask_type() const { return mask_; }
+  bool operator!() const { return !mask_; }
+
+ private:
+  mask_type mask_;
+};
+
+
+#define DEFINE_FLAGS(Type, Enum) typedef ::v8::base::Flags<Enum> Type
+
+#define DEFINE_OPERATORS_FOR_FLAGS(Type)                                       \
+  inline ::v8::base::Flags<Type::flag_type> operator&(                         \
+      Type::flag_type lhs,                                                     \
+      Type::flag_type rhs)V8_UNUSED V8_WARN_UNUSED_RESULT;                     \
+  inline ::v8::base::Flags<Type::flag_type> operator&(Type::flag_type lhs,     \
+                                                      Type::flag_type rhs) {   \
+    return ::v8::base::Flags<Type::flag_type>(lhs) & rhs;                      \
+  }                                                                            \
+  inline ::v8::base::Flags<Type::flag_type> operator&(                         \
+      Type::flag_type lhs, const ::v8::base::Flags<Type::flag_type>& rhs)      \
+      V8_UNUSED V8_WARN_UNUSED_RESULT;                                         \
+  inline ::v8::base::Flags<Type::flag_type> operator&(                         \
+      Type::flag_type lhs, const ::v8::base::Flags<Type::flag_type>& rhs) {    \
+    return rhs & lhs;                                                          \
+  }                                                                            \
+  inline void operator&(Type::flag_type lhs, Type::mask_type rhs)V8_UNUSED;    \
+  inline void operator&(Type::flag_type lhs, Type::mask_type rhs) {}           \
+  inline ::v8::base::Flags<Type::flag_type> operator|(Type::flag_type lhs,     \
+                                                      Type::flag_type rhs)     \
+      V8_UNUSED V8_WARN_UNUSED_RESULT;                                         \
+  inline ::v8::base::Flags<Type::flag_type> operator|(Type::flag_type lhs,     \
+                                                      Type::flag_type rhs) {   \
+    return ::v8::base::Flags<Type::flag_type>(lhs) | rhs;                      \
+  }                                                                            \
+  inline ::v8::base::Flags<Type::flag_type> operator|(                         \
+      Type::flag_type lhs, const ::v8::base::Flags<Type::flag_type>& rhs)      \
+      V8_UNUSED V8_WARN_UNUSED_RESULT;                                         \
+  inline ::v8::base::Flags<Type::flag_type> operator|(                         \
+      Type::flag_type lhs, const ::v8::base::Flags<Type::flag_type>& rhs) {    \
+    return rhs | lhs;                                                          \
+  }                                                                            \
+  inline void operator|(Type::flag_type lhs, Type::mask_type rhs) V8_UNUSED;   \
+  inline void operator|(Type::flag_type lhs, Type::mask_type rhs) {}           \
+  inline ::v8::base::Flags<Type::flag_type> operator^(Type::flag_type lhs,     \
+                                                      Type::flag_type rhs)     \
+      V8_UNUSED V8_WARN_UNUSED_RESULT;                                         \
+  inline ::v8::base::Flags<Type::flag_type> operator^(Type::flag_type lhs,     \
+                                                      Type::flag_type rhs) {   \
+    return ::v8::base::Flags<Type::flag_type>(lhs) ^ rhs;                      \
+  } inline ::v8::base::Flags<Type::flag_type>                                  \
+      operator^(Type::flag_type lhs,                                           \
+                const ::v8::base::Flags<Type::flag_type>& rhs)                 \
+      V8_UNUSED V8_WARN_UNUSED_RESULT;                                         \
+  inline ::v8::base::Flags<Type::flag_type> operator^(                         \
+      Type::flag_type lhs, const ::v8::base::Flags<Type::flag_type>& rhs) {    \
+    return rhs ^ lhs;                                                          \
+  } inline void operator^(Type::flag_type lhs, Type::mask_type rhs) V8_UNUSED; \
+  inline void operator^(Type::flag_type lhs, Type::mask_type rhs) {}
+
+}  // namespace base
+}  // namespace v8
+
+#endif  // V8_BASE_FLAGS_H_
index 1cf638f..6b1b8f2 100644 (file)
@@ -22,6 +22,7 @@
       'sources': [  ### gcmole(all) ###
         'bits-unittest.cc',
         'cpu-unittest.cc',
+        'flags-unittest.cc',
         'platform/condition-variable-unittest.cc',
         'platform/mutex-unittest.cc',
         'platform/platform-unittest.cc',
diff --git a/test/base-unittests/flags-unittest.cc b/test/base-unittests/flags-unittest.cc
new file mode 100644 (file)
index 0000000..da526cb
--- /dev/null
@@ -0,0 +1,103 @@
+// Copyright 2014 the V8 project authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "src/base/flags.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace v8 {
+namespace base {
+
+namespace {
+
+enum Flag1 {
+  kFlag1None = 0,
+  kFlag1First = 1u << 1,
+  kFlag1Second = 1u << 2,
+  kFlag1All = kFlag1None | kFlag1First | kFlag1Second
+};
+DEFINE_FLAGS(Flags1, Flag1);
+
+
+DEFINE_OPERATORS_FOR_FLAGS(Flags1)
+
+
+Flags1 bar(Flags1 flags1) { return flags1; }
+
+}  // namespace
+
+
+TEST(FlagsTest, BasicOperations) {
+  Flags1 a;
+  EXPECT_EQ(kFlag1None, static_cast<int>(a));
+  a |= kFlag1First;
+  EXPECT_EQ(kFlag1First, static_cast<int>(a));
+  a = a | kFlag1Second;
+  EXPECT_EQ(kFlag1All, static_cast<int>(a));
+  a &= kFlag1Second;
+  EXPECT_EQ(kFlag1Second, static_cast<int>(a));
+  a = kFlag1None & a;
+  EXPECT_EQ(kFlag1None, static_cast<int>(a));
+  a ^= (kFlag1All | kFlag1None);
+  EXPECT_EQ(kFlag1All, static_cast<int>(a));
+  Flags1 b = ~a;
+  EXPECT_EQ(kFlag1All, static_cast<int>(a));
+  EXPECT_EQ(~static_cast<int>(a), static_cast<int>(b));
+  Flags1 c = a;
+  EXPECT_EQ(a, c);
+  EXPECT_NE(a, b);
+  EXPECT_EQ(a, bar(a));
+  EXPECT_EQ(a, bar(kFlag1All));
+}
+
+
+namespace {
+namespace foo {
+
+enum Option {
+  kNoOptions = 0,
+  kOption1 = 1,
+  kOption2 = 2,
+  kAllOptions = kNoOptions | kOption1 | kOption2
+};
+DEFINE_FLAGS(Options, Option);
+
+}  // namespace foo
+
+
+DEFINE_OPERATORS_FOR_FLAGS(foo::Options)
+
+}  // namespace
+
+
+TEST(FlagsTest, NamespaceScope) {
+  foo::Options options;
+  options ^= foo::kNoOptions;
+  options |= foo::kOption1 | foo::kOption2;
+  EXPECT_EQ(foo::kAllOptions, static_cast<int>(options));
+}
+
+
+namespace {
+
+struct Foo {
+  enum Enum { kEnum1 = 1, kEnum2 = 2 };
+  DEFINE_FLAGS(Enums, Enum);
+};
+
+
+DEFINE_OPERATORS_FOR_FLAGS(Foo::Enums)
+
+}  // namespace
+
+
+TEST(FlagsTest, ClassScope) {
+  Foo::Enums enums;
+  enums |= Foo::kEnum1;
+  enums |= Foo::kEnum2;
+  EXPECT_TRUE(enums & Foo::kEnum1);
+  EXPECT_TRUE(enums & Foo::kEnum2);
+}
+
+}  // namespace base
+}  // namespace v8
index 96d9c9c..a6f7d28 100644 (file)
         '../../src/base/build_config.h',
         '../../src/base/cpu.cc',
         '../../src/base/cpu.h',
+        '../../src/base/flags.h',
         '../../src/base/lazy-instance.h',
         '../../src/base/logging.cc',
         '../../src/base/logging.h',