Commit 7f2e86f
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
File tree
- examples
- src
- httpserver
- detail
- test
- integ
- unit
Some content is hidden
Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
32 | 32 | | |
33 | 33 | | |
34 | 34 | | |
35 | | - | |
36 | | - | |
37 | | - | |
38 | | - | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
39 | 39 | | |
40 | 40 | | |
41 | 41 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
89 | 89 | | |
90 | 90 | | |
91 | 91 | | |
92 | | - | |
93 | | - | |
| 92 | + | |
| 93 | + | |
94 | 94 | | |
95 | 95 | | |
96 | 96 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
38 | 38 | | |
39 | 39 | | |
40 | 40 | | |
41 | | - | |
42 | | - | |
| 41 | + | |
| 42 | + | |
43 | 43 | | |
44 | 44 | | |
45 | 45 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
51 | 51 | | |
52 | 52 | | |
53 | 53 | | |
54 | | - | |
55 | | - | |
| 54 | + | |
| 55 | + | |
56 | 56 | | |
57 | 57 | | |
58 | 58 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
50 | 50 | | |
51 | 51 | | |
52 | 52 | | |
53 | | - | |
54 | | - | |
| 53 | + | |
| 54 | + | |
55 | 55 | | |
56 | 56 | | |
57 | 57 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
49 | 49 | | |
50 | 50 | | |
51 | 51 | | |
52 | | - | |
53 | | - | |
| 52 | + | |
| 53 | + | |
54 | 54 | | |
55 | 55 | | |
56 | 56 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
73 | 73 | | |
74 | 74 | | |
75 | 75 | | |
76 | | - | |
77 | | - | |
| 76 | + | |
| 77 | + | |
78 | 78 | | |
79 | 79 | | |
80 | 80 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
60 | 60 | | |
61 | 61 | | |
62 | 62 | | |
63 | | - | |
64 | | - | |
| 63 | + | |
| 64 | + | |
65 | 65 | | |
66 | | - | |
67 | | - | |
| 66 | + | |
| 67 | + | |
68 | 68 | | |
69 | 69 | | |
70 | 70 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
154 | 154 | | |
155 | 155 | | |
156 | 156 | | |
157 | | - | |
158 | | - | |
| 157 | + | |
| 158 | + | |
159 | 159 | | |
160 | | - | |
161 | | - | |
| 160 | + | |
| 161 | + | |
162 | 162 | | |
163 | 163 | | |
164 | 164 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
39 | 39 | | |
40 | 40 | | |
41 | 41 | | |
42 | | - | |
43 | | - | |
| 42 | + | |
| 43 | + | |
44 | 44 | | |
45 | 45 | | |
46 | 46 | | |
| |||
0 commit comments