Return pointers in PareTree.findById() and return references in ParseTree.findByName().

This commit is contained in:
Username404 2021-08-08 16:23:46 +02:00
parent dd223026a0
commit e8b2f90263
Signed by: Username404-59
GPG Key ID: 7AB361FBB257A5D1
1 changed files with 15 additions and 17 deletions

View File

@ -24,7 +24,7 @@ struct ParseComponent {
namespace StandardComponents { namespace StandardComponents {
struct [[deprecated]] Expression: ParseComponent {}; struct [[deprecated]] Expression: ParseComponent {};
struct NamedIdentifier: public ParseComponent { struct NamedIdentifier: public ParseComponent {
const string name; string name;
explicit NamedIdentifier(string_view nameText): ParseComponent(), name(nameText) {} explicit NamedIdentifier(string_view nameText): ParseComponent(), name(nameText) {}
}; };
struct Define: NamedIdentifier { struct Define: NamedIdentifier {
@ -50,37 +50,35 @@ class ParseTree {
protected: protected:
mutable vector<ParseComponent*> subComponents; mutable vector<ParseComponent*> subComponents;
public: 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 IS_PARSECOMPONENT
auto findById() const { auto findById() const {
bool typeFoundOnce = false; 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); const bool typeFound = it->getId() == typeid(T);
if (typeFound) typeFoundOnce = true; if (typeFound) typeFoundOnce = true;
return typeFound; return typeFound;
}) | views::transform([](ParseComponent* it) { }) | 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(); } inline size_t getCompCount() const { return subComponents.size(); }
auto& getComponents() { return subComponents; } auto& getComponents() { return subComponents; }
auto getComponents() const { return subComponents; } auto getComponents() const { return subComponents; }
IS_PARSECOMPONENT IS_PARSECOMPONENT
void add(const T& component) const { void add(const T& component) const {
subComponents.push_back(new T(component)); subComponents.push_back(new T(component));
}; void addAll(const vector<ParseComponent>& components) const { }; IS_PARSECOMPONENT void addAll(const initializer_list<T>& components) const {
for (const auto& comp: components) add(comp); for (const auto& comp: components) add<T>(comp);
} }
IS_PARSECOMPONENT IS_PARSECOMPONENT
const ParseTree& operator<<(const T& component) const { add(component); return *this; } const ParseTree& operator<<(const T& component) const { add(component); return *this; }