Skip to content

Commit

Permalink
Bug 1363676. P1 - use Variant as the internal storage of ResolveOrRej…
Browse files Browse the repository at this point in the history
…ectValue. r=gerald

MozReview-Commit-ID: 4B3M3hvfvyz

--HG--
extra : rebase_source : 245add2750849ccb4c5aa0f12bb03ba0679c6c1f
extra : intermediate-source : 5e8c6df9ed9b3c66162a85f2ae101fa77eb3bfe0
extra : source : 4269edd719b617c6c3d9be97b77e91980650c934
  • Loading branch information
jwwang committed May 9, 2017
1 parent a8e2bb1 commit 394ed3f
Showing 1 changed file with 36 additions and 9 deletions.
45 changes: 36 additions & 9 deletions xpcom/threads/MozPromise.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -137,19 +138,32 @@ class MozPromise : public MozPromiseRefcountable
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_>
Expand All @@ -168,16 +182,29 @@ class MozPromise : public MozPromiseRefcountable
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:
Expand Down

0 comments on commit 394ed3f

Please sign in to comment.