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

Base class wrapper for all MNX JSON nodes. More...

#include <BaseTypes.h>

+ Inheritance diagram for mnx::Base:

Public Member Functions

 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.
 

Protected Member Functions

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.
 

Friends

class validation::SemanticValidator
 

Detailed Description

Base class wrapper for all MNX JSON nodes.

Constructor & Destructor Documentation

◆ Base() [1/2]

mnx::Base::Base ( const std::shared_ptr< json > &  root,
json_pointer  pointer 
)
inlineprotected

Wrap a Base instance around a specific JSON reference using a json_pointer.

Parameters
rootReference to the root JSON object.
pointerJSON pointer to the specific node.

◆ Base() [2/2]

mnx::Base::Base ( json &&  jsonRef,
Base parent,
const std::string_view &  key 
)
inlineprotected

Construct a Base reference as a child inside a parent node.

Parameters
jsonRefRvalue reference to a new JSON object or array.
parentReference to the parent instance.
keyThe key under which the new node is stored.

Member Function Documentation

◆ dump()

std::string mnx::Base::dump ( int  indents = -1)
inline

Dumps the branch to a string. Useful in debugging.

Parameters
indentsNumber of indents or -1 for no indents

◆ getChild()

template<typename T >
T mnx::Base::getChild ( const std::string_view &  key) const
inlineprotected

Retrieves and validates a required child node.

Template Parameters
TThe expected MNX type (Object or Array<T>).
Parameters
keyThe key of the child node.
Returns
An instance of the requested type.
Exceptions
std::runtime_errorif the key is missing or the type is incorrect.

◆ getEnclosingElement()

template<typename T >
std::optional< T > mnx::Base::getEnclosingElement ( ) const

Returns the enclosing array element for this instance.

Template Parameters
TThe type to find. A limited list of types are supported, including Part and Sequence. Others may be added as needed.
Returns
the enclosing element, or std::nullopt if not found.

◆ getOptionalChild()

template<typename T >
std::optional< T > mnx::Base::getOptionalChild ( const std::string_view &  key) const
inlineprotected

Retrieves an optional child node.

Template Parameters
TThe expected MNX type (Object or Array<T>).
Parameters
keyThe key of the child node.
Returns
An std::optional<T>, or std::nullopt if the node does not exist or is invalid.
Exceptions
std::runtime_errorif the type is incorrect.

◆ parent()

template<typename T >
T mnx::Base::parent ( ) const
inline

Returns the parent object for this node.

Template Parameters
TThe type to create. Must correctly match whether it is an array or object.
Exceptions
std::invalid_argumentif the type of T does not match the type of the underlying pointer.

◆ ref() [1/2]

json & mnx::Base::ref ( )
inlineprotected

Access the JSON node for modification.

Returns
A reference to the JSON node.

◆ ref() [2/2]

json & mnx::Base::ref ( ) const
inlineprotected

Convert this node for retrieval.

Returns
A reference to the JSON node.

◆ setChild()

template<typename T >
T mnx::Base::setChild ( const std::string_view &  key,
const T &  value 
)
inlineprotected

Sets a child node.

Template Parameters
TThe expected MNX type (Object or Array<T>).
Parameters
keyThe key of the child node.
valueThe value to set.
Returns
The newly created child.