NeoN
A framework for CFD software
Loading...
Searching...
No Matches
document.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2024 - 2026 NeoN authors
2//
3// SPDX-License-Identifier: MIT
4
5#pragma once
6
7#include <string>
8#include <functional>
9#include <atomic>
10
12
13namespace NeoN
14{
15
31using DocumentValidator = std::function<bool(const Dictionary&)>;
32
41bool hasId(const Dictionary& doc);
42
43
51class Document : public Dictionary
52{
53public:
54
59
66 Document(const Dictionary& dict, DocumentValidator validator = hasId);
67
75
84 bool validate() const;
85
91 std::string id() const { return get<std::string>("id"); }
92
93private:
94
100 static std::string generateID()
101 {
102 static std::atomic<int> counter {0};
103 return "doc_" + std::to_string(counter++);
104 }
105 std::string id_;
106 DocumentValidator validator_;
107};
108
118const std::string& name(const NeoN::Document& doc);
119
129std::string& name(NeoN::Document& doc);
130
131} // namespace NeoN
A class representing a dictionary that stores key-value pairs.
T & get(const std::string &key)
Retrieves the value associated with the given key, casting it to the specified type.
Dictionary()=default
A class representing a document in a database.
Definition document.hpp:52
Document()
Constructs a Document with a unique ID.
bool validate() const
Validates the Document.
Document(Dictionary &&dict, DocumentValidator validator=hasId)
Constructs a Document from the given Dictionary, moving its entries.
std::string id() const
Retrieves the ID of the Document.
Definition document.hpp:91
Document(const Dictionary &dict, DocumentValidator validator=hasId)
Constructs a Document with the given Dictionary and validator.
Integer types used throughout NeoN.
Definition array.hpp:18
std::function< bool(const Dictionary &)> DocumentValidator
A type alias for a function that validates a Dictionary object.
Definition document.hpp:31
const std::string & name(const NeoN::Document &doc)
Retrieves the name of a Document.
bool hasId(const Dictionary &doc)
Checks if a Dictionary object has an "id" key.