2012-03-30 21:42:20 -07: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-02 21:35:38 -07:00
|
|
|
#ifndef mozilla_dom_Nullable_h
|
|
|
|
#define mozilla_dom_Nullable_h
|
2012-03-30 21:42:20 -07:00
|
|
|
|
|
|
|
#include "mozilla/Assertions.h"
|
2012-12-18 17:16:06 -08:00
|
|
|
#include "nsTArrayForwardDeclare.h"
|
2012-12-18 17:16:05 -08:00
|
|
|
|
2012-03-30 21:42:20 -07:00
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
|
|
|
// Support for nullable types
|
|
|
|
template <typename T>
|
|
|
|
struct Nullable
|
|
|
|
{
|
|
|
|
private:
|
2012-12-18 17:16:05 -08: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-30 21:42:20 -07:00
|
|
|
bool mIsNull;
|
2012-12-18 17:16:05 -08:00
|
|
|
T mValue;
|
2012-03-30 21:42:20 -07:00
|
|
|
|
|
|
|
public:
|
|
|
|
Nullable()
|
|
|
|
: mIsNull(true)
|
|
|
|
{}
|
|
|
|
|
2012-12-18 17:16:05 -08:00
|
|
|
explicit Nullable(T aValue)
|
|
|
|
: mIsNull(false)
|
|
|
|
, mValue(aValue)
|
2012-03-30 21:42:20 -07:00
|
|
|
{}
|
|
|
|
|
|
|
|
void SetValue(T aValue) {
|
|
|
|
mValue = aValue;
|
|
|
|
mIsNull = false;
|
|
|
|
}
|
|
|
|
|
2012-04-18 09:01:18 -07: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-30 21:42:20 -07:00
|
|
|
void SetNull() {
|
|
|
|
mIsNull = true;
|
|
|
|
}
|
|
|
|
|
2012-04-25 21:42:37 -07:00
|
|
|
const T& Value() const {
|
2012-03-30 21:42:20 -07:00
|
|
|
MOZ_ASSERT(!mIsNull);
|
|
|
|
return mValue;
|
|
|
|
}
|
|
|
|
|
2012-05-15 11:23:29 -07:00
|
|
|
T& Value() {
|
|
|
|
MOZ_ASSERT(!mIsNull);
|
|
|
|
return mValue;
|
|
|
|
}
|
|
|
|
|
2012-03-30 21:42:20 -07:00
|
|
|
bool IsNull() const {
|
|
|
|
return mIsNull;
|
|
|
|
}
|
2012-12-18 17:16:05 -08:00
|
|
|
|
2013-09-10 21:18:36 -07: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-18 17:16:05 -08:00
|
|
|
// Make it possible to use a const Nullable of an array type with other
|
|
|
|
// array types.
|
|
|
|
template<typename U>
|
2012-12-18 17:16:06 -08: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-18 17:16:05 -08:00
|
|
|
(void)arr;
|
2012-12-18 17:16:06 -08:00
|
|
|
return *reinterpret_cast<const Nullable< nsTArray<U> >*>(this);
|
2012-12-18 17:16:05 -08: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-30 21:42:20 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|
|
|
|
|
2012-05-02 21:35:38 -07:00
|
|
|
#endif /* mozilla_dom_Nullable_h */
|