Use smart pointers instead of raw pointers in ParseComponents.hpp, remove an unused dependency in main.cpp and capture fileName by value in main.cpp to avoid using its reference after the current scope
This commit is contained in:
parent
845f356a86
commit
2e9e063d71
|
@ -2,6 +2,7 @@
|
||||||
#define YERBACON_PARSECOMPONENTS_HPP
|
#define YERBACON_PARSECOMPONENTS_HPP
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <memory>
|
||||||
#include <utility>
|
#include <utility>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <typeinfo>
|
#include <typeinfo>
|
||||||
|
@ -50,21 +51,18 @@ namespace StandardComponents {
|
||||||
|
|
||||||
class ParseTree {
|
class ParseTree {
|
||||||
protected:
|
protected:
|
||||||
mutable vector<ParseComponent*> subComponents;
|
mutable vector<unique_ptr<ParseComponent>> subComponents;
|
||||||
public:
|
public:
|
||||||
IS_PARSECOMPONENT
|
IS_PARSECOMPONENT
|
||||||
auto findById() const {
|
auto findById() const {
|
||||||
bool typeFoundOnce = false;
|
return subComponents | views::filter([](unique_ptr<ParseComponent>& it) {
|
||||||
return subComponents | views::filter([&typeFoundOnce](const ParseComponent* it) {
|
return it->getId() == typeid(T);
|
||||||
const bool typeFound = it->getId() == typeid(T);
|
}) | views::transform([](unique_ptr<ParseComponent>& it) {
|
||||||
if (typeFound) typeFoundOnce = true;
|
return reinterpret_cast<T*>(it.get());
|
||||||
return typeFound;
|
|
||||||
}) | views::transform([](ParseComponent* it) {
|
|
||||||
return reinterpret_cast<T*>(it);
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
IS_PARSECOMPONENT
|
IS_PARSECOMPONENT
|
||||||
auto findReferencesById() const {
|
inline auto findReferencesById() const {
|
||||||
return findById<T>() | views::transform([](T* it) {
|
return findById<T>() | views::transform([](T* it) {
|
||||||
return static_cast<T&>(*it);
|
return static_cast<T&>(*it);
|
||||||
});
|
});
|
||||||
|
@ -81,10 +79,10 @@ public:
|
||||||
};
|
};
|
||||||
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; }
|
const 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.emplace_back(new T(component));
|
||||||
}; IS_PARSECOMPONENT void addAll(const initializer_list<T>& components) const {
|
}; IS_PARSECOMPONENT void addAll(const initializer_list<T>& components) const {
|
||||||
for (const auto& comp: components) add<T>(comp);
|
for (const auto& comp: components) add<T>(comp);
|
||||||
}
|
}
|
||||||
|
@ -95,11 +93,6 @@ public:
|
||||||
explicit ParseTree(const T& element): ParseTree() { add(element); }
|
explicit ParseTree(const T& element): ParseTree() { add(element); }
|
||||||
IS_PARSECOMPONENT
|
IS_PARSECOMPONENT
|
||||||
ParseTree(const initializer_list<T>& elements): ParseTree() { addAll(elements); }
|
ParseTree(const initializer_list<T>& elements): ParseTree() { addAll(elements); }
|
||||||
~ParseTree() {
|
|
||||||
for (const auto& pointer: subComponents) {
|
|
||||||
delete pointer;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
#undef IS_PARSECOMPONENT
|
#undef IS_PARSECOMPONENT
|
||||||
|
|
||||||
|
|
|
@ -2,7 +2,6 @@
|
||||||
#include <set>
|
#include <set>
|
||||||
#include "headers/Yerbacon.hpp"
|
#include "headers/Yerbacon.hpp"
|
||||||
#include <future>
|
#include <future>
|
||||||
#include <sstream>
|
|
||||||
#include <variant>
|
#include <variant>
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
|
@ -31,7 +30,7 @@ int main(int argc, char* argv[]) {
|
||||||
else if (currentArg == Argument("parallel")) parallel = true;
|
else if (currentArg == Argument("parallel")) parallel = true;
|
||||||
else if (currentArg.ends_with(".ybcon")) files.push_back(currentArg);
|
else if (currentArg.ends_with(".ybcon")) files.push_back(currentArg);
|
||||||
}
|
}
|
||||||
const auto compile = [&target](string_view& name) {
|
const auto compile = [&target](string_view name) {
|
||||||
string transpiledString = transpile(parseString(getFileContent(name.data())), target);
|
string transpiledString = transpile(parseString(getFileContent(name.data())), target);
|
||||||
name.remove_suffix(6);
|
name.remove_suffix(6);
|
||||||
string outputFile;
|
string outputFile;
|
||||||
|
@ -45,7 +44,7 @@ int main(int argc, char* argv[]) {
|
||||||
const launch& Policy = not parallel ? launch::deferred : launch::async;
|
const launch& Policy = not parallel ? launch::deferred : launch::async;
|
||||||
for (string_view fileName: uniqueFiles) {
|
for (string_view fileName: uniqueFiles) {
|
||||||
if (fileName != "none") {
|
if (fileName != "none") {
|
||||||
Units.push_back(async(Policy, [&fileName, &compile]() {
|
Units.push_back(async(Policy, [fileName, &compile]() {
|
||||||
try {
|
try {
|
||||||
return pair(compile(fileName), optional<Yerbacon::Exception>());
|
return pair(compile(fileName), optional<Yerbacon::Exception>());
|
||||||
} catch (const Yerbacon::Exception& e) {
|
} catch (const Yerbacon::Exception& e) {
|
||||||
|
|
Loading…
Reference in New Issue