25#include <unordered_map>
31#include "nlohmann/json.hpp"
44#define MNX_REQUIRED_PROPERTY(TYPE, NAME) \
46 if (!ref().contains(#NAME)) { \
47 throw std::runtime_error("Missing required property: " #NAME); \
49 return ref()[#NAME].get<TYPE>(); \
51 void set_##NAME(const TYPE& value) { ref()[#NAME] = value; } \
52 static_assert(true, "")
66 #define MNX_ARRAY_ELEMENT_PROPERTY(TYPE, NAME, INDEX) \
67 static_assert(std::is_integral_v<decltype(INDEX)>, "array index must be an integer type"); \
68 TYPE NAME() const { return (*this)[INDEX]; } \
69 void set_##NAME(const TYPE& value) { (*this)[INDEX] = value; } \
70 static_assert(true, "")
88 #define MNX_OPTIONAL_NAMED_PROPERTY(TYPE, NAME, KEY) \
89 std::optional<TYPE> NAME() const { \
90 return ref().contains(KEY) ? std::optional<TYPE>(ref()[KEY].get<TYPE>()) : std::nullopt; \
92 TYPE NAME##_or(const TYPE& defaultVal) const { \
93 return ref().contains(KEY) ? ref()[KEY].get<TYPE>() : defaultVal; \
95 void set_##NAME(const TYPE& value) { ref()[KEY] = value; } \
96 void clear_##NAME() { ref().erase(KEY); } \
97 static_assert(true, "")
112 #define MNX_OPTIONAL_PROPERTY(TYPE, NAME) MNX_OPTIONAL_NAMED_PROPERTY(TYPE, NAME, #NAME)
129#define MNX_OPTIONAL_PROPERTY_WITH_DEFAULT(TYPE, NAME, DEFAULT) \
130 TYPE NAME() const { \
131 return ref().contains(#NAME) ? ref()[#NAME].get<TYPE>() : DEFAULT; \
133 void set_##NAME(const TYPE& value) { ref()[#NAME] = value; } \
134 void clear_##NAME() { ref().erase(#NAME); } \
135 void set_or_clear_##NAME(const TYPE& value) { \
136 if (value == DEFAULT) clear_##NAME(); \
137 else set_##NAME(value); \
139 static_assert(true, "")
152 #define MNX_REQUIRED_CHILD(TYPE, NAME) \
153 TYPE NAME() const { return getChild<TYPE>(#NAME); } \
154 template<typename... Args> \
155 TYPE create_##NAME(Args&&... args) { \
156 return setChild(#NAME, TYPE(*this, #NAME, std::forward<Args>(args)...)); \
158 static_assert(true, "")
172 #define MNX_OPTIONAL_CHILD(TYPE, NAME) \
173 std::optional<TYPE> NAME() const { return getOptionalChild<TYPE>(#NAME); } \
174 template<typename... Args> \
175 TYPE create_##NAME(Args&&... args) { \
176 if (auto child = getOptionalChild<TYPE>(#NAME)) return child.value(); \
177 return setChild(#NAME, TYPE(*this, #NAME, std::forward<Args>(args)...)); \
179 void clear_##NAME() { ref().erase(#NAME); } \
180 static_assert(true, "")
182#define MNX_ASSERT_IF(TEST) \
197using json = nlohmann::ordered_json;
201template <
typename T>
class Array;
203namespace validation {
204class SemanticValidator;
213 virtual ~Base() =
default;
216 Base(
const Base& src) : m_root(src.m_root), m_pointer(src.m_pointer)
221 m_pointer(std::move(src.m_pointer))
228 if (m_root != src.m_root) {
229 throw std::logic_error(
"Assignment from a different JSON document is not allowed.");
231 m_pointer = src.m_pointer;
240 if (m_root != src.m_root) {
241 throw std::logic_error(
"Assignment from a different JSON document is not allowed.");
243 m_pointer = std::move(src.m_pointer);
250 std::string
dump(
int indents = -1)
252 return ref().dump(indents);
258 template <
typename T>
261 static_assert(std::is_base_of_v<Base, T>,
"Template type mush be derived from Base.");
262 return T(m_root, m_pointer.parent_pointer());
268 template <
typename T>
280 json&
ref()
const {
return resolve_pointer(); }
289 const std::shared_ptr<json>&
root()
const {
return m_root; }
306 : m_root(
parent.m_root), m_pointer(
parent.m_pointer / std::string(key))
308 (*m_root)[m_pointer] = std::move(jsonRef);
318 template <
typename T>
321 static_assert(std::is_base_of_v<Base, T>,
"template type must be derived from Base");
323 json_pointer childPointer = m_pointer / std::string(key);
324 if (!checkKeyIsValid<T>(childPointer)) {
325 throw std::runtime_error(
"Missing required child node: " + std::string(key));
328 return T(m_root, childPointer);
338 template <
typename T>
339 T
setChild(
const std::string_view& key,
const T& value)
341 static_assert(std::is_base_of_v<Base, T>,
"template type must be derived from Base");
343 json_pointer childPointer = m_pointer / std::string(key);
344 (*m_root)[childPointer] = value.ref();
345 return T(m_root, childPointer);
355 template <
typename T>
358 static_assert(std::is_base_of_v<Base, T>,
"template type must be derived from Base");
360 json_pointer childPointer = m_pointer / std::string(key);
361 if (!checkKeyIsValid<T>(childPointer)) {
365 return T(m_root, childPointer);
376 template <
typename T>
379 if (!(*m_root).contains(
pointer)) {
385 if constexpr (std::is_base_of_v<Object, T>) {
386 if (!node.is_object()) {
387 throw std::runtime_error(
"Expected an object for: " +
pointer.to_string());
389 }
else if constexpr (std::is_base_of_v<Array<typename T::value_type>, T>) {
390 if (!node.is_array()) {
391 throw std::runtime_error(
"Expected an array for: " +
pointer.to_string());
403 json& resolve_pointer()
const
405 return (*m_root).at(m_pointer);
408 const std::shared_ptr<json> m_root;
411 friend class validation::SemanticValidator;
428 if (!
ref().is_object()) {
429 throw std::invalid_argument(
"mnx::Object must wrap a JSON object.");
445template <
typename T, std::enable_if_t<!std::is_base_of_v<Base, T>,
int> = 0>
448 static_assert(std::is_arithmetic_v<T> || std::is_same_v<T, std::string>,
"This template is for simple JSON classes");
463 return ref().template get<T>();
477 return src ==
ref().template get<T>();
481class ArrayElementObject;
488 static_assert(std::is_arithmetic_v<T> || std::is_same_v<T, std::string> ||
489 std::is_base_of_v<ArrayElementObject, T>,
"Invalid MNX array element type.");
492 template<
typename ArrayType>
497 mutable size_t m_idx;
500 iter(ArrayType* ptr,
size_t idx) : m_ptr(ptr), m_idx(idx) {}
501 T operator*()
const {
return (*m_ptr)[m_idx]; }
502 iter& operator++() { ++m_idx;
return *
this; }
503 bool operator!=(
const iter& o)
const {
return m_idx != o.m_idx; }
518 if (!
ref().is_array()) {
519 throw std::invalid_argument(
"mnx::Array must wrap a JSON array.");
542 if constexpr (std::is_base_of_v<Base, T>) {
543 return getChild<T>(std::to_string(index));
545 return getChild<SimpleType<T>>(std::to_string(index));
553 if constexpr (std::is_base_of_v<Base, T>) {
554 return getChild<T>(std::to_string(index));
556 return getChild<SimpleType<T>>(std::to_string(index));
561 template <
typename U = T>
562 std::enable_if_t<!std::is_base_of_v<Base, U>,
void>
565 ref().push_back(value);
572 template <
typename U = T,
typename... Args,
573 std::enable_if_t<std::is_base_of_v<Base, U>,
int> = 0>
576 if constexpr (std::is_base_of_v<Object, U>) {
577 ref().push_back(json::object());
579 ref().push_back(json::array());
581 return U(*
this, std::to_string(
ref().
size() - 1), std::forward<Args>(args)...);
610 throw std::out_of_range(
"Index out of range");
626 return std::stoul(
pointer().back());
634 template <
typename ContainerType>
637 return parent<Array<ArrayElementObject>>().parent<ContainerType>();
649 using ArrayElementObject::ArrayElementObject;
654 template <
typename T, std::enable_if_t<std::is_base_of_v<ContentObject, T>,
int> = 0>
657 return getTypedObject<T>();
663 template <
typename T, std::enable_if_t<std::is_base_of_v<ContentObject, T>,
int> = 0>
664 T getTypedObject()
const
666 if (type() != T::ContentTypeValue) {
667 throw std::invalid_argument(
"Type mismatch: expected " + std::string(T::ContentTypeValue) +
673 friend class ContentArray;
707 using BaseArray::BaseArray;
710 template <
typename T, std::enable_if_t<std::is_base_of_v<ContentObject, T>,
int> = 0>
718 template <
typename T,
typename... Args,
719 std::enable_if_t<std::is_base_of_v<ContentObject, T>,
int> = 0>
722 auto result = BaseArray::append<T>(std::forward<Args>(args)...);
724 result.set_type(std::string(T::ContentTypeValue));
732 template <
typename T, std::enable_if_t<std::is_base_of_v<ContentObject, T>,
int> = 0>
733 T getTypedObject(
size_t index)
const
736 auto element = (*this)[index];
737 if (element.type() != T::ContentTypeValue) {
738 throw std::invalid_argument(
"Type mismatch: expected " + std::string(T::ContentTypeValue) +
739 ", got " + element.type());
741 return T(
root(),
pointer() / std::to_string(index));
749template <
typename E,
typename = std::enable_if_t<std::is_enum_v<E>>>
757 static const std::unordered_map<E, std::string> reverseMap = []() {
758 std::unordered_map<E, std::string> result;
760 result.emplace(element.second, element.first);
774 static_assert(std::is_arithmetic_v<T> || std::is_same_v<T, std::string> ||
775 std::is_base_of_v<ArrayElementObject, T>,
"Invalid MNX dictionary element type.");
778 template <
typename DictionaryType,
typename IteratorType>
781 using value_type = std::pair<const std::string, T>;
782 using difference_type = std::ptrdiff_t;
783 using iterator_category = std::forward_iterator_tag;
788 DictionaryType* m_ptr;
790 mutable std::unique_ptr<value_type> m_pair;
792 void update_pair()
const {
794 if (m_it != m_ptr->ref().end()) {
795 m_pair = std::make_unique<value_type>(m_it.key(), m_ptr->operator[](m_it.key()));
800 iter(DictionaryType* ptr, IteratorType it) : m_ptr(ptr), m_it(it)
803 reference operator*()
const {
return *m_pair.get(); }
804 pointer operator->()
const {
return m_pair.get(); }
806 iter& operator++() { ++m_it; update_pair();
return *
this; }
807 iter operator++(
int) { iter tmp = *
this; ++(*this);
return tmp; }
809 bool operator!=(
const iter& o)
const {
return m_it != o.m_it; }
810 bool operator==(
const iter& o)
const {
return m_it == o.m_it; }
846 if constexpr (std::is_base_of_v<Base, T>) {
847 return getChild<T>(key);
849 return getChild<SimpleType<T>>(key);
856 if constexpr (std::is_base_of_v<Base, T>) {
857 return getChild<T>(key);
859 return getChild<SimpleType<T>>(key);
864 template <
typename U = T>
865 std::enable_if_t<!std::is_base_of_v<Base, U>,
void>
866 emplace(
const std::string& key,
const U& value)
875 template <
typename U = T,
typename... Args,
876 std::enable_if_t<std::is_base_of_v<Base, U>,
int> = 0>
877 U
append(
const std::string& key, Args&&... args)
879 if constexpr (std::is_base_of_v<Object, U>) {
880 ref()[key] = json::object();
882 ref()[key] = json::array();
884 return U(*
this, key, std::forward<Args>(args)...);
896 auto find(
const std::string& key)
898 auto it =
ref().find(key);
905 auto find(
const std::string& key)
const
907 auto it =
ref().find(key);
931#ifndef DOXYGEN_SHOULD_IGNORE_THIS
938template<
typename EnumType>
939struct adl_serializer<EnumType, std::enable_if_t<std::is_enum_v<EnumType>>>
941 template<
typename BasicJsonType>
942 static EnumType from_json(
const BasicJsonType& j)
946 auto it = map.find(j.get<std::string>());
947 if (it != map.end()) {
954 template<
typename BasicJsonType>
955 static void to_json(BasicJsonType& j,
const EnumType& value)
958 auto it = map.find(value);
959 if (it == map.end()) {
971template<
typename BasicJsonType,
typename EnumType,
972 std::enable_if_t<std::is_enum<EnumType>::value,
int> = 0>
973inline void from_json(
const BasicJsonType& j, EnumType& value)
977 auto it = map.find(j.template get<std::string>());
978 if (it != map.end()) {
986template<
typename BasicJsonType,
typename EnumType,
987 std::enable_if_t<std::is_enum<EnumType>::value,
int> = 0>
988inline void to_json(BasicJsonType& j, EnumType value)
noexcept
991 auto it = map.find(value);
992 if (it != map.end()) {
Represents an MNX object that is included as an array element.
Definition BaseTypes.h:619
ContainerType container() const
Returns the container of the array this element belongs to wrapped as the specified template type.
Definition BaseTypes.h:635
size_t calcArrayIndex() const
Calculates the array index of the current instance within the array.
Definition BaseTypes.h:624
Represents an MNX array, encapsulating property access.
Definition BaseTypes.h:487
void checkIndex(size_t index) const
validates that an index is not out of range
Definition BaseTypes.h:606
auto begin()
Returns an iterator to the beginning of the array.
Definition BaseTypes.h:592
size_t size() const
Get the size of the array.
Definition BaseTypes.h:530
bool empty() const
Check if the array is empty.
Definition BaseTypes.h:533
iter< Array > iterator
non-const iterator type
Definition BaseTypes.h:510
iter< const Array > const_iterator
const iterator type
Definition BaseTypes.h:511
T value_type
The type for elements in this Array.
Definition BaseTypes.h:508
auto operator[](size_t index) const
const operator[]
Definition BaseTypes.h:539
auto begin() const
Returns a const iterator to the beginning of the array.
Definition BaseTypes.h:598
void erase(size_t index)
Remove an element at a given index.
Definition BaseTypes.h:585
auto end()
Returns an iterator to the end of the array.
Definition BaseTypes.h:595
void clear()
Clear all elements.
Definition BaseTypes.h:536
Array(const std::shared_ptr< json > &root, json_pointer pointer)
Wraps an Array class around an existing JSON array node.
Definition BaseTypes.h:516
std::enable_if_t<!std::is_base_of_v< Base, U >, void > push_back(const U &value)
Append a new value to the array. (Available only for primitive types)
Definition BaseTypes.h:563
U append(Args &&... args)
Create a new element at the end of the array. (Available only for Base types)
Definition BaseTypes.h:574
auto end() const
Returns a const iterator to the end of the array.
Definition BaseTypes.h:601
auto operator[](size_t index)
non-const operator[]
Definition BaseTypes.h:550
Array(Base &parent, const std::string_view &key)
Creates a new Array class as a child of a JSON node.
Definition BaseTypes.h:526
Base class wrapper for all MNX JSON nodes.
Definition BaseTypes.h:211
json & ref() const
Convert this node for retrieval.
Definition BaseTypes.h:280
std::optional< T > getEnclosingElement() const
Returns the enclosing array element for this instance.
Definition Implementations.cpp:54
std::string dump(int indents=-1)
Dumps the branch to a string. Useful in debugging.
Definition BaseTypes.h:250
T getChild(const std::string_view &key) const
Retrieves and validates a required child node.
Definition BaseTypes.h:319
json_pointer pointer() const
Returns the json_pointer for this node.
Definition BaseTypes.h:272
Base(json &&jsonRef, Base &parent, const std::string_view &key)
Construct a Base reference as a child inside a parent node.
Definition BaseTypes.h:305
Base(const std::shared_ptr< json > &root, json_pointer pointer)
Wrap a Base instance around a specific JSON reference using a json_pointer.
Definition BaseTypes.h:296
T setChild(const std::string_view &key, const T &value)
Sets a child node.
Definition BaseTypes.h:339
T parent() const
Returns the parent object for this node.
Definition BaseTypes.h:259
json & ref()
Access the JSON node for modification.
Definition BaseTypes.h:286
std::optional< T > getOptionalChild(const std::string_view &key) const
Retrieves an optional child node.
Definition BaseTypes.h:356
Base(const Base &src)
Copy constructor.
Definition BaseTypes.h:216
Base & operator=(const Base &src)
Copy assignment operator.
Definition BaseTypes.h:225
Base(Base &&src) noexcept
Move constructor.
Definition BaseTypes.h:220
Base & operator=(Base &&src)
Move assignment operator.
Definition BaseTypes.h:237
const std::shared_ptr< json > & root() const
Returns the root.
Definition BaseTypes.h:289
Class for content arrays.
Definition BaseTypes.h:704
T get(size_t index) const
Retrieve an element from the array as a specific type.
Definition BaseTypes.h:711
T append(Args &&... args)
Append an element of the specified type.
Definition BaseTypes.h:720
Base class for objects that are elements of content arrays.
Definition BaseTypes.h:644
MNX_OPTIONAL_PROPERTY_WITH_DEFAULT(std::string, type, std::string(ContentTypeValueDefault))
determines our type in the JSON
static constexpr std::string_view ContentTypeValueDefault
default type value that identifies the type within the content array
Definition BaseTypes.h:646
T get() const
Retrieve an element as a specific type.
Definition BaseTypes.h:655
Represents an MNX dictionary, where each key is a user-defined string.
Definition BaseTypes.h:773
iter< Dictionary, json::iterator > iterator
non-const iterator type
Definition BaseTypes.h:817
auto operator[](const std::string &key)
non-const operator[]
Definition BaseTypes.h:854
size_t size() const
Get the size of the array.
Definition BaseTypes.h:835
auto end() const
Returns a const iterator to the end of the dictionary.
Definition BaseTypes.h:926
auto begin() const
Returns a const iterator to the beginning of the dictionary.
Definition BaseTypes.h:923
auto find(const std::string &key) const
Finds an element by key and returns a const iterator.
Definition BaseTypes.h:905
U append(const std::string &key, Args &&... args)
Create a new element using the input key. (Available only for Base types)
Definition BaseTypes.h:877
void erase(const std::string &key)
Remove an element at a given key.
Definition BaseTypes.h:888
iter< const Dictionary, json::const_iterator > const_iterator
const iterator type
Definition BaseTypes.h:818
bool contains(const std::string &key) const
Returns true if the key exists in in the dictionary.
Definition BaseTypes.h:913
T operator[](const std::string &key) const
const operator[]
Definition BaseTypes.h:844
T value_type
The type for elements in this Array.
Definition BaseTypes.h:815
auto begin()
Returns an iterator to the beginning of the dictionary.
Definition BaseTypes.h:917
bool empty() const
Check if the array is empty.
Definition BaseTypes.h:838
std::enable_if_t<!std::is_base_of_v< Base, U >, void > emplace(const std::string &key, const U &value)
Add a new value to the dictonary. (Available only for primitive types)
Definition BaseTypes.h:866
Dictionary(Base &parent, const std::string_view &key)
Creates a new Dictionary class as a child of a JSON node.
Definition BaseTypes.h:831
Dictionary(const std::shared_ptr< json > &root, json_pointer pointer)
Wraps an Dictionary class around an existing JSON node.
Definition BaseTypes.h:823
auto end()
Returns an iterator to the end of the dictionary.
Definition BaseTypes.h:920
auto find(const std::string &key)
Finds an element by key and returns an iterator.
Definition BaseTypes.h:896
void clear()
Clear all elements.
Definition BaseTypes.h:841
Represents an MNX object, encapsulating property access.
Definition BaseTypes.h:421
MNX_OPTIONAL_PROPERTY(std::string, _c)
An optional comment. This serves a similar function as XML or HTML comments.
Object(const std::shared_ptr< json > &root, json_pointer pointer)
Wraps an Object class around an existing JSON object node.
Definition BaseTypes.h:426
Object(Base &parent, const std::string_view &key)
Creates a new Object class as a child of a JSON node.
Definition BaseTypes.h:436
MNX_OPTIONAL_PROPERTY(std::string, id)
Uniquely identifies the object.
Allows access to a fundamental type (number, boolean, string) in a JSON node.
Definition BaseTypes.h:447
SimpleType(const std::shared_ptr< json > &root, json_pointer pointer)
Wraps a SimpleType class around an existing JSON object node.
Definition BaseTypes.h:456
bool operator==(const T &src) const
Equality comparison with value type.
Definition BaseTypes.h:475
T value_type
value type of this SimpleType
Definition BaseTypes.h:451
SimpleType & operator=(const T &src)
Allow assignment to underlying json reference.
Definition BaseTypes.h:468
object model for MNX format
std::function< void(const std::string &message, const Base &location)> ErrorHandler
Error handler type for reporting errors.
Definition BaseTypes.h:415
json::json_pointer json_pointer
JSON pointer class for MNX.
Definition BaseTypes.h:198
constexpr int MNX_VERSION
The MNX version for files generated by the DOM.
Definition BaseTypes.h:195
nlohmann::ordered_json json
JSON class for MNX.
Definition BaseTypes.h:197
Supplies enum string mappings to nlohmann json's serializer.
Definition BaseTypes.h:751
static const std::unordered_map< E, std::string > enumToString()
maps enum values to strings
Definition BaseTypes.h:755
static const std::unordered_map< std::string, E > stringToEnum()
maps strings to enum values