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

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: BUILD.gn 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 Flags(const Flags& flags) : mask_(flags.mask_) {}
Sven Panne 2014/08/27 12:15:02 Do we really need an explicit copy constructor and
31 Flags& operator=(const Flags& flags) {
32 mask_ = flags.mask_;
33 return *this;
34 }
35
36 Flags& operator&=(const Flags& flags) {
37 mask_ &= flags.mask_;
38 return *this;
39 }
40 Flags& operator|=(const Flags& flags) {
41 mask_ |= flags.mask_;
42 return *this;
43 }
44 Flags& operator^=(const Flags& flags) {
45 mask_ ^= flags.mask_;
46 return *this;
47 }
48
49 Flags operator&(const Flags& flags) const { return Flags(*this) &= flags; }
50 Flags operator|(const Flags& flags) const { return Flags(*this) |= flags; }
51 Flags operator^(const Flags& flags) const { return Flags(*this) ^= flags; }
52
53 Flags& operator&=(flag_type flag) { return operator&=(Flags(flag)); }
54 Flags& operator|=(flag_type flag) { return operator|=(Flags(flag)); }
55 Flags& operator^=(flag_type flag) { return operator^=(Flags(flag)); }
56
57 Flags operator&(flag_type flag) const { return operator&(Flags(flag)); }
58 Flags operator|(flag_type flag) const { return operator|(Flags(flag)); }
59 Flags operator^(flag_type flag) const { return operator^(Flags(flag)); }
60
61 Flags operator~() const { return Flags(~mask_); }
62
63 operator mask_type() const { return mask_; }
64 bool operator!() const { return !mask_; }
65
66 private:
67 mask_type mask_;
68 };
69
70
71 #define DEFINE_FLAGS(Type, Enum) typedef ::v8::base::Flags<Enum> Type
72
73 #define DEFINE_OPERATORS_FOR_FLAGS(Type) \
74 inline ::v8::base::Flags<Type::flag_type> operator&( \
75 Type::flag_type lhs, \
76 Type::flag_type rhs)V8_UNUSED V8_WARN_UNUSED_RESULT; \
77 inline ::v8::base::Flags<Type::flag_type> operator&(Type::flag_type lhs, \
78 Type::flag_type rhs) { \
79 return ::v8::base::Flags<Type::flag_type>(lhs) & rhs; \
80 } \
81 inline ::v8::base::Flags<Type::flag_type> operator&( \
82 Type::flag_type lhs, const ::v8::base::Flags<Type::flag_type>& rhs) \
83 V8_UNUSED V8_WARN_UNUSED_RESULT; \
84 inline ::v8::base::Flags<Type::flag_type> operator&( \
85 Type::flag_type lhs, const ::v8::base::Flags<Type::flag_type>& rhs) { \
86 return rhs & lhs; \
87 } \
88 inline void operator&(Type::flag_type lhs, Type::mask_type rhs)V8_UNUSED; \
89 inline void operator&(Type::flag_type lhs, Type::mask_type rhs) {} \
90 inline ::v8::base::Flags<Type::flag_type> operator|(Type::flag_type lhs, \
91 Type::flag_type rhs) \
92 V8_UNUSED V8_WARN_UNUSED_RESULT; \
93 inline ::v8::base::Flags<Type::flag_type> operator|(Type::flag_type lhs, \
94 Type::flag_type rhs) { \
95 return ::v8::base::Flags<Type::flag_type>(lhs) | rhs; \
96 } \
97 inline ::v8::base::Flags<Type::flag_type> operator|( \
98 Type::flag_type lhs, const ::v8::base::Flags<Type::flag_type>& rhs) \
99 V8_UNUSED V8_WARN_UNUSED_RESULT; \
100 inline ::v8::base::Flags<Type::flag_type> operator|( \
101 Type::flag_type lhs, const ::v8::base::Flags<Type::flag_type>& rhs) { \
102 return rhs | lhs; \
103 } \
104 inline void operator|(Type::flag_type lhs, Type::mask_type rhs) V8_UNUSED; \
105 inline void operator|(Type::flag_type lhs, Type::mask_type rhs) {} \
106 inline ::v8::base::Flags<Type::flag_type> operator^(Type::flag_type lhs, \
107 Type::flag_type rhs) \
108 V8_UNUSED V8_WARN_UNUSED_RESULT; \
109 inline ::v8::base::Flags<Type::flag_type> operator^(Type::flag_type lhs, \
110 Type::flag_type rhs) { \
111 return ::v8::base::Flags<Type::flag_type>(lhs) ^ rhs; \
112 } inline ::v8::base::Flags<Type::flag_type> \
113 operator^(Type::flag_type lhs, \
114 const ::v8::base::Flags<Type::flag_type>& rhs) \
115 V8_UNUSED V8_WARN_UNUSED_RESULT; \
116 inline ::v8::base::Flags<Type::flag_type> operator^( \
117 Type::flag_type lhs, const ::v8::base::Flags<Type::flag_type>& rhs) { \
118 return rhs ^ lhs; \
119 } inline void operator^(Type::flag_type lhs, Type::mask_type rhs) V8_UNUSED; \
120 inline void operator^(Type::flag_type lhs, Type::mask_type rhs) {}
121
122 } // namespace base
123 } // namespace v8
124
125 #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