• 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 "wtf/Assertions.h"
9 
10 namespace WebCore {
11 
12 template <typename T>
13 class Nullable {
14 public:
Nullable()15     Nullable()
16         : m_value()
17         , m_isNull(true) { }
18 
Nullable(const T & value)19     Nullable(const T& value)
20         : m_value(value)
21         , m_isNull(false) { }
22 
Nullable(const Nullable & other)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 
get()34     T get() const { ASSERT(!m_isNull); return m_value; }
isNull()35     bool isNull() const { return m_isNull; }
36 
37     operator bool() const { return !m_isNull && m_value; }
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
52