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

Commit 9ae27447 authored by Pirama Arumuga Nainar's avatar Pirama Arumuga Nainar Committed by Sean Kim
Browse files

codec2: Fix ambiguity in operator overloads

Bug: http://b/323152930

Based on
https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2021/p2468r1.html#programming-model,

> If you want an operator== that is used for rewrites (automatically
reversed, and != automatically generated), write only an operator==, and
make sure its return type is bool.

Do this by removing C2Param.h::operator!= for post-C++20.

This still leaves ambiguous definitions because the `using
T::operator==` added in aosp/2877698 creates functions with `const T&`
parameters.  Instead define a new operator== that delegates to the base
class - this function is given preference over rewrites in the base
class.  Define a similar operator!= for pre-c++20 as well.

With this change, -Wambiguous-reversed-operator warnings from
frameworks/av/media are also resolved.

There is one additional test fix to clarify that the template parameter
is also a C2Param.

Test: Presubmit in main branch and manual build in downstream pixel
branch.

Change-Id: If399d4acb06795ba41bfac5ebf1f26ef76e49f06
parent 4c6e3301
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -427,7 +427,9 @@ public:
    inline bool operator==(const C2Param &o) const {
        return equals(o) && memcmp(this, &o, _mSize) == 0;
    }
#if __cplusplus < 202002
    inline bool operator!=(const C2Param &o) const { return !operator==(o); }
#endif

    /// safe(r) type cast from pointer and size
    inline static C2Param* From(void *addr, size_t len) {
+28 −8
Original line number Diff line number Diff line
@@ -212,6 +212,26 @@ protected:
    }
};

/// Define equality (and inequality) operators for params.
#if __cplusplus < 202002

#define DEFINE_EQUALITY_OPERATORS(_Type, T) \
    inline bool operator==(const _Type &o) const { \
        return this->T::operator==(o); \
    } \
    inline bool operator!=(const _Type &o) const { \
    return !operator==(o); \
    }

#else

#define DEFINE_EQUALITY_OPERATORS(_Type, T) \
    inline bool operator==(const _Type &o) const { \
        return this->T::operator==(o); \
    }

#endif

/// Define From() cast operators for params.
#define DEFINE_CAST_OPERATORS(_Type) \
    inline static _Type* From(C2Param *other) { \
@@ -404,12 +424,12 @@ public:
    /// Specialization for an input port parameter.
    struct input : public T, public S,
            public _C2StructCheck<S, ParamIndex, T::PARAM_KIND | T::Index::DIR_INPUT> {
        using T::operator!=;
        _C2_CORE_INDEX_OVERRIDE(ParamIndex)
        /// Wrapper around base structure's constructor.
        template<typename ...Args>
        inline input(const Args(&... args)) : T(sizeof(_Type), input::PARAM_TYPE), S(args...) { }

        DEFINE_EQUALITY_OPERATORS(input, T)
        DEFINE_CAST_OPERATORS(input)

    };
@@ -417,12 +437,12 @@ public:
    /// Specialization for an output port parameter.
    struct output : public T, public S,
            public _C2StructCheck<S, ParamIndex, T::PARAM_KIND | T::Index::DIR_OUTPUT> {
        using T::operator!=;
        _C2_CORE_INDEX_OVERRIDE(ParamIndex)
        /// Wrapper around base structure's constructor.
        template<typename ...Args>
        inline output(const Args(&... args)) : T(sizeof(_Type), output::PARAM_TYPE), S(args...) { }

        DEFINE_EQUALITY_OPERATORS(output, T)
        DEFINE_CAST_OPERATORS(output)
    };
};
@@ -472,7 +492,6 @@ public:
    /// Specialization for an input port parameter.
    struct input : public T,
            public _C2FlexStructCheck<S, ParamIndex, T::PARAM_KIND | T::Index::DIR_INPUT> {
        using T::operator!=;
    private:
        /// Wrapper around base structure's constructor while also specifying port/direction.
        template<typename ...Args>
@@ -482,6 +501,7 @@ public:
    public:
        S m; ///< wrapped flexible structure

        DEFINE_EQUALITY_OPERATORS(input, T)
        DEFINE_FLEXIBLE_METHODS(input, S)
        DEFINE_CAST_OPERATORS(input)
    };
@@ -489,7 +509,6 @@ public:
    /// Specialization for an output port parameter.
    struct output : public T,
            public _C2FlexStructCheck<S, ParamIndex, T::PARAM_KIND | T::Index::DIR_OUTPUT> {
        using T::operator!=;
    private:
        /// Wrapper around base structure's constructor while also specifying port/direction.
        template<typename ...Args>
@@ -499,6 +518,7 @@ public:
    public:
        S m; ///< wrapped flexible structure

        DEFINE_EQUALITY_OPERATORS(output, T)
        DEFINE_FLEXIBLE_METHODS(output, S)
        DEFINE_CAST_OPERATORS(output)
    };
@@ -553,7 +573,6 @@ public:
    struct input : public T, public S,
            public _C2StructCheck<S, ParamIndex,
                    T::PARAM_KIND | T::Index::IS_STREAM_FLAG | T::Type::DIR_INPUT> {
        using T::operator!=;
        _C2_CORE_INDEX_OVERRIDE(ParamIndex)

        /// Default constructor. Stream-ID is undefined.
@@ -565,6 +584,7 @@ public:
        /// Set stream-id. \retval true if the stream-id was successfully set.
        inline bool setStream(unsigned stream) { return C2Param::setStream(stream); }

        DEFINE_EQUALITY_OPERATORS(input, T)
        DEFINE_CAST_OPERATORS(input)
    };

@@ -572,7 +592,6 @@ public:
    struct output : public T, public S,
            public _C2StructCheck<S, ParamIndex,
                    T::PARAM_KIND | T::Index::IS_STREAM_FLAG | T::Type::DIR_OUTPUT> {
        using T::operator!=;
        _C2_CORE_INDEX_OVERRIDE(ParamIndex)

        /// Default constructor. Stream-ID is undefined.
@@ -584,6 +603,7 @@ public:
        /// Set stream-id. \retval true if the stream-id was successfully set.
        inline bool setStream(unsigned stream) { return C2Param::setStream(stream); }

        DEFINE_EQUALITY_OPERATORS(output, T)
        DEFINE_CAST_OPERATORS(output)
    };
};
@@ -640,7 +660,6 @@ public:
    struct input : public T,
            public _C2FlexStructCheck<S, ParamIndex,
                    T::PARAM_KIND | T::Index::IS_STREAM_FLAG | T::Type::DIR_INPUT> {
        using T::operator!=;
    private:
        /// Default constructor. Stream-ID is undefined.
        inline input(size_t flexCount) : T(_Type::CalcSize(flexCount), input::PARAM_TYPE) { }
@@ -655,6 +674,7 @@ public:
        /// Set stream-id. \retval true if the stream-id was successfully set.
        inline bool setStream(unsigned stream) { return C2Param::setStream(stream); }

        DEFINE_EQUALITY_OPERATORS(input, T)
        DEFINE_FLEXIBLE_METHODS(input, S)
        DEFINE_CAST_OPERATORS(input)
    };
@@ -663,7 +683,6 @@ public:
    struct output : public T,
            public _C2FlexStructCheck<S, ParamIndex,
                    T::PARAM_KIND | T::Index::IS_STREAM_FLAG | T::Type::DIR_OUTPUT> {
        using T::operator!=;
    private:
        /// Default constructor. Stream-ID is undefined.
        inline output(size_t flexCount) : T(_Type::CalcSize(flexCount), output::PARAM_TYPE) { }
@@ -678,6 +697,7 @@ public:
        /// Set stream-id. \retval true if the stream-id was successfully set.
        inline bool setStream(unsigned stream) { return C2Param::setStream(stream); }

        DEFINE_EQUALITY_OPERATORS(output, T)
        DEFINE_FLEXIBLE_METHODS(output, S)
        DEFINE_CAST_OPERATORS(output)
    };
+1 −1
Original line number Diff line number Diff line
@@ -235,7 +235,7 @@ void C2CompIntfTest::queryParamAsExpected(const T &expected) {
    // |*heapParams[0]| is a parameter value. The size of |heapParams| has to be one.
    ASSERT_EQ(1u, heapParams.size());
    EXPECT_TRUE(heapParams[0]);
    EXPECT_EQ(*heapParams[0], expected);
    EXPECT_EQ(*heapParams[0], (C2Param &)(expected));
}

template <typename T> void C2CompIntfTest::querySupportedParam() {