NeoFOAM
WIP Prototype of a modern OpenFOAM core
Loading...
Searching...
No Matches
document.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 <string>
7#include <functional>
8#include <atomic>
9
11
12namespace NeoFOAM
13{
14
30using DocumentValidator = std::function<bool(Dictionary)>;
31
40bool hasId(Dictionary doc);
41
42
50class Document : public Dictionary
51{
52public:
53
58
65 Document(const Dictionary& dict, DocumentValidator validator = hasId);
66
75 bool validate() const;
76
82 std::string id() const { return get<std::string>("id"); }
83
84private:
85
91 static std::string generateID()
92 {
93 static std::atomic<int> counter {0};
94 return "doc_" + std::to_string(counter++);
95 }
96 std::string id_;
97 DocumentValidator validator_;
98};
99
109const std::string& name(const NeoFOAM::Document& doc);
110
120std::string& name(NeoFOAM::Document& doc);
121
122} // namespace NeoFOAM
A class representing a dictionary that stores key-value pairs.
A class representing a document in a database.
Definition document.hpp:51
Document(const Dictionary &dict, DocumentValidator validator=hasId)
Constructs a Document with the given Dictionary and validator.
bool validate() const
Validates the Document.
std::string id() const
Retrieves the ID of the Document.
Definition document.hpp:82
Document()
Constructs a Document with a unique ID.
bool hasId(Dictionary doc)
Checks if a Dictionary object has an "id" key.
const std::string & name(const NeoFOAM::Document &doc)
Retrieves the name of a Document.
std::function< bool(Dictionary)> DocumentValidator
A type alias for a function that validates a Dictionary object.
Definition document.hpp:30