Add a [] operator to the ParseTree class

This commit is contained in:
Username404-59 2021-10-20 13:16:40 +02:00
parent e8adf32645
commit ec2af24be3
Signed by: Username404-59
GPG Key ID: 7AB361FBB257A5D1

View File

@ -13,7 +13,7 @@ using namespace std;
#include "../lex.hpp" #include "../lex.hpp"
#define IS(X) template<typename T, typename std::enable_if<std::is_base_of<X, T>::value>::type* = nullptr> #define IS(X) template<typename T = X, typename std::enable_if<std::is_base_of<X, T>::value>::type* = nullptr>
struct ParseComponent { struct ParseComponent {
[[nodiscard]] constexpr const type_info& getId() const { return typeid(*this); } [[nodiscard]] constexpr const type_info& getId() const { return typeid(*this); }
virtual ~ParseComponent() = default; virtual ~ParseComponent() = default;
@ -88,6 +88,8 @@ public:
} }
return optional<reference_wrapper<T>>(); return optional<reference_wrapper<T>>();
}; };
IS(StandardComponents::NamedIdentifier)
inline auto operator[](const string& key) const { return findReferenceByName<T>(key); }
inline size_t getCompCount() const { return subComponents.size(); } inline size_t getCompCount() const { return subComponents.size(); }
IS_PARSECOMPONENT inline void add(const T& component) { addComponent<T>(component); }; IS_PARSECOMPONENT inline void add(const T& component) { addComponent<T>(component); };
IS_PARSECOMPONENT inline void addAll(const initializer_list<T>& components) { addAllComponents<T>(components); } IS_PARSECOMPONENT inline void addAll(const initializer_list<T>& components) { addAllComponents<T>(components); }