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
14
15namespace NeoN
16{
17
18
19// forward declaration
20class Database;
21
31{
32public:
33
40 template<typename CollectionType>
41 Collection(CollectionType collection)
42 : impl_(std::make_unique<CollectionModel<CollectionType>>(std::move(collection)))
43 {}
44
48 Collection(const Collection& other) = delete;
49
53 Collection& operator=(const Collection& other) = delete;
54
58 Collection(Collection&& other) = default;
59
63 Collection& operator=(Collection&& other) = default;
64
71 Document& doc(const std::string& id);
72
79 const Document& doc(const std::string& id) const;
80
88 std::vector<std::string> find(const std::function<bool(const Document&)>& predicate) const;
89
95 size_t size() const;
96
102 std::string type() const;
103
109 std::string name() const;
110
117
123 const Database& db() const;
124
132 template<typename CollectionType>
133 CollectionType& as()
134 {
135 auto derived = dynamic_cast<CollectionModel<CollectionType>*>(impl_.get());
136 if (!derived)
137 {
138 throw std::bad_cast();
139 }
140 return derived->collection_;
141 }
142
143
151 template<typename CollectionType>
152 const CollectionType& as() const
153 {
154 auto derived = dynamic_cast<CollectionModel<CollectionType>*>(impl_.get());
155 if (!derived)
156 {
157 throw std::bad_cast();
158 }
159 return derived->collection_;
160 }
161
162private:
163
164 struct CollectionConcept
165 {
166 virtual ~CollectionConcept() = default;
167 virtual Document& doc(const std::string& id) = 0;
168 virtual const Document& doc(const std::string& id) const = 0;
169 virtual std::vector<std::string> find(const std::function<bool(const Document&)>& predicate
170 ) const = 0;
171 virtual size_t size() const = 0;
172 virtual std::string type() const = 0;
173 virtual std::string name() const = 0;
174 virtual Database& db() = 0;
175 virtual const Database& db() const = 0;
176 };
177
178 template<typename CollectionType>
179 struct CollectionModel : CollectionConcept
180 {
181 CollectionModel(CollectionType collection) : collection_(std::move(collection)) {}
182
183 Document& doc(const std::string& id) override { return collection_.doc(id); }
184
185 const Document& doc(const std::string& id) const override { return collection_.doc(id); }
186
187 std::vector<std::string> find(const std::function<bool(const Document&)>& predicate
188 ) const override
189 {
190 return collection_.find(predicate);
191 }
192
193 size_t size() const override { return collection_.size(); }
194
195 std::string type() const override { return collection_.type(); }
196
197 std::string name() const override { return collection_.name(); }
198
199 Database& db() override { return collection_.db(); }
200
201 const Database& db() const override { return collection_.db(); }
202
203 CollectionType collection_;
204 };
205
206 std::unique_ptr<CollectionConcept> impl_;
207};
208
215template<typename DocumentType>
217{
218
219public:
220
228
233
238
243
248
255 Document& doc(const std::string& id) { return docs_.at(id).doc(); }
256
263 const Document& doc(const std::string& id) const { return docs_.at(id).doc(); }
264
271 std::vector<std::string> find(const std::function<bool(const Document&)>& predicate) const
272 {
273 std::vector<std::string> result;
274 for (const auto& [key, doc] : docs_)
275 {
276 if (predicate(doc.doc()))
277 {
278 result.push_back(doc.id());
279 }
280 }
281 return result;
282 }
283
289 std::size_t size() const { return docs_.size(); }
290
296 const NeoN::Database& db() const { return db_; }
297
303 NeoN::Database& db() { return db_; }
304
310 const std::string& name() const { return name_; }
311
317 std::string type() const { return DocumentType::typeName(); }
318
324 std::vector<std::string> sortedKeys() const
325 {
326 auto k = docs_ | std::views::keys;
327 std::vector<std::string> result(k.begin(), k.end());
328 std::ranges::sort(result);
329 return result;
330 }
331
332protected:
333
334 std::unordered_map<std::string, DocumentType> docs_;
336 std::string name_;
337};
338
339} // namespace NeoN
A mixin class for collection of documents in a database to simplify the implementation of common oper...
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.
std::size_t size() const
Gets the number of documents in the collection.
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
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.
A type-erased interface collection types.
size_t 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
std::string id() const
Retrieves the ID of the Document.
Definition document.hpp:83
Integer types used throughout NeoN.
Definition array.hpp:18
const std::string & name(const NeoN::Document &doc)
Retrieves the name of a Document.