MNX Document Model
Loading...
Searching...
No Matches
Document.h
1/*
2 * Copyright (C) 2025, Robert Patterson
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 * THE SOFTWARE.
21 */
22#pragma once
23
24#include <filesystem>
25#include <fstream>
26
27#include "BaseTypes.h"
28#include "Global.h"
29#include "Layout.h"
30#include "Part.h"
31#include "Score.h"
32
33namespace mnx {
34
35namespace util {
36class IdMapping;
37}
38
43class MnxMetaData : public Object
44{
45public:
46 using Object::Object;
47
51 MnxMetaData(Base& parent, const std::string_view& key)
52 : Object(parent, key)
53 {
54 // required children
55 set_version(MNX_VERSION);
56 }
57
62 class Support : public Object
63 {
64 public:
65 using Object::Object;
66
67 MNX_OPTIONAL_PROPERTY(bool, useAccidentalDisplay);
68 MNX_OPTIONAL_PROPERTY(bool, useBeams);
69 };
70
74 MNX_REQUIRED_PROPERTY(int, version);
75
80};
81
86class Document : public Object
87{
88private:
90 std::shared_ptr<util::IdMapping> m_idMapping;
91
92public:
97 Document() : Object(std::make_shared<json>(json::object()), json_pointer{})
98 {
99 // create required children
100 create_mnx();
101 create_global();
102 create_parts();
103 }
104
107 Document(const std::shared_ptr<json>& root) : Object(root, json_pointer{}) {}
108
110 Document(const Document& src) : Object(src), m_idMapping(nullptr) {}
111
112 using Base::root;
113
118 Document(std::istream& inputStream) : Object(std::make_shared<json>(json::object()), json_pointer{})
119 {
120 inputStream >> *root();
121 }
122
128
135 static Document create(const std::filesystem::path& inputPath)
136 {
137 std::ifstream jsonFile;
138 jsonFile.exceptions(std::ios::failbit | std::ios::badbit);
139 jsonFile.open(inputPath);
140 if (!jsonFile.is_open()) {
141 throw std::runtime_error("Unable to open JSON file: " + inputPath.string());
142 }
143 return Document(jsonFile);
144 }
145
152 void save(const std::filesystem::path& outputPath, std::optional<int> indentSpaces) const
153 {
154 std::ofstream file;
155 file.exceptions(std::ofstream::failbit | std::ofstream::badbit);
156 file.open(outputPath, std::ios::out | std::ios::binary);
157 if (!file.is_open()) {
158 throw std::runtime_error("Unable to write to JSON file: " + outputPath.string());
159 }
160 file << root()->dump(indentSpaces.value_or(-1));
161 file.close();
162 }
163
167 void buildIdMapping(const std::optional<ErrorHandler>& errorHandler = std::nullopt);
168
171 {
172 MNX_ASSERT_IF(!m_idMapping) {
173 throw std::logic_error("Call buildIdMapping before calling getIdMapping.");
174 }
175 return *m_idMapping;
176 }
177
179 bool hasIdMapping() const { return static_cast<bool>(m_idMapping); }
180};
181
182static_assert(std::is_move_constructible<mnx::Document>::value, "Document must be move constructible");
183
184} // namespace mnx
Represents an MNX array, encapsulating property access.
Definition BaseTypes.h:483
Base class wrapper for all MNX JSON nodes.
Definition BaseTypes.h:210
T parent() const
Returns the parent object for this node.
Definition BaseTypes.h:258
const std::shared_ptr< json > & root() const
Returns the root.
Definition BaseTypes.h:288
Represents an MNX document and provides methods for loading and saving.
Definition Document.h:87
void save(const std::filesystem::path &outputPath, std::optional< int > indentSpaces) const
Saves the MNX document to a file.
Definition Document.h:152
const util::IdMapping & getIdMapping() const
Gets a reference to the ID mapping instance for the document.
Definition Document.h:170
MNX_OPTIONAL_CHILD(Array< Layout >, layouts)
List of layouts for the MNX document.
MNX_OPTIONAL_CHILD(Array< Score >, scores)
List of scores for the MNX document.
MNX_REQUIRED_CHILD(Array< Part >, parts)
List of parts for the MNX document.
void buildIdMapping(const std::optional< ErrorHandler > &errorHandler=std::nullopt)
Builds or rebuilds the ID mapping for the document, replacing any existing mapping.
Definition Implementations.cpp:75
static Document create(const std::filesystem::path &inputPath)
Creates a Document from a JSON file.
Definition Document.h:135
Document(const Document &src)
Copy constructor that zaps the id mapping, if any.
Definition Document.h:110
Document()
Constructs an empty MNX document. The resulting instance contains all required fields and should vali...
Definition Document.h:97
Document(const std::shared_ptr< json > &root)
Wrap a document around a root element.
Definition Document.h:107
Document(std::istream &inputStream)
Constructs a Document from an input stream.
Definition Document.h:118
bool hasIdMapping() const
Returns whether am ID mapping currently exists.
Definition Document.h:179
MNX_REQUIRED_CHILD(MnxMetaData, mnx)
Metadata for the MNX document.
const std::shared_ptr< json > & root() const
Returns the root.
Definition BaseTypes.h:288
MNX_REQUIRED_CHILD(Global, global)
Global data for the MNX document.
Represents the global section of an MNX document, containing global measures.
Definition Global.h:321
Represents optional support metadata within an MNX document.
Definition Document.h:63
MNX_OPTIONAL_PROPERTY(bool, useAccidentalDisplay)
Optional property indicating whether accidental display is used.
MNX_OPTIONAL_PROPERTY(bool, useBeams)
Optional property that indicates if beams are encoded.
Represents metadata for an MNX document.
Definition Document.h:44
MNX_OPTIONAL_CHILD(Support, support)
Optional child containing support metadata.
MnxMetaData(Base &parent, const std::string_view &key)
Creates a new MnxMetaData class as a child of a JSON element.
Definition Document.h:51
MNX_REQUIRED_PROPERTY(int, version)
Required property indicating the version of the MNX document.
Represents an MNX object, encapsulating property access.
Definition BaseTypes.h:420
Object(const std::shared_ptr< json > &root, json_pointer pointer)
Wraps an Object class around an existing JSON object node.
Definition BaseTypes.h:425
Provides type-safe ID-based lookup for elements in an MNX document.
Definition IdMapping.h:54
object model for MNX format
json::json_pointer json_pointer
JSON pointer class for MNX.
Definition BaseTypes.h:197
constexpr int MNX_VERSION
The MNX version for files generated by the DOM.
Definition BaseTypes.h:194
nlohmann::ordered_json json
JSON class for MNX.
Definition BaseTypes.h:196