1#ifndef SOURCEMETA_CORE_URITEMPLATE_ROUTER_H_
2#define SOURCEMETA_CORE_URITEMPLATE_ROUTER_H_
4#ifndef SOURCEMETA_CORE_URITEMPLATE_EXPORT
5#include <sourcemeta/core/uritemplate_export.h>
8#include <sourcemeta/core/io.h>
19#include <unordered_map>
24namespace sourcemeta::core {
28#pragma warning(disable : 4251)
46 friend class URITemplateRouterView;
57 std::function<void(
Index, std::string_view, std::string_view)>;
60 using ArgumentValue = std::variant<std::string_view, std::int64_t, bool>;
63 using Argument = std::pair<std::string_view, ArgumentValue>;
123 auto add(
const std::string_view uri_template,
126 const std::span<const Argument>
arguments = {}) ->
void;
131 const std::span<const Argument>
arguments = {}) ->
void;
137 -> std::pair<Identifier, Identifier>;
148 const std::string_view
base_path = {})
const noexcept ->
bool;
151 [[nodiscard]]
auto root() const noexcept -> const
Node &;
162 [[nodiscard]] auto
base_path() const noexcept -> std::string_view;
165 [[nodiscard]] auto
base_url() const noexcept -> std::string_view;
168 [[nodiscard]] auto
size() const noexcept -> std::
size_t;
195 std::
string base_path_;
196 std::
string base_url_;
209 const std::filesystem::path &
path) -> void;
243 const std::string_view
base_path = {})
const noexcept ->
bool;
251 [[nodiscard]]
auto base_path() const noexcept -> std::string_view;
254 [[nodiscard]] auto
base_url() const noexcept -> std::string_view;
257 [[nodiscard]] auto
size() const noexcept -> std::
size_t;
267 [[nodiscard]] auto
at(const std::
size_t index) const
288 const std::uint8_t *data_{
nullptr};
289 std::size_t size_{0};
290 std::unique_ptr<FileView> owner_;
293 std::vector<std::uint64_t> owned_;
URITemplateRouter()=default
Construct an empty router.
auto at(const std::size_t index) const -> URITemplateRouter::Identifier
Get the identifier of the route at the given positional index.
std::function< void(std::string_view, const ArgumentValue &)> ArgumentCallback
The argument callback (name, value).
Definition uritemplate_router.h:66
URITemplateRouterView(const std::filesystem::path &path)
Construct a view by loading a serialized router from a file.
URITemplateRouterView(const std::uint8_t *data, std::size_t size)
auto base_path() const noexcept -> std::string_view
Access the base path prefix.
URITemplateRouter(std::string_view base_path, std::string_view base_url={})
std::function< void(Index, std::string_view, std::string_view)> Callback
The match callback (index, name, value).
Definition uritemplate_router.h:56
auto context(const Identifier identifier) const -> Identifier
auto describes(const std::string_view path, const std::string_view base_path={}) const noexcept -> bool
auto base_url() const noexcept -> std::string_view
Access the base URL associated with the router.
auto path(const Identifier identifier) const -> std::string
auto path(const URITemplateRouter::Identifier identifier) const -> std::string
auto describes(const std::string_view path, const std::string_view base_path={}) const noexcept -> bool
auto operation_id(const Identifier identifier) const -> std::string_view
auto match(const std::string_view path, const URITemplateRouter::Callback &callback) const -> std::pair< URITemplateRouter::Identifier, URITemplateRouter::Identifier >
auto operation(const std::string_view operation_id) const -> std::pair< Identifier, Identifier >
auto context(const URITemplateRouter::Identifier identifier) const -> URITemplateRouter::Identifier
auto arguments(const Identifier identifier, const ArgumentCallback &callback) const -> void
Access the stored arguments for a given route identifier.
auto base_url() const noexcept -> std::string_view
Access the base URL associated with the router.
auto root() const noexcept -> const Node &
Access the root node of the trie.
auto match(const std::string_view path, const Callback &callback) const -> std::pair< Identifier, Identifier >
auto size() const noexcept -> std::size_t
Get the number of registered routes.
auto operation_id(const URITemplateRouter::Identifier identifier) const -> std::string_view
static auto save(const URITemplateRouter &router, const std::filesystem::path &path) -> void
Save a router to a binary file.
auto operation(const std::string_view operation_id) const -> std::pair< URITemplateRouter::Identifier, URITemplateRouter::Identifier >
std::variant< std::string_view, std::int64_t, bool > ArgumentValue
The value of a route argument.
Definition uritemplate_router.h:60
auto at(const std::size_t index) const -> Identifier
Get the identifier of the route at the given positional index.
auto base_path() const noexcept -> std::string_view
Access the base path prefix.
std::uint16_t Identifier
A handler identifier 0 means "no handler".
Definition uritemplate_router.h:50
std::uint8_t Index
The variable index type.
Definition uritemplate_router.h:53
std::pair< std::string_view, ArgumentValue > Argument
A named route argument.
Definition uritemplate_router.h:63
auto add(const std::string_view uri_template, const std::string_view operation_id, const Identifier identifier, const Identifier context=0, const std::span< const Argument > arguments={}) -> void
NodeType
The type of a node in the router trie.
Definition uritemplate_router.h:70
@ Root
The root of the trie.
Definition uritemplate_router.h:72
auto size() const noexcept -> std::size_t
Get the number of registered routes.
auto otherwise(const Identifier context, const std::span< const Argument > arguments={}) -> void
auto arguments(const URITemplateRouter::Identifier identifier, const URITemplateRouter::ArgumentCallback &callback) const -> void
Access the stored arguments for a given route identifier.