From e4fd6c863a93a29394131e7eb061833a97ef7e5b Mon Sep 17 00:00:00 2001 From: apio Date: Wed, 22 Jun 2022 18:12:50 +0200 Subject: [PATCH] Updated Lexer::make_lexer to return a unique_ptr This is part of my quest to replace all unneeded std::shared_ptrs for std::unique_ptrs to improve performance, since there is no need for reference counting for pointers that are uniquely used. I also had to change the first parameter of Lexer::assign_parent_location to a std::unique_ptr& so that it can accept the new result of make_lexer. --- src/Lexer.cpp | 6 +++--- src/Lexer.h | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Lexer.cpp b/src/Lexer.cpp index fe4c84b..47ccd98 100644 --- a/src/Lexer.cpp +++ b/src/Lexer.cpp @@ -60,12 +60,12 @@ std::string Lexer::recalculate_current_line(const std::string& text) return final_str; } -std::shared_ptr Lexer::make_lexer(const std::string& fname) +std::unique_ptr Lexer::make_lexer(const std::string& fname) { - return std::shared_ptr(new Lexer(fname)); // not using make_shared because the constructor is private + return std::unique_ptr(new Lexer(fname)); // not using make_shared because the constructor is private } -void Lexer::assign_parent_location(std::shared_ptr& lexer, const std::shared_ptr& loc) +void Lexer::assign_parent_location(std::unique_ptr& lexer, const std::shared_ptr& loc) { lexer->loc.parent = loc; } diff --git a/src/Lexer.h b/src/Lexer.h index 0493cb4..0994ddd 100644 --- a/src/Lexer.h +++ b/src/Lexer.h @@ -46,8 +46,8 @@ class Lexer TokenStream lex(const std::string& text); /* Create a new Lexer and return a pointer to it. */ - static std::shared_ptr make_lexer(const std::string& fname); + static std::unique_ptr make_lexer(const std::string& fname); /* If the Lexer is lexing an impòrted file, give it the location in the parent file at which it was imported. */ - static void assign_parent_location(std::shared_ptr& lexer, const std::shared_ptr& loc); + static void assign_parent_location(std::unique_ptr& lexer, const std::shared_ptr& loc); };