Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 83c6f9ee authored by Lloyd Pique's avatar Lloyd Pique
Browse files

Easier ftl::Flags construction

As demonstrated by the existing test, while it is possible to construct
a ftl::Flags<E> from multiple values, it requires an extra
`using namespace ftl::flag_operators` or alternatively a
`using ftl::flag_operators::operator|` to implicitly construct a
`ftl::Flags<E>` type.

But there is an easier way -- allow implicit construction from a
`std::initializer_list<E>`.

This means as an alternative to:

    using namespace ftl::flag_operators;
    ftl::Flags<E> x = E::A | E::B;

... one can instead write:

    ftl::Flags<E> x = {E::A, E::B};

... and achieve the same initial value.

This change adds the new constructor overload.

Assignment from an initializer list automatically works, without having
to define a explicit `operator=(std::initializer_list<E>)`. Instead the
copy constuctor is used.

As a useful side effect, you can now also clear the flags by assigning
an empty initializer list:

    ftl::Flags<E> x;
    x = {};  // Clears x to zero

Bug: 185536303
Flag: EXEMPT New library code
Test: atest ftl_test
Change-Id: I1de7857c93ebef4fc5e6157aac9cf162b49943e1
parent 1a824fd3
Loading
Loading
Loading
Loading
+10 −0
Original line number Diff line number Diff line
@@ -22,6 +22,7 @@
#include <bitset>
#include <cstdint>
#include <iterator>
#include <initializer_list>
#include <string>
#include <type_traits>

@@ -40,6 +41,7 @@ class Flags {

public:
    constexpr Flags(F f) : mFlags(static_cast<U>(f)) {}
    constexpr Flags(std::initializer_list<F> fs) : mFlags(combine(fs)) {}
    constexpr Flags() : mFlags(0) {}
    constexpr Flags(const Flags<F>& f) : mFlags(f.mFlags) {}

@@ -197,6 +199,14 @@ public:
private:
    U mFlags;

    static constexpr U combine(std::initializer_list<F> fs) {
        U result = 0;
        for (const F f : fs) {
            result |= static_cast<U>(f);
        }
        return result;
    }

    static void appendFlag(std::string& str, const std::string_view& flag, bool& first) {
        if (first) {
            first = false;
+13 −1
Original line number Diff line number Diff line
@@ -17,7 +17,7 @@
#include <ftl/flags.h>
#include <gtest/gtest.h>

#include <type_traits>
#include <initializer_list>

namespace android::test {

@@ -59,6 +59,18 @@ TEST(Flags, All) {
    ASSERT_FALSE(flags.all(TestFlags::ONE | TestFlags::TWO | TestFlags::THREE));
}

TEST(Flags, ImplicitConstructionAndAssignmentFromInitializerList) {
    Flags<TestFlags> flags = {TestFlags::ONE, TestFlags::THREE};
    ASSERT_TRUE(flags.test(TestFlags::ONE));
    ASSERT_FALSE(flags.test(TestFlags::TWO));
    ASSERT_TRUE(flags.test(TestFlags::THREE));

    flags = {};
    ASSERT_FALSE(flags.test(TestFlags::ONE));
    ASSERT_FALSE(flags.test(TestFlags::TWO));
    ASSERT_FALSE(flags.test(TestFlags::THREE));
}

TEST(Flags, DefaultConstructor_hasNoFlagsSet) {
    Flags<TestFlags> flags;
    ASSERT_FALSE(flags.any(TestFlags::ONE | TestFlags::TWO | TestFlags::THREE));