-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror_condition_example.cpp
More file actions
85 lines (72 loc) · 1.84 KB
/
error_condition_example.cpp
File metadata and controls
85 lines (72 loc) · 1.84 KB
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
84
85
#include <iostream>
#include <system_error>
namespace Heresy
{
enum class ErrorCondition
{
FileError = 1,
NetworkError
};
// define error_category
class ErrorCategory : public std::error_category
{
public:
std::string message(int c) const override
{
switch (static_cast<ErrorCondition>(c))
{
case ErrorCondition::FileError:
return "File-related error";
case ErrorCondition::NetworkError:
return "network related error";
default:
return "Undefined";
}
}
const char* name() const noexcept override
{
return "Error condition category by Heresy";
}
bool equivalent(const std::error_code& ec, int iCond) const noexcept override
{
switch (static_cast<ErrorCondition>(iCond))
{
case ErrorCondition::FileError:
return (ec == std::errc::file_exists ||
ec == std::errc::file_too_large ||
ec == std::errc::no_such_file_or_directory ||
ec == std::errc::read_only_file_system);
case ErrorCondition::NetworkError:
return (ec == std::errc::network_down ||
ec == std::errc::network_reset ||
ec == std::errc::network_unreachable);
}
return false;
}
public:
static const std::error_category& get()
{
const static ErrorCategory sCategory;
return sCategory;
}
};
std::error_condition make_error_condition(ErrorCondition ec)
{
return std::error_condition(static_cast<int>(ec), ErrorCategory::get());
}
}
namespace std
{
// let compiler know that Heresy::ErrorCode is compatible with std::error_condition
template <>
struct is_error_condition_enum<Heresy::ErrorCondition> : true_type {};
}
int main()
{
std::error_code ec = std::make_error_code(std::errc::file_exists);
if (ec == Heresy::ErrorCondition::FileError)
std::cout << ec.message() << " is a kind of file error";
else
std::cout << ec.message() << " is not a kind of file error";
return 0;
}