-
Notifications
You must be signed in to change notification settings - Fork 0
/
result.hpp
executable file
·64 lines (53 loc) · 1.09 KB
/
result.hpp
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
/*
* MIT License
* Copyright (c) 2024 BlockyDeer<[email protected]>
* Licensed under the MIT License. See LICENSE file in the project root for full
* license information.
*/
#pragma once
#include <glog/logging.h>
#include <string>
#include "error.hpp"
namespace AhogeUtil {
template <typename Content_t>
class Result final {
private:
Content_t content;
bool not_nil;
public:
Result() { not_nil = false; }
Result(Content_t const &content) {
this->content = content;
not_nil = true;
}
bool is_not_nil() { return not_nil; }
bool operator()() { return !not_nil; }
Content_t &get() {
if (!not_nil) {
LOG(FATAL) << "Null content";
}
return content;
}
Content_t release() {
if (not_nil) {
not_nil = false;
}
return content;
}
void set_content(Content_t const &content) {
if (!not_nil) {
not_nil = true;
}
this->content = content;
}
void panic_if_nil(std::string const &msg) const {
if (!not_nil) {
LOG(FATAL) << msg;
}
}
Content_t &wrap(std::string const &error_msg = "") {
panic_if_nil(error_msg);
return content;
}
};
} // namespace AhogeUtil