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"
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
|
|
|
// Support for nullable types
|
|
|
|
template <typename T>
|
|
|
|
struct Nullable
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
T mValue;
|
|
|
|
bool mIsNull;
|
|
|
|
|
|
|
|
public:
|
|
|
|
Nullable()
|
|
|
|
: mIsNull(true)
|
|
|
|
{}
|
|
|
|
|
|
|
|
Nullable(T aValue)
|
|
|
|
: mValue(aValue)
|
|
|
|
, mIsNull(false)
|
|
|
|
{}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|
|
|
|
|
2012-05-02 21:35:38 -07:00
|
|
|
#endif /* mozilla_dom_Nullable_h */
|