ReUseX  0.0.5
3D Point Cloud Processing for Building Reuse
Loading...
Searching...
No Matches
resource_router.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2025 Povl Filip Sonne-Frederiksen
2//
3// SPDX-License-Identifier: GPL-3.0-or-later
4
5#pragma once
6
7#include "format_handler.hpp"
8#include "path_parser.hpp"
9
10#include <reusex/core/ProjectDB.hpp>
11
12#include <filesystem>
13#include <map>
14#include <memory>
15#include <string_view>
16#include <vector>
17
18namespace rux::database {
19
27 public:
28 explicit ResourceRouter(std::shared_ptr<reusex::ProjectDB> db) : db_(db) {}
29 virtual ~ResourceRouter() = default;
30
38 virtual DataPayload get(const std::vector<PathComponent> &components) = 0;
39
47 virtual void set(const std::vector<PathComponent> &components,
48 const DataPayload &data) = 0;
49
56 virtual void del(const std::vector<PathComponent> &components) = 0;
57
66 virtual std::vector<std::string> list() const = 0;
67
74 std::optional<std::string> resolve_index(int index) const {
75 auto items = list();
76 if (index < 0 || index >= static_cast<int>(items.size())) {
77 return std::nullopt;
78 }
79 return items[index];
80 }
81
82 protected:
83 std::shared_ptr<reusex::ProjectDB> db_;
84};
85
90 public:
94 explicit RouterRegistry(std::shared_ptr<reusex::ProjectDB> db);
95
103 ResourceRouter &get_router(std::string_view collection);
104
107 std::vector<std::string> collection_names() const;
108
109 private:
110 std::shared_ptr<reusex::ProjectDB> db_;
111 std::map<std::string, std::unique_ptr<ResourceRouter>> routers_;
112};
113
121std::vector<std::vector<PathComponent>>
122expand_wildcards(const std::vector<PathComponent> &components,
123 const ResourceRouter &router);
124
125} // namespace rux::database
Base interface for resource routers.
virtual ~ResourceRouter()=default
virtual void set(const std::vector< PathComponent > &components, const DataPayload &data)=0
Set resource data at the given path.
ResourceRouter(std::shared_ptr< reusex::ProjectDB > db)
virtual void del(const std::vector< PathComponent > &components)=0
Delete resource at the given path.
virtual DataPayload get(const std::vector< PathComponent > &components)=0
Get resource data at the given path.
virtual std::vector< std::string > list() const =0
List all items in this collection.
std::optional< std::string > resolve_index(int index) const
Resolve array index to item name/id.
std::shared_ptr< reusex::ProjectDB > db_
ResourceRouter & get_router(std::string_view collection)
Get router for a collection.
std::vector< std::string > collection_names() const
Sorted list of registered collection names — use this instead of a hardcoded array so the help text a...
RouterRegistry(std::shared_ptr< reusex::ProjectDB > db)
Create routers for all resource types.
std::variant< std::string, std::vector< uint8_t >, nlohmann::json > DataPayload
Data payload that can be returned by routers.
std::vector< std::vector< PathComponent > > expand_wildcards(const std::vector< PathComponent > &components, const ResourceRouter &router)
Helper to expand wildcards in path components.