Skip to content

Commit

Permalink
[single_headers] Add the generated files
Browse files Browse the repository at this point in the history
  • Loading branch information
cong1920 committed Dec 31, 2024
1 parent e722e84 commit 9db72df
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 52 deletions.
86 changes: 60 additions & 26 deletions single_headers/lithium.hh
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
#include <tuple>
#include <unistd.h>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <variant>
#include <vector>
Expand Down Expand Up @@ -6510,9 +6511,21 @@ namespace li {

namespace internal {

template <typename V> struct drt_node {
// A simple memory pool for drt_node objects
template <typename T> struct drt_node_pool {
template <typename... Args> T* allocate(Args&&... args) {
auto new_node = std::make_unique<T>(std::forward<Args>(args)...);
T* ptr = new_node.get();
pool_.emplace_back(std::move(new_node));
return ptr;
}

drt_node() : v_{0, nullptr} {}
std::vector<std::unique_ptr<T>> pool_;
};

template <typename V> struct drt_node {
drt_node() : pool_(nullptr), v_{0, nullptr} {}
drt_node(drt_node_pool<drt_node>& pool) : pool_(pool), v_{0, nullptr} {}

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

auto it = children_.find(k);
if (it != children_.end())
return children_[k]->find_or_create(r, c);
else {
auto new_node = std::make_shared<drt_node>();
children_shared_pointers_.push_back(new_node);
children_.insert({k, new_node.get()});
return new_node->find_or_create(r, c);
if (children_.find(k) == children_.end()) {
children_[k] = pool_.allocate(pool_);
}

return v_;
return children_[k]->find_or_create(r, c);
}

template <typename F> void for_all_routes(F f, std::string prefix = "") const {
Expand All @@ -6556,8 +6562,8 @@ template <typename V> struct drt_node {
else {
if (prefix.size() && prefix.back() != '/')
prefix += '/';
for (auto pair : children_)
pair.second->for_all_routes(f, prefix + std::string(pair.first));
for (const auto& kv : children_)
kv.second->for_all_routes(f, prefix + std::string(kv.first));
}
}

Expand Down Expand Up @@ -6594,7 +6600,7 @@ template <typename V> struct drt_node {

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

V v_;
std::unordered_map<std::string_view, drt_node*> children_;
std::vector<std::shared_ptr<drt_node>> children_shared_pointers_;
drt_node_pool<drt_node>& pool_;
};
} // namespace internal

template <typename V> struct dynamic_routing_table {
template <typename V> struct dynamic_routing_table_impl {
dynamic_routing_table_impl() : root(pool) {}

// Find a route and return reference to a procedure.
auto& operator[](const std::string_view& r) {
strings.push_back(std::make_shared<std::string>(r));
std::string_view r2(*strings.back());
auto [itr, is_inserted] = strings.emplace(std::string(r));
std::string_view r2(*itr);
return root.find_or_create(r2, 0);
}
auto& operator[](const std::string& r) {
strings.push_back(std::make_shared<std::string>(r));
std::string_view r2(*strings.back());
return root.find_or_create(r2, 0);
auto& operator[](const std::string& s) {
auto [itr, is_inserted] = strings.emplace(s);
std::string_view r(*itr);
return root.find_or_create(r, 0);
}

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

std::vector<std::shared_ptr<std::string>> strings;
internal::drt_node<V> root;
std::unordered_set<std::string> strings;
drt_node_pool<drt_node<V>> pool;
drt_node<V> root;
};
} // namespace internal

template <typename V> struct dynamic_routing_table {
dynamic_routing_table() : impl_(std::make_shared<internal::dynamic_routing_table_impl<V>>()) {}
dynamic_routing_table(const dynamic_routing_table& other) : impl_(other.impl_) {}

// Assignment operator
dynamic_routing_table& operator=(const dynamic_routing_table& other) {
if (this != &other) {
impl_ = other.impl_;
}
return *this;
}

// Find a route and return reference to a procedure.
auto& operator[](const std::string_view& r) { return impl_->operator[](r); }
auto& operator[](const std::string& s) { return impl_->operator[](s); }

// Find a route and return an iterator.
auto find(const std::string_view& r) const { return impl_->find(r); }

template <typename F> void for_all_routes(F f) const { impl_->for_all_routes(f); }
auto end() const { return impl_->end(); }

private:
std::shared_ptr<internal::dynamic_routing_table_impl<V>> impl_;
};

} // namespace li
Expand Down
86 changes: 60 additions & 26 deletions single_headers/lithium_http_server.hh
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
#include <tuple>
#include <unistd.h>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <variant>
#include <vector>
Expand Down Expand Up @@ -3330,9 +3331,21 @@ namespace li {

namespace internal {

template <typename V> struct drt_node {
// A simple memory pool for drt_node objects
template <typename T> struct drt_node_pool {
template <typename... Args> T* allocate(Args&&... args) {
auto new_node = std::make_unique<T>(std::forward<Args>(args)...);
T* ptr = new_node.get();
pool_.emplace_back(std::move(new_node));
return ptr;
}

drt_node() : v_{0, nullptr} {}
std::vector<std::unique_ptr<T>> pool_;
};

template <typename V> struct drt_node {
drt_node() : pool_(nullptr), v_{0, nullptr} {}
drt_node(drt_node_pool<drt_node>& pool) : pool_(pool), v_{0, nullptr} {}

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

auto it = children_.find(k);
if (it != children_.end())
return children_[k]->find_or_create(r, c);
else {
auto new_node = std::make_shared<drt_node>();
children_shared_pointers_.push_back(new_node);
children_.insert({k, new_node.get()});
return new_node->find_or_create(r, c);
if (children_.find(k) == children_.end()) {
children_[k] = pool_.allocate(pool_);
}

return v_;
return children_[k]->find_or_create(r, c);
}

template <typename F> void for_all_routes(F f, std::string prefix = "") const {
Expand All @@ -3376,8 +3382,8 @@ template <typename V> struct drt_node {
else {
if (prefix.size() && prefix.back() != '/')
prefix += '/';
for (auto pair : children_)
pair.second->for_all_routes(f, prefix + std::string(pair.first));
for (const auto& kv : children_)
kv.second->for_all_routes(f, prefix + std::string(kv.first));
}
}

Expand Down Expand Up @@ -3414,7 +3420,7 @@ template <typename V> struct drt_node {

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

V v_;
std::unordered_map<std::string_view, drt_node*> children_;
std::vector<std::shared_ptr<drt_node>> children_shared_pointers_;
drt_node_pool<drt_node>& pool_;
};
} // namespace internal

template <typename V> struct dynamic_routing_table {
template <typename V> struct dynamic_routing_table_impl {
dynamic_routing_table_impl() : root(pool) {}

// Find a route and return reference to a procedure.
auto& operator[](const std::string_view& r) {
strings.push_back(std::make_shared<std::string>(r));
std::string_view r2(*strings.back());
auto [itr, is_inserted] = strings.emplace(std::string(r));
std::string_view r2(*itr);
return root.find_or_create(r2, 0);
}
auto& operator[](const std::string& r) {
strings.push_back(std::make_shared<std::string>(r));
std::string_view r2(*strings.back());
return root.find_or_create(r2, 0);
auto& operator[](const std::string& s) {
auto [itr, is_inserted] = strings.emplace(s);
std::string_view r(*itr);
return root.find_or_create(r, 0);
}

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

std::vector<std::shared_ptr<std::string>> strings;
internal::drt_node<V> root;
std::unordered_set<std::string> strings;
drt_node_pool<drt_node<V>> pool;
drt_node<V> root;
};
} // namespace internal

template <typename V> struct dynamic_routing_table {
dynamic_routing_table() : impl_(std::make_shared<internal::dynamic_routing_table_impl<V>>()) {}
dynamic_routing_table(const dynamic_routing_table& other) : impl_(other.impl_) {}

// Assignment operator
dynamic_routing_table& operator=(const dynamic_routing_table& other) {
if (this != &other) {
impl_ = other.impl_;
}
return *this;
}

// Find a route and return reference to a procedure.
auto& operator[](const std::string_view& r) { return impl_->operator[](r); }
auto& operator[](const std::string& s) { return impl_->operator[](s); }

// Find a route and return an iterator.
auto find(const std::string_view& r) const { return impl_->find(r); }

template <typename F> void for_all_routes(F f) const { impl_->for_all_routes(f); }
auto end() const { return impl_->end(); }

private:
std::shared_ptr<internal::dynamic_routing_table_impl<V>> impl_;
};

} // namespace li
Expand Down
1 change: 1 addition & 0 deletions single_headers/lithium_mysql.hh
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <cassert>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
Expand Down
1 change: 1 addition & 0 deletions single_headers/lithium_pgsql.hh
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <cassert>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#if __APPLE__
#include <libkern/OSByteOrder.h>
Expand Down

0 comments on commit 9db72df

Please sign in to comment.