Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef Nullable_h | |
| 6 #define Nullable_h | |
| 7 | |
| 8 #include "wtf/Assertions.h" | |
| 9 | |
| 10 namespace WebCore { | |
|
jsbell
2014/02/27 20:59:06
Could be in wtf, but I started conservatively.
| |
| 11 | |
| 12 template <typename T> | |
| 13 class Nullable { | |
| 14 public: | |
| 15 Nullable() | |
|
jsbell
2014/02/27 20:59:06
I also considered Nullable<type>::null() as a stat
| |
| 16 : m_value() | |
| 17 , m_isNull(true) { } | |
| 18 | |
| 19 Nullable(const T& value) | |
| 20 : m_value(value) | |
| 21 , m_isNull(false) { } | |
| 22 | |
| 23 Nullable(const Nullable& other) | |
| 24 : m_value(other.m_value) | |
| 25 , m_isNull(other.m_isNull) { } | |
| 26 | |
| 27 Nullable& operator=(const Nullable& other) | |
| 28 { | |
| 29 m_value = other.m_value; | |
| 30 m_isNull = other.m_isNull; | |
| 31 return *this; | |
| 32 } | |
| 33 | |
| 34 T get() const { ASSERT(!m_isNull); return m_value; } | |
|
jsbell
2014/02/27 20:59:06
I did NOT expose an `operator T()` conversion, fav
| |
| 35 bool isNull() const { return m_isNull; } | |
| 36 | |
| 37 operator bool() const { return !m_isNull && m_value; } | |
|
jsbell
2014/02/27 20:59:06
This was required by the Dictionary. I'll admit I
| |
| 38 | |
| 39 bool operator==(const Nullable& other) const | |
| 40 { | |
| 41 return (m_isNull && other.m_isNull) || (!m_isNull && !other.m_isNull && m_value == other.m_value); | |
| 42 } | |
| 43 | |
| 44 private: | |
| 45 T m_value; | |
| 46 bool m_isNull; | |
| 47 }; | |
| 48 | |
| 49 } // namespace WebCore | |
| 50 | |
| 51 #endif // Nullable_h | |
| OLD | NEW |