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 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); } |
20 T* fromJust() const { DCHECK(m_value); return m_value.get(); } | 21 T* fromJust() const { DCHECK(m_value); return m_value.get(); } |
21 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; } |
22 bool isJust() const { return !!m_value; } | 23 bool isJust() const { return !!m_value; } |
23 std::unique_ptr<T> takeJust() { DCHECK(m_value); return m_value.release(); } | 24 std::unique_ptr<T> takeJust() { DCHECK(m_value); return m_value.release(); } |
24 private: | 25 private: |
25 std::unique_ptr<T> m_value; | 26 std::unique_ptr<T> m_value; |
26 }; | 27 }; |
27 | 28 |
28 template<typename T> | 29 template<typename T> |
29 class MaybeBase { | 30 class MaybeBase { |
30 public: | 31 public: |
31 MaybeBase() : m_isJust(false) { } | 32 MaybeBase() : m_isJust(false) { } |
32 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)) { } |
33 void operator=(T value) { m_value = value; m_isJust = true; } | 35 void operator=(T value) { m_value = value; m_isJust = true; } |
34 T fromJust() const { DCHECK(m_isJust); return m_value; } | 36 T fromJust() const { DCHECK(m_isJust); return m_value; } |
35 T fromMaybe(const T& defaultValue) const { return m_isJust ? m_value : defau
ltValue; } | 37 T fromMaybe(const T& defaultValue) const { return m_isJust ? m_value : defau
ltValue; } |
36 bool isJust() const { return m_isJust; } | 38 bool isJust() const { return m_isJust; } |
37 T takeJust() { DCHECK(m_isJust); return m_value; } | 39 T takeJust() { DCHECK(m_isJust); return m_value; } |
38 | 40 |
39 protected: | 41 protected: |
40 bool m_isJust; | 42 bool m_isJust; |
41 T m_value; | 43 T m_value; |
42 }; | 44 }; |
43 | 45 |
44 template<> | 46 template<> |
45 class Maybe<bool> : public MaybeBase<bool> { | 47 class Maybe<bool> : public MaybeBase<bool> { |
46 public: | 48 public: |
47 Maybe() { } | 49 Maybe() { } |
48 Maybe(bool value) : MaybeBase(value) { } | 50 Maybe(bool value) : MaybeBase(value) { } |
| 51 Maybe(Maybe&& other) : MaybeBase(std::move(other)) { } |
49 using MaybeBase::operator=; | 52 using MaybeBase::operator=; |
50 }; | 53 }; |
51 | 54 |
52 template<> | 55 template<> |
53 class Maybe<int> : public MaybeBase<int> { | 56 class Maybe<int> : public MaybeBase<int> { |
54 public: | 57 public: |
55 Maybe() { } | 58 Maybe() { } |
56 Maybe(int value) : MaybeBase(value) { } | 59 Maybe(int value) : MaybeBase(value) { } |
| 60 Maybe(Maybe&& other) : MaybeBase(std::move(other)) { } |
57 using MaybeBase::operator=; | 61 using MaybeBase::operator=; |
58 }; | 62 }; |
59 | 63 |
60 template<> | 64 template<> |
61 class Maybe<double> : public MaybeBase<double> { | 65 class Maybe<double> : public MaybeBase<double> { |
62 public: | 66 public: |
63 Maybe() { } | 67 Maybe() { } |
64 Maybe(double value) : MaybeBase(value) { } | 68 Maybe(double value) : MaybeBase(value) { } |
| 69 Maybe(Maybe&& other) : MaybeBase(std::move(other)) { } |
65 using MaybeBase::operator=; | 70 using MaybeBase::operator=; |
66 }; | 71 }; |
67 | 72 |
68 template<> | 73 template<> |
69 class Maybe<String> : public MaybeBase<String> { | 74 class Maybe<String> : public MaybeBase<String> { |
70 public: | 75 public: |
71 Maybe() { } | 76 Maybe() { } |
72 Maybe(const String& value) : MaybeBase(value) { } | 77 Maybe(const String& value) : MaybeBase(value) { } |
| 78 Maybe(Maybe&& other) : MaybeBase(std::move(other)) { } |
73 using MaybeBase::operator=; | 79 using MaybeBase::operator=; |
74 }; | 80 }; |
75 | 81 |
76 {% for namespace in config.protocol.namespace %} | 82 {% for namespace in config.protocol.namespace %} |
77 } // namespace {{namespace}} | 83 } // namespace {{namespace}} |
78 {% endfor %} | 84 {% endfor %} |
79 | 85 |
80 #endif // !defined({{"_".join(config.protocol.namespace)}}_Maybe_h) | 86 #endif // !defined({{"_".join(config.protocol.namespace)}}_Maybe_h) |
OLD | NEW |