Skip to content

Commit f253440

Browse files
committed
flat routers are shared
1 parent 685db0b commit f253440

3 files changed

Lines changed: 67 additions & 12 deletions

File tree

include/boost/http/server/flat_router.hpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <boost/capy/task.hpp>
1919
#include <boost/assert.hpp>
2020
#include <exception>
21+
#include <memory>
2122
#include <string_view>
2223
#include <type_traits>
2324

@@ -39,11 +40,9 @@ class BOOST_HTTP_DECL
3940
flat_router
4041
{
4142
struct impl;
42-
impl* impl_;
43+
std::shared_ptr<impl> impl_;
4344

4445
public:
45-
~flat_router();
46-
4746
flat_router(
4847
detail::router_base&&);
4948

src/server/flat_router.cpp

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -338,16 +338,10 @@ struct flat_router::impl
338338

339339
//------------------------------------------------
340340

341-
flat_router::
342-
~flat_router()
343-
{
344-
delete impl_;
345-
}
346-
347341
flat_router::
348342
flat_router(
349343
detail::router_base&& src)
350-
: impl_(new impl)
344+
: impl_(std::make_shared<impl>())
351345
{
352346
impl_->flatten(*src.impl_);
353347
}

test/unit/server/flat_router.cpp

Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,79 @@
1010
// Test that header file is self-contained.
1111
#include <boost/http/server/flat_router.hpp>
1212

13+
// Full functional tests are in beast2/test/unit/server/router.cpp
14+
15+
#include <boost/http/server/router.hpp>
16+
#include <boost/capy/ex/run_sync.hpp>
17+
1318
#include "test_suite.hpp"
1419

1520
namespace boost {
1621
namespace http {
1722

1823
struct flat_router_test
1924
{
25+
using params = route_params_base;
26+
using test_router = router<params>;
27+
28+
void testCopyConstruction()
29+
{
30+
auto counter = std::make_shared<int>(0);
31+
test_router r;
32+
r.all("/", [counter](params&) -> capy::task<route_result>
33+
{
34+
++(*counter);
35+
co_return route_result{};
36+
});
37+
38+
flat_router fr1(std::move(r));
39+
flat_router fr2(fr1);
40+
41+
params req;
42+
capy::run_sync()(fr1.dispatch(
43+
http::method::get, urls::url_view("/"), req));
44+
BOOST_TEST_EQ(*counter, 1);
45+
46+
capy::run_sync()(fr2.dispatch(
47+
http::method::get, urls::url_view("/"), req));
48+
BOOST_TEST_EQ(*counter, 2);
49+
}
50+
51+
void testCopyAssignment()
52+
{
53+
auto counter = std::make_shared<int>(0);
54+
test_router r;
55+
r.all("/", [counter](params&) -> capy::task<route_result>
56+
{
57+
++(*counter);
58+
co_return route_result{};
59+
});
60+
61+
flat_router fr1(std::move(r));
62+
63+
test_router r2;
64+
r2.all("/", [](params&) -> capy::task<route_result>
65+
{
66+
co_return route_result{};
67+
});
68+
flat_router fr2(std::move(r2));
69+
70+
fr2 = fr1;
71+
72+
params req;
73+
capy::run_sync()(fr1.dispatch(
74+
http::method::get, urls::url_view("/"), req));
75+
BOOST_TEST_EQ(*counter, 1);
76+
77+
capy::run_sync()(fr2.dispatch(
78+
http::method::get, urls::url_view("/"), req));
79+
BOOST_TEST_EQ(*counter, 2);
80+
}
81+
2082
void run()
2183
{
22-
// Header compilation test only.
23-
// Functional tests are in router.cpp.
84+
testCopyConstruction();
85+
testCopyAssignment();
2486
}
2587
};
2688

0 commit comments

Comments
 (0)