Bug 1363676. P1 - use Variant as the internal storage of ResolveOrRejectValue. r=gerald

MozReview-Commit-ID: 4B3M3hvfvyz

--HG--
extra : rebase_source : 245add2750849ccb4c5aa0f12bb03ba0679c6c1f
extra : intermediate-source : 5e8c6df9ed9b3c66162a85f2ae101fa77eb3bfe0
extra : source : 4269edd719b617c6c3d9be97b77e91980650c934
This commit is contained in:
JW Wang 2017-05-09 18:24:18 +08:00
Родитель a8e2bb1720
Коммит 394ed3f08b
1 изменённых файлов: 36 добавлений и 9 удалений

Просмотреть файл

@ -15,6 +15,7 @@
#include "mozilla/Monitor.h"
#include "mozilla/Tuple.h"
#include "mozilla/TypeTraits.h"
#include "mozilla/Variant.h"
#include "nsTArray.h"
#include "nsThreadUtils.h"
@ -137,19 +138,32 @@ public:
typedef RejectValueT RejectValueType;
class ResolveOrRejectValue
{
template <int, typename T>
struct Holder
{
template <typename... Args>
explicit Holder(Args&&... aArgs) : mData(Forward<Args>(aArgs)...) { }
T mData;
};
// Ensure Holder<0, T1> and Holder<1, T2> are different types
// which is required by Variant.
using ResolveValueHolder = Holder<0, ResolveValueType>;
using RejectValueHolder = Holder<1, RejectValueType>;
public:
template<typename ResolveValueType_>
void SetResolve(ResolveValueType_&& aResolveValue)
{
MOZ_ASSERT(IsNothing());
mResolveValue.emplace(Forward<ResolveValueType_>(aResolveValue));
mValue = AsVariant(ResolveValueHolder(Forward<ResolveValueType_>(aResolveValue)));
}
template<typename RejectValueType_>
void SetReject(RejectValueType_&& aRejectValue)
{
MOZ_ASSERT(IsNothing());
mRejectValue.emplace(Forward<RejectValueType_>(aRejectValue));
mValue = AsVariant(RejectValueHolder(Forward<RejectValueType_>(aRejectValue)));
}
template<typename ResolveValueType_>
@ -168,16 +182,29 @@ public:
return val;
}
bool IsResolve() const { return mResolveValue.isSome(); }
bool IsReject() const { return mRejectValue.isSome(); }
bool IsNothing() const { return mResolveValue.isNothing() && mRejectValue.isNothing(); }
bool IsResolve() const { return mValue.template is<ResolveValueHolder>(); }
bool IsReject() const { return mValue.template is<RejectValueHolder>(); }
bool IsNothing() const { return mValue.template is<Nothing>(); }
const ResolveValueType& ResolveValue() const { return mResolveValue.ref(); }
const RejectValueType& RejectValue() const { return mRejectValue.ref(); }
const ResolveValueType& ResolveValue() const
{
return mValue.template as<ResolveValueHolder>().mData;
}
ResolveValueType& ResolveValue()
{
return mValue.template as<ResolveValueHolder>().mData;
}
const RejectValueType& RejectValue() const
{
return mValue.template as<RejectValueHolder>().mData;
}
RejectValueType& RejectValue()
{
return mValue.template as<RejectValueHolder>().mData;
}
private:
Maybe<ResolveValueType> mResolveValue;
Maybe<RejectValueType> mRejectValue;
Variant<Nothing, ResolveValueHolder, RejectValueHolder> mValue = AsVariant(Nothing{});
};
protected: