Skip to content

Commit 9db72df

Browse files
committed
[single_headers] Add the generated files
1 parent e722e84 commit 9db72df

File tree

4 files changed

+122
-52
lines changed

4 files changed

+122
-52
lines changed

single_headers/lithium.hh

Lines changed: 60 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
#include <tuple>
7474
#include <unistd.h>
7575
#include <unordered_map>
76+
#include <unordered_set>
7677
#include <utility>
7778
#include <variant>
7879
#include <vector>
@@ -6510,9 +6511,21 @@ namespace li {
65106511

65116512
namespace internal {
65126513

6513-
template <typename V> struct drt_node {
6514+
// A simple memory pool for drt_node objects
6515+
template <typename T> struct drt_node_pool {
6516+
template <typename... Args> T* allocate(Args&&... args) {
6517+
auto new_node = std::make_unique<T>(std::forward<Args>(args)...);
6518+
T* ptr = new_node.get();
6519+
pool_.emplace_back(std::move(new_node));
6520+
return ptr;
6521+
}
65146522

6515-
drt_node() : v_{0, nullptr} {}
6523+
std::vector<std::unique_ptr<T>> pool_;
6524+
};
6525+
6526+
template <typename V> struct drt_node {
6527+
drt_node() : pool_(nullptr), v_{0, nullptr} {}
6528+
drt_node(drt_node_pool<drt_node>& pool) : pool_(pool), v_{0, nullptr} {}
65166529

65176530
struct iterator {
65186531
const drt_node<V>* ptr;
@@ -6537,17 +6550,10 @@ template <typename V> struct drt_node {
65376550
c++;
65386551
std::string_view k = r.substr(s, c - s);
65396552

6540-
auto it = children_.find(k);
6541-
if (it != children_.end())
6542-
return children_[k]->find_or_create(r, c);
6543-
else {
6544-
auto new_node = std::make_shared<drt_node>();
6545-
children_shared_pointers_.push_back(new_node);
6546-
children_.insert({k, new_node.get()});
6547-
return new_node->find_or_create(r, c);
6553+
if (children_.find(k) == children_.end()) {
6554+
children_[k] = pool_.allocate(pool_);
65486555
}
6549-
6550-
return v_;
6556+
return children_[k]->find_or_create(r, c);
65516557
}
65526558

65536559
template <typename F> void for_all_routes(F f, std::string prefix = "") const {
@@ -6556,8 +6562,8 @@ template <typename V> struct drt_node {
65566562
else {
65576563
if (prefix.size() && prefix.back() != '/')
65586564
prefix += '/';
6559-
for (auto pair : children_)
6560-
pair.second->for_all_routes(f, prefix + std::string(pair.first));
6565+
for (const auto& kv : children_)
6566+
kv.second->for_all_routes(f, prefix + std::string(kv.first));
65616567
}
65626568
}
65636569

@@ -6594,7 +6600,7 @@ template <typename V> struct drt_node {
65946600

65956601
{
65966602
// if one child is a url param {{param_name}}, choose it
6597-
for (auto& kv : children_) {
6603+
for (const auto& kv : children_) {
65986604
auto name = kv.first;
65996605
if (name.size() > 4 and name[0] == '{' and name[1] == '{' and
66006606
name[name.size() - 2] == '}' and name[name.size() - 1] == '}')
@@ -6606,22 +6612,22 @@ template <typename V> struct drt_node {
66066612

66076613
V v_;
66086614
std::unordered_map<std::string_view, drt_node*> children_;
6609-
std::vector<std::shared_ptr<drt_node>> children_shared_pointers_;
6615+
drt_node_pool<drt_node>& pool_;
66106616
};
6611-
} // namespace internal
66126617

6613-
template <typename V> struct dynamic_routing_table {
6618+
template <typename V> struct dynamic_routing_table_impl {
6619+
dynamic_routing_table_impl() : root(pool) {}
66146620

66156621
// Find a route and return reference to a procedure.
66166622
auto& operator[](const std::string_view& r) {
6617-
strings.push_back(std::make_shared<std::string>(r));
6618-
std::string_view r2(*strings.back());
6623+
auto [itr, is_inserted] = strings.emplace(std::string(r));
6624+
std::string_view r2(*itr);
66196625
return root.find_or_create(r2, 0);
66206626
}
6621-
auto& operator[](const std::string& r) {
6622-
strings.push_back(std::make_shared<std::string>(r));
6623-
std::string_view r2(*strings.back());
6624-
return root.find_or_create(r2, 0);
6627+
auto& operator[](const std::string& s) {
6628+
auto [itr, is_inserted] = strings.emplace(s);
6629+
std::string_view r(*itr);
6630+
return root.find_or_create(r, 0);
66256631
}
66266632

66276633
// Find a route and return an iterator.
@@ -6630,8 +6636,36 @@ template <typename V> struct dynamic_routing_table {
66306636
template <typename F> void for_all_routes(F f) const { root.for_all_routes(f); }
66316637
auto end() const { return root.end(); }
66326638

6633-
std::vector<std::shared_ptr<std::string>> strings;
6634-
internal::drt_node<V> root;
6639+
std::unordered_set<std::string> strings;
6640+
drt_node_pool<drt_node<V>> pool;
6641+
drt_node<V> root;
6642+
};
6643+
} // namespace internal
6644+
6645+
template <typename V> struct dynamic_routing_table {
6646+
dynamic_routing_table() : impl_(std::make_shared<internal::dynamic_routing_table_impl<V>>()) {}
6647+
dynamic_routing_table(const dynamic_routing_table& other) : impl_(other.impl_) {}
6648+
6649+
// Assignment operator
6650+
dynamic_routing_table& operator=(const dynamic_routing_table& other) {
6651+
if (this != &other) {
6652+
impl_ = other.impl_;
6653+
}
6654+
return *this;
6655+
}
6656+
6657+
// Find a route and return reference to a procedure.
6658+
auto& operator[](const std::string_view& r) { return impl_->operator[](r); }
6659+
auto& operator[](const std::string& s) { return impl_->operator[](s); }
6660+
6661+
// Find a route and return an iterator.
6662+
auto find(const std::string_view& r) const { return impl_->find(r); }
6663+
6664+
template <typename F> void for_all_routes(F f) const { impl_->for_all_routes(f); }
6665+
auto end() const { return impl_->end(); }
6666+
6667+
private:
6668+
std::shared_ptr<internal::dynamic_routing_table_impl<V>> impl_;
66356669
};
66366670

66376671
} // namespace li

single_headers/lithium_http_server.hh

Lines changed: 60 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
#include <tuple>
6363
#include <unistd.h>
6464
#include <unordered_map>
65+
#include <unordered_set>
6566
#include <utility>
6667
#include <variant>
6768
#include <vector>
@@ -3330,9 +3331,21 @@ namespace li {
33303331

33313332
namespace internal {
33323333

3333-
template <typename V> struct drt_node {
3334+
// A simple memory pool for drt_node objects
3335+
template <typename T> struct drt_node_pool {
3336+
template <typename... Args> T* allocate(Args&&... args) {
3337+
auto new_node = std::make_unique<T>(std::forward<Args>(args)...);
3338+
T* ptr = new_node.get();
3339+
pool_.emplace_back(std::move(new_node));
3340+
return ptr;
3341+
}
33343342

3335-
drt_node() : v_{0, nullptr} {}
3343+
std::vector<std::unique_ptr<T>> pool_;
3344+
};
3345+
3346+
template <typename V> struct drt_node {
3347+
drt_node() : pool_(nullptr), v_{0, nullptr} {}
3348+
drt_node(drt_node_pool<drt_node>& pool) : pool_(pool), v_{0, nullptr} {}
33363349

33373350
struct iterator {
33383351
const drt_node<V>* ptr;
@@ -3357,17 +3370,10 @@ template <typename V> struct drt_node {
33573370
c++;
33583371
std::string_view k = r.substr(s, c - s);
33593372

3360-
auto it = children_.find(k);
3361-
if (it != children_.end())
3362-
return children_[k]->find_or_create(r, c);
3363-
else {
3364-
auto new_node = std::make_shared<drt_node>();
3365-
children_shared_pointers_.push_back(new_node);
3366-
children_.insert({k, new_node.get()});
3367-
return new_node->find_or_create(r, c);
3373+
if (children_.find(k) == children_.end()) {
3374+
children_[k] = pool_.allocate(pool_);
33683375
}
3369-
3370-
return v_;
3376+
return children_[k]->find_or_create(r, c);
33713377
}
33723378

33733379
template <typename F> void for_all_routes(F f, std::string prefix = "") const {
@@ -3376,8 +3382,8 @@ template <typename V> struct drt_node {
33763382
else {
33773383
if (prefix.size() && prefix.back() != '/')
33783384
prefix += '/';
3379-
for (auto pair : children_)
3380-
pair.second->for_all_routes(f, prefix + std::string(pair.first));
3385+
for (const auto& kv : children_)
3386+
kv.second->for_all_routes(f, prefix + std::string(kv.first));
33813387
}
33823388
}
33833389

@@ -3414,7 +3420,7 @@ template <typename V> struct drt_node {
34143420

34153421
{
34163422
// if one child is a url param {{param_name}}, choose it
3417-
for (auto& kv : children_) {
3423+
for (const auto& kv : children_) {
34183424
auto name = kv.first;
34193425
if (name.size() > 4 and name[0] == '{' and name[1] == '{' and
34203426
name[name.size() - 2] == '}' and name[name.size() - 1] == '}')
@@ -3426,22 +3432,22 @@ template <typename V> struct drt_node {
34263432

34273433
V v_;
34283434
std::unordered_map<std::string_view, drt_node*> children_;
3429-
std::vector<std::shared_ptr<drt_node>> children_shared_pointers_;
3435+
drt_node_pool<drt_node>& pool_;
34303436
};
3431-
} // namespace internal
34323437

3433-
template <typename V> struct dynamic_routing_table {
3438+
template <typename V> struct dynamic_routing_table_impl {
3439+
dynamic_routing_table_impl() : root(pool) {}
34343440

34353441
// Find a route and return reference to a procedure.
34363442
auto& operator[](const std::string_view& r) {
3437-
strings.push_back(std::make_shared<std::string>(r));
3438-
std::string_view r2(*strings.back());
3443+
auto [itr, is_inserted] = strings.emplace(std::string(r));
3444+
std::string_view r2(*itr);
34393445
return root.find_or_create(r2, 0);
34403446
}
3441-
auto& operator[](const std::string& r) {
3442-
strings.push_back(std::make_shared<std::string>(r));
3443-
std::string_view r2(*strings.back());
3444-
return root.find_or_create(r2, 0);
3447+
auto& operator[](const std::string& s) {
3448+
auto [itr, is_inserted] = strings.emplace(s);
3449+
std::string_view r(*itr);
3450+
return root.find_or_create(r, 0);
34453451
}
34463452

34473453
// Find a route and return an iterator.
@@ -3450,8 +3456,36 @@ template <typename V> struct dynamic_routing_table {
34503456
template <typename F> void for_all_routes(F f) const { root.for_all_routes(f); }
34513457
auto end() const { return root.end(); }
34523458

3453-
std::vector<std::shared_ptr<std::string>> strings;
3454-
internal::drt_node<V> root;
3459+
std::unordered_set<std::string> strings;
3460+
drt_node_pool<drt_node<V>> pool;
3461+
drt_node<V> root;
3462+
};
3463+
} // namespace internal
3464+
3465+
template <typename V> struct dynamic_routing_table {
3466+
dynamic_routing_table() : impl_(std::make_shared<internal::dynamic_routing_table_impl<V>>()) {}
3467+
dynamic_routing_table(const dynamic_routing_table& other) : impl_(other.impl_) {}
3468+
3469+
// Assignment operator
3470+
dynamic_routing_table& operator=(const dynamic_routing_table& other) {
3471+
if (this != &other) {
3472+
impl_ = other.impl_;
3473+
}
3474+
return *this;
3475+
}
3476+
3477+
// Find a route and return reference to a procedure.
3478+
auto& operator[](const std::string_view& r) { return impl_->operator[](r); }
3479+
auto& operator[](const std::string& s) { return impl_->operator[](s); }
3480+
3481+
// Find a route and return an iterator.
3482+
auto find(const std::string_view& r) const { return impl_->find(r); }
3483+
3484+
template <typename F> void for_all_routes(F f) const { impl_->for_all_routes(f); }
3485+
auto end() const { return impl_->end(); }
3486+
3487+
private:
3488+
std::shared_ptr<internal::dynamic_routing_table_impl<V>> impl_;
34553489
};
34563490

34573491
} // namespace li

single_headers/lithium_mysql.hh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <cassert>
1414
#include <cstring>
1515
#include <deque>
16+
#include <functional>
1617
#include <iostream>
1718
#include <map>
1819
#include <memory>

single_headers/lithium_pgsql.hh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <cassert>
1515
#include <cstring>
1616
#include <deque>
17+
#include <functional>
1718
#include <iostream>
1819
#if __APPLE__
1920
#include <libkern/OSByteOrder.h>

0 commit comments

Comments
 (0)