2012-03-31 04:42:20 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-*/
|
|
|
|
/* vim: set ts=2 sw=2 et tw=79: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
2012-05-03 04:35:38 +00:00
|
|
|
#ifndef mozilla_dom_Nullable_h
|
|
|
|
#define mozilla_dom_Nullable_h
|
2012-03-31 04:42:20 +00:00
|
|
|
|
|
|
|
#include "mozilla/Assertions.h"
|
2012-12-19 01:16:06 +00:00
|
|
|
#include "nsTArrayForwardDeclare.h"
|
2014-05-23 21:32:38 +00:00
|
|
|
#include "mozilla/Move.h"
|
2012-12-19 01:16:05 +00:00
|
|
|
|
2014-06-23 20:03:58 +00:00
|
|
|
class nsCycleCollectionTraversalCallback;
|
|
|
|
|
2012-03-31 04:42:20 +00:00
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
|
|
|
// Support for nullable types
|
|
|
|
template <typename T>
|
|
|
|
struct Nullable
|
|
|
|
{
|
|
|
|
private:
|
2012-12-19 01:16:05 +00:00
|
|
|
// mIsNull MUST COME FIRST because otherwise the casting in our array
|
|
|
|
// conversion operators would shift where it is found in the struct.
|
2012-03-31 04:42:20 +00:00
|
|
|
bool mIsNull;
|
2012-12-19 01:16:05 +00:00
|
|
|
T mValue;
|
2012-03-31 04:42:20 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
Nullable()
|
|
|
|
: mIsNull(true)
|
|
|
|
{}
|
|
|
|
|
2012-12-19 01:16:05 +00:00
|
|
|
explicit Nullable(T aValue)
|
|
|
|
: mIsNull(false)
|
|
|
|
, mValue(aValue)
|
2012-03-31 04:42:20 +00:00
|
|
|
{}
|
|
|
|
|
2014-05-23 21:32:38 +00:00
|
|
|
explicit Nullable(Nullable<T>&& aOther)
|
|
|
|
: mIsNull(aOther.mIsNull)
|
|
|
|
, mValue(mozilla::Move(aOther.mValue))
|
|
|
|
{}
|
|
|
|
|
|
|
|
Nullable(const Nullable<T>& aOther)
|
|
|
|
: mIsNull(aOther.mIsNull)
|
|
|
|
, mValue(aOther.mValue)
|
|
|
|
{}
|
|
|
|
|
|
|
|
void operator=(const Nullable<T>& aOther)
|
|
|
|
{
|
|
|
|
mIsNull = aOther.mIsNull;
|
|
|
|
mValue = aOther.mValue;
|
|
|
|
}
|
|
|
|
|
2012-03-31 04:42:20 +00:00
|
|
|
void SetValue(T aValue) {
|
|
|
|
mValue = aValue;
|
|
|
|
mIsNull = false;
|
|
|
|
}
|
|
|
|
|
2012-04-18 16:01:18 +00:00
|
|
|
// For cases when |T| is some type with nontrivial copy behavior, we may want
|
|
|
|
// to get a reference to our internal copy of T and work with it directly
|
|
|
|
// instead of relying on the copying version of SetValue().
|
|
|
|
T& SetValue() {
|
|
|
|
mIsNull = false;
|
|
|
|
return mValue;
|
|
|
|
}
|
|
|
|
|
2012-03-31 04:42:20 +00:00
|
|
|
void SetNull() {
|
|
|
|
mIsNull = true;
|
|
|
|
}
|
|
|
|
|
2012-04-26 04:42:37 +00:00
|
|
|
const T& Value() const {
|
2012-03-31 04:42:20 +00:00
|
|
|
MOZ_ASSERT(!mIsNull);
|
|
|
|
return mValue;
|
|
|
|
}
|
|
|
|
|
2012-05-15 18:23:29 +00:00
|
|
|
T& Value() {
|
|
|
|
MOZ_ASSERT(!mIsNull);
|
|
|
|
return mValue;
|
|
|
|
}
|
|
|
|
|
2012-03-31 04:42:20 +00:00
|
|
|
bool IsNull() const {
|
|
|
|
return mIsNull;
|
|
|
|
}
|
2012-12-19 01:16:05 +00:00
|
|
|
|
2013-09-11 04:18:36 +00:00
|
|
|
bool Equals(const Nullable<T>& aOtherNullable) const
|
|
|
|
{
|
|
|
|
return (mIsNull && aOtherNullable.mIsNull) ||
|
|
|
|
(!mIsNull && !aOtherNullable.mIsNull &&
|
|
|
|
mValue == aOtherNullable.mValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator==(const Nullable<T>& aOtherNullable) const
|
|
|
|
{
|
|
|
|
return Equals(aOtherNullable);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator!=(const Nullable<T>& aOtherNullable) const
|
|
|
|
{
|
|
|
|
return !Equals(aOtherNullable);
|
|
|
|
}
|
|
|
|
|
2012-12-19 01:16:05 +00:00
|
|
|
// Make it possible to use a const Nullable of an array type with other
|
|
|
|
// array types.
|
|
|
|
template<typename U>
|
2012-12-19 01:16:06 +00:00
|
|
|
operator const Nullable< nsTArray<U> >&() const {
|
|
|
|
// Make sure that T is ok to reinterpret to nsTArray<U>
|
|
|
|
const nsTArray<U>& arr = mValue;
|
2012-12-19 01:16:05 +00:00
|
|
|
(void)arr;
|
2012-12-19 01:16:06 +00:00
|
|
|
return *reinterpret_cast<const Nullable< nsTArray<U> >*>(this);
|
2012-12-19 01:16:05 +00:00
|
|
|
}
|
|
|
|
template<typename U>
|
|
|
|
operator const Nullable< FallibleTArray<U> >&() const {
|
|
|
|
// Make sure that T is ok to reinterpret to FallibleTArray<U>
|
|
|
|
const FallibleTArray<U>& arr = mValue;
|
|
|
|
(void)arr;
|
|
|
|
return *reinterpret_cast<const Nullable< FallibleTArray<U> >*>(this);
|
|
|
|
}
|
2012-03-31 04:42:20 +00:00
|
|
|
};
|
|
|
|
|
2014-06-23 20:03:58 +00:00
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
void
|
|
|
|
ImplCycleCollectionTraverse(nsCycleCollectionTraversalCallback& aCallback,
|
|
|
|
Nullable<T>& aNullable,
|
|
|
|
const char* aName,
|
|
|
|
uint32_t aFlags = 0)
|
|
|
|
{
|
|
|
|
if (!aNullable.IsNull()) {
|
|
|
|
ImplCycleCollectionTraverse(aCallback, aNullable.Value(), aName, aFlags);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
void
|
|
|
|
ImplCycleCollectionUnlink(Nullable<T>& aNullable)
|
|
|
|
{
|
|
|
|
if (!aNullable.IsNull()) {
|
|
|
|
ImplCycleCollectionUnlink(aNullable.Value());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-31 04:42:20 +00:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|
|
|
|
|
2012-05-03 04:35:38 +00:00
|
|
|
#endif /* mozilla_dom_Nullable_h */
|