NeoN
WIP Prototype of a modern OpenFOAM core
Loading...
Searching...
No Matches
database.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// SPDX-FileCopyrightText: 2024 NeoN authors
3
4#pragma once
5
6#include <unordered_map>
7#include <string>
8#include <memory>
9
11
12namespace NeoN
13{
14
15
17{
18public:
19
27 Collection& insert(const std::string& key, Collection&& col);
28
35 bool contains(const std::string& name) const;
36
45 bool remove(const std::string& name);
46
57 Collection& at(const std::string& name);
58
69 const Collection& at(const std::string& name) const;
70
81 template<typename CollectionType = Collection>
82 CollectionType& at(const std::string& name)
83 {
84 Collection& collection = at(name);
85 return collection.as<CollectionType>();
86 }
87
88
96 std::size_t size() const;
97
98
99private:
100
107 std::unordered_map<std::string, Collection> collections_;
108};
109
110
119template<typename Type>
120void validateRegistration(const Type& field, const std::string& errorMessage)
121{
122 if (!field.registered())
123 {
124 throw std::runtime_error {errorMessage};
125 }
126}
127
128} // namespace NeoN
A type-erased interface collection types.
CollectionType & as()
Casts the collection to a specific collection type.
bool contains(const std::string &name) const
Checks if the database contains an collection with the specified name.
Collection & at(const std::string &name)
Retrieves a collection by its name.
const Collection & at(const std::string &name) const
Retrieves a collection by its name (const version).
CollectionType & at(const std::string &name)
Retrieves a collection by its name and casts it to the specified type.
Definition database.hpp:82
Collection & insert(const std::string &key, Collection &&col)
Inserts a collection into the database with the specified key.
std::size_t size() const
Returns the size of the database.
bool remove(const std::string &name)
Removes a collection from the database.
void validateRegistration(const Type &field, const std::string &errorMessage)
Validates that a field is registered in the database.
Definition database.hpp:120
const std::string & name(const NeoN::Document &doc)
Retrieves the name of a Document.