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 HEADLESS_PUBLIC_UTIL_MAYBE_H_ | |
6 #define HEADLESS_PUBLIC_UTIL_MAYBE_H_ | |
7 | |
8 #include <algorithm> | |
9 | |
10 #include "base/logging.h" | |
11 #include "base/macros.h" | |
12 | |
13 namespace headless { | |
14 | |
15 // A simple Maybe which may or may not have a value. Based on v8::Maybe. | |
16 template <typename T> | |
17 class Maybe { | |
18 public: | |
19 Maybe() : has_value_(false) {} | |
20 | |
21 bool IsNothing() const { return !has_value_; } | |
22 bool IsJust() const { return has_value_; } | |
23 | |
24 // Will crash if the Maybe<> is nothing. | |
25 T& FromJust() { | |
26 DCHECK(IsJust()); | |
27 return value_; | |
28 } | |
29 const T& FromJust() const { | |
30 DCHECK(IsJust()); | |
31 return value_; | |
32 } | |
33 | |
34 T FromMaybe(const T& default_value) const { | |
35 return has_value_ ? value_ : default_value; | |
36 } | |
37 | |
38 bool operator==(const Maybe& other) const { | |
39 return (IsJust() == other.IsJust()) && | |
40 (!IsJust() || FromJust() == other.FromJust()); | |
41 } | |
42 | |
43 bool operator!=(const Maybe& other) const { return !operator==(other); } | |
44 | |
45 Maybe& operator=(Maybe&& other) { | |
46 has_value_ = other.has_value_; | |
47 std::swap(value_, other.value_); | |
alex clarke (OOO till 29th)
2016/04/07 11:45:32
shouldn't this be value_ = std::move(other.value_)
Sami
2016/04/07 12:00:43
Good point, done. I've also added a question to ht
| |
48 return *this; | |
49 } | |
50 | |
51 Maybe& operator=(const Maybe& other) { | |
52 has_value_ = other.has_value_; | |
53 value_ = other.value_; | |
54 return *this; | |
55 } | |
56 | |
57 Maybe(const Maybe& other) { *this = other; } | |
alex clarke (OOO till 29th)
2016/04/07 11:45:32
I wonder if this and the one below could be = defa
Sami
2016/04/07 12:00:43
Yep, they can, fixed. If you're wondering why we n
| |
58 | |
59 Maybe(Maybe&& other) { *this = std::move(other); } | |
60 | |
61 private: | |
62 template <class U> | |
63 friend Maybe<U> Nothing(); | |
64 template <class U> | |
65 friend Maybe<U> Just(const U& u); | |
66 template <class U> | |
67 friend Maybe<typename std::remove_reference<U>::type> Just(U&& u); | |
68 | |
69 explicit Maybe(const T& t) : has_value_(true), value_(t) {} | |
70 explicit Maybe(T&& t) : has_value_(true), value_(std::move(t)) {} | |
71 | |
72 bool has_value_; | |
73 T value_; | |
74 }; | |
75 | |
76 template <class T> | |
77 Maybe<T> Nothing() { | |
78 return Maybe<T>(); | |
79 } | |
80 | |
81 template <class T> | |
82 Maybe<T> Just(const T& t) { | |
83 return Maybe<T>(t); | |
84 } | |
85 | |
86 template <class T> | |
87 Maybe<typename std::remove_reference<T>::type> Just(T&& t) { | |
88 return Maybe<typename std::remove_reference<T>::type>(std::move(t)); | |
89 } | |
90 | |
91 } // namespace headless | |
92 | |
93 #endif // HEADLESS_PUBLIC_UTIL_MAYBE_H_ | |
OLD | NEW |