|
36 | 36 |
|
37 | 37 | #include <fstream> |
38 | 38 | #include <iterator> |
| 39 | +#include <type_traits> |
39 | 40 |
|
40 | 41 | //------------------------------------------------------------------------- |
41 | 42 | // RecRegisterStatXXX |
42 | 43 | //------------------------------------------------------------------------- |
43 | | -#define REC_REGISTER_STAT_XXX(A, B) \ |
44 | | - ink_assert((rec_type == RECT_NODE) || (rec_type == RECT_PROCESS) || (rec_type == RECT_LOCAL) || (rec_type == RECT_PLUGIN)); \ |
45 | | - RecRecord *r; \ |
46 | | - RecData my_data_default; \ |
47 | | - my_data_default.A = data_default; \ |
48 | | - if ((r = RecRegisterStat(rec_type, name, B, my_data_default, persist_type)) != nullptr) { \ |
49 | | - if (i_am_the_record_owner(r->rec_type)) { \ |
50 | | - r->sync_required = r->sync_required | REC_PEER_SYNC_REQUIRED; \ |
51 | | - } \ |
52 | | - return REC_ERR_OKAY; \ |
53 | | - } else { \ |
54 | | - return REC_ERR_FAIL; \ |
| 44 | +namespace |
| 45 | +{ |
| 46 | + |
| 47 | +template <typename> struct always_false : std::false_type { |
| 48 | +}; |
| 49 | + |
| 50 | +// Tag types for template dispatch - distinguishes RecInt from RecCounter |
| 51 | +// even though they're both int64_t type aliases. |
| 52 | +// Needed to make sure the data_type is set correctly for the given type. |
| 53 | +// without this, the data_type would be set to RECD_INT for both RecInt and RecCounter. |
| 54 | +namespace rec_detail |
| 55 | +{ |
| 56 | + struct IntTag { |
| 57 | + using type = RecInt; |
| 58 | + static constexpr RecDataT data_type = RECD_INT; |
| 59 | + }; |
| 60 | + struct CounterTag { |
| 61 | + using type = RecCounter; |
| 62 | + static constexpr RecDataT data_type = RECD_COUNTER; |
| 63 | + }; |
| 64 | + struct FloatTag { |
| 65 | + using type = RecFloat; |
| 66 | + static constexpr RecDataT data_type = RECD_FLOAT; |
| 67 | + }; |
| 68 | + struct StringTag { |
| 69 | + using type = RecString; |
| 70 | + static constexpr RecDataT data_type = RECD_STRING; |
| 71 | + }; |
| 72 | +} // namespace rec_detail |
| 73 | + |
| 74 | +template <typename Tag> |
| 75 | +RecErrT |
| 76 | +RecRegisterStatImpl(RecT rec_type, const char *name, typename Tag::type data_default, RecPersistT persist_type) |
| 77 | +{ |
| 78 | + ink_assert((rec_type == RECT_NODE) || (rec_type == RECT_PROCESS) || (rec_type == RECT_LOCAL) || (rec_type == RECT_PLUGIN)); |
| 79 | + |
| 80 | + RecData my_data_default; |
| 81 | + |
| 82 | + if constexpr (std::is_same_v<Tag, rec_detail::IntTag>) { |
| 83 | + my_data_default.rec_int = data_default; |
| 84 | + } else if constexpr (std::is_same_v<Tag, rec_detail::CounterTag>) { |
| 85 | + my_data_default.rec_counter = data_default; |
| 86 | + } else if constexpr (std::is_same_v<Tag, rec_detail::FloatTag>) { |
| 87 | + my_data_default.rec_float = data_default; |
| 88 | + } else { |
| 89 | + static_assert(always_false<Tag>::value, "Unsupported tag for RecRegisterStat"); |
| 90 | + } |
| 91 | + |
| 92 | + if (RecRecord *r = RecRegisterStat(rec_type, name, Tag::data_type, my_data_default, persist_type); r != nullptr) { |
| 93 | + if (i_am_the_record_owner(r->rec_type)) { |
| 94 | + r->sync_required = r->sync_required | REC_PEER_SYNC_REQUIRED; |
| 95 | + } |
| 96 | + return REC_ERR_OKAY; |
55 | 97 | } |
56 | 98 |
|
| 99 | + return REC_ERR_FAIL; |
| 100 | +} |
| 101 | +} // namespace |
| 102 | + |
57 | 103 | RecErrT |
58 | 104 | _RecRegisterStatInt(RecT rec_type, const char *name, RecInt data_default, RecPersistT persist_type) |
59 | 105 | { |
60 | | - REC_REGISTER_STAT_XXX(rec_int, RECD_INT); |
| 106 | + return RecRegisterStatImpl<rec_detail::IntTag>(rec_type, name, data_default, persist_type); |
61 | 107 | } |
62 | 108 |
|
63 | 109 | RecErrT |
64 | 110 | _RecRegisterStatFloat(RecT rec_type, const char *name, RecFloat data_default, RecPersistT persist_type) |
65 | 111 | { |
66 | | - REC_REGISTER_STAT_XXX(rec_float, RECD_FLOAT); |
| 112 | + return RecRegisterStatImpl<rec_detail::FloatTag>(rec_type, name, data_default, persist_type); |
67 | 113 | } |
68 | 114 |
|
69 | 115 | RecErrT |
70 | 116 | _RecRegisterStatCounter(RecT rec_type, const char *name, RecCounter data_default, RecPersistT persist_type) |
71 | 117 | { |
72 | | - REC_REGISTER_STAT_XXX(rec_counter, RECD_COUNTER); |
| 118 | + return RecRegisterStatImpl<rec_detail::CounterTag>(rec_type, name, data_default, persist_type); |
73 | 119 | } |
74 | 120 |
|
75 | 121 | //------------------------------------------------------------------------- |
76 | 122 | // RecRegisterConfigXXX |
77 | 123 | //------------------------------------------------------------------------- |
78 | | -#define REC_REGISTER_CONFIG_XXX(A, B) \ |
79 | | - RecRecord *r; \ |
80 | | - RecData my_data_default; \ |
81 | | - my_data_default.A = data_default; \ |
82 | | - if ((r = RecRegisterConfig(rec_type, name, B, my_data_default, update_type, check_type, check_regex, source, access_type)) != \ |
83 | | - nullptr) { \ |
84 | | - if (i_am_the_record_owner(r->rec_type)) { \ |
85 | | - r->sync_required = r->sync_required | REC_PEER_SYNC_REQUIRED; \ |
86 | | - } \ |
87 | | - return REC_ERR_OKAY; \ |
88 | | - } else { \ |
89 | | - return REC_ERR_FAIL; \ |
| 124 | +namespace |
| 125 | +{ |
| 126 | + |
| 127 | +template <typename Tag> |
| 128 | +RecErrT |
| 129 | +RecRegisterConfigImpl(RecT rec_type, const char *name, typename Tag::type data_default, RecUpdateT update_type, |
| 130 | + RecCheckT check_type, const char *check_regex, RecSourceT source, RecAccessT access_type) |
| 131 | +{ |
| 132 | + ink_assert((rec_type == RECT_CONFIG) || (rec_type == RECT_LOCAL)); |
| 133 | + |
| 134 | + RecData my_data_default; |
| 135 | + |
| 136 | + if constexpr (std::is_same_v<Tag, rec_detail::IntTag>) { |
| 137 | + my_data_default.rec_int = data_default; |
| 138 | + } else if constexpr (std::is_same_v<Tag, rec_detail::CounterTag>) { |
| 139 | + my_data_default.rec_counter = data_default; |
| 140 | + } else if constexpr (std::is_same_v<Tag, rec_detail::FloatTag>) { |
| 141 | + my_data_default.rec_float = data_default; |
| 142 | + } else if constexpr (std::is_same_v<Tag, rec_detail::StringTag>) { |
| 143 | + my_data_default.rec_string = data_default; |
| 144 | + } else { |
| 145 | + static_assert(always_false<Tag>::value, "Unsupported tag for RecRegisterConfig"); |
90 | 146 | } |
91 | 147 |
|
| 148 | + if (RecRecord *r = RecRegisterConfig(rec_type, name, Tag::data_type, my_data_default, update_type, check_type, check_regex, |
| 149 | + source, access_type); |
| 150 | + r != nullptr) { |
| 151 | + if (i_am_the_record_owner(r->rec_type)) { |
| 152 | + r->sync_required = r->sync_required | REC_PEER_SYNC_REQUIRED; |
| 153 | + } |
| 154 | + return REC_ERR_OKAY; |
| 155 | + } |
| 156 | + |
| 157 | + return REC_ERR_FAIL; |
| 158 | +} |
| 159 | +} // namespace |
| 160 | + |
92 | 161 | RecErrT |
93 | 162 | RecRegisterConfigInt(RecT rec_type, const char *name, RecInt data_default, RecUpdateT update_type, RecCheckT check_type, |
94 | 163 | const char *check_regex, RecSourceT source, RecAccessT access_type) |
95 | 164 | { |
96 | | - ink_assert((rec_type == RECT_CONFIG) || (rec_type == RECT_LOCAL)); |
97 | | - REC_REGISTER_CONFIG_XXX(rec_int, RECD_INT); |
| 165 | + return RecRegisterConfigImpl<rec_detail::IntTag>(rec_type, name, data_default, update_type, check_type, check_regex, source, |
| 166 | + access_type); |
98 | 167 | } |
99 | 168 |
|
100 | 169 | RecErrT |
101 | 170 | RecRegisterConfigFloat(RecT rec_type, const char *name, RecFloat data_default, RecUpdateT update_type, RecCheckT check_type, |
102 | 171 | const char *check_regex, RecSourceT source, RecAccessT access_type) |
103 | 172 | { |
104 | | - ink_assert((rec_type == RECT_CONFIG) || (rec_type == RECT_LOCAL)); |
105 | | - REC_REGISTER_CONFIG_XXX(rec_float, RECD_FLOAT); |
| 173 | + return RecRegisterConfigImpl<rec_detail::FloatTag>(rec_type, name, data_default, update_type, check_type, check_regex, source, |
| 174 | + access_type); |
106 | 175 | } |
107 | 176 |
|
108 | 177 | RecErrT |
109 | | -RecRegisterConfigString(RecT rec_type, const char *name, const char *data_default_tmp, RecUpdateT update_type, RecCheckT check_type, |
| 178 | +RecRegisterConfigString(RecT rec_type, const char *name, const char *data_default, RecUpdateT update_type, RecCheckT check_type, |
110 | 179 | const char *check_regex, RecSourceT source, RecAccessT access_type) |
111 | 180 | { |
112 | | - RecString data_default = const_cast<RecString>(data_default_tmp); |
113 | | - ink_assert((rec_type == RECT_CONFIG) || (rec_type == RECT_LOCAL)); |
114 | | - REC_REGISTER_CONFIG_XXX(rec_string, RECD_STRING); |
| 181 | + return RecRegisterConfigImpl<rec_detail::StringTag>(rec_type, name, const_cast<RecString>(data_default), update_type, check_type, |
| 182 | + check_regex, source, access_type); |
115 | 183 | } |
116 | 184 |
|
117 | 185 | RecErrT |
118 | 186 | RecRegisterConfigCounter(RecT rec_type, const char *name, RecCounter data_default, RecUpdateT update_type, RecCheckT check_type, |
119 | 187 | const char *check_regex, RecSourceT source, RecAccessT access_type) |
120 | 188 | { |
121 | | - ink_assert((rec_type == RECT_CONFIG) || (rec_type == RECT_LOCAL)); |
122 | | - REC_REGISTER_CONFIG_XXX(rec_counter, RECD_COUNTER); |
| 189 | + return RecRegisterConfigImpl<rec_detail::CounterTag>(rec_type, name, data_default, update_type, check_type, check_regex, source, |
| 190 | + access_type); |
123 | 191 | } |
124 | 192 |
|
125 | 193 | //------------------------------------------------------------------------- |
|
0 commit comments