SlideIO 2.0.0
Open-source library for reading of medical images
Loading...
Searching...
No Matches
metadata.hpp
1// This file is part of slideio project.
2// It is subject to the license terms in the LICENSE file found in the top-level directory
3// of this distribution and at http://slideio.com/license.html.
4#pragma once
5
6#include "slideio/core/slideio_core_def.hpp"
7#include <cstdint>
8#include <memory>
9#include <string>
10#include <vector>
11
12#if defined(_MSC_VER)
13#pragma warning( push )
14#pragma warning(disable: 4251)
15#endif
16
17namespace slideio
18{
25 class SLIDEIO_CORE_EXPORTS Metadata
26 {
27 public:
28 enum class Type { Null, Bool, Int, Double, String, Array, Object };
29
30 Metadata(); // Null node
31 ~Metadata();
32 Metadata(const Metadata&);
33 Metadata(Metadata&&) noexcept;
34 Metadata& operator=(const Metadata&);
35 Metadata& operator=(Metadata&&) noexcept;
36
37 Type type() const;
38 bool isNull() const { return type() == Type::Null; }
39 bool isObject() const { return type() == Type::Object; }
40 bool isArray() const { return type() == Type::Array; }
41
42 bool asBool() const;
43 int64_t asInt() const;
44 double asDouble() const;
45 std::string asString() const;
46
47 size_t size() const;
48 bool contains(const std::string& key) const;
49 Metadata operator[](const std::string& key) const;
50 Metadata operator[](size_t index) const;
51 Metadata find(const std::string& jsonPointer) const;
52 std::vector<std::string> keys() const;
53
54 std::string toJson(int indent = -1) const;
55
56 struct Impl;
57 static Metadata fromImpl(std::shared_ptr<const Impl> impl);
58
59 private:
60 explicit Metadata(std::shared_ptr<const Impl> impl);
61 std::shared_ptr<const Impl> m_impl;
62 };
63
64 class SLIDEIO_CORE_EXPORTS MetadataBuilder
65 {
66 public:
67 MetadataBuilder();
68 ~MetadataBuilder();
69 MetadataBuilder(const MetadataBuilder&);
70 MetadataBuilder(MetadataBuilder&&) noexcept;
71 MetadataBuilder& operator=(const MetadataBuilder&);
72 MetadataBuilder& operator=(MetadataBuilder&&) noexcept;
73
74 // Leaf assignment — replaces the current node with a scalar value.
75 void set(const std::string& value);
76 void set(bool value);
77 void set(int64_t value);
78 void set(double value);
79 void set(const char* value);
80
81 // Navigation. Returns a sub-builder sharing root storage with this one.
82 // Auto-creates and coerces: operator[](key) on a Null node turns it into
83 // an Object; on an Object node it returns a sub-view (auto-creating the
84 // key with Null if absent). Throws slideio::RuntimeError on type mismatch
85 // (e.g. operator[](key) on an Array or scalar node).
86 MetadataBuilder operator[](const std::string& key);
87
88 // operator[](index) on a Null node coerces to Array and grows to
89 // index+1 (new slots default to empty Objects, not Null, so the
90 // common pattern b[i][key].set(v) works without intermediate
91 // makeObject() on the new slot). Throws on type mismatch
92 // (e.g. operator[](index) on an Object or scalar node).
93 MetadataBuilder operator[](size_t index);
94
95 // Ensures the current node is an Object. Idempotent if already
96 // an Object (content preserved); replaces a scalar/Null/Array otherwise.
97 void makeObject();
98
99 // Ensures the current node is an Array. Idempotent if already
100 // an Array (content preserved); replaces a scalar/Null/Object otherwise.
101 void makeArray();
102
103 // Inspection.
104 bool isNull() const;
105 bool isObject() const;
106 bool isArray() const;
107 size_t size() const;
108
109 // Snapshot the current state into an immutable Metadata.
110 Metadata freeze() const;
111
112 struct Impl;
113 static MetadataBuilder fromImpl(std::shared_ptr<Impl> impl);
114
115 private:
116 explicit MetadataBuilder(std::shared_ptr<Impl> impl);
117 std::shared_ptr<Impl> m_impl;
118 };
119}
120
121#if defined(_MSC_VER)
122#pragma warning( pop )
123#endif
Read-only tree view over slide/scene metadata.
Definition: metadata.hpp:26
Definition: exceptions.hpp:15