2015-05-03 22:32:37 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-03-31 08:42:20 +04:00
|
|
|
/* 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
|
|
|
|
2017-01-17 00:55:29 +03:00
|
|
|
#include <ostream>
|
|
|
|
|
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
|
|
|
{}
|
|
|
|
|
2016-01-30 20:05:36 +03:00
|
|
|
MOZ_IMPLICIT Nullable(const decltype(nullptr)&)
|
|
|
|
: mValue()
|
|
|
|
{}
|
|
|
|
|
2015-09-17 19:16:43 +03:00
|
|
|
explicit Nullable(const T& aValue)
|
2014-08-29 00:37:00 +04:00
|
|
|
: mValue()
|
|
|
|
{
|
|
|
|
mValue.emplace(aValue);
|
|
|
|
}
|
2012-03-31 08:42:20 +04:00
|
|
|
|
2016-01-30 20:05:36 +03:00
|
|
|
MOZ_IMPLICIT Nullable(T&& aValue)
|
2015-09-17 19:16:43 +03:00
|
|
|
: mValue()
|
|
|
|
{
|
2018-05-30 22:15:35 +03:00
|
|
|
mValue.emplace(std::move(aValue));
|
2015-09-17 19:16:43 +03:00
|
|
|
}
|
|
|
|
|
2015-09-03 17:51:42 +03:00
|
|
|
Nullable(Nullable<T>&& aOther)
|
2018-05-30 22:15:35 +03:00
|
|
|
: mValue(std::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;
|
|
|
|
}
|
|
|
|
|
2015-09-17 19:16:43 +03:00
|
|
|
void SetValue(const T& aArgs)
|
|
|
|
{
|
|
|
|
mValue.reset();
|
|
|
|
mValue.emplace(aArgs);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetValue(T&& aArgs)
|
|
|
|
{
|
2014-08-29 00:37:00 +04:00
|
|
|
mValue.reset();
|
2018-05-30 22:15:35 +03:00
|
|
|
mValue.emplace(std::move(aArgs));
|
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);
|
|
|
|
}
|
2017-01-17 00:55:29 +03:00
|
|
|
|
|
|
|
friend std::ostream& operator<<(std::ostream& aStream,
|
|
|
|
const Nullable& aNullable) {
|
|
|
|
return aStream << aNullable.mValue;
|
|
|
|
}
|
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 */
|