OLD | NEW |
| (Empty) |
1 // Copyright 2016 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 Maybe_h | |
6 #define Maybe_h | |
7 | |
8 #include "platform/inspector_protocol/Platform.h" | |
9 #include "platform/inspector_protocol/String16.h" | |
10 | |
11 #include <memory> | |
12 | |
13 namespace blink { | |
14 namespace protocol { | |
15 | |
16 template<typename T> | |
17 class Maybe { | |
18 public: | |
19 Maybe() : m_value() { } | |
20 Maybe(std::unique_ptr<T> value) : m_value(std::move(value)) { } | |
21 void operator=(std::unique_ptr<T> value) { m_value = std::move(value); } | |
22 T* fromJust() const { DCHECK(m_value); return m_value.get(); } | |
23 T* fromMaybe(T* defaultValue) const { return m_value ? m_value.get() : defau
ltValue; } | |
24 bool isJust() const { return !!m_value; } | |
25 std::unique_ptr<T> takeJust() { DCHECK(m_value); return m_value.release(); } | |
26 private: | |
27 std::unique_ptr<T> m_value; | |
28 }; | |
29 | |
30 template<typename T> | |
31 class MaybeBase { | |
32 public: | |
33 MaybeBase() : m_isJust(false) { } | |
34 MaybeBase(T value) : m_isJust(true), m_value(value) { } | |
35 void operator=(T value) { m_value = value; m_isJust = true; } | |
36 T fromJust() const { DCHECK(m_isJust); return m_value; } | |
37 T fromMaybe(const T& defaultValue) const { return m_isJust ? m_value : defau
ltValue; } | |
38 bool isJust() const { return m_isJust; } | |
39 T takeJust() { DCHECK(m_isJust); return m_value; } | |
40 | |
41 protected: | |
42 bool m_isJust; | |
43 T m_value; | |
44 }; | |
45 | |
46 template<> | |
47 class Maybe<bool> : public MaybeBase<bool> { | |
48 public: | |
49 Maybe() { } | |
50 Maybe(bool value) : MaybeBase(value) { } | |
51 using MaybeBase::operator=; | |
52 }; | |
53 | |
54 template<> | |
55 class Maybe<int> : public MaybeBase<int> { | |
56 public: | |
57 Maybe() { } | |
58 Maybe(int value) : MaybeBase(value) { } | |
59 using MaybeBase::operator=; | |
60 }; | |
61 | |
62 template<> | |
63 class Maybe<double> : public MaybeBase<double> { | |
64 public: | |
65 Maybe() { } | |
66 Maybe(double value) : MaybeBase(value) { } | |
67 using MaybeBase::operator=; | |
68 }; | |
69 | |
70 template<> | |
71 class Maybe<InspectorProtocolConvenienceStringType> : public MaybeBase<Inspector
ProtocolConvenienceStringType> { | |
72 public: | |
73 Maybe() { } | |
74 Maybe(const InspectorProtocolConvenienceStringType& value) : MaybeBase(value
) { } | |
75 using MaybeBase::operator=; | |
76 }; | |
77 | |
78 template<> | |
79 class Maybe<String16> : public MaybeBase<String16> { | |
80 public: | |
81 Maybe() { } | |
82 Maybe(const String16& value) : MaybeBase(value) { } | |
83 using MaybeBase::operator=; | |
84 }; | |
85 | |
86 } // namespace platform | |
87 } // namespace blink | |
88 | |
89 #endif // !defined(Maybe_h) | |
OLD | NEW |