Revision: 23453
Author: [email protected]
Date: Wed Aug 27 12:16:36 2014 UTC
Log: Add Flags<T> class as a type-safe way of storing OR-combinations
of enums.
TEST=base-unittests
[email protected]
Review URL: https://codereview.chromium.org/510773002
https://code.google.com/p/v8/source/detail?r=23453
Added:
/branches/bleeding_edge/src/base/flags.h
/branches/bleeding_edge/test/base-unittests/flags-unittest.cc
Modified:
/branches/bleeding_edge/BUILD.gn
/branches/bleeding_edge/test/base-unittests/base-unittests.gyp
/branches/bleeding_edge/tools/gyp/v8.gyp
=======================================
--- /dev/null
+++ /branches/bleeding_edge/src/base/flags.h Wed Aug 27 12:16:36 2014 UTC
@@ -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_
=======================================
--- /dev/null
+++ /branches/bleeding_edge/test/base-unittests/flags-unittest.cc Wed Aug
27 12:16:36 2014 UTC
@@ -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
=======================================
--- /branches/bleeding_edge/BUILD.gn Wed Aug 27 10:55:44 2014 UTC
+++ /branches/bleeding_edge/BUILD.gn Wed Aug 27 12:16:36 2014 UTC
@@ -1165,6 +1165,7 @@
"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",
=======================================
--- /branches/bleeding_edge/test/base-unittests/base-unittests.gyp Wed Aug
27 08:29:22 2014 UTC
+++ /branches/bleeding_edge/test/base-unittests/base-unittests.gyp Wed Aug
27 12:16:36 2014 UTC
@@ -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',
=======================================
--- /branches/bleeding_edge/tools/gyp/v8.gyp Wed Aug 27 08:29:22 2014 UTC
+++ /branches/bleeding_edge/tools/gyp/v8.gyp Wed Aug 27 12:16:36 2014 UTC
@@ -1130,6 +1130,7 @@
'../../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',
--
--
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev
---
You received this message because you are subscribed to the Google Groups "v8-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.