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 Optional_h | |
6 #define Optional_h | |
7 | |
8 #include "wtf/Assertions.h" | |
9 | |
10 namespace WebCore { | |
11 | |
12 template <typename T> | |
13 class Optional { | |
14 public: | |
15 Optional(const T& value, bool isMissing) | |
16 : m_value(value) | |
17 , m_isMissing(isMissing) { } | |
18 | |
19 Optional(const Optional& other) | |
20 : m_value(other.m_value) | |
21 , m_isMissing(other.m_isMissing) { } | |
22 | |
23 template <typename U> | |
24 Optional(const Optional<U>& other) | |
Jens Widell
2014/06/17 09:53:37
This operator might be useful (or not work at all)
| |
25 : m_value(other.m_value) | |
26 , m_isMissing(other.m_isMissing) { } | |
27 | |
28 Optional& operator=(const Optional& other) | |
29 { | |
30 m_value = other.m_value; | |
31 m_isMissing = other.m_isMissing; | |
32 return *this; | |
33 } | |
34 | |
35 T get() const { ASSERT(!m_isMissing); return m_value; } | |
36 bool isMissing() const { return m_isMissing; } | |
37 | |
38 operator bool() const { return !m_isMissing && m_value; } | |
39 | |
40 bool operator==(const Optional& other) const | |
41 { | |
42 return (m_isMissing && other.m_isMissing) || (!m_isMissing && !other.m_i sMissing && m_value == other.m_value); | |
43 } | |
44 | |
45 private: | |
46 T m_value; | |
47 bool m_isMissing; | |
48 }; | |
49 | |
50 template <typename T> | |
51 Optional<T> makeOptional(T value, bool isMissing) | |
52 { | |
53 return Optional<T>(value, isMissing); | |
54 } | |
55 | |
56 } // namespace WebCore | |
57 | |
58 #endif // Optional_h | |
OLD | NEW |