Apollo  6.0
Open source self driving car software
status.h
Go to the documentation of this file.
1 /******************************************************************************
2  * Copyright 2017 The Apollo Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *****************************************************************************/
16 
21 #pragma once
22 
23 #include <string>
24 
25 #include "google/protobuf/descriptor.h"
26 #include "modules/common/proto/error_code.pb.h"
28 
33 namespace apollo {
34 namespace common {
35 
43 class Status {
44  public:
51  explicit Status(ErrorCode code = ErrorCode::OK, std::string_view msg = "")
52  : code_(code), msg_(msg.data()) {}
53 
54  ~Status() = default;
55 
60  static Status OK() { return Status(); }
61 
67  bool ok() const { return code_ == ErrorCode::OK; }
68 
73  ErrorCode code() const { return code_; }
74 
78  bool operator==(const Status &rh) const {
79  return (this->code_ == rh.code_) && (this->msg_ == rh.msg_);
80  }
81 
85  bool operator!=(const Status &rh) const { return !(*this == rh); }
86 
91  const std::string &error_message() const { return msg_; }
92 
98  std::string ToString() const {
99  if (ok()) {
100  return "OK";
101  }
102  return ErrorCode_Name(code_) + ": " + msg_;
103  }
104 
109  void Save(StatusPb *status_pb) {
110  if (!status_pb) {
111  return;
112  }
113  status_pb->set_error_code(code_);
114  if (!msg_.empty()) {
115  status_pb->set_msg(msg_);
116  }
117  }
118 
119  private:
120  ErrorCode code_;
121  std::string msg_;
122 };
123 
124 inline std::ostream &operator<<(std::ostream &os, const Status &s) {
125  os << s.ToString();
126  return os;
127 }
128 
129 } // namespace common
130 } // namespace apollo
bool operator==(const Status &rh) const
defines the logic of testing if two Status are equal
Definition: status.h:78
PlanningContext is the runtime context in planning. It is persistent across multiple frames...
Definition: atomic_hash_map.h:25
void Save(StatusPb *status_pb)
save the error_code and error message to protobuf
Definition: status.h:109
const std::string & error_message() const
returns the error message of the status, empty if the status is OK.
Definition: status.h:91
bool ok() const
check whether the return status is OK.
Definition: status.h:67
static Status OK()
generate a success status.
Definition: status.h:60
ErrorCode code() const
get the error code
Definition: status.h:73
std::ostream & operator<<(std::ostream &os, const Status &s)
Definition: status.h:124
A general class to denote the return status of an API call. It can either be an OK status for success...
Definition: status.h:43
bool operator!=(const Status &rh) const
defines the logic of testing if two Status are unequal
Definition: status.h:85
bool OK()
Definition: state.h:44
std::string ToString() const
returns a string representation in a readable format.
Definition: status.h:98
Status(ErrorCode code=ErrorCode::OK, std::string_view msg="")
Create a status with the specified error code and msg as a human-readable string containing more deta...
Definition: status.h:51