Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(241)

Side by Side Diff: headless/public/util/maybe.h

Issue 1906493002: headless: Replace headless::Maybe with base::Optional (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « headless/lib/browser/types_h.template ('k') | headless/public/util/maybe_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 // TODO(skyostil): Replace this with base::Optional once it is available.
17 template <typename T>
18 class Maybe {
19 public:
20 Maybe() : has_value_(false) {}
21
22 bool IsNothing() const { return !has_value_; }
23 bool IsJust() const { return has_value_; }
24
25 // Will crash if the Maybe<> is nothing.
26 T& FromJust() {
27 DCHECK(IsJust());
28 return value_;
29 }
30 const T& FromJust() const {
31 DCHECK(IsJust());
32 return value_;
33 }
34
35 T FromMaybe(const T& default_value) const {
36 return has_value_ ? value_ : default_value;
37 }
38
39 bool operator==(const Maybe& other) const {
40 return (IsJust() == other.IsJust()) &&
41 (!IsJust() || FromJust() == other.FromJust());
42 }
43
44 bool operator!=(const Maybe& other) const { return !operator==(other); }
45
46 Maybe& operator=(Maybe&& other) {
47 has_value_ = other.has_value_;
48 value_ = std::move(other.value_);
49 return *this;
50 }
51
52 Maybe& operator=(const Maybe& other) {
53 has_value_ = other.has_value_;
54 value_ = other.value_;
55 return *this;
56 }
57
58 Maybe(const Maybe& other) = default;
59 Maybe(Maybe&& other) = default;
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_
OLDNEW
« no previous file with comments | « headless/lib/browser/types_h.template ('k') | headless/public/util/maybe_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698