From da84b38430fdafa5bd3e8c666357afce09f352f0 Mon Sep 17 00:00:00 2001 From: ridiculousfish Date: Sun, 18 Feb 2018 18:39:03 -0800 Subject: [PATCH] Constructors to accept more parameters by value In cases where the constructor needs to take ownership of parameters, pass them by value and use std::move. --- muparser-2.2.5/include/muParserDef.h | 6 +++--- muparser-2.2.5/src/muParserError.cpp | 14 ++++++++------ src/autoload.cpp | 4 ++-- src/autoload.h | 2 +- src/builtin_string.cpp | 8 ++++---- src/builtin_test.cpp | 13 +++++++------ src/complete.cpp | 8 ++++---- src/env_universal_common.cpp | 4 ++-- src/env_universal_common.h | 6 +++--- src/highlight.cpp | 4 ++-- src/history.cpp | 4 ++-- src/history.h | 2 +- src/input.cpp | 6 +++--- src/input_common.cpp | 1 + src/parser.cpp | 5 +++-- src/parser.h | 2 +- src/proc.cpp | 5 +++-- src/proc.h | 2 +- src/wildcard.cpp | 4 ++-- 19 files changed, 53 insertions(+), 47 deletions(-) diff --git a/muparser-2.2.5/include/muParserDef.h b/muparser-2.2.5/include/muParserDef.h index f96e6e207..36074a296 100644 --- a/muparser-2.2.5/include/muParserDef.h +++ b/muparser-2.2.5/include/muParserDef.h @@ -306,9 +306,9 @@ class ParserError { ParserError(); explicit ParserError(EErrorCodes a_iErrc); explicit ParserError(const string_type &sMsg); - ParserError(EErrorCodes a_iErrc, const string_type &sTok, int a_iPos = -1); - ParserError(EErrorCodes a_iErrc, int a_iPos, const string_type &sTok); - ParserError(const char_type *a_szMsg, int a_iPos = -1, const string_type &sTok = string_type()); + ParserError(EErrorCodes a_iErrc, string_type sTok, int a_iPos = -1); + ParserError(EErrorCodes a_iErrc, int a_iPos, string_type sTok); + ParserError(const char_type *a_szMsg, int a_iPos = -1, string_type sTok = string_type()); ParserError(ParserError &&) = default; ParserError &operator=(ParserError &&) = default; ParserError(const ParserError &a_Obj) = default; diff --git a/muparser-2.2.5/src/muParserError.cpp b/muparser-2.2.5/src/muParserError.cpp index f02be129d..6e4a88917 100644 --- a/muparser-2.2.5/src/muParserError.cpp +++ b/muparser-2.2.5/src/muParserError.cpp @@ -26,6 +26,8 @@ #include +#include + namespace mu { //--------------------------------------------------------------------------- string_type parser_error_for_code(EErrorCodes code) { @@ -144,8 +146,8 @@ ParserError::ParserError(const string_type &sMsg) { \param [in] sTok The token string related to this error. \param [in] a_iPos the position in the expression where the error occurred. */ -ParserError::ParserError(EErrorCodes iErrc, const string_type &sTok, int iPos) - : m_strTok(sTok), m_iPos(iPos), m_iErrc(iErrc) { +ParserError::ParserError(EErrorCodes iErrc, string_type sTok, int iPos) + : m_strTok(std::move(sTok)), m_iPos(iPos), m_iErrc(iErrc) { m_strMsg = parser_error_for_code(m_iErrc); stringstream_type stream; stream << (int)m_iPos; @@ -159,8 +161,8 @@ ParserError::ParserError(EErrorCodes iErrc, const string_type &sTok, int iPos) \param [in] iPos the position in the expression where the error occurred. \param [in] sTok The token string related to this error. */ -ParserError::ParserError(EErrorCodes iErrc, int iPos, const string_type &sTok) - : m_strMsg(), m_strTok(sTok), m_iPos(iPos), m_iErrc(iErrc) { +ParserError::ParserError(EErrorCodes iErrc, int iPos, string_type sTok) + : m_strMsg(), m_strTok(std::move(sTok)), m_iPos(iPos), m_iErrc(iErrc) { m_strMsg = parser_error_for_code(m_iErrc); stringstream_type stream; stream << (int)m_iPos; @@ -174,8 +176,8 @@ ParserError::ParserError(EErrorCodes iErrc, int iPos, const string_type &sTok) \param [in] iPos the position related to the error. \param [in] sTok The token string related to this error. */ -ParserError::ParserError(const char_type *szMsg, int iPos, const string_type &sTok) - : m_strMsg(szMsg), m_strTok(sTok), m_iPos(iPos), m_iErrc(ecGENERIC) { +ParserError::ParserError(const char_type *szMsg, int iPos, string_type sTok) + : m_strMsg(szMsg), m_strTok(std::move(sTok)), m_iPos(iPos), m_iErrc(ecGENERIC) { stringstream_type stream; stream << (int)m_iPos; ReplaceSubString(m_strMsg, _T("$POS$"), stream.str()); diff --git a/src/autoload.cpp b/src/autoload.cpp index d5e51b226..6d577fc46 100644 --- a/src/autoload.cpp +++ b/src/autoload.cpp @@ -46,9 +46,9 @@ file_access_attempt_t access_file(const wcstring &path, int mode) { return result; } -autoload_t::autoload_t(const wcstring &env_var_name_var, +autoload_t::autoload_t(wcstring env_var_name_var, command_removed_function_t cmd_removed_callback) - : env_var_name(env_var_name_var), command_removed(cmd_removed_callback) {} + : env_var_name(std::move(env_var_name_var)), command_removed(cmd_removed_callback) {} void autoload_t::entry_was_evicted(wcstring key, autoload_function_t node) { // This should only ever happen on the main thread. diff --git a/src/autoload.h b/src/autoload.h index 01d5612da..83614b761 100644 --- a/src/autoload.h +++ b/src/autoload.h @@ -71,7 +71,7 @@ class autoload_t : public lru_cache_t { void entry_was_evicted(wcstring key, autoload_function_t node); // Create an autoload_t for the given environment variable name. - autoload_t(const wcstring &env_var_name_var, command_removed_function_t callback); + autoload_t(wcstring env_var_name_var, command_removed_function_t callback); /// Autoload the specified file, if it exists in the specified path. Do not load it multiple /// times unless its timestamp changes or parse_util_unload is called. diff --git a/src/builtin_string.cpp b/src/builtin_string.cpp index 5d7630894..1813a3302 100644 --- a/src/builtin_string.cpp +++ b/src/builtin_string.cpp @@ -682,8 +682,8 @@ class string_matcher_t { int total_matched; public: - string_matcher_t(const options_t &opts_, io_streams_t &streams_) - : opts(opts_), streams(streams_), total_matched(0) {} + string_matcher_t(options_t opts_, io_streams_t &streams_) + : opts(std::move(opts_)), streams(streams_), total_matched(0) {} virtual ~string_matcher_t() {} virtual bool report_matches(const wchar_t *arg) = 0; @@ -956,8 +956,8 @@ class string_replacer_t { io_streams_t &streams; public: - string_replacer_t(const wchar_t *argv0_, const options_t &opts_, io_streams_t &streams_) - : argv0(argv0_), opts(opts_), total_replaced(0), streams(streams_) {} + string_replacer_t(const wchar_t *argv0_, options_t opts_, io_streams_t &streams_) + : argv0(argv0_), opts(std::move(opts_)), total_replaced(0), streams(streams_) {} virtual ~string_replacer_t() {} int replace_count() { return total_replaced; } diff --git a/src/builtin_test.cpp b/src/builtin_test.cpp index fb843966f..086017971 100644 --- a/src/builtin_test.cpp +++ b/src/builtin_test.cpp @@ -13,6 +13,7 @@ #include #include #include +#include #include "builtin.h" #include "common.h" @@ -155,7 +156,7 @@ class test_parser { const wcstring &arg(unsigned int idx) { return strings.at(idx); } public: - explicit test_parser(const wcstring_list_t &val) : strings(val) {} + explicit test_parser(wcstring_list_t val) : strings(std::move(val)) {} unique_ptr parse_expression(unsigned int start, unsigned int end); unique_ptr parse_3_arg_expression(unsigned int start, unsigned int end); @@ -183,7 +184,7 @@ struct range_t { /// Base class for expressions. class expression { protected: - expression(token_t what, range_t where) : token(what), range(where) {} + expression(token_t what, range_t where) : token(what), range(std::move(where)) {} public: const token_t token; @@ -199,8 +200,8 @@ class expression { class unary_primary : public expression { public: wcstring arg; - unary_primary(token_t tok, range_t where, const wcstring &what) - : expression(tok, where), arg(what) {} + unary_primary(token_t tok, range_t where, wcstring what) + : expression(tok, where), arg(std::move(what)) {} bool evaluate(wcstring_list_t &errors); }; @@ -210,8 +211,8 @@ class binary_primary : public expression { wcstring arg_left; wcstring arg_right; - binary_primary(token_t tok, range_t where, const wcstring &left, const wcstring &right) - : expression(tok, where), arg_left(left), arg_right(right) {} + binary_primary(token_t tok, range_t where, wcstring left, wcstring right) + : expression(tok, where), arg_left(std::move(left)), arg_right(std::move(right)) {} bool evaluate(wcstring_list_t &errors); }; diff --git a/src/complete.cpp b/src/complete.cpp index 3b94ef658..5b28487ad 100644 --- a/src/complete.cpp +++ b/src/complete.cpp @@ -153,8 +153,8 @@ class completion_entry_t { void add_option(const complete_entry_opt_t &opt); bool remove_option(const wcstring &option, complete_option_type_t type); - completion_entry_t(const wcstring &c, bool type) - : cmd(c), cmd_is_path(type), order(++kCompleteOrder) {} + completion_entry_t(wcstring c, bool type) + : cmd(std::move(c)), cmd_is_path(type), order(++kCompleteOrder) {} }; /// Set of all completion entries. @@ -212,7 +212,7 @@ completion_t::completion_t(wcstring comp, wcstring desc, string_fuzzy_match_t ma complete_flags_t flags_val) : completion(std::move(comp)), description(std::move(desc)), - match(mat), + match(std::move(mat)), flags(resolve_auto_space(completion, flags_val)) {} completion_t::completion_t(const completion_t &him) = default; @@ -310,7 +310,7 @@ class completer_t { } public: - completer_t(const wcstring &c, completion_request_flags_t f) : flags(f), initial_cmd(c) {} + completer_t(wcstring c, completion_request_flags_t f) : flags(f), initial_cmd(std::move(c)) {} bool empty() const { return completions.empty(); } const std::vector &get_completions() { return completions; } diff --git a/src/env_universal_common.cpp b/src/env_universal_common.cpp index 1bce23c75..ef279d507 100644 --- a/src/env_universal_common.cpp +++ b/src/env_universal_common.cpp @@ -255,8 +255,8 @@ static bool append_file_entry(fish_message_type_t type, const wcstring &key_in, return success; } -env_universal_t::env_universal_t(const wcstring &path) - : explicit_vars_path(path), tried_renaming(false), last_read_file(kInvalidFileID) {} +env_universal_t::env_universal_t(wcstring path) + : explicit_vars_path(std::move(path)), tried_renaming(false), last_read_file(kInvalidFileID) {} maybe_t env_universal_t::get(const wcstring &name) const { var_table_t::const_iterator where = vars.find(name); diff --git a/src/env_universal_common.h b/src/env_universal_common.h index 842ae01a0..ed4f6b562 100644 --- a/src/env_universal_common.h +++ b/src/env_universal_common.h @@ -22,8 +22,8 @@ struct callback_data_t { wcstring key; wcstring val; - callback_data_t(fish_message_type_t t, const wcstring &k, const wcstring &v) - : type(t), key(k), val(v) {} + callback_data_t(fish_message_type_t t, wcstring k, wcstring v) + : type(t), key(std::move(k)), val(std::move(v)) {} }; typedef std::vector callback_data_list_t; @@ -68,7 +68,7 @@ class env_universal_t { static var_table_t read_message_internal(int fd); public: - explicit env_universal_t(const wcstring &path); + explicit env_universal_t(wcstring path); // Get the value of the variable with the specified name. maybe_t get(const wcstring &name) const; diff --git a/src/highlight.cpp b/src/highlight.cpp index fa10aa516..58bca6d13 100644 --- a/src/highlight.cpp +++ b/src/highlight.cpp @@ -687,12 +687,12 @@ class highlighter_t { public: // Constructor highlighter_t(const wcstring &str, size_t pos, const env_vars_snapshot_t &ev, - const wcstring &wd, bool can_do_io) + wcstring wd, bool can_do_io) : buff(str), cursor_pos(pos), vars(ev), io_ok(can_do_io), - working_directory(wd), + working_directory(std::move(wd)), color_array(str.size()) { // Parse the tree. parse_tree_from_string(buff, parse_flag_continue_after_error | parse_flag_include_comments, diff --git a/src/history.cpp b/src/history.cpp index abca082ef..be0066a42 100644 --- a/src/history.cpp +++ b/src/history.cpp @@ -721,8 +721,8 @@ history_t &history_t::history_with_name(const wcstring &name) { return histories.get_creating(name); } -history_t::history_t(const wcstring &pname) - : name(pname), +history_t::history_t(wcstring pname) + : name(std::move(pname)), first_unwritten_new_item_index(0), has_pending_item(false), disable_automatic_save_counter(0), diff --git a/src/history.h b/src/history.h index 9b411689a..1317d1576 100644 --- a/src/history.h +++ b/src/history.h @@ -216,7 +216,7 @@ class history_t { history_item_t item_at_index_assume_locked(size_t idx); public: - explicit history_t(const wcstring &); // constructor + explicit history_t(wcstring ); // constructor // Returns history with the given name, creating it if necessary. static history_t &history_with_name(const wcstring &name); diff --git a/src/input.cpp b/src/input.cpp index 6a5fcc87a..12c13b1af 100644 --- a/src/input.cpp +++ b/src/input.cpp @@ -45,9 +45,9 @@ struct input_mapping_t { /// New mode that should be switched to after command evaluation. wcstring sets_mode; - input_mapping_t(const wcstring &s, const std::vector &c, const wcstring &m, - const wcstring &sm) - : seq(s), commands(c), mode(m), sets_mode(sm) { + input_mapping_t(wcstring s, std::vector c, wcstring m, + wcstring sm) + : seq(std::move(s)), commands(std::move(c)), mode(std::move(m)), sets_mode(std::move(sm)) { static unsigned int s_last_input_map_spec_order = 0; specification_order = ++s_last_input_map_spec_order; } diff --git a/src/input_common.cpp b/src/input_common.cpp index 209349e7f..4f6c21f02 100644 --- a/src/input_common.cpp +++ b/src/input_common.cpp @@ -17,6 +17,7 @@ #include #include #include +#include #include "common.h" #include "env.h" diff --git a/src/parser.cpp b/src/parser.cpp index 8b84a4ae1..6658c281e 100644 --- a/src/parser.cpp +++ b/src/parser.cpp @@ -6,6 +6,7 @@ #include #include +#include #include "common.h" #include "env.h" @@ -857,8 +858,8 @@ if_block_t::if_block_t() : block_t(IF) {} event_block_t::event_block_t(const event_t &evt) : block_t(EVENT), event(evt) {} -function_block_t::function_block_t(const process_t *p, const wcstring &n, bool shadows) - : block_t(shadows ? FUNCTION_CALL : FUNCTION_CALL_NO_SHADOW), process(p), name(n) {} +function_block_t::function_block_t(const process_t *p, wcstring n, bool shadows) + : block_t(shadows ? FUNCTION_CALL : FUNCTION_CALL_NO_SHADOW), process(p), name(std::move(n)) {} source_block_t::source_block_t(const wchar_t *src) : block_t(SOURCE), source_file(src) {} diff --git a/src/parser.h b/src/parser.h index 35ca8922f..2e861968a 100644 --- a/src/parser.h +++ b/src/parser.h @@ -111,7 +111,7 @@ struct event_block_t : public block_t { struct function_block_t : public block_t { const process_t *process; wcstring name; - function_block_t(const process_t *p, const wcstring &n, bool shadows); + function_block_t(const process_t *p, wcstring n, bool shadows); }; struct source_block_t : public block_t { diff --git a/src/proc.cpp b/src/proc.cpp index f9ce29532..0776dd091 100644 --- a/src/proc.cpp +++ b/src/proc.cpp @@ -32,6 +32,7 @@ #include // IWYU pragma: keep #include +#include #include #include "common.h" @@ -351,8 +352,8 @@ process_t::process_t() {} /// the Linux kernel will use it for kernel processes. /// -1 should not be used; it is a possible return value of the getpgid() /// function -job_t::job_t(job_id_t jobid, const io_chain_t &bio) - : block_io(bio), pgid(-2), tmodes(), job_id(jobid), flags(0) {} +job_t::job_t(job_id_t jobid, io_chain_t bio) + : block_io(std::move(bio)), pgid(-2), tmodes(), job_id(jobid), flags(0) {} job_t::~job_t() { release_job_id(job_id); } diff --git a/src/proc.h b/src/proc.h index 74f8ebe41..2990f5543 100644 --- a/src/proc.h +++ b/src/proc.h @@ -177,7 +177,7 @@ class job_t { void operator=(const job_t &) = delete; public: - job_t(job_id_t jobid, const io_chain_t &bio); + job_t(job_id_t jobid, io_chain_t bio); ~job_t(); /// Returns whether the command is empty. diff --git a/src/wildcard.cpp b/src/wildcard.cpp index ee924c1d4..48ce0671e 100644 --- a/src/wildcard.cpp +++ b/src/wildcard.cpp @@ -611,8 +611,8 @@ class wildcard_expander_t { } public: - wildcard_expander_t(const wcstring &wd, expand_flags_t f, std::vector *r) - : working_directory(wd), + wildcard_expander_t(wcstring wd, expand_flags_t f, std::vector *r) + : working_directory(std::move(wd)), flags(f), resolved_completions(r), did_interrupt(false),