Skip to content

Commit ae35af4

Browse files
authored
Merge pull request #11 from freesurfer-rge/freesurfer.rge/compiler-warnings
Turn on compiler warnings
2 parents 374e165 + f2f2f5d commit ae35af4

15 files changed

+103
-35
lines changed

CMakeLists.txt

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,23 @@ install(DIRECTORY include/ DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
1515

1616
add_library(coverage_config INTERFACE)
1717

18+
# Warning options for the compiler
19+
string(
20+
APPEND _warning_opts
21+
"$<$<OR:$<CXX_COMPILER_ID:Clang>,$<CXX_COMPILER_ID:AppleClang>,$<CXX_COMPILER_ID:GNU>>:-Wall;-Wextra;-Weffc++;-Werror;>"
22+
"$<$<OR:$<CXX_COMPILER_ID:Clang>,$<CXX_COMPILER_ID:AppleClang>>:-Wthread-safety;-Wpedantic;>"
23+
"$<$<CXX_COMPILER_ID:GNU>:-pedantic;-pedantic-errors;>"
24+
)
25+
26+
1827
if (COMPILE_TESTS)
1928
if (CODE_COVERAGE)
2029
message("Enabled coverage flags")
2130
target_compile_options(coverage_config INTERFACE -O0 -g --coverage)
2231
target_link_libraries(coverage_config INTERFACE --coverage)
2332
endif ()
2433
add_executable(jsonrpccpp-test test/main.cpp test/client.cpp test/typemapper.cpp test/dispatcher.cpp test/server.cpp test/batchclient.cpp test/testclientconnector.hpp examples/warehouse/warehouseapp.cpp test/warehouseapp.cpp)
34+
target_compile_options(jsonrpccpp-test PUBLIC "${_warning_opts}")
2535
target_include_directories(jsonrpccpp-test PRIVATE vendor examples)
2636
target_link_libraries(jsonrpccpp-test coverage_config json-rpc-cxx)
2737
enable_testing()
@@ -31,8 +41,10 @@ endif ()
3141
if (COMPILE_EXAMPLES)
3242
find_package(Threads)
3343
add_executable(example-warehouse examples/warehouse/main.cpp examples/warehouse/warehouseapp.cpp examples/warehouse/types.h examples/inmemoryconnector.hpp)
44+
target_compile_options(example-warehouse PUBLIC "${_warning_opts}")
3445
target_link_libraries(example-warehouse json-rpc-cxx Threads::Threads)
35-
target_include_directories(example-warehouse PRIVATE vendor examples)
46+
target_include_directories(example-warehouse SYSTEM PRIVATE vendor)
47+
target_include_directories(example-warehouse PRIVATE examples)
3648
add_test(NAME example COMMAND example-warehouse)
3749
endif ()
3850

examples/cpphttplibconnector.hpp

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,17 @@ class CppHttpLibClientConnector : public jsonrpccxx::IClientConnector {
2222

2323
class CppHttpLibServerConnector {
2424
public:
25-
explicit CppHttpLibServerConnector(jsonrpccxx::JsonRpcServer &server, int port) : server(server), port(port) {
26-
httpServer.Post("/jsonrpc", [&](const httplib::Request &req, httplib::Response &res) {
27-
res.status = 200;
28-
res.set_content(server.HandleRequest(req.body), "application/json");
29-
});
25+
explicit CppHttpLibServerConnector(jsonrpccxx::JsonRpcServer &server, int port) :
26+
thread(),
27+
server(server),
28+
httpServer(),
29+
port(port) {
30+
httpServer.Post("/jsonrpc",
31+
[this](const httplib::Request &req, httplib::Response &res) {
32+
this->PostAction(req, res);
33+
});
3034
}
35+
3136
virtual ~CppHttpLibServerConnector() { StopListening(); }
3237

3338
bool StartListening() {
@@ -49,4 +54,10 @@ class CppHttpLibServerConnector {
4954
jsonrpccxx::JsonRpcServer &server;
5055
httplib::Server httpServer;
5156
int port;
52-
};
57+
58+
void PostAction(const httplib::Request &req,
59+
httplib::Response &res) {
60+
res.status = 200;
61+
res.set_content(this->server.HandleRequest(req.body), "application/json");
62+
}
63+
};

examples/warehouse/main.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,11 @@ class WareHouseClient {
2222
void doWarehouseStuff(IClientConnector &clientConnector) {
2323
JsonRpcClient client(clientConnector, version::v2);
2424
WareHouseClient appClient(client);
25-
Product p = {"0xff", 22.4, "Product 1", category::cash_carry};
25+
Product p;
26+
p.id = "0xff";
27+
p.price = 22.4;
28+
p.name = "Product 1";
29+
p.cat = category::cash_carry;
2630
cout << "Adding product: " << std::boolalpha << appClient.AddProduct(p) << "\n";
2731

2832
Product p2 = appClient.GetProduct("0xff");
@@ -54,4 +58,4 @@ int main() {
5458
doWarehouseStuff(httpClient);
5559

5660
return 0;
57-
}
61+
}

examples/warehouse/types.h

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,28 @@
22
#include <nlohmann/json.hpp>
33

44
enum class category { order, cash_carry };
5+
56
struct Product {
7+
public:
8+
Product() : id(), price(), name(), cat() {}
69
std::string id;
710
double price;
811
std::string name;
912
category cat;
1013
};
1114

12-
NLOHMANN_JSON_SERIALIZE_ENUM(category, {{category::order, "order"}, {category::cash_carry, "cc"}});
13-
inline void to_json(nlohmann::json &j, const Product &p) { j = nlohmann::json{{"id", p.id}, {"price", p.price}, {"name", p.name}, {"category", p.cat}}; }
15+
NLOHMANN_JSON_SERIALIZE_ENUM(category, {{category::order, "order"}, {category::cash_carry, "cc"}})
16+
17+
inline void to_json(nlohmann::json &j, const Product &p) {
18+
j = nlohmann::json{{"id", p.id},
19+
{"price", p.price},
20+
{"name", p.name},
21+
{"category", p.cat}};
22+
}
23+
1424
inline void from_json(const nlohmann::json &j, Product &p) {
1525
j.at("name").get_to(p.name);
1626
j.at("id").get_to(p.id);
1727
j.at("price").get_to(p.price);
1828
j.at("category").get_to(p.cat);
19-
}
29+
}

examples/warehouse/warehouseapp.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55

66
class WarehouseServer {
77
public:
8+
WarehouseServer() :
9+
products() {}
10+
811
bool AddProduct(const Product &p);
912
const Product& GetProduct(const std::string& id);
1013

include/jsonrpccxx/batchclient.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ namespace jsonrpccxx {
4646

4747
class BatchResponse {
4848
public:
49-
explicit BatchResponse(json &&response) : response(response) {
49+
explicit BatchResponse(json &&response) : response(response), results(), errors(), nullIds() {
5050
for (auto &[key, value] : response.items()) {
5151
if (value.is_object() && valid_id_not_null(value) && has_key(value, "result")) {
5252
results[value["id"]] = std::stoi(key);
@@ -98,4 +98,4 @@ namespace jsonrpccxx {
9898
}
9999
}
100100
};
101-
}
101+
}

include/jsonrpccxx/dispatcher.hpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ namespace jsonrpccxx {
1212

1313
class Dispatcher {
1414
public:
15+
Dispatcher() :
16+
methods(),
17+
notifications(),
18+
mapping() {}
19+
1520
bool Add(const std::string &name, MethodHandle callback, const NamedParamMapping &mapping = NAMED_PARAM_MAPPING) {
1621
if (contains(name))
1722
return false;
@@ -98,4 +103,4 @@ namespace jsonrpccxx {
98103
throw JsonRpcException(-32600, "invalid request: params field must be an array, object");
99104
}
100105
};
101-
} // namespace jsonrpccxx
106+
} // namespace jsonrpccxx

include/jsonrpccxx/server.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
namespace jsonrpccxx {
88
class JsonRpcServer {
99
public:
10+
JsonRpcServer() : dispatcher() {}
1011
virtual ~JsonRpcServer() = default;
1112
virtual std::string HandleRequest(const std::string &request) = 0;
1213

@@ -106,4 +107,4 @@ namespace jsonrpccxx {
106107
}
107108
}
108109
};
109-
}
110+
}

test/client.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ struct F {
1313
TestClientConnector c;
1414
JsonRpcClient clientV1;
1515
JsonRpcClient clientV2;
16-
F() : clientV1(c, version::v1), clientV2(c, version::v2) {}
16+
F() : c(), clientV1(c, version::v1), clientV2(c, version::v2) {}
1717
};
1818

1919
TEST_CASE_METHOD(F, "v2_method_noparams", TEST_MODULE) {

test/integrationtest.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
using namespace jsonrpccxx;
99

1010
struct IntegrationTest {
11-
IntegrationTest() : connector(rpcServer), client(connector, version::v2) {}
11+
IntegrationTest() : rpcServer(), connector(rpcServer), client(connector, version::v2) {}
1212
JsonRpc2Server rpcServer;
1313
InMemoryConnector connector;
1414
JsonRpcClient client;
15-
};
15+
};

0 commit comments

Comments
 (0)