OR-Tools  8.0
statusor.h
Go to the documentation of this file.
1 // Copyright 2010-2018 Google LLC
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13 
14 #ifndef OR_TOOLS_BASE_STATUSOR_H_
15 #define OR_TOOLS_BASE_STATUSOR_H_
16 
17 #include "absl/status/status.h"
18 #include "ortools/base/logging.h"
19 
20 namespace absl {
21 
22 // WARNING: This makes a copy of its payload. Ugly.
23 template <class T>
24 struct StatusOr {
25  // Non-explicit constructors, by design.
26  StatusOr(T value) : value_(value) {} // NOLINT
27  StatusOr(const Status& status) : status_(status) { // NOLINT
28  CHECK(!status_.ok()) << status.ToString();
29  }
30 
31  // Copy constructor.
32  StatusOr(const StatusOr& other)
33  : value_(other.value_), status_(other.status_) {}
34 
35  bool ok() const { return status_.ok(); }
36  const T& value() const {
37  CHECK(ok());
38  return value_;
39  }
40 
41  Status status() const { return status_; }
42 
43  template <typename U>
44  T value_or(U&& default_value) const& {
45  if (ok()) {
46  return value_;
47  }
48  return std::forward<U>(default_value);
49  }
50 
51  private:
52  T value_;
53  Status status_;
54 };
55 
56 } // namespace absl
57 
58 #endif // OR_TOOLS_BASE_STATUSOR_H_
absl::StatusOr::value_or
T value_or(U &&default_value) const &
Definition: statusor.h:44
absl::StatusOr::ok
bool ok() const
Definition: statusor.h:35
logging.h
absl::StatusOr::value
const T & value() const
Definition: statusor.h:36
absl::StatusOr::StatusOr
StatusOr(const Status &status)
Definition: statusor.h:27
absl::StatusOr::StatusOr
StatusOr(const StatusOr &other)
Definition: statusor.h:32
absl::StatusOr::StatusOr
StatusOr(T value)
Definition: statusor.h:26
absl::StatusOr::status
Status status() const
Definition: statusor.h:41
absl
Definition: cleanup.h:22
absl::StatusOr
Definition: statusor.h:24