зеркало из https://github.com/mozilla/gecko-dev.git
Bug 1184839 - Provide an overloaded == operator for mozilla::Variant; r=Waldo
This commit is contained in:
Родитель
5311ff1ffc
Коммит
bdd6b06c87
|
@ -114,6 +114,13 @@ struct VariantImplementation<N, T> {
|
|||
static void destroy(Variant& aV) {
|
||||
aV.template as<T>().~T();
|
||||
}
|
||||
|
||||
template<typename Variant>
|
||||
static bool
|
||||
equal(const Variant& aLhs, const Variant& aRhs)
|
||||
{
|
||||
return aLhs.template as<T>() == aRhs.template as<T>();
|
||||
}
|
||||
};
|
||||
|
||||
// VariantImplementation for some variant type T.
|
||||
|
@ -154,6 +161,16 @@ struct VariantImplementation<N, T, Ts...>
|
|||
Next::destroy(aV);
|
||||
}
|
||||
}
|
||||
|
||||
template<typename Variant>
|
||||
static bool equal(const Variant& aLhs, const Variant& aRhs) {
|
||||
if (aLhs.template is<T>()) {
|
||||
MOZ_ASSERT(aRhs.template is<T>());
|
||||
return aLhs.template as<T>() == aRhs.template as<T>();
|
||||
} else {
|
||||
return Next::equal(aLhs, aRhs);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace detail
|
||||
|
@ -314,6 +331,23 @@ public:
|
|||
return Impl::template tag<T>() == tag;
|
||||
}
|
||||
|
||||
/**
|
||||
* Operator == overload that defers to the variant type's operator==
|
||||
* implementation if the rhs is tagged as the same type as this one.
|
||||
*/
|
||||
bool operator==(const Variant& aRhs) const {
|
||||
return tag == aRhs.tag && Impl::equal(*this, aRhs);
|
||||
}
|
||||
|
||||
/**
|
||||
* Operator != overload that defers to the negation of the variant type's
|
||||
* operator== implementation if the rhs is tagged as the same type as this
|
||||
* one.
|
||||
*/
|
||||
bool operator!=(const Variant& aRhs) const {
|
||||
return !(*this == aRhs);
|
||||
}
|
||||
|
||||
// Accessors for working with the contained variant value.
|
||||
|
||||
/** Mutable reference. */
|
||||
|
|
|
@ -93,6 +93,36 @@ testDestructor()
|
|||
MOZ_RELEASE_ASSERT(Destroyer::destroyedCount == 2); // d is destroyed.
|
||||
}
|
||||
|
||||
static void
|
||||
testEquality()
|
||||
{
|
||||
printf("testEquality\n");
|
||||
using V = Variant<char, int>;
|
||||
|
||||
V v0('a');
|
||||
V v1('b');
|
||||
V v2('b');
|
||||
V v3(42);
|
||||
V v4(27);
|
||||
V v5(27);
|
||||
V v6(int('b'));
|
||||
|
||||
MOZ_RELEASE_ASSERT(v0 != v1);
|
||||
MOZ_RELEASE_ASSERT(v1 == v2);
|
||||
MOZ_RELEASE_ASSERT(v2 != v3);
|
||||
MOZ_RELEASE_ASSERT(v3 != v4);
|
||||
MOZ_RELEASE_ASSERT(v4 == v5);
|
||||
MOZ_RELEASE_ASSERT(v1 != v6);
|
||||
|
||||
MOZ_RELEASE_ASSERT(v0 == v0);
|
||||
MOZ_RELEASE_ASSERT(v1 == v1);
|
||||
MOZ_RELEASE_ASSERT(v2 == v2);
|
||||
MOZ_RELEASE_ASSERT(v3 == v3);
|
||||
MOZ_RELEASE_ASSERT(v4 == v4);
|
||||
MOZ_RELEASE_ASSERT(v5 == v5);
|
||||
MOZ_RELEASE_ASSERT(v6 == v6);
|
||||
}
|
||||
|
||||
int
|
||||
main()
|
||||
{
|
||||
|
@ -100,6 +130,7 @@ main()
|
|||
testCopy();
|
||||
testMove();
|
||||
testDestructor();
|
||||
testEquality();
|
||||
|
||||
printf("TestVariant OK!\n");
|
||||
return 0;
|
||||
|
|
Загрузка…
Ссылка в новой задаче