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:
Username404 2021-08-19 23:31:00 +02:00
parent 845f356a86
commit 2e9e063d71
Signed by: Username404-59
GPG Key ID: 7AB361FBB257A5D1
2 changed files with 11 additions and 19 deletions

View File

@ -2,6 +2,7 @@
#define YERBACON_PARSECOMPONENTS_HPP
#include <iostream>
#include <memory>
#include <utility>
#include <vector>
#include <typeinfo>
@ -50,21 +51,18 @@ namespace StandardComponents {
class ParseTree {
protected:
mutable vector<ParseComponent*> subComponents;
mutable vector<unique_ptr<ParseComponent>> subComponents;
public:
IS_PARSECOMPONENT
auto findById() const {
bool typeFoundOnce = false;
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 subComponents | views::filter([](unique_ptr<ParseComponent>& it) {
return it->getId() == typeid(T);
}) | views::transform([](unique_ptr<ParseComponent>& it) {
return reinterpret_cast<T*>(it.get());
});
}
IS_PARSECOMPONENT
auto findReferencesById() const {
inline auto findReferencesById() const {
return findById<T>() | views::transform([](T* it) {
return static_cast<T&>(*it);
});
@ -81,10 +79,10 @@ public:
};
inline size_t getCompCount() const { return subComponents.size(); }
auto& getComponents() { return subComponents; }
auto getComponents() const { return subComponents; }
const auto& getComponents() const { return subComponents; }
IS_PARSECOMPONENT
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 {
for (const auto& comp: components) add<T>(comp);
}
@ -95,11 +93,6 @@ public:
explicit ParseTree(const T& element): ParseTree() { add(element); }
IS_PARSECOMPONENT
ParseTree(const initializer_list<T>& elements): ParseTree() { addAll(elements); }
~ParseTree() {
for (const auto& pointer: subComponents) {
delete pointer;
}
}
};
#undef IS_PARSECOMPONENT

View File

@ -2,7 +2,6 @@
#include <set>
#include "headers/Yerbacon.hpp"
#include <future>
#include <sstream>
#include <variant>
using namespace std;
@ -31,7 +30,7 @@ int main(int argc, char* argv[]) {
else if (currentArg == Argument("parallel")) parallel = true;
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);
name.remove_suffix(6);
string outputFile;
@ -45,7 +44,7 @@ int main(int argc, char* argv[]) {
const launch& Policy = not parallel ? launch::deferred : launch::async;
for (string_view fileName: uniqueFiles) {
if (fileName != "none") {
Units.push_back(async(Policy, [&fileName, &compile]() {
Units.push_back(async(Policy, [fileName, &compile]() {
try {
return pair(compile(fileName), optional<Yerbacon::Exception>());
} catch (const Yerbacon::Exception& e) {