blob: e1fcf672db8e5b690ef2a5c45fb8067ccc1a7662 [file] [log] [blame]
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef Nullable_h
#define Nullable_h
#include "platform/heap/Handle.h"
#include "wtf/Assertions.h"
namespace blink {
enum class UnionTypeConversionMode {
Nullable,
NotNullable,
};
template <typename T>
class Nullable {
DISALLOW_NEW();
public:
Nullable() : m_value(), m_isNull(true) {}
Nullable(std::nullptr_t) : m_value(), m_isNull(true) {}
Nullable(const T& value) : m_value(value), m_isNull(false) {}
Nullable(const Nullable& other)
: m_value(other.m_value), m_isNull(other.m_isNull) {}
Nullable& operator=(const Nullable& other) {
m_value = other.m_value;
m_isNull = other.m_isNull;
return *this;
}
Nullable& operator=(std::nullptr_t) {
m_value = T();
m_isNull = true;
return *this;
}
void set(const T& value) {
m_value = value;
m_isNull = false;
}
void set(std::nullptr_t) {
m_value = T();
m_isNull = true;
}
const T& get() const {
ASSERT(!m_isNull);
return m_value;
}
T& get() {
ASSERT(!m_isNull);
return m_value;
}
bool isNull() const { return m_isNull; }
explicit operator bool() const { return !m_isNull; }
bool operator==(const Nullable& other) const {
return (m_isNull && other.m_isNull) ||
(!m_isNull && !other.m_isNull && m_value == other.m_value);
}
DEFINE_INLINE_TRACE() { TraceIfNeeded<T>::trace(visitor, m_value); }
private:
T m_value;
bool m_isNull;
};
} // namespace blink
#endif // Nullable_h