Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
status_or.h
Go to the documentation of this file.
1// Copyright 2013 The Flutter 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 FLUTTER_FML_STATUS_OR_H_
6#define FLUTTER_FML_STATUS_OR_H_
7
8#include <optional>
9
10#include "flutter/fml/logging.h"
11#include "flutter/fml/status.h"
12
13namespace fml {
14
15// TODO(https://github.com/flutter/flutter/issues/134741): Replace with
16// absl::StatusOr.
17/// Represents a union type of an object of type `T` and an fml::Status.
18///
19/// This is often used as a replacement for C++ exceptions where a function that
20/// could fail may return an error or a result. These are typically used for
21/// errors that are meant to be recovered from. If there is no recovery
22/// available `FML_CHECK` is more appropriate.
23///
24/// Example:
25/// StatusOr<int> div(int n, int d) {
26/// if (d == 0) {
27/// return Status(StatusCode::kFailedPrecondition, "div by zero");
28/// }
29/// return n / d;
30/// }
31template <typename T>
32class StatusOr {
33 public:
34 // These constructors are intended be compatible with absl::status_or.
35 // NOLINTNEXTLINE(google-explicit-constructor)
36 StatusOr(const T& value) : status_(), value_(value) {}
37
38 // These constructors are intended be compatible with absl::status_or.
39 // NOLINTNEXTLINE(google-explicit-constructor)
40 StatusOr(T&& value) : status_(), value_(std::move(value)) {}
41
42 // These constructors are intended be compatible with absl::status_or.
43 // NOLINTNEXTLINE(google-explicit-constructor)
44 StatusOr(const Status& status) : status_(status), value_() {
45 // It's not valid to construct a StatusOr with an OK status and no value.
46 FML_CHECK(!status_.ok());
47 }
48
49 StatusOr(const StatusOr&) = default;
50 StatusOr(StatusOr&&) = default;
51
52 StatusOr& operator=(const StatusOr&) = default;
54
56 status_ = Status();
57 value_ = value;
58 return *this;
59 }
60
62 status_ = Status();
63 value_ = std::move(value);
64 return *this;
65 }
66
68 status_ = value;
69 value_ = std::nullopt;
70 return *this;
71 }
72
73 const Status& status() const { return status_; }
74
75 bool ok() const { return status_.ok(); }
76
77 const T& value() const {
78 if (value_.has_value()) {
79 FML_DCHECK(status_.ok());
80 return value_.value();
81 }
82 FML_LOG(FATAL) << "StatusOr::value() called on error Status";
84 }
85
86 T& value() {
87 if (value_.has_value()) {
88 FML_DCHECK(status_.ok());
89 return value_.value();
90 }
91 FML_LOG(FATAL) << "StatusOr::value() called on error Status";
93 }
94
95 private:
96 Status status_;
97 std::optional<T> value_;
98};
99
100} // namespace fml
101
102#endif // FLUTTER_FML_STATUS_OR_H_
const Status & status() const
Definition status_or.h:73
StatusOr(const T &value)
Definition status_or.h:36
const T & value() const
Definition status_or.h:77
StatusOr(StatusOr &&)=default
StatusOr & operator=(const T &&value)
Definition status_or.h:61
StatusOr & operator=(const StatusOr &)=default
StatusOr(const StatusOr &)=default
StatusOr & operator=(const T &value)
Definition status_or.h:55
StatusOr(T &&value)
Definition status_or.h:40
bool ok() const
Definition status_or.h:75
StatusOr(const Status &status)
Definition status_or.h:44
StatusOr & operator=(StatusOr &&)=default
StatusOr & operator=(const Status &value)
Definition status_or.h:67
bool ok() const
Definition status.h:71
#define FATAL(error)
#define FML_LOG(severity)
Definition logging.h:82
#define FML_CHECK(condition)
Definition logging.h:85
#define FML_UNREACHABLE()
Definition logging.h:109
#define FML_DCHECK(condition)
Definition logging.h:103
Definition ref_ptr.h:256
#define T