cesium-native 0.46.0
All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Pages
PagedList.h
1#pragma once
2
3#include <CesiumAsync/AsyncSystem.h>
4#include <CesiumAsync/Future.h>
5#include <CesiumUtility/JsonHelpers.h>
6#include <CesiumUtility/Result.h>
7
8#include <rapidjson/document.h>
9
10#include <functional>
11#include <optional>
12#include <string>
13#include <vector>
14
15namespace CesiumITwinClient {
16
17class Connection;
18
26template <typename T> class PagedList {
27public:
31 using PageOperation = std::function<CesiumAsync::Future<
32 CesiumUtility::Result<PagedList<T>>>(Connection&, const std::string&)>;
33
44 rapidjson::Document& doc,
45 std::vector<T>&& items,
46 PageOperation&& operation)
47 : _operation(std::move(operation)), _items(std::move(items)) {
48 const auto& linksMember = doc.FindMember("_links");
49 if (linksMember != doc.MemberEnd() && linksMember->value.IsObject()) {
50 const auto& selfMember = linksMember->value.FindMember("self");
51 if (selfMember != doc.MemberEnd() && selfMember->value.IsObject()) {
52 this->_selfUrl = CesiumUtility::JsonHelpers::getStringOrDefault(
53 selfMember->value,
54 "href",
55 "");
56 }
57
58 const auto& nextMember = linksMember->value.FindMember("next");
59 if (nextMember != doc.MemberEnd() && nextMember->value.IsObject()) {
60 this->_nextUrl = CesiumUtility::JsonHelpers::getStringOrDefault(
61 nextMember->value,
62 "href",
63 "");
64 }
65
66 const auto& prevMember = linksMember->value.FindMember("prev");
67 if (prevMember != doc.MemberEnd() && prevMember->value.IsObject()) {
68 this->_prevUrl = CesiumUtility::JsonHelpers::getStringOrDefault(
69 prevMember->value,
70 "href",
71 "");
72 }
73 }
74 }
75
79 T& operator[](size_t index) { return _items[index]; }
80
84 const T& operator[](size_t index) const { return _items[index]; }
85
89 size_t size() const { return _items.size(); }
90
94 auto begin() { return _items.begin(); }
98 auto begin() const { return _items.begin(); }
99
103 auto end() { return _items.end(); }
107 auto end() const { return _items.end(); }
108
117 next(CesiumAsync::AsyncSystem& asyncSystem, Connection& connection) const {
118 if (!this->_nextUrl.has_value()) {
119 return asyncSystem.createResolvedFuture(
120 CesiumUtility::Result<PagedList<T>>(std::nullopt));
121 }
122
123 return _operation(connection, *this->_nextUrl);
124 }
125
134 prev(CesiumAsync::AsyncSystem& asyncSystem, Connection& connection) const {
135 if (!this->_prevUrl.has_value()) {
136 return asyncSystem.createResolvedFuture(
137 CesiumUtility::Result<PagedList<T>>(std::nullopt));
138 }
139
140 return _operation(connection, *this->_prevUrl);
141 }
142
143private:
144 PageOperation _operation;
145 std::vector<T> _items;
146 std::optional<std::string> _selfUrl = std::nullopt;
147 std::optional<std::string> _nextUrl = std::nullopt;
148 std::optional<std::string> _prevUrl = std::nullopt;
149};
150}; // namespace CesiumITwinClient
A system for managing asynchronous requests and tasks.
Definition AsyncSystem.h:36
Future< T > createResolvedFuture(T &&value) const
Creates a future that is already resolved.
A value that will be available in the future, as produced by AsyncSystem.
Definition Promise.h:12
Represents a connection to the Bentley iTwin API.
Definition Connection.h:75
Allows access to a set of resources from a paginated list.
Definition PagedList.h:26
T & operator[](size_t index)
Returns the contained item at index.
Definition PagedList.h:79
const T & operator[](size_t index) const
Returns the contained item at index.
Definition PagedList.h:84
auto begin() const
The begin iterator of the underlying vector.
Definition PagedList.h:98
std::function< CesiumAsync::Future< CesiumUtility::Result< PagedList< T > > >(Connection &, const std::string &)> PageOperation
Callback used to obtain a page of results from a URL.
Definition PagedList.h:31
CesiumAsync::Future< CesiumUtility::Result< PagedList< T > > > next(CesiumAsync::AsyncSystem &asyncSystem, Connection &connection) const
Returns a future that will return the next page of items.
Definition PagedList.h:117
auto end()
The end iterator of the underlying vector.
Definition PagedList.h:103
CesiumAsync::Future< CesiumUtility::Result< PagedList< T > > > prev(CesiumAsync::AsyncSystem &asyncSystem, Connection &connection) const
Returns a future that will return the previous page of items.
Definition PagedList.h:134
auto begin()
The begin iterator of the underlying vector.
Definition PagedList.h:94
size_t size() const
Returns the number of contained items.
Definition PagedList.h:89
PagedList(rapidjson::Document &doc, std::vector< T > &&items, PageOperation &&operation)
Creates a new PagedList from a set of items, an iTwin API response, and a callback to retrieve more i...
Definition PagedList.h:43
auto end() const
The end iterator of the underlying vector.
Definition PagedList.h:107
Classes for interacting with the iTwin API.
STL namespace.
Holds the result of an operation. If the operation succeeds, it will provide a value....
Definition Result.h:16