6#include <unordered_map>
38 template<
typename CollectionType>
40 : impl_(std::make_unique<CollectionModel<CollectionType>>(std::move(collection)))
86 std::vector<std::string>
find(
const std::function<
bool(
const Document&)>& predicate)
const;
130 template<
typename CollectionType>
133 auto derived =
dynamic_cast<CollectionModel<CollectionType>*
>(impl_.get());
136 throw std::bad_cast();
138 return derived->collection_;
149 template<
typename CollectionType>
150 const CollectionType&
as()
const
152 auto derived =
dynamic_cast<CollectionModel<CollectionType>*
>(impl_.get());
155 throw std::bad_cast();
157 return derived->collection_;
162 struct CollectionConcept
164 virtual ~CollectionConcept() =
default;
165 virtual Document& doc(
const std::string&
id) = 0;
166 virtual const Document& doc(
const std::string&
id)
const = 0;
167 virtual std::vector<std::string> find(
const std::function<
bool(
const Document&)>& predicate
169 virtual size_t size()
const = 0;
170 virtual std::string type()
const = 0;
171 virtual std::string
name()
const = 0;
173 virtual const Database& db()
const = 0;
176 template<
typename CollectionType>
177 struct CollectionModel : CollectionConcept
179 CollectionModel(CollectionType collection) : collection_(std::move(collection)) {}
181 Document& doc(
const std::string&
id)
override {
return collection_.doc(
id); }
183 const Document& doc(
const std::string&
id)
const override {
return collection_.doc(
id); }
185 std::vector<std::string> find(
const std::function<
bool(
const Document&)>& predicate
188 return collection_.find(predicate);
191 size_t size()
const override {
return collection_.size(); }
193 std::string type()
const override {
return collection_.type(); }
195 std::string
name()
const override {
return collection_.name(); }
197 Database& db()
override {
return collection_.db(); }
199 const Database& db()
const override {
return collection_.db(); }
201 CollectionType collection_;
204 std::unique_ptr<CollectionConcept> impl_;
213template<
typename DocumentType>
269 std::vector<std::string>
find(
const std::function<
bool(
const Document&)>& predicate)
const
271 std::vector<std::string> result;
272 for (
const auto& [key,
doc] :
docs_)
274 if (predicate(
doc.doc()))
276 result.push_back(
doc.
id());
315 std::string
type()
const {
return DocumentType::typeName(); }
324 std::vector<std::string> result;
325 for (
const auto& [key,
doc] :
docs_)
327 result.push_back(key);
329 std::sort(result.begin(), result.end());
335 std::unordered_map<std::string, DocumentType>
docs_;
A mixin class for collection of documents in a database to simplify the implementation of common oper...
std::unordered_map< std::string, DocumentType > docs_
The map of document IDs to documents.
const std::string & name() const
Gets the name of the collection.
Document & doc(const std::string &id)
Retrieves a document by its ID.
CollectionMixin(CollectionMixin &&)=default
CollectionMixin & operator=(CollectionMixin &&)=delete
CollectionMixin(const CollectionMixin &)=delete
std::size_t size() const
Gets the number of documents in the collection.
std::vector< std::string > find(const std::function< bool(const Document &)> &predicate) const
Finds documents that match a given predicate.
std::string type() const
Gets the type name of the documents in the collection.
const NeoFOAM::Database & db() const
Gets a const reference to the database.
NeoFOAM::Database & db_
The reference to the database.
CollectionMixin(NeoFOAM::Database &db, std::string name)
Constructs a CollectionMixin with the given database and collection name.
std::string name_
The name of the collection.
CollectionMixin & operator=(const CollectionMixin &)=delete
std::vector< std::string > sortedKeys() const
Gets the sorted keys of the documents in the collection.
NeoFOAM::Database & db()
Gets a reference to the database.
const Document & doc(const std::string &id) const
Retrieves a document by its ID (const version).
A type-erased interface collection types.
std::vector< std::string > find(const std::function< bool(const Document &)> &predicate) const
Finds documents that match a given predicate.
const Document & doc(const std::string &id) const
Retrieves a document by its ID (const version).
Collection & operator=(const Collection &other)=delete
A Collection is not copyable, only moveable.
const Database & db() const
Returns a const reference to the database containing the collection.
Collection(const Collection &other)=delete
A Collection is not copyable, only moveable.
std::string type() const
Returns the type of the collection.
const CollectionType & as() const
Casts the collection to a specific collection type (const version).
Collection(Collection &&other)=default
A Collection is moveable, but not copyable.
Document & doc(const std::string &id)
Retrieves a document by its ID.
std::string name() const
Returns the name of the collection.
size_t size() const
Returns the number of documents in the collection.
Database & db()
Returns a reference to the database containing the collection.
Collection(CollectionType collection)
Constructs a Collection from a specific collection type.
Collection & operator=(Collection &&other)=default
A Collection is moveable, but not copyable.
CollectionType & as()
Casts the collection to a specific collection type.
A class representing a document in a database.
std::string id() const
Retrieves the ID of the Document.
const std::string & name(const NeoFOAM::Document &doc)
Retrieves the name of a Document.