GameApi 28
Library used by GameApi Builder tool
Loading...
Searching...
No Matches
status.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_H_
16#define DRACO_CORE_STATUS_H_
17
18#include <ostream>
19#include <string>
20
21namespace draco {
22
23// Class encapsulating a return status of an operation with an optional error
24// message. Intended to be used as a return type for functions instead of bool.
25class Status {
26 public:
27 enum Code {
28 OK = 0,
29 DRACO_ERROR = -1, // Used for general errors.
30 IO_ERROR = -2, // Error when handling input or output stream.
31 INVALID_PARAMETER = -3, // Invalid parameter passed to a function.
32 UNSUPPORTED_VERSION = -4, // Input not compatible with the current version.
33 UNKNOWN_VERSION = -5, // Input was created with an unknown version of
34 // the library.
35 UNSUPPORTED_FEATURE = -6, // Input contains feature that is not supported.
36 };
37
38 Status() : code_(OK) {}
39 Status(const Status &status) = default;
40 Status(Status &&status) = default;
41 explicit Status(Code code) : code_(code) {}
42 Status(Code code, const std::string &error_msg)
43 : code_(code), error_msg_(error_msg) {}
44
45 Code code() const { return code_; }
46 const std::string &error_msg_string() const { return error_msg_; }
47 const char *error_msg() const { return error_msg_.c_str(); }
48 std::string code_string() const;
49 std::string code_and_error_string() const;
50
51 bool operator==(Code code) const { return code == code_; }
52 bool ok() const { return code_ == OK; }
53
54 Status &operator=(const Status &) = default;
55
56 private:
57 Code code_;
58 std::string error_msg_;
59};
60
61inline std::ostream &operator<<(std::ostream &os, const Status &status) {
62 os << status.error_msg_string();
63 return os;
64}
65
66inline Status OkStatus() { return Status(Status::OK); }
67inline Status ErrorStatus(const std::string &msg) {
68 return Status(Status::DRACO_ERROR, msg);
69}
70
71// Evaluates an expression that returns draco::Status. If the status is not OK,
72// the macro returns the status object.
73#define DRACO_RETURN_IF_ERROR(expression) \
74 { \
75 const draco::Status _local_status = (expression); \
76 if (!_local_status.ok()) { \
77 return _local_status; \
78 } \
79 }
80
81} // namespace draco
82
83#endif // DRACO_CORE_STATUS_H_
Definition status.h:25
const std::string & error_msg_string() const
Definition status.h:46
Status(Code code, const std::string &error_msg)
Definition status.h:42
std::string code_string() const
Definition status.cc:20
std::string code_and_error_string() const
Definition status.cc:40
Status & operator=(const Status &)=default
Status()
Definition status.h:38
Status(const Status &status)=default
bool operator==(Code code) const
Definition status.h:51
Code code() const
Definition status.h:45
Code
Definition status.h:27
@ IO_ERROR
Definition status.h:30
@ UNSUPPORTED_FEATURE
Definition status.h:35
@ INVALID_PARAMETER
Definition status.h:31
@ UNKNOWN_VERSION
Definition status.h:33
@ OK
Definition status.h:28
@ DRACO_ERROR
Definition status.h:29
@ UNSUPPORTED_VERSION
Definition status.h:32
bool ok() const
Definition status.h:52
Status(Status &&status)=default
const char * error_msg() const
Definition status.h:47
Status(Code code)
Definition status.h:41
Definition attribute_octahedron_transform.cc:21
Status OkStatus()
Definition status.h:66
Status ErrorStatus(const std::string &msg)
Definition status.h:67
std::ostream & operator<<(std::ostream &os, IndexType< ValueTypeT, TagT > index)
Definition draco_index_type.h:165