Skip to content

Commit cb3c827

Browse files
committed
router: use vector instead of list
1 parent beffe14 commit cb3c827

File tree

2 files changed

+24
-26
lines changed

2 files changed

+24
-26
lines changed

include/cgimap/router.hpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212

1313
#include "cgimap/types.hpp"
1414

15-
#include <string>
16-
#include <list>
17-
#include <stdexcept>
1815
#include <iostream>
16+
#include <stdexcept>
17+
#include <string>
18+
#include <vector>
1919

2020
#include <boost/fusion/container/generation/make_cons.hpp>
2121
#include <boost/fusion/include/make_cons.hpp>
@@ -43,7 +43,7 @@ using boost::fusion::as_list;
4343
namespace result_of = boost::fusion::result_of;
4444

4545
// iterates over the split up parts of the item being matched.
46-
using part_iterator = std::list<std::string>::const_iterator;
46+
using part_iterator = std::vector<std::string>::const_iterator;
4747

4848
/**
4949
* thrown when a match error occurs, giving some information about the

src/routes.cpp

+20-22
Original file line numberDiff line numberDiff line change
@@ -52,16 +52,14 @@
5252
#include "cgimap/http.hpp"
5353
#include "cgimap/mime_types.hpp"
5454

55+
#include <memory>
5556
#include <optional>
57+
#include <string>
58+
#include <vector>
5659

5760
#include <boost/algorithm/string.hpp>
5861
#include <fmt/core.h>
5962

60-
using std::list;
61-
using std::string;
62-
using std::pair;
63-
using std::unique_ptr;
64-
6563
using boost::fusion::make_cons;
6664
using boost::fusion::invoke;
6765

@@ -76,7 +74,7 @@ struct router {
7674
// interface through which all matches and constructions are performed.
7775
struct rule_base {
7876
virtual ~rule_base() = default;
79-
virtual bool invoke_if(const list<string> &, request &, handler_ptr_t &) = 0;
77+
virtual bool invoke_if(const std::vector<std::string> &, request &, handler_ptr_t &) = 0;
8078
};
8179

8280
using rule_ptr = std::unique_ptr<rule_base>;
@@ -97,7 +95,7 @@ struct router {
9795

9896
// try to match the expression. if it succeeds, call the provided function
9997
// with the provided params and the matched DSL arguments.
100-
bool invoke_if(const list<string> &parts,
98+
bool invoke_if(const std::vector<std::string> &parts,
10199
request &params,
102100
handler_ptr_t &ptr) override {
103101
try {
@@ -164,7 +162,7 @@ struct router {
164162
* params.
165163
*/
166164

167-
handler_ptr_t match(const list<string> &p, request &params) {
165+
handler_ptr_t match(const std::vector<std::string> &p, request &params) {
168166

169167
handler_ptr_t hptr;
170168

@@ -223,9 +221,9 @@ struct router {
223221
}
224222

225223
private:
226-
list<rule_ptr> rules_get;
227-
list<rule_ptr> rules_post;
228-
list<rule_ptr> rules_put;
224+
std::vector<rule_ptr> rules_get;
225+
std::vector<rule_ptr> rules_post;
226+
std::vector<rule_ptr> rules_put;
229227
};
230228

231229
routes::routes()
@@ -290,36 +288,36 @@ namespace {
290288
* figures out the mime type from the path specification, e.g: a resource ending
291289
* in .xml should be application/xml, .json should be application/json, etc...
292290
*/
293-
pair<string, mime::type> resource_mime_type(const string &path) {
291+
std::pair<std::string, mime::type> resource_mime_type(const std::string &path) {
294292

295293
#if HAVE_YAJL
296294
{
297295
std::size_t json_found = path.rfind(".json");
298296

299-
if (json_found != string::npos && json_found == path.length() - 5) {
300-
return make_pair(path.substr(0, json_found), mime::type::application_json);
297+
if (json_found != std::string::npos && json_found == path.length() - 5) {
298+
return std::make_pair(path.substr(0, json_found), mime::type::application_json);
301299
}
302300
}
303301
#endif
304302

305303
{
306304
std::size_t xml_found = path.rfind(".xml");
307305

308-
if (xml_found != string::npos && xml_found == path.length() - 4) {
306+
if (xml_found != std::string::npos && xml_found == path.length() - 4) {
309307
return make_pair(path.substr(0, xml_found), mime::type::application_xml);
310308
}
311309
}
312310

313311
return make_pair(path, mime::type::unspecified_type);
314312
}
315313

316-
handler_ptr_t route_resource(request &req, const string &path,
317-
const unique_ptr<router> &r) {
314+
handler_ptr_t route_resource(request &req, const std::string &path,
315+
const std::unique_ptr<router> &r) {
318316
// strip off the format-spec, if there is one
319-
pair<string, mime::type> resource = resource_mime_type(path);
317+
std::pair<std::string, mime::type> resource = resource_mime_type(path);
320318

321319
// split the URL into bits to be matched.
322-
list<string> path_components;
320+
std::vector<std::string> path_components;
323321
al::split(path_components, resource.first, al::is_any_of("/"));
324322

325323
handler_ptr_t hptr(r->match(path_components, req));
@@ -338,16 +336,16 @@ handler_ptr_t route_resource(request &req, const string &path,
338336

339337
handler_ptr_t routes::operator()(request &req) const {
340338
// full path from request handler
341-
string path = get_request_path(req);
339+
auto path = get_request_path(req);
342340
handler_ptr_t hptr;
343341
// check the prefix
344342
if (path.compare(0, common_prefix.size(), common_prefix) == 0) {
345-
hptr = route_resource(req, string(path, common_prefix.size()), r);
343+
hptr = route_resource(req, std::string(path, common_prefix.size()), r);
346344

347345
#ifdef ENABLE_API07
348346
} else if (path.compare(0, experimental_prefix.size(), experimental_prefix) ==
349347
0) {
350-
hptr = route_resource(req, string(path, experimental_prefix.size()),
348+
hptr = route_resource(req, std::string(path, experimental_prefix.size()),
351349
r_experimental);
352350
#endif /* ENABLE_API07 */
353351
}

0 commit comments

Comments
 (0)