OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef Nullable_h | 5 #ifndef Nullable_h |
6 #define Nullable_h | 6 #define Nullable_h |
7 | 7 |
8 #include "platform/heap/Handle.h" | 8 #include "platform/heap/Handle.h" |
9 #include "wtf/Assertions.h" | 9 #include "wtf/Assertions.h" |
10 | 10 |
(...skipping 15 matching lines...) Expand all Loading... |
26 : m_value(other.m_value) | 26 : m_value(other.m_value) |
27 , m_isNull(other.m_isNull) { } | 27 , m_isNull(other.m_isNull) { } |
28 | 28 |
29 Nullable& operator=(const Nullable& other) | 29 Nullable& operator=(const Nullable& other) |
30 { | 30 { |
31 m_value = other.m_value; | 31 m_value = other.m_value; |
32 m_isNull = other.m_isNull; | 32 m_isNull = other.m_isNull; |
33 return *this; | 33 return *this; |
34 } | 34 } |
35 | 35 |
| 36 void set(const T& value) |
| 37 { |
| 38 m_value = value; |
| 39 m_isNull = false; |
| 40 } |
36 const T& get() const { ASSERT(!m_isNull); return m_value; } | 41 const T& get() const { ASSERT(!m_isNull); return m_value; } |
| 42 T& get() { ASSERT(!m_isNull); return m_value; } |
37 bool isNull() const { return m_isNull; } | 43 bool isNull() const { return m_isNull; } |
38 | 44 |
39 operator bool() const { return !m_isNull && m_value; } | 45 // See comment in RefPtr.h about what UnspecifiedBoolType is. |
| 46 typedef const T* UnspecifiedBoolType; |
| 47 operator UnspecifiedBoolType() const { return m_isNull ? 0 : &m_value; } |
40 | 48 |
41 bool operator==(const Nullable& other) const | 49 bool operator==(const Nullable& other) const |
42 { | 50 { |
43 return (m_isNull && other.m_isNull) || (!m_isNull && !other.m_isNull &&
m_value == other.m_value); | 51 return (m_isNull && other.m_isNull) || (!m_isNull && !other.m_isNull &&
m_value == other.m_value); |
44 } | 52 } |
45 | 53 |
46 void trace(Visitor* visitor) | 54 void trace(Visitor* visitor) |
47 { | 55 { |
48 TraceIfNeeded<T>::trace(visitor, &m_value); | 56 TraceIfNeeded<T>::trace(visitor, &m_value); |
49 } | 57 } |
50 | 58 |
51 private: | 59 private: |
52 T m_value; | 60 T m_value; |
53 bool m_isNull; | 61 bool m_isNull; |
54 }; | 62 }; |
55 | 63 |
56 } // namespace blink | 64 } // namespace blink |
57 | 65 |
58 #endif // Nullable_h | 66 #endif // Nullable_h |
OLD | NEW |