NeoFOAM
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 NeoFOAM authors
3
4#pragma once
5
6#include <unordered_map>
7#include <string>
8#include <memory>
9
11
12namespace NeoFOAM
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
118template<typename Type>
119void validateRegistration(const Type& obj, const std::string errorMessage)
120{
121 if (!obj.registered())
122 {
123 throw std::runtime_error(errorMessage);
124 }
125}
126
127} // namespace NeoFOAM
A type-erased interface collection types.
CollectionType & as()
Casts the collection to a specific collection type.
CollectionType & at(const std::string &name)
Retrieves a collection by its name and casts it to the specified type.
Definition database.hpp:82
Collection & at(const std::string &name)
Retrieves a collection by its name.
std::size_t size() const
Returns the size of the database.
bool contains(const std::string &name) const
Checks if the database contains an collection with the specified name.
const Collection & at(const std::string &name) const
Retrieves a collection by its name (const version).
Collection & insert(const std::string &key, Collection &&col)
Inserts a collection into the database with the specified key.
bool remove(const std::string &name)
Removes a collection from the database.
void validateRegistration(const Type &obj, const std::string errorMessage)
Validates that a field is registered in the database.
Definition database.hpp:119
const std::string & name(const NeoFOAM::Document &doc)
Retrieves the name of a Document.