Skip to content

Commit

Permalink
basic fixes
Browse files Browse the repository at this point in the history
-removed superfluous semicolon
- added static_cast where it will definitely casue no change in behaviour
- small adaptions in member and parameter types
  • Loading branch information
gittiver committed Aug 18, 2024
1 parent 4ce823a commit a573a5c
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 44 deletions.
2 changes: 1 addition & 1 deletion examples/example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ int main()
auto x = crow::json::load(req.body);
if (!x)
return crow::response(400);
int sum = x["a"].i() + x["b"].i();
int64_t sum = x["a"].i() + x["b"].i();
std::ostringstream os;
os << sum;
return crow::response{os.str()};
Expand Down
4 changes: 2 additions & 2 deletions include/crow/app.h
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ namespace crow
}

/// \brief Run the server on multiple threads using a specific number
self_t& concurrency(std::uint16_t concurrency)
self_t& concurrency(unsigned int concurrency)
{
if (concurrency < 2) // Crow can have a minimum of 2 threads running
concurrency = 2;
Expand Down Expand Up @@ -733,7 +733,7 @@ namespace crow
private:
std::uint8_t timeout_{5};
uint16_t port_ = 80;
uint16_t concurrency_ = 2;
unsigned int concurrency_ = 2;
uint64_t max_payload_{UINT64_MAX};
std::string server_name_ = std::string("Crow/") + VERSION;
std::string bindaddr_ = "0.0.0.0";
Expand Down
2 changes: 1 addition & 1 deletion include/crow/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ namespace crow
{
if (CROW_LIKELY(method < HTTPMethod::InternalMethodCount))
{
return method_strings[(unsigned char)method];
return method_strings[static_cast<unsigned int>(method)];
}
return "invalid";
}
Expand Down
10 changes: 5 additions & 5 deletions include/crow/http_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ namespace crow // NOTE: Already documented in "crow/app.h"
class Server
{
public:
Server(Handler* handler, std::string bindaddr, uint16_t port, std::string server_name = std::string("Crow/") + VERSION, std::tuple<Middlewares...>* middlewares = nullptr, uint16_t concurrency = 1, uint8_t timeout = 5, typename Adaptor::context* adaptor_ctx = nullptr):
Server(Handler* handler, std::string bindaddr, uint16_t port, std::string server_name = std::string("Crow/") + VERSION, std::tuple<Middlewares...>* middlewares = nullptr, unsigned int concurrency = 1, uint8_t timeout = 5, typename Adaptor::context* adaptor_ctx = nullptr):
acceptor_(io_service_, tcp::endpoint(asio::ip::address::from_string(bindaddr), port)),
signals_(io_service_),
tick_timer_(io_service_),
Expand Down Expand Up @@ -211,9 +211,9 @@ namespace crow // NOTE: Already documented in "crow/app.h"
}

private:
uint16_t pick_io_service_idx()
size_t pick_io_service_idx()
{
uint16_t min_queue_idx = 0;
size_t min_queue_idx = 0;

// TODO improve load balancing
// size_t is used here to avoid the security issue https://codeql.github.com/codeql-query-help/cpp/cpp-comparison-with-wider-type/
Expand All @@ -231,7 +231,7 @@ namespace crow // NOTE: Already documented in "crow/app.h"
{
if (!shutting_down_)
{
uint16_t service_idx = pick_io_service_idx();
size_t service_idx = pick_io_service_idx();
asio::io_service& is = *io_service_pool_[service_idx];
task_queue_length_pool_[service_idx]++;
CROW_LOG_DEBUG << &is << " {" << service_idx << "} queue length: " << task_queue_length_pool_[service_idx];
Expand Down Expand Up @@ -283,7 +283,7 @@ namespace crow // NOTE: Already documented in "crow/app.h"
asio::basic_waitable_timer<std::chrono::high_resolution_clock> tick_timer_;

Handler* handler_;
uint16_t concurrency_{2};
unsigned int concurrency_{2};
std::uint8_t timeout_;
std::string server_name_;
uint16_t port_;
Expand Down
34 changes: 17 additions & 17 deletions include/crow/json.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,9 @@ namespace crow // NOTE: Already documented in "crow/app.h"
/// A read string implementation with comparison functionality.
struct r_string
{
r_string(){};
r_string(){}
r_string(char* s, char* e):
s_(s), e_(e){};
s_(s), e_(e){}
~r_string()
{
if (owned_)
Expand Down Expand Up @@ -552,15 +552,15 @@ namespace crow // NOTE: Already documented in "crow/app.h"
bool operator()(const rvalue& l, const rvalue& r) const
{
return l.key_ < r.key_;
};
}
bool operator()(const rvalue& l, const std::string& r) const
{
return l.key_ < r;
};
}
bool operator()(const std::string& l, const rvalue& r) const
{
return l < r.key_;
};
}
};
if (!is_cached())
{
Expand Down Expand Up @@ -647,15 +647,15 @@ namespace crow // NOTE: Already documented in "crow/app.h"
bool operator()(const rvalue& l, const rvalue& r) const
{
return l.key_ < r.key_;
};
}
bool operator()(const rvalue& l, const std::string& r) const
{
return l.key_ < r;
};
}
bool operator()(const std::string& l, const rvalue& r) const
{
return l < r.key_;
};
}
};
if (!is_cached())
{
Expand Down Expand Up @@ -849,24 +849,24 @@ namespace crow // NOTE: Already documented in "crow/app.h"
return l != r.s();
}

inline bool operator==(const rvalue& l, double r)
inline bool operator==(const rvalue& l, const int& r)
{
return l.d() == r;
return l.i() == r;
}

inline bool operator==(double l, const rvalue& r)
inline bool operator==(const int& l, const rvalue& r)
{
return l == r.d();
return l == r.i();
}

inline bool operator!=(const rvalue& l, double r)
inline bool operator!=(const rvalue& l, const int& r)
{
return l.d() != r;
return l.i() != r;
}

inline bool operator!=(double l, const rvalue& r)
inline bool operator!=(const int& l, const rvalue& r)
{
return l != r.d();
return l != r.i();
}


Expand Down Expand Up @@ -895,7 +895,7 @@ namespace crow // NOTE: Already documented in "crow/app.h"
{
while (*data == ' ' || *data == '\t' || *data == '\r' || *data == '\n')
++data;
};
}

rvalue decode_string()
{
Expand Down
2 changes: 1 addition & 1 deletion include/crow/returnable.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ namespace crow
content_type{ctype}
{}

virtual ~returnable(){};
virtual ~returnable(){}
};
} // namespace crow
10 changes: 5 additions & 5 deletions include/crow/routing.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#pragma once
#pragma once

#include <cstdint>
#include <utility>
Expand Down Expand Up @@ -785,7 +785,7 @@ namespace crow // NOTE: Already documented in "crow/app.h"
}
}

void debug_node_print(const Node& node, int level)
void debug_node_print(const Node& node, size_t level)
{
if (node.param != ParamType::MAX)
{
Expand Down Expand Up @@ -1107,13 +1107,13 @@ namespace crow // NOTE: Already documented in "crow/app.h"
: prefix_(prefix),
static_dir_(prefix),
templates_dir_(prefix)
{};
{}

Blueprint(const std::string& prefix, const std::string& static_dir):
prefix_(prefix), static_dir_(static_dir){};
prefix_(prefix), static_dir_(static_dir){}

Blueprint(const std::string& prefix, const std::string& static_dir, const std::string& templates_dir):
prefix_(prefix), static_dir_(static_dir), templates_dir_(templates_dir){};
prefix_(prefix), static_dir_(static_dir), templates_dir_(templates_dir){}

/*
Blueprint(Blueprint& other)
Expand Down
23 changes: 11 additions & 12 deletions tests/unittest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ TEST_CASE("RoutingTest")

CHECK(5000 == A);
CHECK(3 == B);
CHECK(-2.71828 == C);
REQUIRE_THAT(-2.71828, Catch::Matchers::WithinAbs(C, 1e-9));
CHECK("hellhere" == D);
}
{
Expand All @@ -284,7 +284,7 @@ TEST_CASE("RoutingTest")

CHECK(-5 == A);
CHECK(999 == B);
CHECK(3.141592 == C);
REQUIRE_THAT(3.141592, Catch::Matchers::WithinAbs(C, 1e-9));
CHECK("hello_there" == D);
CHECK("a/b/c/d" == E);
}
Expand Down Expand Up @@ -312,7 +312,7 @@ TEST_CASE("simple_response_routing_params")
CHECK(1 == rp.get<int64_t>(0));
CHECK(5 == rp.get<int64_t>(1));
CHECK(2 == rp.get<uint64_t>(0));
CHECK(3 == rp.get<double>(0));
REQUIRE_THAT(3, Catch::Matchers::WithinAbs(rp.get<double>(0), 1e-9));
CHECK("hello" == rp.get<string>(0));
} // simple_response_routing_params

Expand Down Expand Up @@ -784,18 +784,18 @@ TEST_CASE("json_read")
R"({"int":3, "ints" :[1,2,3,4,5], "bigint":1234567890 })";
auto y = json::load(s);
CHECK(3 == y["int"]);
CHECK(3.0 == y["int"]);
CHECK(3.01 != y["int"]);
// CHECK(3.0 == y["int"]);
// CHECK(3.01 != y["int"]);
CHECK(5 == y["ints"].size());
CHECK(1 == y["ints"][0]);
CHECK(2 == y["ints"][1]);
CHECK(3 == y["ints"][2]);
CHECK(4 == y["ints"][3]);
CHECK(5 == y["ints"][4]);
CHECK(1u == y["ints"][0]);
CHECK(1.f == y["ints"][0]);
REQUIRE_THAT(1.f, Catch::Matchers::WithinAbs(y["ints"][0].d(), 1e-9));

int q = (int)y["ints"][1];
int q = static_cast<int>(y["ints"][1]);
CHECK(2 == q);
q = y["ints"][2].i();
CHECK(3 == q);
Expand All @@ -807,8 +807,8 @@ TEST_CASE("json_read")
CHECK(2 == z["doubles"].size());
CHECK(true == z["bools"][0].b());
CHECK(false == z["bools"][1].b());
CHECK(1.2 == z["doubles"][0].d());
CHECK(-3.4 == z["doubles"][1].d());
REQUIRE_THAT(1.2, Catch::Matchers::WithinAbs(z["doubles"][0].d(), 1e-9));
REQUIRE_THAT(-3.4 , Catch::Matchers::WithinAbs(z["doubles"][1].d(), 1e-9));

std::string s3 = R"({"uint64": 18446744073709551615})";
auto z1 = json::load(s3);
Expand Down Expand Up @@ -865,7 +865,7 @@ TEST_CASE("json_read_real")
for (auto x : v)
{
CROW_LOG_DEBUG << x;
CHECK(json::load(x).d() == utility::lexical_cast<double>(x));
REQUIRE_THAT(json::load(x).d(), Catch::Matchers::WithinAbs(utility::lexical_cast<double>(x), 1e-9));
}

auto ret = json::load(
Expand Down Expand Up @@ -2289,8 +2289,7 @@ TEST_CASE("simple_url_params")
c.close();

CHECK(utility::lexical_cast<int>(last_url_params.get("int")) == 100);
CHECK(utility::lexical_cast<double>(last_url_params.get("double")) ==
123.45);
REQUIRE_THAT(123.45, Catch::Matchers::WithinAbs(utility::lexical_cast<double>(last_url_params.get("double")), 1e-9));
CHECK(utility::lexical_cast<bool>(last_url_params.get("boolean")));
}
// check single array value
Expand Down

0 comments on commit a573a5c

Please sign in to comment.