Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1)

Side by Side Diff: src/base/flags.h

Issue 510773002: Add Flags<T> class as a type-safe way of storing OR-combinations of enums. (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Drop copy & assign Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « BUILD.gn ('k') | test/base-unittests/base-unittests.gyp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2014 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifndef V8_BASE_FLAGS_H_
6 #define V8_BASE_FLAGS_H_
7
8 #include "include/v8config.h"
9
10 namespace v8 {
11 namespace base {
12
13 // The Flags class provides a type-safe way of storing OR-combinations of enum
14 // values. The Flags<T> class is a template class, where T is an enum type.
15 //
16 // The traditional C++ approach for storing OR-combinations of enum values is to
17 // use an int or unsigned int variable. The inconvenience with this approach is
18 // that there's no type checking at all; any enum value can be OR'd with any
19 // other enum value and passed on to a function that takes an int or unsigned
20 // int.
21 template <typename T>
22 class Flags V8_FINAL {
23 public:
24 typedef T flag_type;
25 typedef int mask_type;
26
27 Flags() : mask_(0) {}
28 Flags(flag_type flag) : mask_(flag) {} // NOLINT(runtime/explicit)
29 explicit Flags(mask_type mask) : mask_(mask) {}
30
31 Flags& operator&=(const Flags& flags) {
32 mask_ &= flags.mask_;
33 return *this;
34 }
35 Flags& operator|=(const Flags& flags) {
36 mask_ |= flags.mask_;
37 return *this;
38 }
39 Flags& operator^=(const Flags& flags) {
40 mask_ ^= flags.mask_;
41 return *this;
42 }
43
44 Flags operator&(const Flags& flags) const { return Flags(*this) &= flags; }
45 Flags operator|(const Flags& flags) const { return Flags(*this) |= flags; }
46 Flags operator^(const Flags& flags) const { return Flags(*this) ^= flags; }
47
48 Flags& operator&=(flag_type flag) { return operator&=(Flags(flag)); }
49 Flags& operator|=(flag_type flag) { return operator|=(Flags(flag)); }
50 Flags& operator^=(flag_type flag) { return operator^=(Flags(flag)); }
51
52 Flags operator&(flag_type flag) const { return operator&(Flags(flag)); }
53 Flags operator|(flag_type flag) const { return operator|(Flags(flag)); }
54 Flags operator^(flag_type flag) const { return operator^(Flags(flag)); }
55
56 Flags operator~() const { return Flags(~mask_); }
57
58 operator mask_type() const { return mask_; }
59 bool operator!() const { return !mask_; }
60
61 private:
62 mask_type mask_;
63 };
64
65
66 #define DEFINE_FLAGS(Type, Enum) typedef ::v8::base::Flags<Enum> Type
67
68 #define DEFINE_OPERATORS_FOR_FLAGS(Type) \
69 inline ::v8::base::Flags<Type::flag_type> operator&( \
70 Type::flag_type lhs, \
71 Type::flag_type rhs)V8_UNUSED V8_WARN_UNUSED_RESULT; \
72 inline ::v8::base::Flags<Type::flag_type> operator&(Type::flag_type lhs, \
73 Type::flag_type rhs) { \
74 return ::v8::base::Flags<Type::flag_type>(lhs) & rhs; \
75 } \
76 inline ::v8::base::Flags<Type::flag_type> operator&( \
77 Type::flag_type lhs, const ::v8::base::Flags<Type::flag_type>& rhs) \
78 V8_UNUSED V8_WARN_UNUSED_RESULT; \
79 inline ::v8::base::Flags<Type::flag_type> operator&( \
80 Type::flag_type lhs, const ::v8::base::Flags<Type::flag_type>& rhs) { \
81 return rhs & lhs; \
82 } \
83 inline void operator&(Type::flag_type lhs, Type::mask_type rhs)V8_UNUSED; \
84 inline void operator&(Type::flag_type lhs, Type::mask_type rhs) {} \
85 inline ::v8::base::Flags<Type::flag_type> operator|(Type::flag_type lhs, \
86 Type::flag_type rhs) \
87 V8_UNUSED V8_WARN_UNUSED_RESULT; \
88 inline ::v8::base::Flags<Type::flag_type> operator|(Type::flag_type lhs, \
89 Type::flag_type rhs) { \
90 return ::v8::base::Flags<Type::flag_type>(lhs) | rhs; \
91 } \
92 inline ::v8::base::Flags<Type::flag_type> operator|( \
93 Type::flag_type lhs, const ::v8::base::Flags<Type::flag_type>& rhs) \
94 V8_UNUSED V8_WARN_UNUSED_RESULT; \
95 inline ::v8::base::Flags<Type::flag_type> operator|( \
96 Type::flag_type lhs, const ::v8::base::Flags<Type::flag_type>& rhs) { \
97 return rhs | lhs; \
98 } \
99 inline void operator|(Type::flag_type lhs, Type::mask_type rhs) V8_UNUSED; \
100 inline void operator|(Type::flag_type lhs, Type::mask_type rhs) {} \
101 inline ::v8::base::Flags<Type::flag_type> operator^(Type::flag_type lhs, \
102 Type::flag_type rhs) \
103 V8_UNUSED V8_WARN_UNUSED_RESULT; \
104 inline ::v8::base::Flags<Type::flag_type> operator^(Type::flag_type lhs, \
105 Type::flag_type rhs) { \
106 return ::v8::base::Flags<Type::flag_type>(lhs) ^ rhs; \
107 } inline ::v8::base::Flags<Type::flag_type> \
108 operator^(Type::flag_type lhs, \
109 const ::v8::base::Flags<Type::flag_type>& rhs) \
110 V8_UNUSED V8_WARN_UNUSED_RESULT; \
111 inline ::v8::base::Flags<Type::flag_type> operator^( \
112 Type::flag_type lhs, const ::v8::base::Flags<Type::flag_type>& rhs) { \
113 return rhs ^ lhs; \
114 } inline void operator^(Type::flag_type lhs, Type::mask_type rhs) V8_UNUSED; \
115 inline void operator^(Type::flag_type lhs, Type::mask_type rhs) {}
116
117 } // namespace base
118 } // namespace v8
119
120 #endif // V8_BASE_FLAGS_H_
OLDNEW
« no previous file with comments | « BUILD.gn ('k') | test/base-unittests/base-unittests.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698