OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 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 {{"_".join(config.protocol.namespace)}}_Maybe_h | 5 #ifndef {{"_".join(config.protocol.namespace)}}_Maybe_h |
6 #define {{"_".join(config.protocol.namespace)}}_Maybe_h | 6 #define {{"_".join(config.protocol.namespace)}}_Maybe_h |
7 | 7 |
8 //#include "Forward.h" | 8 //#include "Forward.h" |
9 | 9 |
10 {% for namespace in config.protocol.namespace %} | 10 {% for namespace in config.protocol.namespace %} |
11 namespace {{namespace}} { | 11 namespace {{namespace}} { |
12 {% endfor %} | 12 {% endfor %} |
13 | 13 |
14 template<typename T> | 14 template<typename T> |
15 class Maybe { | 15 class Maybe { |
16 public: | 16 public: |
17 Maybe() : m_value() { } | 17 Maybe() : m_value() { } |
18 Maybe(std::unique_ptr<T> value) : m_value(std::move(value)) { } | 18 Maybe(std::unique_ptr<T> value) : m_value(std::move(value)) { } |
19 Maybe(Maybe&& other) : m_value(std::move(other.m_value)) { } | 19 Maybe(Maybe&& other) : m_value(std::move(other.m_value)) { } |
20 void operator=(std::unique_ptr<T> value) { m_value = std::move(value); } | 20 void operator=(std::unique_ptr<T> value) { m_value = std::move(value); } |
21 T* fromJust() const { DCHECK(m_value); return m_value.get(); } | 21 T* fromJust() const { DCHECK(m_value); return m_value.get(); } |
22 T* fromMaybe(T* defaultValue) const { return m_value ? m_value.get() : defau
ltValue; } | 22 T* fromMaybe(T* defaultValue) const { return m_value ? m_value.get() : defau
ltValue; } |
23 bool isJust() const { return !!m_value; } | 23 bool isJust() const { return !!m_value; } |
24 std::unique_ptr<T> takeJust() { DCHECK(m_value); return m_value.release(); } | 24 std::unique_ptr<T> takeJust() { DCHECK(m_value); return std::move(m_value);
} |
25 private: | 25 private: |
26 std::unique_ptr<T> m_value; | 26 std::unique_ptr<T> m_value; |
27 }; | 27 }; |
28 | 28 |
29 template<typename T> | 29 template<typename T> |
30 class MaybeBase { | 30 class MaybeBase { |
31 public: | 31 public: |
32 MaybeBase() : m_isJust(false) { } | 32 MaybeBase() : m_isJust(false) { } |
33 MaybeBase(T value) : m_isJust(true), m_value(value) { } | 33 MaybeBase(T value) : m_isJust(true), m_value(value) { } |
34 MaybeBase(MaybeBase&& other) : m_isJust(other.m_isJust), m_value(std::move(o
ther.m_value)) { } | 34 MaybeBase(MaybeBase&& other) : m_isJust(other.m_isJust), m_value(std::move(o
ther.m_value)) { } |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
77 Maybe(const String& value) : MaybeBase(value) { } | 77 Maybe(const String& value) : MaybeBase(value) { } |
78 Maybe(Maybe&& other) : MaybeBase(std::move(other)) { } | 78 Maybe(Maybe&& other) : MaybeBase(std::move(other)) { } |
79 using MaybeBase::operator=; | 79 using MaybeBase::operator=; |
80 }; | 80 }; |
81 | 81 |
82 {% for namespace in config.protocol.namespace %} | 82 {% for namespace in config.protocol.namespace %} |
83 } // namespace {{namespace}} | 83 } // namespace {{namespace}} |
84 {% endfor %} | 84 {% endfor %} |
85 | 85 |
86 #endif // !defined({{"_".join(config.protocol.namespace)}}_Maybe_h) | 86 #endif // !defined({{"_".join(config.protocol.namespace)}}_Maybe_h) |
OLD | NEW |