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