1313namespace {{namespace}} {
1414{% endfor %}
1515
16+ namespace detail {
1617template<typename T>
17- class Maybe {
18+ class PtrMaybe {
1819public:
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)) {}
2223 void operator=(std::unique_ptr<T> value) { m_value = std::move(value); }
2324 T* fromJust() const { DCHECK(m_value); return m_value.get(); }
2425 T* fromMaybe(T* defaultValue) const { return m_value ? m_value.get() : defaultValue; }
@@ -29,68 +30,45 @@ private:
2930};
3031
3132template<typename T>
32- class MaybeBase {
33+ class ValueMaybe {
3334public:
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
3738 : m_isJust(other.m_isJust),
3839 m_value(std::move(other.m_value)) {}
3940 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; }
4243 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:
4646 bool m_isJust;
4747 T m_value;
4848};
4949
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; };
5852
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; };
6755
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; };
7658
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; };
8561
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;
9472
9573{% for namespace in config.protocol.namespace %}
9674} // namespace {{namespace}}
0 commit comments