GameApi 28
Library used by GameApi Builder tool
Loading...
Searching...
No Matches
status_or.h
Go to the documentation of this file.
1// Copyright 2017 The Draco Authors.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14//
15#ifndef DRACO_CORE_STATUS_OR_H_
16#define DRACO_CORE_STATUS_OR_H_
17
18#include "draco/core/macros.h"
19#include "draco/core/status.h"
20
21namespace draco {
22
23// Class StatusOr is used to wrap a Status along with a value of a specified
24// type |T|. StatusOr is intended to be returned from functions in situations
25// where it is desirable to carry over more information about the potential
26// errors encountered during the function execution. If there are not errors,
27// the caller can simply use the return value, otherwise the Status object
28// provides more info about the encountered problem.
29template <class T>
30class StatusOr {
31 public:
33 // Note: Constructors are intentionally not explicit to allow returning
34 // Status or the return value directly from functions.
35 StatusOr(const StatusOr &) = default;
36 StatusOr(StatusOr &&) = default;
37 StatusOr(const Status &status) : status_(status) {}
38 StatusOr(const T &value) : status_(OkStatus()), value_(value) {}
39 StatusOr(T &&value) : status_(OkStatus()), value_(std::move(value)) {}
40 StatusOr(const Status &status, const T &value)
41 : status_(status), value_(value) {}
42
43 const Status &status() const { return status_; }
44 const T &value() const & { return value_; }
45 const T &&value() const && { return std::move(value_); }
46 T &&value() && { return std::move(value_); }
47
48 // For consistency with existing Google StatusOr API we also include
49 // ValueOrDie() that currently returns the value().
50 const T &ValueOrDie() const & { return value(); }
51 T &&ValueOrDie() && { return std::move(value()); }
52
53 bool ok() const { return status_.ok(); }
54
55 private:
56 Status status_;
57 T value_;
58};
59
60// In case StatusOr<T> is ok(), this macro assigns value stored in StatusOr<T>
61// to |lhs|, otherwise it returns the error Status.
62//
63// DRACO_ASSIGN_OR_RETURN(lhs, expression)
64//
65#define DRACO_ASSIGN_OR_RETURN(lhs, expression) \
66 DRACO_ASSIGN_OR_RETURN_IMPL_(DRACO_MACROS_IMPL_CONCAT_(_statusor, __LINE__), \
67 lhs, expression, _status)
68
69// The actual implementation of the above macro.
70#define DRACO_ASSIGN_OR_RETURN_IMPL_(statusor, lhs, expression, error_expr) \
71 auto statusor = (expression); \
72 if (!statusor.ok()) { \
73 auto _status = std::move(statusor.status()); \
74 (void)_status; /* error_expression may not use it */ \
75 return error_expr; \
76 } \
77 lhs = std::move(statusor).value();
78
79} // namespace draco
80
81#endif // DRACO_CORE_STATUS_OR_H_
const T & ValueOrDie() const &
Definition status_or.h:50
const T & value() const &
Definition status_or.h:44
StatusOr(StatusOr &&)=default
T && value() &&
Definition status_or.h:46
StatusOr(T &&value)
Definition status_or.h:39
const T && value() const &&
Definition status_or.h:45
StatusOr(const Status &status)
Definition status_or.h:37
StatusOr(const Status &status, const T &value)
Definition status_or.h:40
StatusOr()
Definition status_or.h:32
T && ValueOrDie() &&
Definition status_or.h:51
StatusOr(const T &value)
Definition status_or.h:38
StatusOr(const StatusOr &)=default
bool ok() const
Definition status_or.h:53
const Status & status() const
Definition status_or.h:43
Definition status.h:25
Definition attribute_octahedron_transform.cc:21
Status OkStatus()
Definition status.h:66
STL namespace.
Definition GameApi.h:42