Volume Cartographer 2.27.0
Metadata.hpp
Go to the documentation of this file.
1#pragma once
2
5#include <optional>
6
7#include <nlohmann/json.hpp>
8
10#include "vc/core/util/Json.hpp"
11
12namespace volcart
13{
27{
28
29public:
32 Metadata() = default;
33
39 explicit Metadata(const filesystem::path& fileLocation);
44 [[nodiscard]] auto path() const -> filesystem::path;
45
47 void setPath(const filesystem::path& path);
48
54 void save() const;
55
57 void save(const filesystem::path& path) const;
62 [[nodiscard]] auto hasKey(const std::string& key) const -> bool;
63
78 template <typename T>
79 auto get(const std::string& key) const -> std::optional<T>
80 {
81 if (not json_.contains(key)) {
82 const auto msg = "could not find key '" + key + "' in metadata";
83 throw std::runtime_error(msg);
84 }
85
86 if (json_[key].is_null()) {
87 return std::optional<T>();
88 }
89
90 return json_[key].get<T>();
91 }
92
98 template <typename T>
99 void set(const std::string& key, T value)
100 {
101 json_[key] = value;
102 }
103
110 void printObject() const;
112protected:
114 nlohmann::json json_{};
116 filesystem::path path_;
117};
118} // namespace volcart
Generic interface for storing metadata as key/value pairs.
Definition: Metadata.hpp:27
Metadata()=default
Default constructor.
void set(const std::string &key, T value)
Set a metadata key and value.
Definition: Metadata.hpp:99
auto hasKey(const std::string &key) const -> bool
Return whether the given key is defined.
auto get(const std::string &key) const -> std::optional< T >
Get a metadata value by key.
Definition: Metadata.hpp:79
filesystem::path path_
Definition: Metadata.hpp:116
void setPath(const filesystem::path &path)
Set the path where the metadata file will be written.
auto path() const -> filesystem::path
Get the path where the metadata file will be written.
nlohmann::json json_
Definition: Metadata.hpp:114
Metadata(const filesystem::path &fileLocation)
Read a metadata file from disk.
void printObject() const
Print an object representation of the metadata to std::cout.
void save() const
Save the metadata file to the stored path.
Volume Cartographer library