1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
// Copyright 2017 The Draco Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#ifndef DRACO_CORE_STATUS_H_
#define DRACO_CORE_STATUS_H_
#include <string>
namespace draco {
// Class encapsulating a return status of an operation with an optional error
// message. Intended to be used as a return type for functions instead of bool.
class Status {
public:
enum Code {
OK = 0,
ERROR = -1, // Used for general errors.
IO_ERROR = -2, // Error when handling input or output stream.
INVALID_PARAMETER = -3, // Invalid parameter passed to a function.
UNSUPPORTED_VERSION = -4, // Input not compatible with the current version.
UNKNOWN_VERSION = -5, // Input was created with an unknown version of
// the library.
};
Status() : code_(OK) {}
Status(const Status &status) = default;
Status(Status &&status) = default;
explicit Status(Code code) : code_(code) {}
Status(Code code, const std::string &error_msg)
: code_(code), error_msg_(error_msg) {}
Code code() const { return code_; }
const std::string &error_msg_string() const { return error_msg_; }
const char *error_msg() const { return error_msg_.c_str(); }
bool operator==(Code code) const { return code == code_; }
bool ok() const { return code_ == OK; }
Status &operator=(const Status &) = default;
private:
Code code_;
std::string error_msg_;
};
inline std::ostream &operator<<(std::ostream &os, const Status &status) {
os << status.error_msg_string();
return os;
}
inline Status OkStatus() { return Status(Status::OK); }
// Evaluates an expression that returns draco::Status. If the status is not OK,
// the macro returns the status object.
#define DRACO_RETURN_IF_ERROR(expression) \
{ \
const draco::Status _local_status = (expression); \
if (!_local_status.ok()) \
return _local_status; \
}
} // namespace draco
#endif // DRACO_CORE_STATUS_H_
|