Chromium Code Reviews| 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 |
| 11 namespace blink { | 11 namespace blink { |
| 12 | 12 |
| 13 template <typename T> | 13 template <typename T> |
| 14 class Nullable { | 14 class Nullable { |
| 15 DISALLOW_ALLOCATION(); | 15 DISALLOW_ALLOCATION(); |
| 16 public: | 16 public: |
| 17 Nullable() | 17 Nullable() |
| 18 : m_value() | 18 : m_value() |
| 19 , m_isNull(true) { } | 19 , m_isNull(true) { } |
| 20 | 20 |
| 21 Nullable(const T& value) | 21 Nullable(const T& value) |
|
haraken
2014/08/13 13:07:10
Probably add 'explicit' ?
Jens Widell
2014/08/13 13:11:08
That would require changes to for instance WebGLRe
haraken
2014/08/13 13:12:54
Makes sense.
| |
| 22 : m_value(value) | 22 : m_value(value) |
| 23 , m_isNull(false) { } | 23 , m_isNull(false) { } |
| 24 | 24 |
| 25 Nullable(const Nullable& other) | 25 Nullable(const Nullable& other) |
| 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 operator bool() const { return !m_isNull && m_value; } |
|
haraken
2014/08/13 13:07:10
In order to allow implicit conversion to bool but
bashi
2014/08/13 13:45:56
Will do.
bashi
2014/08/15 02:43:53
It turns out we need a meta-function or need to re
| |
| 40 | 46 |
| 41 bool operator==(const Nullable& other) const | 47 bool operator==(const Nullable& other) const |
| 42 { | 48 { |
| 43 return (m_isNull && other.m_isNull) || (!m_isNull && !other.m_isNull && m_value == other.m_value); | 49 return (m_isNull && other.m_isNull) || (!m_isNull && !other.m_isNull && m_value == other.m_value); |
| 44 } | 50 } |
| 45 | 51 |
| 46 void trace(Visitor* visitor) | 52 void trace(Visitor* visitor) |
| 47 { | 53 { |
| 48 TraceIfNeeded<T>::trace(visitor, &m_value); | 54 TraceIfNeeded<T>::trace(visitor, &m_value); |
| 49 } | 55 } |
| 50 | 56 |
| 51 private: | 57 private: |
| 52 T m_value; | 58 T m_value; |
| 53 bool m_isNull; | 59 bool m_isNull; |
| 54 }; | 60 }; |
| 55 | 61 |
| 56 } // namespace blink | 62 } // namespace blink |
| 57 | 63 |
| 58 #endif // Nullable_h | 64 #endif // Nullable_h |
| OLD | NEW |