-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathstatus.h
83 lines (72 loc) · 2.34 KB
/
status.h
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
76
77
78
79
80
81
82
83
// Copyright (c) 2018 leosocy. All rights reserved.
// Use of this source code is governed by a MIT-style license
// that can be found in the LICENSE file.
#ifndef ROBUST_PALM_ROI_INCLUDE_ROBUST_PALM_ROI_STATUS_H_
#define ROBUST_PALM_ROI_INCLUDE_ROBUST_PALM_ROI_STATUS_H_
#include <stdio.h>
#include <assert.h>
#include <string.h>
namespace rpr {
class Status {
public:
enum Code {
kOk = 0,
kLoadImageError,
kImageLowQualityError,
kLoadConfigYamlError,
kCApiOutBufferInsufficient,
};
static Status Ok(const char* msg = NULL) { return Status(kOk, msg); }
static Status LoadImageError(const char* msg = NULL) { return Status(kLoadImageError, msg); }
static Status ImageLowQualityError(const char* msg = NULL) { return Status(kImageLowQualityError, msg); }
static Status LoadConfigYamlError(const char* msg = NULL) { return Status(kLoadConfigYamlError, msg); }
static Status CApiOutBufferInsufficient(const char* msg = NULL) { return Status(kCApiOutBufferInsufficient, msg); }
Status() : state_(NULL) {}
Status(const Status& s) {
state_ = (s.state_ == NULL ? NULL : CopyState(s.state_));
}
void operator=(const Status& s) {
if (this != &s) {
DeleteState();
state_ = (s.state_ == NULL ? NULL : CopyState(s.state_));
}
}
~Status() { DeleteState(); }
Code code() {
assert (state_ != NULL);
return static_cast<Code>(state_[0]);
}
const char* msg() {
assert (state_ != NULL);
return state_ + 1;
}
bool IsOk() { return code() == kOk; }
private:
Status(Code code, const char* msg = NULL);
const char* CopyState(const char* state);
void DeleteState();
const char *state_;
};
inline Status::Status(Code code, const char* msg) {
size_t msg_len = (msg == NULL) ? 0 : strlen(msg);
char *result = new char[msg_len + 2];
result[0] = static_cast<char>(code);
strncpy(result + 1, msg, msg_len);
result[msg_len + 1] = '\0';
state_ = result;
}
inline const char* Status::CopyState(const char *state) {
assert(state != NULL);
size_t state_len = 1 + strlen(state + 1) + 1; // code_len + msg_len + '\0'
char *result = new char[state_len];
memcpy(result, state, state_len);
return result;
}
inline void Status::DeleteState() {
if (state_ != NULL) {
delete[] state_;
state_ = NULL;
}
}
} // namespace rpr
#endif // ROBUST_PALM_ROI_INCLUDE_ROBUST_PALM_ROI_STATUS_H_