NeoN
A framework for CFD software
Loading...
Searching...
No Matches
fieldDatabase.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2025 - 2026 NeoN authors
2//
3// SPDX-License-Identifier: MIT
4
5#pragma once
6
7#include <optional>
8#include <string>
9#include <stdexcept>
10
12
14{
15
25{
26public:
27
28 FieldDatabaseMixin() = default;
29
30 FieldDatabaseMixin(Database& db, std::string keyIn, std::string collectionName)
31 : key(std::move(keyIn)), fieldCollectionName(std::move(collectionName)), db_(&db)
32 {}
33
34 bool hasDatabase() const { return db_.has_value(); }
35
37 {
38 if (!db_.has_value())
39 {
40 throw std::runtime_error {
41 "Database not set: make sure the field is registered in the database"
42 };
43 }
44 return *db_.value();
45 }
46
47 const Database& db() const
48 {
49 if (!db_.has_value())
50 {
51 throw std::runtime_error {
52 "Database not set: make sure the field is registered in the database"
53 };
54 }
55 return *db_.value();
56 }
57
58 /* @brief tests whether the given object has been registered in a database */
59 bool registered() const
60 {
61 return !key.empty() && !fieldCollectionName.empty() && db_.has_value();
62 }
63
64 // Keep names to match your existing API
65 std::string key; // key in DB
66 std::string fieldCollectionName; // collection name in DB
67
68protected:
69
70 std::optional<Database*> db_;
71};
72
73} // namespace NeoN::finiteVolume::cellCentred
Mixin for database registration for fields (volume/surface).
FieldDatabaseMixin(Database &db, std::string keyIn, std::string collectionName)