Skip to content

Commit 0f05302

Browse files
authored
Merge pull request zerebubuth#372 from mmd-osm/patch/formatter1
Change some enums to enum class
2 parents 972d3ff + 054f621 commit 0f05302

24 files changed

+137
-160
lines changed

include/cgimap/handler.hpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ class handler {
6161
http::method::HEAD |
6262
http::method::OPTIONS;
6363

64-
handler(mime::type default_type = mime::unspecified_type,
64+
handler(mime::type default_type = mime::type::unspecified_type,
6565
http::method methods = default_methods);
6666
virtual ~handler() = default;
6767

@@ -90,7 +90,7 @@ using handler_ptr_t = std::unique_ptr<handler>;
9090

9191
class payload_enabled_handler : public handler {
9292
public:
93-
payload_enabled_handler(mime::type default_type = mime::unspecified_type,
93+
payload_enabled_handler(mime::type default_type = mime::type::unspecified_type,
9494
http::method methods = http::method::POST | http::method::OPTIONS);
9595

9696
// Responder used to update the database

include/cgimap/mime_types.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
* simple set of supported mime types.
1717
*/
1818
namespace mime {
19-
enum type {
19+
enum class type {
2020
unspecified_type, // a "null" type, used to indicate no choice.
2121
text_plain,
2222
application_xml,

include/cgimap/output_formatter.hpp

+28-10
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,38 @@
2525
/**
2626
* What type of element the formatter is starting to write.
2727
*/
28-
enum element_type {
29-
element_type_changeset,
30-
element_type_node,
31-
element_type_way,
32-
element_type_relation
28+
enum class element_type {
29+
changeset,
30+
node,
31+
way,
32+
relation
3333
};
3434

35-
// TODO: document me.
36-
enum action_type {
37-
action_type_create,
38-
action_type_modify,
39-
action_type_delete
35+
enum class action_type {
36+
create,
37+
modify,
38+
del // delete is a reserved keyword
4039
};
4140

41+
namespace {
42+
43+
const char* element_type_name(element_type elt) {
44+
45+
switch (elt) {
46+
case element_type::node:
47+
return "node";
48+
case element_type::way:
49+
return "way";
50+
case element_type::relation:
51+
return "relation";
52+
case element_type::changeset:
53+
return "changeset";
54+
}
55+
return "";
56+
}
57+
58+
} // anonymous namespace
59+
4260
struct element_info {
4361

4462
element_info() = default;

src/api06/changeset_close_handler.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ changeset_close_responder::changeset_close_responder(mime::type mt,
4040

4141
changeset_close_handler::changeset_close_handler(const request &,
4242
osm_changeset_id_t id)
43-
: payload_enabled_handler(mime::text_plain,
43+
: payload_enabled_handler(mime::type::text_plain,
4444
http::method::PUT | http::method::OPTIONS),
4545
id(id) {}
4646

src/api06/changeset_create_handler.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ changeset_create_responder::changeset_create_responder(mime::type mt,
4242
}
4343

4444
changeset_create_handler::changeset_create_handler(const request &)
45-
: payload_enabled_handler(mime::text_plain,
45+
: payload_enabled_handler(mime::type::text_plain,
4646
http::method::PUT | http::method::OPTIONS) {}
4747

4848
std::string changeset_create_handler::log_name() const {

src/api06/changeset_update_handler.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ changeset_update_sel_responder::changeset_update_sel_responder(
5454

5555

5656
changeset_update_handler::changeset_update_handler(const request &req, osm_changeset_id_t id)
57-
: payload_enabled_handler(mime::application_xml,
57+
: payload_enabled_handler(mime::type::application_xml,
5858
http::method::PUT | http::method::OPTIONS),
5959
id(id) {}
6060

src/api06/changeset_upload_handler.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ changeset_upload_responder::changeset_upload_responder(mime::type mt,
7575

7676
changeset_upload_handler::changeset_upload_handler(const request &,
7777
osm_changeset_id_t id)
78-
: payload_enabled_handler(mime::unspecified_type,
78+
: payload_enabled_handler(mime::type::unspecified_type,
7979
http::method::POST | http::method::OPTIONS),
8080
id(id) {}
8181

src/backend/apidb/common_pgsql_selection.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -109,17 +109,17 @@ element_type type_from_name(const char *name) {
109109
switch (name[0]) {
110110
case 'N':
111111
case 'n':
112-
type = element_type_node;
112+
type = element_type::node;
113113
break;
114114

115115
case 'W':
116116
case 'w':
117-
type = element_type_way;
117+
type = element_type::way;
118118
break;
119119

120120
case 'R':
121121
case 'r':
122-
type = element_type_relation;
122+
type = element_type::relation;
123123
break;
124124

125125
default:

src/backend/staticxml/staticxml.cpp

+9-9
Original file line numberDiff line numberDiff line change
@@ -203,11 +203,11 @@ struct xml_parser {
203203
member_info m;
204204
auto member_type = get_attribute<std::string>("type", attributes);
205205
if (member_type == "node") {
206-
m.type = element_type_node;
206+
m.type = element_type::node;
207207
} else if (member_type == "way") {
208-
m.type = element_type_way;
208+
m.type = element_type::way;
209209
} else if (member_type == "relation") {
210-
m.type = element_type_relation;
210+
m.type = element_type::relation;
211211
} else {
212212
throw std::runtime_error(fmt::format("Unknown member type `{}'.", member_type));
213213
}
@@ -399,7 +399,7 @@ struct static_data_selection : public data_selection {
399399
auto r = find_current<relation>(id);
400400
if (r) {
401401
for (const member_info &m : r->get().m_members) {
402-
if (m.type == element_type_node) {
402+
if (m.type == element_type::node) {
403403
m_nodes.insert(m.ref);
404404
}
405405
}
@@ -430,7 +430,7 @@ struct static_data_selection : public data_selection {
430430
auto r = find_current<relation>(id);
431431
if (r) {
432432
for (const member_info &m : r->get().m_members) {
433-
if (m.type == element_type_way) {
433+
if (m.type == element_type::way) {
434434
m_ways.insert(m.ref);
435435
}
436436
}
@@ -447,7 +447,7 @@ struct static_data_selection : public data_selection {
447447
const relation &r = itr->second;
448448
if (next == end || next->second.m_info.id != r.m_info.id) {
449449
for (const member_info &m : r.m_members) {
450-
if ((m.type == element_type_way) && (m_ways.count(m.ref) > 0)) {
450+
if ((m.type == element_type::way) && (m_ways.count(m.ref) > 0)) {
451451
m_relations.insert(r.m_info.id);
452452
break;
453453
}
@@ -468,7 +468,7 @@ struct static_data_selection : public data_selection {
468468
void select_relations_from_nodes() override {
469469
for (auto const & [_, r] : m_db.m_relations) {
470470
for (const member_info &m : r.m_members) {
471-
if ((m.type == element_type_node) && (m_nodes.count(m.ref) > 0)) {
471+
if ((m.type == element_type::node) && (m_nodes.count(m.ref) > 0)) {
472472
m_relations.insert(r.m_info.id);
473473
break;
474474
}
@@ -480,7 +480,7 @@ struct static_data_selection : public data_selection {
480480
std::set<osm_nwr_id_t> tmp_relations;
481481
for (auto const & [_, r] : m_db.m_relations) {
482482
for (const member_info &m : r.m_members) {
483-
if ((m.type == element_type_relation) &&
483+
if ((m.type == element_type::relation) &&
484484
(m_relations.count(m.ref) > 0)) {
485485
tmp_relations.insert(r.m_info.id);
486486
break;
@@ -497,7 +497,7 @@ struct static_data_selection : public data_selection {
497497
auto r = find_current<relation>(id);
498498
if (r) {
499499
for (const member_info &m : r->get().m_members) {
500-
if (m.type == element_type_relation) {
500+
if (m.type == element_type::relation) {
501501
m_relations.insert(m.ref);
502502
}
503503
}

src/choose_formatter.cpp

+13-13
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class acceptable_types {
5454
public:
5555
explicit acceptable_types(const std::string &accept_header);
5656
bool is_acceptable(mime::type) const;
57-
// note: returns mime::unspecified_type if none were acceptable
57+
// note: returns mime::type::unspecified_type if none were acceptable
5858
mime::type most_acceptable_of(const std::vector<mime::type> &available) const;
5959

6060
private:
@@ -121,10 +121,10 @@ struct http_accept_grammar
121121
};
122122
/*
123123
= lit("* / *") [_val = mime::any_type]
124-
| lit("text/xml") [_val = mime::application_xml]
125-
| lit("application/xml") [_val = mime::application_xml]
124+
| lit("text/xml") [_val = mime::type::application_xml]
125+
| lit("application/xml") [_val = mime::type::application_xml]
126126
#if HAVE_YAJL
127-
| lit("application/json")[_val = mime::application_json]
127+
| lit("application/json")[_val = mime::type::application_json]
128128
#endif
129129
;
130130
*/
@@ -144,7 +144,7 @@ acceptable_types::acceptable_types(const std::string &accept_header) {
144144
for (media_range range : ranges) {
145145
// figure out the mime::type from the string.
146146
mime::type mime_type = mime::parse_from(range.mime_type);
147-
if (mime_type == mime::unspecified_type) {
147+
if (mime_type == mime::type::unspecified_type) {
148148
// if it's unknown then skip this type...
149149
continue;
150150
}
@@ -174,7 +174,7 @@ bool acceptable_types::is_acceptable(mime::type mt) const {
174174
}
175175

176176
mime::type acceptable_types::most_acceptable_of(const std::vector<mime::type> &available) const {
177-
mime::type best = mime::unspecified_type;
177+
mime::type best = mime::type::unspecified_type;
178178
float score = std::numeric_limits<float>::min();
179179
for (mime::type type : available) {
180180
auto itr = mapping.find(type);
@@ -188,7 +188,7 @@ mime::type acceptable_types::most_acceptable_of(const std::vector<mime::type> &a
188188

189189
// also check the full wildcard.
190190
if (!available.empty()) {
191-
auto itr = mapping.find(mime::any_type);
191+
auto itr = mapping.find(mime::type::any_type);
192192
if ((itr != mapping.end()) && (itr->second > score)) {
193193
best = available.front();
194194
}
@@ -230,7 +230,7 @@ mime::type choose_best_mime_type(const request &req, const responder& hptr) {
230230
mime::type best_type = hptr.resource_type();
231231
// check if the handler is capable of supporting an acceptable set of mime
232232
// types.
233-
if (best_type != mime::unspecified_type) {
233+
if (best_type != mime::type::unspecified_type) {
234234
// check that this doesn't conflict with anything in the Accept header.
235235
if (!hptr.is_available(best_type))
236236
throw http::not_acceptable(fmt::format("Acceptable formats for {} are: {}",
@@ -243,11 +243,11 @@ mime::type choose_best_mime_type(const request &req, const responder& hptr) {
243243
} else {
244244
best_type = types.most_acceptable_of(types_available);
245245
// if none were acceptable then...
246-
if (best_type == mime::unspecified_type) {
246+
if (best_type == mime::type::unspecified_type) {
247247
throw http::not_acceptable(fmt::format("Acceptable formats for {} are: {}",
248248
get_request_path(req),
249249
mime_types_to_string(types_available)));
250-
} else if (best_type == mime::any_type) {
250+
} else if (best_type == mime::type::any_type) {
251251
// choose the first of the available types if nothing is preferred.
252252
best_type = *(hptr.types_available().begin());
253253
}
@@ -260,14 +260,14 @@ mime::type choose_best_mime_type(const request &req, const responder& hptr) {
260260
std::unique_ptr<output_formatter> create_formatter(mime::type best_type, output_buffer& out) {
261261

262262
switch (best_type) {
263-
case mime::application_xml:
263+
case mime::type::application_xml:
264264
return std::make_unique<xml_formatter>(std::make_unique<xml_writer>(out, true));
265265

266266
#if HAVE_YAJL
267-
case mime::application_json:
267+
case mime::type::application_json:
268268
return std::make_unique<json_formatter>(std::make_unique<json_writer>(out, false));
269269
#endif
270-
case mime::text_plain:
270+
case mime::type::text_plain:
271271
return std::make_unique<text_formatter>(std::make_unique<text_writer>(out, true));
272272

273273
default:

src/json_formatter.cpp

+1-21
Original file line numberDiff line numberDiff line change
@@ -11,31 +11,11 @@
1111

1212
#include <chrono>
1313

14-
namespace {
15-
16-
const std::string &element_type_name(element_type elt) {
17-
static std::string name_node("node"), name_way("way"),
18-
name_relation("relation");
19-
20-
switch (elt) {
21-
case element_type_node:
22-
return name_node;
23-
case element_type_way:
24-
return name_way;
25-
case element_type_relation:
26-
return name_relation;
27-
default:
28-
throw std::runtime_error("Unknown element type in element_type_name().");
29-
}
30-
}
31-
32-
} // anonymous namespace
33-
3414
json_formatter::json_formatter(std::unique_ptr<json_writer> w) : writer(std::move(w)) {}
3515

3616
json_formatter::~json_formatter() = default;
3717

38-
mime::type json_formatter::mime_type() const { return mime::application_json; }
18+
mime::type json_formatter::mime_type() const { return mime::type::application_json; }
3919

4020
void json_formatter::write_tags(const tags_t &tags) {
4121

src/mime_types.cpp

+12-13
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@ using std::runtime_error;
1515

1616
namespace mime {
1717
string to_string(type t) {
18-
if (any_type == t) {
18+
if (mime::type::any_type == t) {
1919
return "*/*";
20-
} else if (text_plain == t) {
20+
} else if (mime::type::text_plain == t) {
2121
return "text/plain";
22-
} else if (application_xml == t) {
22+
} else if (mime::type::application_xml == t) {
2323
return "application/xml";
2424
#if HAVE_YAJL
25-
} else if (application_json == t) {
25+
} else if (mime::type::application_json == t) {
2626
return "application/json";
2727
#endif
2828
} else {
@@ -31,26 +31,25 @@ string to_string(type t) {
3131
}
3232

3333
type parse_from(const std::string &name) {
34-
type t = unspecified_type;
3534

3635
if (name == "*") {
37-
t = any_type;
36+
return mime::type::any_type;
3837
} else if (name == "*/*") {
39-
t = any_type;
38+
return mime::type::any_type;
4039
} else if (name == "text/*") {
41-
t = any_type;
40+
return mime::type::any_type;
4241
} else if (name == "text/plain") {
43-
t = text_plain;
42+
return mime::type::text_plain;
4443
} else if (name == "text/xml") { // alias according to RFC 7303, section 9.2
45-
t = application_xml;
44+
return mime::type:: application_xml;
4645
} else if (name == "application/xml") {
47-
t = application_xml;
46+
return mime::type::application_xml;
4847
#if HAVE_YAJL
4948
} else if (name == "application/json") {
50-
t = application_json;
49+
return mime::type::application_json;
5150
#endif
5251
}
5352

54-
return t;
53+
return mime::type::unspecified_type;
5554
}
5655
}

0 commit comments

Comments
 (0)