#ifndef __URLROUTER_H__ #define __URLROUTER_H__ #include "TemplateMatchs.h" #include "UrlRouterPrivate.h" #include template class UrlRouter : public UrlRouterPrivate { public: template void insert(std::string_view pattern, U &&v) { BOOST_STATIC_ASSERT(std::is_same::value || std::is_convertible::value || std::is_base_of::value); using ResourceType = typename std::decay_t, U, Resource>>; class Implementation : public AnyResource { public: explicit Implementation(U &&u_) : resource(std::forward(u_)) { } void const *get() const noexcept override { return static_cast(&resource); } private: ResourceType resource; }; auto resource = std::make_shared(std::forward(v)); insertImpl(pattern, std::dynamic_pointer_cast(resource)); } const Resource *find(boost::urls::segments_encoded_view path, TemplateMatchStorageBase &matches) const noexcept { boost::core::string_view *matches_it = matches.matches(); boost::core::string_view *ids_it = matches.ids(); AnyResource const *p = findImpl(path, matches_it, ids_it); if (p) { BOOST_ASSERT(matches_it >= matches.matches()); matches.resize(static_cast(matches_it - matches.matches())); return reinterpret_cast(p->get()); } matches.resize(0); return nullptr; }; }; #endif // __URLROUTER_H__