Skip to content

Commit 7f2e86f

Browse files
committed
TASK-023: smart-pointer register_resource overloads (GREEN)
Replace the raw-pointer register_resource overload with two smart-pointer overloads expressing ownership at the call site: void register_resource(const std::string& path, std::unique_ptr<T> res, bool family = false); // T derived from http_resource void register_resource(const std::string& path, std::shared_ptr<http_resource> res, bool family = false); Why two: - unique_ptr: the webserver takes sole ownership; resource dtor runs on webserver destruction or unregister_resource. This matches the common case where the caller has no further use for the resource. - shared_ptr: caller and webserver share ownership; useful when the caller wants to mutate or query the resource after registration. - The unique_ptr overload is templated over the derived type so that `register_resource(path, std::make_unique<my_resource>())` resolves unambiguously without competing with the shared_ptr overload via an implicit unique-to-shared conversion. Internal storage in webserver_impl now holds shared_ptr<http_resource> in all three route-table maps and the route_cache_entry. The dispatch path (finalize_answer) holds a shared_ptr copy across the rendering call, closing a latent race where a concurrent unregister_resource could invalidate a raw pointer mid-call. Return type changes from bool to void. Duplicate registration now throws std::invalid_argument (replaces v1's silent `return false`). This is the safer interpretation: a moved-in unique_ptr would be destroyed inside the conversion to shared_ptr before the duplicate check runs, so a soft-fail return value would leave the caller with no way to recover the resource. Throwing surfaces the failure. Migration: - All 38 examples now use std::make_shared<resource_type>() at the call site. Where the local resource was used to call mutators (disallow_all, set_allowing) before registration, those calls now use ->. - Integration tests use a small as_shared() test-only helper that wraps a stack-local http_resource& in a shared_ptr with a no-op deleter, preserving the established "declare resource on stack, pass to register_resource" pattern across ~127 call sites. - The duplicate-detection tests in basic.cpp and ws_start_stop.cpp are rewritten to use LT_CHECK_THROW, asserting the new throw semantics. Acceptance criteria: - `auto r = std::make_unique<my_resource>(); ws.register_resource( "/foo", std::move(r));` compiles and serves: covered by webserver_register_smartptr_test.cpp::unique_ptr_overload_compiles_and_serves. - The raw-pointer overload no longer exists in the public header: enforced by has_raw_register_resource SFINAE static_assert. - A test verifies the resource destructor runs when the webserver is destroyed: webserver_register_smartptr_test.cpp:: unique_ptr_dtor_runs_on_webserver_destruction. - All 33 tests pass (release and debug builds).
1 parent 003f37e commit 7f2e86f

47 files changed

Lines changed: 549 additions & 352 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

examples/allowing_disallowing_methods.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@ class hello_world_resource : public httpserver::http_resource {
3232
int main() {
3333
httpserver::webserver ws = httpserver::create_webserver(8080);
3434

35-
hello_world_resource hwr;
36-
hwr.disallow_all();
37-
hwr.set_allowing(httpserver::http_method::get, true);
38-
ws.register_resource("/hello", &hwr);
35+
auto hwr = std::make_shared<hello_world_resource>();
36+
hwr->disallow_all();
37+
hwr->set_allowing(httpserver::http_method::get, true);
38+
ws.register_resource("/hello", hwr);
3939
ws.start(true);
4040

4141
return 0;

examples/args_processing.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,8 @@ class args_resource : public httpserver::http_resource {
8989
int main() {
9090
httpserver::webserver ws = httpserver::create_webserver(8080);
9191

92-
args_resource ar;
93-
ws.register_resource("/args", &ar);
92+
auto ar = std::make_shared<args_resource>();
93+
ws.register_resource("/args", ar);
9494

9595
std::cout << "Server running on http://localhost:8080/args\n";
9696
std::cout << "Try: http://localhost:8080/args?name=john&age=30\n";

examples/basic_authentication.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ class user_pass_resource : public httpserver::http_resource {
3838
int main() {
3939
httpserver::webserver ws = httpserver::create_webserver(8080);
4040

41-
user_pass_resource hwr;
42-
ws.register_resource("/hello", &hwr);
41+
auto hwr = std::make_shared<user_pass_resource>();
42+
ws.register_resource("/hello", hwr);
4343
ws.start(true);
4444

4545
return 0;

examples/benchmark_nodelay.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ int main(int argc, char** argv) {
5151
std::shared_ptr<httpserver::http_response> hello = std::shared_ptr<httpserver::http_response>(new httpserver::http_response(httpserver::http_response::string(BODY)));
5252
hello->with_header("Server", "libhttpserver");
5353

54-
hello_world_resource hwr(hello);
55-
ws.register_resource(PATH, &hwr, false);
54+
auto hwr = std::make_shared<hello_world_resource>(hello);
55+
ws.register_resource(PATH, hwr, false);
5656

5757
ws.start(true);
5858

examples/benchmark_select.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ int main(int argc, char** argv) {
5050
std::shared_ptr<httpserver::http_response> hello = std::shared_ptr<httpserver::http_response>(new httpserver::http_response(httpserver::http_response::string(BODY)));
5151
hello->with_header("Server", "libhttpserver");
5252

53-
hello_world_resource hwr(hello);
54-
ws.register_resource(PATH, &hwr, false);
53+
auto hwr = std::make_shared<hello_world_resource>(hello);
54+
ws.register_resource(PATH, hwr, false);
5555

5656
ws.start(true);
5757

examples/benchmark_threads.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ int main(int argc, char** argv) {
4949
std::shared_ptr<httpserver::http_response> hello = std::shared_ptr<httpserver::http_response>(new httpserver::http_response(httpserver::http_response::string(BODY)));
5050
hello->with_header("Server", "libhttpserver");
5151

52-
hello_world_resource hwr(hello);
53-
ws.register_resource(PATH, &hwr, false);
52+
auto hwr = std::make_shared<hello_world_resource>(hello);
53+
ws.register_resource(PATH, hwr, false);
5454

5555
ws.start(true);
5656

examples/binary_buffer_response.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ class image_resource : public httpserver::http_resource {
7373
int main() {
7474
httpserver::webserver ws = httpserver::create_webserver(8080);
7575

76-
image_resource ir;
77-
ws.register_resource("/image", &ir);
76+
auto ir = std::make_shared<image_resource>();
77+
ws.register_resource("/image", ir);
7878
ws.start(true);
7979

8080
return 0;

examples/centralized_authentication.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,11 @@ int main() {
6060
.auth_handler(auth_handler)
6161
.auth_skip_paths({"/health", "/public/*"});
6262

63-
hello_resource hello;
64-
health_resource health;
63+
auto hello = std::make_shared<hello_resource>();
64+
auto health = std::make_shared<health_resource>();
6565

66-
ws.register_resource("/api", &hello);
67-
ws.register_resource("/health", &health);
66+
ws.register_resource("/api", hello);
67+
ws.register_resource("/health", health);
6868

6969
ws.start(true);
7070

examples/client_cert_auth.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -154,11 +154,11 @@ int main() {
154154
.https_mem_cert("server_cert.pem") // Server certificate
155155
.https_mem_trust("ca_cert.pem"); // CA certificate for verifying client certs
156156

157-
secure_resource secure;
158-
info_resource info;
157+
auto secure = std::make_shared<secure_resource>();
158+
auto info = std::make_shared<info_resource>();
159159

160-
ws.register_resource("/secure", &secure);
161-
ws.register_resource("/info", &info);
160+
ws.register_resource("/secure", secure);
161+
ws.register_resource("/info", info);
162162

163163
std::cout << "Server started. Press Ctrl+C to stop.\n\n";
164164
std::cout << "Test commands:\n";

examples/custom_access_log.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ int main() {
3939
httpserver::webserver ws = httpserver::create_webserver(8080)
4040
.log_access(custom_access_log);
4141

42-
hello_world_resource hwr;
43-
ws.register_resource("/hello", &hwr);
42+
auto hwr = std::make_shared<hello_world_resource>();
43+
ws.register_resource("/hello", hwr);
4444
ws.start(true);
4545

4646
return 0;

0 commit comments

Comments
 (0)