2012-03-31 08:42:20 +04: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 08:35:38 +04:00
|
|
|
#ifndef mozilla_dom_Nullable_h
|
|
|
|
#define mozilla_dom_Nullable_h
|
2012-03-31 08:42:20 +04:00
|
|
|
|
|
|
|
#include "mozilla/Assertions.h"
|
2012-12-19 05:16:06 +04:00
|
|
|
#include "nsTArrayForwardDeclare.h"
|
2014-05-24 01:32:38 +04:00
|
|
|
#include "mozilla/Move.h"
|
2014-08-29 00:37:00 +04:00
|
|
|
#include "mozilla/Maybe.h"
|
2012-12-19 05:16:05 +04:00
|
|
|
|
2014-06-24 00:03:58 +04:00
|
|
|
class nsCycleCollectionTraversalCallback;
|
|
|
|
|
2012-03-31 08:42:20 +04:00
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
|
|
|
// Support for nullable types
|
|
|
|
template <typename T>
|
|
|
|
struct Nullable
|
|
|
|
{
|
|
|
|
private:
|
2014-08-29 00:37:00 +04:00
|
|
|
Maybe<T> mValue;
|
2012-03-31 08:42:20 +04:00
|
|
|
|
|
|
|
public:
|
|
|
|
Nullable()
|
2014-08-29 00:37:00 +04:00
|
|
|
: mValue()
|
2012-03-31 08:42:20 +04:00
|
|
|
{}
|
|
|
|
|
2012-12-19 05:16:05 +04:00
|
|
|
explicit Nullable(T aValue)
|
2014-08-29 00:37:00 +04:00
|
|
|
: mValue()
|
|
|
|
{
|
|
|
|
mValue.emplace(aValue);
|
|
|
|
}
|
2012-03-31 08:42:20 +04:00
|
|
|
|
2014-05-24 01:32:38 +04:00
|
|
|
explicit Nullable(Nullable<T>&& aOther)
|
2014-08-29 00:37:00 +04:00
|
|
|
: mValue(mozilla::Move(aOther.mValue))
|
2014-05-24 01:32:38 +04:00
|
|
|
{}
|
|
|
|
|
|
|
|
Nullable(const Nullable<T>& aOther)
|
2014-08-29 00:37:00 +04:00
|
|
|
: mValue(aOther.mValue)
|
2014-05-24 01:32:38 +04:00
|
|
|
{}
|
|
|
|
|
|
|
|
void operator=(const Nullable<T>& aOther)
|
|
|
|
{
|
|
|
|
mValue = aOther.mValue;
|
|
|
|
}
|
|
|
|
|
2012-03-31 08:42:20 +04:00
|
|
|
void SetValue(T aValue) {
|
2014-08-29 00:37:00 +04:00
|
|
|
mValue.reset();
|
|
|
|
mValue.emplace(aValue);
|
2012-03-31 08:42:20 +04:00
|
|
|
}
|
|
|
|
|
2012-04-18 20:01:18 +04: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() {
|
2014-08-29 00:37:00 +04:00
|
|
|
if (mValue.isNothing()) {
|
|
|
|
mValue.emplace();
|
|
|
|
}
|
|
|
|
return mValue.ref();
|
2012-04-18 20:01:18 +04:00
|
|
|
}
|
|
|
|
|
2012-03-31 08:42:20 +04:00
|
|
|
void SetNull() {
|
2014-08-29 00:37:00 +04:00
|
|
|
mValue.reset();
|
2012-03-31 08:42:20 +04:00
|
|
|
}
|
|
|
|
|
2012-04-26 08:42:37 +04:00
|
|
|
const T& Value() const {
|
2014-08-29 00:37:00 +04:00
|
|
|
return mValue.ref();
|
2012-03-31 08:42:20 +04:00
|
|
|
}
|
|
|
|
|
2012-05-15 22:23:29 +04:00
|
|
|
T& Value() {
|
2014-08-29 00:37:00 +04:00
|
|
|
return mValue.ref();
|
2012-05-15 22:23:29 +04:00
|
|
|
}
|
|
|
|
|
2012-03-31 08:42:20 +04:00
|
|
|
bool IsNull() const {
|
2014-08-29 00:37:00 +04:00
|
|
|
return mValue.isNothing();
|
2012-03-31 08:42:20 +04:00
|
|
|
}
|
2012-12-19 05:16:05 +04:00
|
|
|
|
2013-09-11 08:18:36 +04:00
|
|
|
bool Equals(const Nullable<T>& aOtherNullable) const
|
|
|
|
{
|
2014-08-29 00:37:00 +04:00
|
|
|
return mValue == aOtherNullable.mValue;
|
2013-09-11 08:18:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool operator==(const Nullable<T>& aOtherNullable) const
|
|
|
|
{
|
|
|
|
return Equals(aOtherNullable);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator!=(const Nullable<T>& aOtherNullable) const
|
|
|
|
{
|
|
|
|
return !Equals(aOtherNullable);
|
|
|
|
}
|
2012-03-31 08:42:20 +04:00
|
|
|
};
|
|
|
|
|
2014-06-24 00:03:58 +04: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 08:42:20 +04:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|
|
|
|
|
2012-05-03 08:35:38 +04:00
|
|
|
#endif /* mozilla_dom_Nullable_h */
|