Debian Codegraph @

yo/advancedtemplates/examples/errcode/category/category.h

raw
1 #ifndef INCLUDED_CATEGORY_
2 #define INCLUDED_CATEGORY_
3
4 #include <unordered_map>
5 #include <system_error>
6
7 //caterr
8 enum class CatErr
9 {
10 Err1 = 1, // causes of errors
11 Err2,
12 Err3
13 };
14 //=
15
16 //iscodeenum
17 namespace std
18 {
19 template <>
20 struct is_error_code_enum<CatErr>: public true_type
21 {};
22 }
23 //=
24
25 std::error_code make_error_code(CatErr ce);
26
27 // -------------------------------------------------------------------
28
29 //cond
30 enum class Cond
31 {
32 NoCond = -1,
33 Cond1 = 1,
34 Cond2,
35 Cond3,
36 };
37 //=
38
39 //iscondenum
40 namespace std
41 {
42 template <>
43 struct is_error_condition_enum<Cond>: public true_type
44 {};
45 }
46 //=
47
48 std::error_condition make_error_condition(Cond ec);
49
50 // -------------------------------------------------------------------
51
52 //category
53 class Category: public std::error_category
54 {
55 static Category s_instance;
56 struct POD
57 {
58 Cond cond;
59 char const *msg;
60 };
61
62 static std::unordered_map<CatErr, POD> s_map;
63
64 public:
65 Category(Category const &other) = delete;
66
67 static Category &instance();
68
69 bool equivalent(std::error_code const &ec, int condNr)
70 const noexcept override;
71 bool equivalent(int ev, std::error_condition const &condition)
72 const noexcept override;
73 std::error_condition default_error_condition(int ev)
74 const noexcept override;
75
76 std::string message(int ce) const override;
77 char const *name() const noexcept override;
78
79 private:
80 Category() = default;
81
82 template <typename Enum>
83 static constexpr Enum as(int err);
84 };
85 //=
86
87
88 template <typename Enum>
89 inline constexpr Enum Category::as(int value)
90 {
91 return static_cast<Enum>(value);
92 }
93
94 #endif