From ac8b27fcb15188f17ee6ff85965d59cb06bdc1b9 Mon Sep 17 00:00:00 2001 From: ridiculousfish Date: Sat, 21 Jan 2017 15:02:41 -0800 Subject: [PATCH] Implement and use make_unique Allows avoiding some explicit calls to new(), which can look suspicious --- src/builtin_string.cpp | 4 ++-- src/common.h | 6 ++++++ src/parser.cpp | 5 ++--- 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/builtin_string.cpp b/src/builtin_string.cpp index f717cef15..8860d8d9d 100644 --- a/src/builtin_string.cpp +++ b/src/builtin_string.cpp @@ -572,9 +572,9 @@ static int string_match(parser_t &parser, io_streams_t &streams, int argc, wchar std::unique_ptr matcher; if (regex) { - matcher.reset(new pcre2_matcher_t(argv[0], pattern, opts, streams)); + matcher = make_unique(argv[0], pattern, opts, streams); } else { - matcher.reset(new wildcard_matcher_t(argv[0], pattern, opts, streams)); + matcher = make_unique(argv[0], pattern, opts, streams); } const wchar_t *arg; diff --git a/src/common.h b/src/common.h index 023518cac..ffd071d11 100644 --- a/src/common.h +++ b/src/common.h @@ -644,6 +644,12 @@ wcstring vformat_string(const wchar_t *format, va_list va_orig); void append_format(wcstring &str, const wchar_t *format, ...); void append_formatv(wcstring &str, const wchar_t *format, va_list ap); +/// make_unique implementation +template +std::unique_ptr make_unique(Args&&... args) { + return std::unique_ptr(new T(std::forward(args)...)); +} + /// This functions returns the end of the quoted substring beginning at \c in. The type of quoting /// character is detemrined by examining \c in. Returns 0 on error. /// diff --git a/src/parser.cpp b/src/parser.cpp index cdcdb3ece..bfc14eaed 100644 --- a/src/parser.cpp +++ b/src/parser.cpp @@ -573,7 +573,7 @@ job_t *parser_t::job_get_from_pid(int pid) { profile_item_t *parser_t::create_profile_item() { profile_item_t *result = nullptr; if (g_profiling_active) { - profile_items.emplace_back(new profile_item_t()); + profile_items.push_back(make_unique()); result = profile_items.back().get(); } return result; @@ -612,8 +612,7 @@ int parser_t::eval_acquiring_tree(const wcstring &cmd, const io_chain_t &io, (execution_contexts.empty() ? -1 : execution_contexts.back()->current_eval_level()); // Append to the execution context stack. - // Note usage of unique_ptr, so we don't have to delete - execution_contexts.emplace_back(new parse_execution_context_t(tree, cmd, this, exec_eval_level)); + execution_contexts.push_back(make_unique(tree, cmd, this, exec_eval_level)); const parse_execution_context_t *ctx = execution_contexts.back().get(); // Execute the first node.