Sourcemeta Core 0.0.0
Loading...
Searching...
No Matches
uritemplate_router.h
1#ifndef SOURCEMETA_CORE_URITEMPLATE_ROUTER_H_
2#define SOURCEMETA_CORE_URITEMPLATE_ROUTER_H_
3
4#ifndef SOURCEMETA_CORE_URITEMPLATE_EXPORT
5#include <sourcemeta/core/uritemplate_export.h>
6#endif
7
8#include <sourcemeta/core/io.h>
9
10#include <cstddef> // std::size_t
11#include <cstdint> // std::uint16_t, std::uint32_t, std::uint8_t, std::int64_t
12#include <filesystem> // std::filesystem::path
13#include <functional> // std::function
14#include <limits> // std::numeric_limits
15#include <memory> // std::unique_ptr
16#include <span> // std::span
17#include <string> // std::string
18#include <string_view> // std::string_view
19#include <tuple> // std::tuple
20#include <unordered_map> // std::unordered_map
21#include <utility> // std::pair
22#include <variant> // std::variant
23#include <vector> // std::vector
24
25namespace sourcemeta::core {
26
27#if defined(_MSC_VER)
28#pragma warning(push)
29#pragma warning(disable : 4251)
30#endif
31
46class SOURCEMETA_CORE_URITEMPLATE_EXPORT URITemplateRouter {
47 friend class URITemplateRouterView;
48
49public:
51 using Identifier = std::uint16_t;
52
54 using Index = std::uint8_t;
55
57 using Callback =
58 std::function<void(Index, std::string_view, std::string_view)>;
59
61 using ArgumentValue = std::variant<std::string_view, std::int64_t, bool>;
62
64 using Argument = std::pair<std::string_view, ArgumentValue>;
65
68 std::function<void(std::string_view, const ArgumentValue &)>;
69
71 enum class NodeType : std::uint8_t {
73 Root = 0,
75 Literal = 1,
77 Variable = 2,
79 Expansion = 3,
81 OptionalExpansion = 4
82 };
83
85 struct Node {
93 std::string_view value;
94
95 // This children distinction enforces that there can only be one non-literal
96 // child at the type level. Also allows us to more efficiently search on
97 // literals
99 std::vector<std::unique_ptr<Node>> literals;
101 std::unique_ptr<Node> variable;
102 };
103
105 URITemplateRouter() = default;
106
111 explicit URITemplateRouter(std::string_view base_path,
112 std::string_view base_url = {});
113
114 // To avoid mistakes
115 URITemplateRouter(const URITemplateRouter &) = delete;
117 auto operator=(const URITemplateRouter &) -> URITemplateRouter & = delete;
118 auto operator=(URITemplateRouter &&) -> URITemplateRouter & = delete;
119
124 auto add(const std::string_view uri_template,
125 const std::string_view operation_id, const Identifier identifier,
126 const Identifier context = 0,
127 const std::span<const Argument> arguments = {}) -> void;
128
132 const std::span<const Argument> arguments = {}) -> void;
133
136 [[nodiscard]] auto match(const std::string_view path,
137 const Callback &callback) const
138 -> std::pair<Identifier, Identifier>;
139
147 [[nodiscard]] auto
148 describes(const std::string_view path,
149 const std::string_view base_path = {}) const noexcept -> bool;
150
152 [[nodiscard]] auto root() const noexcept -> const Node &;
153
155 auto arguments(const Identifier identifier,
156 const ArgumentCallback &callback) const -> void;
157
159 [[nodiscard]] auto arguments() const noexcept
160 -> const std::vector<std::pair<Identifier, std::vector<Argument>>> &;
161
163 [[nodiscard]] auto base_path() const noexcept -> std::string_view;
164
166 [[nodiscard]] auto base_url() const noexcept -> std::string_view;
167
169 [[nodiscard]] auto size() const noexcept -> std::size_t;
170
172 [[nodiscard]] auto at(const std::size_t index) const -> Identifier;
173
176 [[nodiscard]] auto context(const Identifier identifier) const -> Identifier;
177
180 [[nodiscard]] auto path(const Identifier identifier) const -> std::string;
181
185 [[nodiscard]] auto operation(const std::string_view operation_id) const
186 -> std::pair<Identifier, Identifier>;
187
190 [[nodiscard]] auto operation_id(const Identifier identifier) const
191 -> std::string_view;
192
193private:
194 Node root_;
195 Node otherwise_;
196 std::string base_path_;
197 std::string base_url_;
198 std::vector<std::pair<Identifier, std::vector<Argument>>> arguments_;
199 std::vector<std::tuple<Identifier, Identifier, std::string_view>> entries_;
200 std::unordered_map<std::string_view, std::pair<Identifier, Identifier>>
201 operations_;
202};
203
206class SOURCEMETA_CORE_URITEMPLATE_EXPORT URITemplateRouterView {
207public:
209 static auto save(const URITemplateRouter &router,
210 const std::filesystem::path &path) -> void;
211
213 URITemplateRouterView(const std::filesystem::path &path);
214
217 URITemplateRouterView(const std::uint8_t *data, std::size_t size);
218
220
221 // To avoid mistakes
224 auto operator=(const URITemplateRouterView &)
225 -> URITemplateRouterView & = delete;
226 auto operator=(URITemplateRouterView &&) -> URITemplateRouterView & = delete;
227
230 [[nodiscard]] auto match(const std::string_view path,
231 const URITemplateRouter::Callback &callback) const
234
242 [[nodiscard]] auto
243 describes(const std::string_view path,
244 const std::string_view base_path = {}) const noexcept -> bool;
245
248 const URITemplateRouter::ArgumentCallback &callback) const
249 -> void;
250
252 [[nodiscard]] auto base_path() const noexcept -> std::string_view;
253
255 [[nodiscard]] auto base_url() const noexcept -> std::string_view;
256
258 [[nodiscard]] auto size() const noexcept -> std::size_t;
259
263 [[nodiscard]] auto operation(const std::string_view operation_id) const
264 -> std::pair<URITemplateRouter::Identifier,
265 URITemplateRouter::Identifier>;
266
268 [[nodiscard]] auto at(const std::size_t index) const
269 -> URITemplateRouter::Identifier;
270
273 [[nodiscard]] auto
274 context(const URITemplateRouter::Identifier identifier) const
275 -> URITemplateRouter::Identifier;
276
279 [[nodiscard]] auto path(const URITemplateRouter::Identifier identifier) const
280 -> std::string;
281
284 [[nodiscard]] auto
285 operation_id(const URITemplateRouter::Identifier identifier) const
286 -> std::string_view;
287
288private:
289 const std::uint8_t *data_{nullptr};
290 std::size_t size_{0};
291 std::unique_ptr<FileView> owner_;
292 // Holds an eight-byte-aligned copy of an unaligned external buffer, so the
293 // over-aligned serialized nodes are always read from aligned storage
294 std::vector<std::uint64_t> owned_;
295};
296
297#if defined(_MSC_VER)
298#pragma warning(pop)
299#endif
300
311inline constexpr std::size_t URITEMPLATE_ROUTER_MAXIMUM_VARIABLES{
312 static_cast<std::size_t>(
313 std::numeric_limits<URITemplateRouter::Index>::max()) +
314 1};
315
316} // namespace sourcemeta::core
317
318#endif
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:67
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:57
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:61
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:51
std::uint8_t Index
The variable index type.
Definition uritemplate_router.h:54
std::pair< std::string_view, ArgumentValue > Argument
A named route argument.
Definition uritemplate_router.h:64
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:71
@ Root
The root of the trie.
Definition uritemplate_router.h:73
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.
Definition uritemplate_router.h:46
constexpr std::size_t URITEMPLATE_ROUTER_MAXIMUM_VARIABLES
Definition uritemplate_router.h:311
A node in the router trie.
Definition uritemplate_router.h:85
std::string_view value
The literal text or variable name of this node.
Definition uritemplate_router.h:93
Identifier context
The context identifier associated with this node.
Definition uritemplate_router.h:89
std::vector< std::unique_ptr< Node > > literals
The literal children of this node.
Definition uritemplate_router.h:99
Identifier identifier
The handler identifier of the route ending at this node.
Definition uritemplate_router.h:87
NodeType type
The kind of component this node represents.
Definition uritemplate_router.h:91
std::unique_ptr< Node > variable
The single non-literal child of this node.
Definition uritemplate_router.h:101