From e8b2f902634a054249a8bd2d7c21d4ab81977ce9 Mon Sep 17 00:00:00 2001 From: Username404 Date: Sun, 8 Aug 2021 16:23:46 +0200 Subject: [PATCH] Return pointers in PareTree.findById() and return references in ParseTree.findByName(). --- src/headers/parsing/ParseComponents.hpp | 32 ++++++++++++------------- 1 file changed, 15 insertions(+), 17 deletions(-) diff --git a/src/headers/parsing/ParseComponents.hpp b/src/headers/parsing/ParseComponents.hpp index f8ceedb..fdf61d4 100644 --- a/src/headers/parsing/ParseComponents.hpp +++ b/src/headers/parsing/ParseComponents.hpp @@ -24,7 +24,7 @@ struct ParseComponent { namespace StandardComponents { struct [[deprecated]] Expression: ParseComponent {}; struct NamedIdentifier: public ParseComponent { - const string name; + string name; explicit NamedIdentifier(string_view nameText): ParseComponent(), name(nameText) {} }; struct Define: NamedIdentifier { @@ -50,37 +50,35 @@ class ParseTree { protected: mutable vector subComponents; public: - IS(StandardComponents::NamedIdentifier) - optional> findByName(const string& name) { - auto identifiers = findById(); - for (auto&& identifier: identifiers) { - if (identifier.getId() == typeid(T) && identifier.name == name) { - return make_optional(reference_wrapper(identifier)); - break; - } - } - return optional>(); - }; IS_PARSECOMPONENT auto findById() const { bool typeFoundOnce = false; - auto foundPointers = subComponents | views::filter([&typeFoundOnce](const ParseComponent* it) { + return subComponents | views::filter([&typeFoundOnce](const ParseComponent* it) { const bool typeFound = it->getId() == typeid(T); if (typeFound) typeFoundOnce = true; return typeFound; }) | views::transform([](ParseComponent* it) { - return reinterpret_cast(*it); + return reinterpret_cast(it); }); - return foundPointers; } + IS(StandardComponents::NamedIdentifier) + optional> findByName(const string& name) const { + auto identifiers = findById(); + for (T* identifier: identifiers) { + if (identifier->getId() == typeid(T) && identifier->name == name) { + return make_optional(ref(*identifier)); + } + } + return optional>(); + }; inline size_t getCompCount() const { return subComponents.size(); } auto& getComponents() { return subComponents; } auto getComponents() const { return subComponents; } IS_PARSECOMPONENT void add(const T& component) const { subComponents.push_back(new T(component)); - }; void addAll(const vector& components) const { - for (const auto& comp: components) add(comp); + }; IS_PARSECOMPONENT void addAll(const initializer_list& components) const { + for (const auto& comp: components) add(comp); } IS_PARSECOMPONENT const ParseTree& operator<<(const T& component) const { add(component); return *this; }