From 2e9e063d71d320464e44bfff3da617ac2f2a33ab Mon Sep 17 00:00:00 2001 From: Username404 Date: Thu, 19 Aug 2021 23:31:00 +0200 Subject: [PATCH] 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 --- src/headers/parsing/ParseComponents.hpp | 25 +++++++++---------------- src/main.cpp | 5 ++--- 2 files changed, 11 insertions(+), 19 deletions(-) diff --git a/src/headers/parsing/ParseComponents.hpp b/src/headers/parsing/ParseComponents.hpp index 6da557f..38e762e 100644 --- a/src/headers/parsing/ParseComponents.hpp +++ b/src/headers/parsing/ParseComponents.hpp @@ -2,6 +2,7 @@ #define YERBACON_PARSECOMPONENTS_HPP #include +#include #include #include #include @@ -50,21 +51,18 @@ namespace StandardComponents { class ParseTree { protected: - mutable vector subComponents; + mutable vector> 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(it); + return subComponents | views::filter([](unique_ptr& it) { + return it->getId() == typeid(T); + }) | views::transform([](unique_ptr& it) { + return reinterpret_cast(it.get()); }); } IS_PARSECOMPONENT - auto findReferencesById() const { + inline auto findReferencesById() const { return findById() | views::transform([](T* it) { return static_cast(*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& components) const { for (const auto& comp: components) add(comp); } @@ -95,11 +93,6 @@ public: explicit ParseTree(const T& element): ParseTree() { add(element); } IS_PARSECOMPONENT ParseTree(const initializer_list& elements): ParseTree() { addAll(elements); } - ~ParseTree() { - for (const auto& pointer: subComponents) { - delete pointer; - } - } }; #undef IS_PARSECOMPONENT diff --git a/src/main.cpp b/src/main.cpp index bb66e33..5e4a66c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -2,7 +2,6 @@ #include #include "headers/Yerbacon.hpp" #include -#include #include 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()); } catch (const Yerbacon::Exception& e) {