Return pointers in PareTree.findById() and return references in ParseTree.findByName().
This commit is contained in:
		
							parent
							
								
									dd223026a0
								
							
						
					
					
						commit
						e8b2f90263
					
				@ -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<ParseComponent*> subComponents;
 | 
			
		||||
public:
 | 
			
		||||
    IS(StandardComponents::NamedIdentifier)
 | 
			
		||||
    optional<reference_wrapper<T>> findByName(const string& name) {
 | 
			
		||||
        auto identifiers = findById<T>();
 | 
			
		||||
        for (auto&& identifier: identifiers) {
 | 
			
		||||
            if (identifier.getId() == typeid(T) && identifier.name == name) {
 | 
			
		||||
                return make_optional(reference_wrapper<T>(identifier));
 | 
			
		||||
                break;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        return optional<reference_wrapper<T>>();
 | 
			
		||||
    };
 | 
			
		||||
    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<T&>(*it);
 | 
			
		||||
            return reinterpret_cast<T*>(it);
 | 
			
		||||
        });
 | 
			
		||||
        return foundPointers;
 | 
			
		||||
    }
 | 
			
		||||
    IS(StandardComponents::NamedIdentifier)
 | 
			
		||||
    optional<reference_wrapper<T>> findByName(const string& name) const {
 | 
			
		||||
        auto identifiers = findById<T>();
 | 
			
		||||
        for (T* identifier: identifiers) {
 | 
			
		||||
            if (identifier->getId() == typeid(T) && identifier->name == name) {
 | 
			
		||||
                return make_optional(ref(*identifier));
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        return optional<reference_wrapper<T>>();
 | 
			
		||||
    };
 | 
			
		||||
    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<ParseComponent>& components) const {
 | 
			
		||||
        for (const auto& comp: components) add(comp);
 | 
			
		||||
    }; IS_PARSECOMPONENT void addAll(const initializer_list<T>& components) const {
 | 
			
		||||
        for (const auto& comp: components) add<T>(comp);
 | 
			
		||||
    }
 | 
			
		||||
    IS_PARSECOMPONENT
 | 
			
		||||
    const ParseTree& operator<<(const T& component) const { add(component); return *this; }
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user