MNX Document Model
Loading...
Searching...
No Matches
mnx::Document Class Reference

Represents an MNX document and provides methods for loading and saving. More...

#include <Document.h>

+ Inheritance diagram for mnx::Document:

Public Member Functions

 Document ()
 Constructs an empty MNX document. The resulting instance contains all required fields and should validate against the schema.
 
 Document (const std::shared_ptr< json > &root)
 Wrap a document around a root element.
 
 Document (const Document &src)
 Copy constructor that zaps the id mapping, if any.
 
 Document (std::istream &inputStream)
 Constructs a Document from an input stream.
 
 MNX_REQUIRED_CHILD (Global, global)
 Global data for the MNX document.
 
 MNX_REQUIRED_CHILD (MnxMetaData, mnx)
 Metadata for the MNX document.
 
 MNX_OPTIONAL_CHILD (Array< Layout >, layouts)
 List of layouts for the MNX document.
 
 MNX_REQUIRED_CHILD (Array< Part >, parts)
 List of parts for the MNX document.
 
 MNX_OPTIONAL_CHILD (Array< Score >, scores)
 List of scores for the MNX document.
 
void save (const std::filesystem::path &outputPath, std::optional< int > indentSpaces) const
 Saves the MNX document to a file.
 
void buildIdMapping (const std::optional< ErrorHandler > &errorHandler=std::nullopt)
 Builds or rebuilds the ID mapping for the document, replacing any existing mapping.
 
const util::IdMappinggetIdMapping () const
 Gets a reference to the ID mapping instance for the document.
 
bool hasIdMapping () const
 Returns whether am ID mapping currently exists.
 
const std::shared_ptr< json > & root () const
 Returns the root.
 
- Public Member Functions inherited from mnx::Object
 Object (const std::shared_ptr< json > &root, json_pointer pointer)
 Wraps an Object class around an existing JSON object node.
 
 Object (Base &parent, const std::string_view &key)
 Creates a new Object class as a child of a JSON node.
 
- Public Member Functions inherited from mnx::Base
 Base (const Base &src)
 Copy constructor.
 
 Base (Base &&src) noexcept
 Move constructor.
 
Baseoperator= (const Base &src)
 Copy assignment operator.
 
Baseoperator= (Base &&src)
 Move assignment operator.
 
std::string dump (int indents=-1)
 Dumps the branch to a string. Useful in debugging.
 
template<typename T >
parent () const
 Returns the parent object for this node.
 
template<typename T >
std::optional< T > getEnclosingElement () const
 Returns the enclosing array element for this instance.
 
json_pointer pointer () const
 Returns the json_pointer for this node.
 

Static Public Member Functions

static Document create (const std::filesystem::path &inputPath)
 Creates a Document from a JSON file.
 

Additional Inherited Members

- Protected Member Functions inherited from mnx::Base
jsonref () const
 Convert this node for retrieval.
 
jsonref ()
 Access the JSON node for modification.
 
const std::shared_ptr< json > & root () const
 Returns the root.
 
 Base (const std::shared_ptr< json > &root, json_pointer pointer)
 Wrap a Base instance around a specific JSON reference using a json_pointer.
 
 Base (json &&jsonRef, Base &parent, const std::string_view &key)
 Construct a Base reference as a child inside a parent node.
 
template<typename T >
getChild (const std::string_view &key) const
 Retrieves and validates a required child node.
 
template<typename T >
setChild (const std::string_view &key, const T &value)
 Sets a child node.
 
template<typename T >
std::optional< T > getOptionalChild (const std::string_view &key) const
 Retrieves an optional child node.
 

Detailed Description

Represents an MNX document and provides methods for loading and saving.

Constructor & Destructor Documentation

◆ Document() [1/2]

mnx::Document::Document ( const std::shared_ptr< json > &  root)
inline

Wrap a document around a root element.

Parameters
root

◆ Document() [2/2]

mnx::Document::Document ( std::istream &  inputStream)
inline

Constructs a Document from an input stream.

Parameters
inputStreamThe input stream containing the MNX JSON data.

Member Function Documentation

◆ buildIdMapping()

void mnx::Document::buildIdMapping ( const std::optional< ErrorHandler > &  errorHandler = std::nullopt)

Builds or rebuilds the ID mapping for the document, replacing any existing mapping.

Parameters
errorHandlerAn optional error handler. If provided, the function does not throw on duplicate keys added.
Exceptions
util::mapping_erroron duplicate keys if no errorHandler is provided.

◆ create()

static Document mnx::Document::create ( const std::filesystem::path &  inputPath)
inlinestatic

Creates a Document from a JSON file.

Parameters
inputPathThe path to the JSON file.
Returns
A Document instance populated with the parsed data.
Exceptions
std::runtime_errorif the file cannot be opened.

◆ save()

void mnx::Document::save ( const std::filesystem::path &  outputPath,
std::optional< int >  indentSpaces 
) const
inline

Saves the MNX document to a file.

Parameters
outputPathThe file path to save the document.
indentSpacesOptional number of spaces for indentation; if not provided, no indentation is applied.
Exceptions
std::ios_base::failureif file operations fail.