• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "platform/heap/Handle.h"
9 #include "wtf/Assertions.h"
10 
11 namespace blink {
12 
13 template <typename T>
14 class Nullable {
15     DISALLOW_ALLOCATION();
16 public:
Nullable()17     Nullable()
18         : m_value()
19         , m_isNull(true) { }
20 
Nullable(const T & value)21     Nullable(const T& value)
22         : m_value(value)
23         , m_isNull(false) { }
24 
Nullable(const Nullable & other)25     Nullable(const Nullable& other)
26         : m_value(other.m_value)
27         , m_isNull(other.m_isNull) { }
28 
29     Nullable& operator=(const Nullable& other)
30     {
31         m_value = other.m_value;
32         m_isNull = other.m_isNull;
33         return *this;
34     }
35 
set(const T & value)36     void set(const T& value)
37     {
38         m_value = value;
39         m_isNull = false;
40     }
get()41     const T& get() const { ASSERT(!m_isNull); return m_value; }
get()42     T& get() { ASSERT(!m_isNull); return m_value; }
isNull()43     bool isNull() const { return m_isNull; }
44 
45     // See comment in RefPtr.h about what UnspecifiedBoolType is.
46     typedef const T* UnspecifiedBoolType;
UnspecifiedBoolType()47     operator UnspecifiedBoolType() const { return m_isNull ? 0 : &m_value; }
48 
49     bool operator==(const Nullable& other) const
50     {
51         return (m_isNull && other.m_isNull) || (!m_isNull && !other.m_isNull && m_value == other.m_value);
52     }
53 
trace(Visitor * visitor)54     void trace(Visitor* visitor)
55     {
56         TraceIfNeeded<T>::trace(visitor, &m_value);
57     }
58 
59 private:
60     T m_value;
61     bool m_isNull;
62 };
63 
64 } // namespace blink
65 
66 #endif // Nullable_h
67