NeoN
A framework for CFD software
Loading...
Searching...
No Matches
collection.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2024 - 2026 NeoN authors
2//
3// SPDX-License-Identifier: MIT
4
5#pragma once
6
7#include <algorithm>
8#include <ranges>
9#include <string>
10#include <unordered_map>
11#include <memory>
12
15
16namespace NeoN
17{
18
19
20// forward declaration
21class Database;
22
32{
33public:
34
41 template<typename CollectionType>
42 Collection(CollectionType collection)
43 : impl_(std::make_unique<CollectionModel<CollectionType>>(std::move(collection)))
44 {}
45
49 Collection(const Collection& other) = delete;
50
54 Collection& operator=(const Collection& other) = delete;
55
59 Collection(Collection&& other) = default;
60
64 Collection& operator=(Collection&& other) = default;
65
72 Document& doc(const std::string& id);
73
80 const Document& doc(const std::string& id) const;
81
89 std::vector<std::string> find(const std::function<bool(const Document&)>& predicate) const;
90
96 sizeType size() const;
97
103 std::string type() const;
104
110 std::string name() const;
111
118
124 const Database& db() const;
125
133 template<typename CollectionType>
134 CollectionType& as()
135 {
136 auto derived = dynamic_cast<CollectionModel<CollectionType>*>(impl_.get());
137 if (!derived)
138 {
139 throw std::bad_cast();
140 }
141 return derived->collection_;
142 }
143
144
152 template<typename CollectionType>
153 const CollectionType& as() const
154 {
155 auto derived = dynamic_cast<CollectionModel<CollectionType>*>(impl_.get());
156 if (!derived)
157 {
158 throw std::bad_cast();
159 }
160 return derived->collection_;
161 }
162
163private:
164
165 struct CollectionConcept
166 {
167 virtual ~CollectionConcept() = default;
168 virtual Document& doc(const std::string& id) = 0;
169 virtual const Document& doc(const std::string& id) const = 0;
170 virtual std::vector<std::string> find(const std::function<bool(const Document&)>& predicate
171 ) const = 0;
172 virtual sizeType size() const = 0;
173 virtual std::string type() const = 0;
174 virtual std::string name() const = 0;
175 virtual Database& db() = 0;
176 virtual const Database& db() const = 0;
177 };
178
179 template<typename CollectionType>
180 struct CollectionModel : CollectionConcept
181 {
182 CollectionModel(CollectionType collection) : collection_(std::move(collection)) {}
183
184 Document& doc(const std::string& id) override { return collection_.doc(id); }
185
186 const Document& doc(const std::string& id) const override { return collection_.doc(id); }
187
188 std::vector<std::string> find(const std::function<bool(const Document&)>& predicate
189 ) const override
190 {
191 return collection_.find(predicate);
192 }
193
194 sizeType size() const override { return collection_.size(); }
195
196 std::string type() const override { return collection_.type(); }
197
198 std::string name() const override { return collection_.name(); }
199
200 Database& db() override { return collection_.db(); }
201
202 const Database& db() const override { return collection_.db(); }
203
204 CollectionType collection_;
205 };
206
207 std::unique_ptr<CollectionConcept> impl_;
208};
209
216template<typename DocumentType>
218{
219
220public:
221
229
234
239
244
249
256 Document& doc(const std::string& id) { return docs_.at(id).doc(); }
257
264 const Document& doc(const std::string& id) const { return docs_.at(id).doc(); }
265
272 std::vector<std::string> find(const std::function<bool(const Document&)>& predicate) const
273 {
274 std::vector<std::string> result;
275 for (const auto& [key, doc] : docs_)
276 {
277 if (predicate(doc.doc()))
278 {
279 result.push_back(doc.id());
280 }
281 }
282 return result;
283 }
284
290 sizeType size() const { return docs_.size(); }
291
297 const NeoN::Database& db() const { return db_; }
298
304 NeoN::Database& db() { return db_; }
305
311 const std::string& name() const { return name_; }
312
318 std::string type() const { return DocumentType::typeName(); }
319
325 std::vector<std::string> sortedKeys() const
326 {
327 auto k = docs_ | std::views::keys;
328 std::vector<std::string> result(k.begin(), k.end());
329 std::ranges::sort(result);
330 return result;
331 }
332
333protected:
334
335 std::unordered_map<std::string, DocumentType> docs_;
337 std::string name_;
338};
339
340} // namespace NeoN
std::string name_
The name of the collection.
CollectionMixin & operator=(CollectionMixin &&)=delete
NeoN::Database & db()
Gets a reference to the database.
const std::string & name() const
Gets the name of the collection.
const Document & doc(const std::string &id) const
Retrieves a document by its ID (const version).
CollectionMixin(NeoN::Database &db, std::string name)
Constructs a CollectionMixin with the given database and collection name.
std::vector< std::string > find(const std::function< bool(const Document &)> &predicate) const
Finds documents that match a given predicate.
CollectionMixin(CollectionMixin &&)=default
const NeoN::Database & db() const
Gets a const reference to the database.
std::string type() const
Gets the type name of the documents in the collection.
std::unordered_map< std::string, DocumentType > docs_
The map of document IDs to documents.
NeoN::Database & db_
The reference to the database.
CollectionMixin & operator=(const CollectionMixin &)=delete
CollectionMixin(const CollectionMixin &)=delete
sizeType size() const
Gets the number of documents in the collection.
std::vector< std::string > sortedKeys() const
Gets the sorted keys of the documents in the collection.
Document & doc(const std::string &id)
Retrieves a document by its ID.
sizeType size() const
Returns the number of documents in the collection.
CollectionType & as()
Casts the collection to a specific collection type.
std::string type() const
Returns the type of the collection.
Collection & operator=(Collection &&other)=default
A Collection is moveable, but not copyable.
Collection(const Collection &other)=delete
A Collection is not copyable, only moveable.
const CollectionType & as() const
Casts the collection to a specific collection type (const version).
const Document & doc(const std::string &id) const
Retrieves a document by its ID (const version).
std::vector< std::string > find(const std::function< bool(const Document &)> &predicate) const
Finds documents that match a given predicate.
Collection(CollectionType collection)
Constructs a Collection from a specific collection type.
Document & doc(const std::string &id)
Retrieves a document by its ID.
const Database & db() const
Returns a const reference to the database containing the collection.
Collection & operator=(const Collection &other)=delete
A Collection is not copyable, only moveable.
std::string name() const
Returns the name of the collection.
Collection(Collection &&other)=default
A Collection is moveable, but not copyable.
Database & db()
Returns a reference to the database containing the collection.
A class representing a document in a database.
Definition document.hpp:52
Integer types used throughout NeoN.
Definition array.hpp:18
std::size_t sizeType
Definition label.hpp:56