File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 2222#include < bitset>
2323#include < cstdint>
2424#include < iterator>
25+ #include < initializer_list>
2526#include < string>
2627#include < type_traits>
2728
@@ -40,6 +41,7 @@ class Flags {
4041
4142public:
4243 constexpr Flags (F f) : mFlags(static_cast <U>(f)) {}
44+ constexpr Flags (std::initializer_list<F> fs) : mFlags(combine(fs)) {}
4345 constexpr Flags () : mFlags(0 ) {}
4446 constexpr Flags (const Flags<F>& f) : mFlags(f.mFlags ) {}
4547
@@ -197,6 +199,14 @@ class Flags {
197199private:
198200 U mFlags ;
199201
202+ static constexpr U combine (std::initializer_list<F> fs) {
203+ U result = 0 ;
204+ for (const F f : fs) {
205+ result |= static_cast <U>(f);
206+ }
207+ return result;
208+ }
209+
200210 static void appendFlag (std::string& str, const std::string_view& flag, bool & first) {
201211 if (first) {
202212 first = false ;
Original file line number Diff line number Diff line change 1717#include < ftl/flags.h>
1818#include < gtest/gtest.h>
1919
20- #include < type_traits >
20+ #include < initializer_list >
2121
2222namespace android ::test {
2323
@@ -59,6 +59,18 @@ TEST(Flags, All) {
5959 ASSERT_FALSE (flags.all (TestFlags::ONE | TestFlags::TWO | TestFlags::THREE));
6060}
6161
62+ TEST (Flags, ImplicitConstructionAndAssignmentFromInitializerList) {
63+ Flags<TestFlags> flags = {TestFlags::ONE, TestFlags::THREE};
64+ ASSERT_TRUE (flags.test (TestFlags::ONE));
65+ ASSERT_FALSE (flags.test (TestFlags::TWO));
66+ ASSERT_TRUE (flags.test (TestFlags::THREE));
67+
68+ flags = {};
69+ ASSERT_FALSE (flags.test (TestFlags::ONE));
70+ ASSERT_FALSE (flags.test (TestFlags::TWO));
71+ ASSERT_FALSE (flags.test (TestFlags::THREE));
72+ }
73+
6274TEST (Flags, DefaultConstructor_hasNoFlagsSet) {
6375 Flags<TestFlags> flags;
6476 ASSERT_FALSE (flags.any (TestFlags::ONE | TestFlags::TWO | TestFlags::THREE));
You can’t perform that action at this time.
0 commit comments