Debian Codegraph @

yo/advancedtemplates/examples/errcode/category/equivalent1.cc

raw
1 //#define XERR
2 #include "category.ih"
3
4 //equiv
5 bool Category::equivalent(std::error_code const &ec, int condNr)
6 const noexcept
7 {
8 if (*this != ec.category())
9 return false;
10
11 if (ec.value() == 0) // no error in ec?
12 return condNr == 0; // then condNr must
13 // also be 0
14
15 auto iter = s_map.find(as<CatErr>(ec.value())); // info associated
16 // with ec's CatErr
17
18 return iter == s_map.end() ?
19 false // not found or
20 : // compare Cond values
21 iter->second.cond == as<Cond>(condNr);
22 }
23 //=
24
25 /*************************************************************************
26
27 The error_category::equivalent() virtual function is used to determine
28 equivalence between error codes and conditions. In fact, there are two
29 overloads of the error_category::equivalent() function.
30
31 The first overload:
32
33 bool equivalent(error_code const &code, int condition) const;
34 ---------------------- ---------------
35 arbitrary error error condition
36 code in the current
37 cat
38
39 defines equivalence between error_conditions in the current category with
40 error_codes from any category.
41
42
43 The second:
44
45 bool equivalent(int code, error_condition const &condition) const;
46 --------- ---------------
47 error code arbitrary error
48 in the current condition
49 cat.
50
51 is used to establish equivalence between error_codes
52 in the current category with arbitrary error_conditions.
53
54
55 Since you are creating custom error conditions, it is the first overload that
56 you must override. Defining equivalence is simple: return true if you want an
57 error_code to be equivalent to your condition, otherwise return false.
58
59
60 If your intent is to abstract OS-specific errors, you might implement
61 error_category::equivalent() like this:
62
63 (api_category_impl::)
64 bool equivalent(std::error_code const &code, int condition) const
65 {
66 switch (condition)
67 {
68 case api_error::name_not_found:
69 return code == std::error_code(system_category());
70
71 default:
72 return false;
73 }
74 }
75
76 bool equivalent(std::error_code const &code, int condition) const
77 {
78 switch (condition) // available condition
79 {
80 // map the error code enum values in the current class
81 // on the conditions used in the current class and
82 // verify that the error code values are equal to
83 // the values of error_code code:
84
85 case low_system_resources: // the error condition
86 return code == not_enough_memory
87 || code == resource_unavailable_try_again
88 || code == too_many_files_open
89 || code == too_many_files_open_in_system;
90 ...
91
92 case api_error::no_such_entry:
93 return code == std::errc::no_such_file_or_directory;
94
95 default:
96 return false;
97 }
98 }
99
100 ******************/