NeoN
A framework for CFD software
Loading...
Searching...
No Matches
collection.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2024 - 2025 NeoN authors
2//
3// SPDX-License-Identifier: MIT
4
5#pragma once
6
7#include <unordered_map>
8#include <string>
9#include <memory>
10#include <algorithm> // for std::sort
11
13
14namespace NeoN
15{
16
17
18// forward declaration
19class Database;
20
30{
31public:
32
39 template<typename CollectionType>
40 Collection(CollectionType collection)
41 : impl_(std::make_unique<CollectionModel<CollectionType>>(std::move(collection)))
42 {}
43
47 Collection(const Collection& other) = delete;
48
52 Collection& operator=(const Collection& other) = delete;
53
57 Collection(Collection&& other) = default;
58
62 Collection& operator=(Collection&& other) = default;
63
70 Document& doc(const std::string& id);
71
78 const Document& doc(const std::string& id) const;
79
87 std::vector<std::string> find(const std::function<bool(const Document&)>& predicate) const;
88
94 size_t size() const;
95
101 std::string type() const;
102
108 std::string name() const;
109
116
122 const Database& db() const;
123
131 template<typename CollectionType>
132 CollectionType& as()
133 {
134 auto derived = dynamic_cast<CollectionModel<CollectionType>*>(impl_.get());
135 if (!derived)
136 {
137 throw std::bad_cast();
138 }
139 return derived->collection_;
140 }
141
142
150 template<typename CollectionType>
151 const CollectionType& as() const
152 {
153 auto derived = dynamic_cast<CollectionModel<CollectionType>*>(impl_.get());
154 if (!derived)
155 {
156 throw std::bad_cast();
157 }
158 return derived->collection_;
159 }
160
161private:
162
163 struct CollectionConcept
164 {
165 virtual ~CollectionConcept() = default;
166 virtual Document& doc(const std::string& id) = 0;
167 virtual const Document& doc(const std::string& id) const = 0;
168 virtual std::vector<std::string> find(const std::function<bool(const Document&)>& predicate
169 ) const = 0;
170 virtual size_t size() const = 0;
171 virtual std::string type() const = 0;
172 virtual std::string name() const = 0;
173 virtual Database& db() = 0;
174 virtual const Database& db() const = 0;
175 };
176
177 template<typename CollectionType>
178 struct CollectionModel : CollectionConcept
179 {
180 CollectionModel(CollectionType collection) : collection_(std::move(collection)) {}
181
182 Document& doc(const std::string& id) override { return collection_.doc(id); }
183
184 const Document& doc(const std::string& id) const override { return collection_.doc(id); }
185
186 std::vector<std::string> find(const std::function<bool(const Document&)>& predicate
187 ) const override
188 {
189 return collection_.find(predicate);
190 }
191
192 size_t size() const override { return collection_.size(); }
193
194 std::string type() const override { return collection_.type(); }
195
196 std::string name() const override { return collection_.name(); }
197
198 Database& db() override { return collection_.db(); }
199
200 const Database& db() const override { return collection_.db(); }
201
202 CollectionType collection_;
203 };
204
205 std::unique_ptr<CollectionConcept> impl_;
206};
207
214template<typename DocumentType>
216{
217
218public:
219
227
232
237
242
247
254 Document& doc(const std::string& id) { return docs_.at(id).doc(); }
255
262 const Document& doc(const std::string& id) const { return docs_.at(id).doc(); }
263
270 std::vector<std::string> find(const std::function<bool(const Document&)>& predicate) const
271 {
272 std::vector<std::string> result;
273 for (const auto& [key, doc] : docs_)
274 {
275 if (predicate(doc.doc()))
276 {
277 result.push_back(doc.id());
278 }
279 }
280 return result;
281 }
282
288 std::size_t size() const { return docs_.size(); }
289
295 const NeoN::Database& db() const { return db_; }
296
302 NeoN::Database& db() { return db_; }
303
309 const std::string& name() const { return name_; }
310
316 std::string type() const { return DocumentType::typeName(); }
317
323 std::vector<std::string> sortedKeys() const
324 {
325 std::vector<std::string> result;
326 for (const auto& [key, doc] : docs_)
327 {
328 result.push_back(key);
329 }
330 std::sort(result.begin(), result.end());
331 return result;
332 }
333
334protected:
335
336 std::unordered_map<std::string, DocumentType> docs_;
338 std::string name_;
339};
340
341} // 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
Definition array.hpp:20
const std::string & name(const NeoN::Document &doc)
Retrieves the name of a Document.