Skip to content

Commit 1aa3fbb

Browse files
Mohamed Ikbel Boulabiargittiver
authored andcommitted
Replace uint16_t with size_t for rule/blueprint indices across routing (routing_handle_result and Trie/Router) to eliminate narrowing warnings
1 parent d5ca7f8 commit 1aa3fbb

2 files changed

Lines changed: 21 additions & 20 deletions

File tree

include/crow/common.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -280,19 +280,19 @@ namespace crow
280280
struct routing_handle_result
281281
{
282282
bool catch_all{false};
283-
uint16_t rule_index;
284-
std::vector<uint16_t> blueprint_indices;
283+
size_t rule_index;
284+
std::vector<size_t> blueprint_indices;
285285
routing_params r_params;
286286
HTTPMethod method;
287287

288288
routing_handle_result() {}
289289

290-
routing_handle_result(uint16_t rule_index_, std::vector<uint16_t> blueprint_indices_, routing_params r_params_):
290+
routing_handle_result(size_t rule_index_, std::vector<size_t> blueprint_indices_, routing_params r_params_):
291291
rule_index(rule_index_),
292292
blueprint_indices(blueprint_indices_),
293293
r_params(r_params_) {}
294294

295-
routing_handle_result(uint16_t rule_index_, std::vector<uint16_t> blueprint_indices_, routing_params r_params_, HTTPMethod method_):
295+
routing_handle_result(size_t rule_index_, std::vector<size_t> blueprint_indices_, routing_params r_params_, HTTPMethod method_):
296296
rule_index(rule_index_),
297297
blueprint_indices(blueprint_indices_),
298298
r_params(r_params_),

include/crow/routing.h

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#pragma once
22

33
#include <cstdint>
4+
#include <limits>
45
#include <utility>
56
#include <tuple>
67
#include <unordered_map>
@@ -23,7 +24,7 @@
2324
namespace crow // NOTE: Already documented in "crow/app.h"
2425
{
2526

26-
constexpr const uint16_t INVALID_BP_ID{((uint16_t)-1)};
27+
constexpr size_t INVALID_BP_ID{SIZE_MAX};
2728

2829
namespace detail
2930
{
@@ -718,7 +719,7 @@ namespace crow // NOTE: Already documented in "crow/app.h"
718719
std::function<void(crow::request&, crow::response&, Args...)> handler_;
719720
};
720721

721-
const int RULE_SPECIAL_REDIRECT_SLASH = 1;
722+
constexpr size_t RULE_SPECIAL_REDIRECT_SLASH = 1;
722723

723724

724725
/// A search tree.
@@ -727,9 +728,9 @@ namespace crow // NOTE: Already documented in "crow/app.h"
727728
public:
728729
struct Node
729730
{
730-
uint16_t rule_index{};
731+
size_t rule_index{};
731732
// Assign the index to the maximum 32 unsigned integer value by default so that any other number (specifically 0) is a valid BP id.
732-
uint16_t blueprint_index{INVALID_BP_ID};
733+
size_t blueprint_index{INVALID_BP_ID};
733734
std::string key;
734735
ParamType param = ParamType::MAX; // MAX = No param.
735736
std::vector<Node> children;
@@ -852,19 +853,19 @@ namespace crow // NOTE: Already documented in "crow/app.h"
852853
}
853854

854855
//Rule_index, Blueprint_index, routing_params
855-
routing_handle_result find(const std::string& req_url, const Node& node, unsigned pos = 0, routing_params* params = nullptr, std::vector<uint16_t>* blueprints = nullptr) const
856+
routing_handle_result find(const std::string& req_url, const Node& node, size_t pos = 0, routing_params* params = nullptr, std::vector<size_t>* blueprints = nullptr) const
856857
{
857858
//start params as an empty struct
858859
routing_params empty;
859860
if (params == nullptr)
860861
params = &empty;
861862
//same for blueprint vector
862-
std::vector<uint16_t> MT;
863+
std::vector<size_t> MT;
863864
if (blueprints == nullptr)
864865
blueprints = &MT;
865866

866-
uint16_t found{}; //The rule index to be found
867-
std::vector<uint16_t> found_BP; //The Blueprint indices to be found
867+
size_t found{}; //The rule index to be found
868+
std::vector<size_t> found_BP; //The Blueprint indices to be found
868869
routing_params match_params; //supposedly the final matched parameters
869870

870871
auto update_found = [&found, &found_BP, &match_params](routing_handle_result& ret) {
@@ -1016,7 +1017,7 @@ namespace crow // NOTE: Already documented in "crow/app.h"
10161017
}
10171018

10181019
//This functions assumes any blueprint info passed is valid
1019-
void add(const std::string& url, uint16_t rule_index, unsigned bp_prefix_length = 0, uint16_t blueprint_index = INVALID_BP_ID)
1020+
void add(const std::string& url, size_t rule_index, unsigned bp_prefix_length = 0, size_t blueprint_index = INVALID_BP_ID)
10201021
{
10211022
auto idx = &head_;
10221023

@@ -1301,7 +1302,7 @@ namespace crow // NOTE: Already documented in "crow/app.h"
13011302
internal_add_rule_object(rule, ruleObject, INVALID_BP_ID, blueprints_);
13021303
}
13031304

1304-
void internal_add_rule_object(const std::string& rule, BaseRule* ruleObject, const uint16_t& BP_index, std::vector<Blueprint*>& blueprints)
1305+
void internal_add_rule_object(const std::string& rule, BaseRule* ruleObject, const size_t& BP_index, std::vector<Blueprint*>& blueprints)
13051306
{
13061307
bool has_trailing_slash = false;
13071308
std::string rule_without_trailing_slash;
@@ -1434,7 +1435,7 @@ namespace crow // NOTE: Already documented in "crow/app.h"
14341435

14351436
auto& per_method = per_methods_[static_cast<int>(req.method)];
14361437
auto& rules = per_method.rules;
1437-
unsigned rule_index = per_method.trie.find(req.url).rule_index;
1438+
size_t rule_index = per_method.trie.find(req.url).rule_index;
14381439

14391440
if (!rule_index)
14401441
{
@@ -1483,7 +1484,7 @@ namespace crow // NOTE: Already documented in "crow/app.h"
14831484
}
14841485
}
14851486

1486-
void get_found_bp(const std::vector<uint16_t>& bp_i, const std::vector<Blueprint*>& blueprints, std::vector<Blueprint*>& found_bps, uint16_t index = 0)
1487+
void get_found_bp(const std::vector<size_t>& bp_i, const std::vector<Blueprint*>& blueprints, std::vector<Blueprint*>& found_bps, size_t index = 0)
14871488
{
14881489
// This statement makes 3 assertions:
14891490
// 1. The index is above 0.
@@ -1529,7 +1530,7 @@ namespace crow // NOTE: Already documented in "crow/app.h"
15291530
get_found_bp(found.blueprint_indices, blueprints_, bps_found);
15301531
for (int i = bps_found.size() - 1; i > 0; i--)
15311532
{
1532-
std::vector<uint16_t> bpi = found.blueprint_indices;
1533+
std::vector<size_t> bpi = found.blueprint_indices;
15331534
if (bps_found[i]->catchall_rule().has_handler()) {
15341535
return bps_found[i]->catchall_rule();
15351536
}
@@ -1545,7 +1546,7 @@ namespace crow // NOTE: Already documented in "crow/app.h"
15451546
get_found_bp(found.blueprint_indices, blueprints_, bps_found);
15461547
for (int i = bps_found.size() - 1; i > 0; i--)
15471548
{
1548-
std::vector<uint16_t> bpi = found.blueprint_indices;
1549+
std::vector<size_t> bpi = found.blueprint_indices;
15491550
if (bps_found[i]->catchall_rule().has_handler())
15501551
{
15511552
#ifdef CROW_ENABLE_DEBUG
@@ -1573,7 +1574,7 @@ namespace crow // NOTE: Already documented in "crow/app.h"
15731574
std::unique_ptr<routing_handle_result> found{
15741575
new routing_handle_result(
15751576
0,
1576-
std::vector<uint16_t>(),
1577+
std::vector<size_t>(),
15771578
routing_params(),
15781579
HTTPMethod::InternalMethodCount)}; // This is always returned to avoid a null pointer dereference.
15791580

@@ -1715,7 +1716,7 @@ namespace crow // NOTE: Already documented in "crow/app.h"
17151716
} else {
17161717
HTTPMethod method_actual = found.method;
17171718
const auto& rules = per_methods_[static_cast<int>(method_actual)].rules;
1718-
const unsigned rule_index = found.rule_index;
1719+
const size_t rule_index = found.rule_index;
17191720

17201721
if (rule_index >= rules.size())
17211722
throw std::runtime_error("Trie internal structure corrupted!");

0 commit comments

Comments
 (0)