13
13
namespace {{namespace}} {
14
14
{% endfor %}
15
15
16
+ namespace detail {
16
17
template<typename T>
17
- class Maybe {
18
+ class PtrMaybe {
18
19
public:
19
- Maybe () : m_value() { }
20
- Maybe (std::unique_ptr<T> value) : m_value(std::move(value)) { }
21
- Maybe(Maybe && other) noexcept : m_value(std::move(other.m_value)) {}
20
+ PtrMaybe () = default;
21
+ PtrMaybe (std::unique_ptr<T> value) : m_value(std::move(value)) { }
22
+ PtrMaybe(PtrMaybe && other) noexcept : m_value(std::move(other.m_value)) {}
22
23
void operator=(std::unique_ptr<T> value) { m_value = std::move(value); }
23
24
T* fromJust() const { DCHECK(m_value); return m_value.get(); }
24
25
T* fromMaybe(T* defaultValue) const { return m_value ? m_value.get() : defaultValue; }
@@ -29,68 +30,45 @@ private:
29
30
};
30
31
31
32
template<typename T>
32
- class MaybeBase {
33
+ class ValueMaybe {
33
34
public:
34
- MaybeBase () : m_isJust(false) { }
35
- MaybeBase (T value) : m_isJust(true), m_value(value) { }
36
- MaybeBase(MaybeBase && other) noexcept
35
+ ValueMaybe () : m_isJust(false), m_value( ) { }
36
+ ValueMaybe (T value) : m_isJust(true), m_value(std::move( value) ) { }
37
+ ValueMaybe(ValueMaybe && other) noexcept
37
38
: m_isJust(other.m_isJust),
38
39
m_value(std::move(other.m_value)) {}
39
40
void operator=(T value) { m_value = value; m_isJust = true; }
40
- T fromJust() const { DCHECK(m_isJust); return m_value; }
41
- T fromMaybe(const T& defaultValue) const { return m_isJust ? m_value : defaultValue; }
41
+ const T& fromJust() const { DCHECK(m_isJust); return m_value; }
42
+ const T& fromMaybe(const T& defaultValue) const { return m_isJust ? m_value : defaultValue; }
42
43
bool isJust() const { return m_isJust; }
43
- T takeJust() { DCHECK(m_isJust); return m_value; }
44
-
45
- protected:
44
+ T takeJust() { DCHECK(m_isJust); return std::move(m_value); }
45
+ private:
46
46
bool m_isJust;
47
47
T m_value;
48
48
};
49
49
50
- template<>
51
- class Maybe<bool> : public MaybeBase<bool> {
52
- public:
53
- Maybe() { m_value = false; }
54
- Maybe(bool value) : MaybeBase(value) { }
55
- Maybe(Maybe&& other) noexcept : MaybeBase(std::move(other)) {}
56
- using MaybeBase::operator=;
57
- };
50
+ template <typename T>
51
+ struct MaybeTypedef { typedef PtrMaybe<T> type; };
58
52
59
- template<>
60
- class Maybe<int> : public MaybeBase<int> {
61
- public:
62
- Maybe() { m_value = 0; }
63
- Maybe(int value) : MaybeBase(value) { }
64
- Maybe(Maybe&& other) noexcept : MaybeBase(std::move(other)) {}
65
- using MaybeBase::operator=;
66
- };
53
+ template <>
54
+ struct MaybeTypedef<bool> { typedef ValueMaybe<bool> type; };
67
55
68
- template<>
69
- class Maybe<double> : public MaybeBase<double> {
70
- public:
71
- Maybe() { m_value = 0; }
72
- Maybe(double value) : MaybeBase(value) { }
73
- Maybe(Maybe&& other) noexcept : MaybeBase(std::move(other)) {}
74
- using MaybeBase::operator=;
75
- };
56
+ template <>
57
+ struct MaybeTypedef<int> { typedef ValueMaybe<int> type; };
76
58
77
- template<>
78
- class Maybe<String> : public MaybeBase<String> {
79
- public:
80
- Maybe() { }
81
- Maybe(const String& value) : MaybeBase(value) { }
82
- Maybe(Maybe&& other) noexcept : MaybeBase(std::move(other)) {}
83
- using MaybeBase::operator=;
84
- };
59
+ template <>
60
+ struct MaybeTypedef<double> { typedef ValueMaybe<double> type; };
85
61
86
- template<>
87
- class Maybe<Binary> : public MaybeBase<Binary> {
88
- public:
89
- Maybe() { }
90
- Maybe(Binary value) : MaybeBase(value) { }
91
- Maybe(Maybe&& other) noexcept : MaybeBase(std::move(other)) {}
92
- using MaybeBase::operator=;
93
- };
62
+ template <>
63
+ struct MaybeTypedef<String> { typedef ValueMaybe<String> type; };
64
+
65
+ template <>
66
+ struct MaybeTypedef<Binary> { typedef ValueMaybe<Binary> type; };
67
+
68
+ } // namespace detail
69
+
70
+ template <typename T>
71
+ using Maybe = typename detail::MaybeTypedef<T>::type;
94
72
95
73
{% for namespace in config.protocol.namespace %}
96
74
} // namespace {{namespace}}
0 commit comments