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.
This commit is contained in:
ridiculousfish 2018-02-18 18:39:03 -08:00
parent 74e6a82849
commit da84b38430
19 changed files with 53 additions and 47 deletions

View file

@ -306,9 +306,9 @@ class ParserError {
ParserError(); ParserError();
explicit ParserError(EErrorCodes a_iErrc); explicit ParserError(EErrorCodes a_iErrc);
explicit ParserError(const string_type &sMsg); explicit ParserError(const string_type &sMsg);
ParserError(EErrorCodes a_iErrc, const string_type &sTok, int a_iPos = -1); ParserError(EErrorCodes a_iErrc, string_type sTok, int a_iPos = -1);
ParserError(EErrorCodes a_iErrc, int a_iPos, const string_type &sTok); ParserError(EErrorCodes a_iErrc, int a_iPos, string_type sTok);
ParserError(const char_type *a_szMsg, int a_iPos = -1, const string_type &sTok = string_type()); ParserError(const char_type *a_szMsg, int a_iPos = -1, string_type sTok = string_type());
ParserError(ParserError &&) = default; ParserError(ParserError &&) = default;
ParserError &operator=(ParserError &&) = default; ParserError &operator=(ParserError &&) = default;
ParserError(const ParserError &a_Obj) = default; ParserError(const ParserError &a_Obj) = default;

View file

@ -26,6 +26,8 @@
#include <assert.h> #include <assert.h>
#include <utility>
namespace mu { namespace mu {
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
string_type parser_error_for_code(EErrorCodes code) { 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] sTok The token string related to this error.
\param [in] a_iPos the position in the expression where the error occurred. \param [in] a_iPos the position in the expression where the error occurred.
*/ */
ParserError::ParserError(EErrorCodes iErrc, const string_type &sTok, int iPos) ParserError::ParserError(EErrorCodes iErrc, string_type sTok, int iPos)
: m_strTok(sTok), m_iPos(iPos), m_iErrc(iErrc) { : m_strTok(std::move(sTok)), m_iPos(iPos), m_iErrc(iErrc) {
m_strMsg = parser_error_for_code(m_iErrc); m_strMsg = parser_error_for_code(m_iErrc);
stringstream_type stream; stringstream_type stream;
stream << (int)m_iPos; 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] iPos the position in the expression where the error occurred.
\param [in] sTok The token string related to this error. \param [in] sTok The token string related to this error.
*/ */
ParserError::ParserError(EErrorCodes iErrc, int iPos, const string_type &sTok) ParserError::ParserError(EErrorCodes iErrc, int iPos, string_type sTok)
: m_strMsg(), m_strTok(sTok), m_iPos(iPos), m_iErrc(iErrc) { : m_strMsg(), m_strTok(std::move(sTok)), m_iPos(iPos), m_iErrc(iErrc) {
m_strMsg = parser_error_for_code(m_iErrc); m_strMsg = parser_error_for_code(m_iErrc);
stringstream_type stream; stringstream_type stream;
stream << (int)m_iPos; 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] iPos the position related to the error.
\param [in] sTok The token string related to this error. \param [in] sTok The token string related to this error.
*/ */
ParserError::ParserError(const char_type *szMsg, int iPos, const string_type &sTok) ParserError::ParserError(const char_type *szMsg, int iPos, string_type sTok)
: m_strMsg(szMsg), m_strTok(sTok), m_iPos(iPos), m_iErrc(ecGENERIC) { : m_strMsg(szMsg), m_strTok(std::move(sTok)), m_iPos(iPos), m_iErrc(ecGENERIC) {
stringstream_type stream; stringstream_type stream;
stream << (int)m_iPos; stream << (int)m_iPos;
ReplaceSubString(m_strMsg, _T("$POS$"), stream.str()); ReplaceSubString(m_strMsg, _T("$POS$"), stream.str());

View file

@ -46,9 +46,9 @@ file_access_attempt_t access_file(const wcstring &path, int mode) {
return result; 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) 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) { void autoload_t::entry_was_evicted(wcstring key, autoload_function_t node) {
// This should only ever happen on the main thread. // This should only ever happen on the main thread.

View file

@ -71,7 +71,7 @@ class autoload_t : public lru_cache_t<autoload_t, autoload_function_t> {
void entry_was_evicted(wcstring key, autoload_function_t node); void entry_was_evicted(wcstring key, autoload_function_t node);
// Create an autoload_t for the given environment variable name. // 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 /// 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. /// times unless its timestamp changes or parse_util_unload is called.

View file

@ -682,8 +682,8 @@ class string_matcher_t {
int total_matched; int total_matched;
public: public:
string_matcher_t(const options_t &opts_, io_streams_t &streams_) string_matcher_t(options_t opts_, io_streams_t &streams_)
: opts(opts_), streams(streams_), total_matched(0) {} : opts(std::move(opts_)), streams(streams_), total_matched(0) {}
virtual ~string_matcher_t() {} virtual ~string_matcher_t() {}
virtual bool report_matches(const wchar_t *arg) = 0; virtual bool report_matches(const wchar_t *arg) = 0;
@ -956,8 +956,8 @@ class string_replacer_t {
io_streams_t &streams; io_streams_t &streams;
public: public:
string_replacer_t(const wchar_t *argv0_, const options_t &opts_, io_streams_t &streams_) string_replacer_t(const wchar_t *argv0_, options_t opts_, io_streams_t &streams_)
: argv0(argv0_), opts(opts_), total_replaced(0), streams(streams_) {} : argv0(argv0_), opts(std::move(opts_)), total_replaced(0), streams(streams_) {}
virtual ~string_replacer_t() {} virtual ~string_replacer_t() {}
int replace_count() { return total_replaced; } int replace_count() { return total_replaced; }

View file

@ -13,6 +13,7 @@
#include <memory> #include <memory>
#include <string> #include <string>
#include <type_traits> #include <type_traits>
#include <utility>
#include "builtin.h" #include "builtin.h"
#include "common.h" #include "common.h"
@ -155,7 +156,7 @@ class test_parser {
const wcstring &arg(unsigned int idx) { return strings.at(idx); } const wcstring &arg(unsigned int idx) { return strings.at(idx); }
public: public:
explicit test_parser(const wcstring_list_t &val) : strings(val) {} explicit test_parser(wcstring_list_t val) : strings(std::move(val)) {}
unique_ptr<expression> parse_expression(unsigned int start, unsigned int end); unique_ptr<expression> parse_expression(unsigned int start, unsigned int end);
unique_ptr<expression> parse_3_arg_expression(unsigned int start, unsigned int end); unique_ptr<expression> parse_3_arg_expression(unsigned int start, unsigned int end);
@ -183,7 +184,7 @@ struct range_t {
/// Base class for expressions. /// Base class for expressions.
class expression { class expression {
protected: 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: public:
const token_t token; const token_t token;
@ -199,8 +200,8 @@ class expression {
class unary_primary : public expression { class unary_primary : public expression {
public: public:
wcstring arg; wcstring arg;
unary_primary(token_t tok, range_t where, const wcstring &what) unary_primary(token_t tok, range_t where, wcstring what)
: expression(tok, where), arg(what) {} : expression(tok, where), arg(std::move(what)) {}
bool evaluate(wcstring_list_t &errors); bool evaluate(wcstring_list_t &errors);
}; };
@ -210,8 +211,8 @@ class binary_primary : public expression {
wcstring arg_left; wcstring arg_left;
wcstring arg_right; wcstring arg_right;
binary_primary(token_t tok, range_t where, const wcstring &left, const wcstring &right) binary_primary(token_t tok, range_t where, wcstring left, wcstring right)
: expression(tok, where), arg_left(left), arg_right(right) {} : expression(tok, where), arg_left(std::move(left)), arg_right(std::move(right)) {}
bool evaluate(wcstring_list_t &errors); bool evaluate(wcstring_list_t &errors);
}; };

View file

@ -153,8 +153,8 @@ class completion_entry_t {
void add_option(const complete_entry_opt_t &opt); void add_option(const complete_entry_opt_t &opt);
bool remove_option(const wcstring &option, complete_option_type_t type); bool remove_option(const wcstring &option, complete_option_type_t type);
completion_entry_t(const wcstring &c, bool type) completion_entry_t(wcstring c, bool type)
: cmd(c), cmd_is_path(type), order(++kCompleteOrder) {} : cmd(std::move(c)), cmd_is_path(type), order(++kCompleteOrder) {}
}; };
/// Set of all completion entries. /// 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) complete_flags_t flags_val)
: completion(std::move(comp)), : completion(std::move(comp)),
description(std::move(desc)), description(std::move(desc)),
match(mat), match(std::move(mat)),
flags(resolve_auto_space(completion, flags_val)) {} flags(resolve_auto_space(completion, flags_val)) {}
completion_t::completion_t(const completion_t &him) = default; completion_t::completion_t(const completion_t &him) = default;
@ -310,7 +310,7 @@ class completer_t {
} }
public: 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(); } bool empty() const { return completions.empty(); }
const std::vector<completion_t> &get_completions() { return completions; } const std::vector<completion_t> &get_completions() { return completions; }

View file

@ -255,8 +255,8 @@ static bool append_file_entry(fish_message_type_t type, const wcstring &key_in,
return success; return success;
} }
env_universal_t::env_universal_t(const wcstring &path) env_universal_t::env_universal_t(wcstring path)
: explicit_vars_path(path), tried_renaming(false), last_read_file(kInvalidFileID) {} : explicit_vars_path(std::move(path)), tried_renaming(false), last_read_file(kInvalidFileID) {}
maybe_t<env_var_t> env_universal_t::get(const wcstring &name) const { maybe_t<env_var_t> env_universal_t::get(const wcstring &name) const {
var_table_t::const_iterator where = vars.find(name); var_table_t::const_iterator where = vars.find(name);

View file

@ -22,8 +22,8 @@ struct callback_data_t {
wcstring key; wcstring key;
wcstring val; wcstring val;
callback_data_t(fish_message_type_t t, const wcstring &k, const wcstring &v) callback_data_t(fish_message_type_t t, wcstring k, wcstring v)
: type(t), key(k), val(v) {} : type(t), key(std::move(k)), val(std::move(v)) {}
}; };
typedef std::vector<struct callback_data_t> callback_data_list_t; typedef std::vector<struct callback_data_t> callback_data_list_t;
@ -68,7 +68,7 @@ class env_universal_t {
static var_table_t read_message_internal(int fd); static var_table_t read_message_internal(int fd);
public: public:
explicit env_universal_t(const wcstring &path); explicit env_universal_t(wcstring path);
// Get the value of the variable with the specified name. // Get the value of the variable with the specified name.
maybe_t<env_var_t> get(const wcstring &name) const; maybe_t<env_var_t> get(const wcstring &name) const;

View file

@ -687,12 +687,12 @@ class highlighter_t {
public: public:
// Constructor // Constructor
highlighter_t(const wcstring &str, size_t pos, const env_vars_snapshot_t &ev, 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), : buff(str),
cursor_pos(pos), cursor_pos(pos),
vars(ev), vars(ev),
io_ok(can_do_io), io_ok(can_do_io),
working_directory(wd), working_directory(std::move(wd)),
color_array(str.size()) { color_array(str.size()) {
// Parse the tree. // Parse the tree.
parse_tree_from_string(buff, parse_flag_continue_after_error | parse_flag_include_comments, parse_tree_from_string(buff, parse_flag_continue_after_error | parse_flag_include_comments,

View file

@ -721,8 +721,8 @@ history_t &history_t::history_with_name(const wcstring &name) {
return histories.get_creating(name); return histories.get_creating(name);
} }
history_t::history_t(const wcstring &pname) history_t::history_t(wcstring pname)
: name(pname), : name(std::move(pname)),
first_unwritten_new_item_index(0), first_unwritten_new_item_index(0),
has_pending_item(false), has_pending_item(false),
disable_automatic_save_counter(0), disable_automatic_save_counter(0),

View file

@ -216,7 +216,7 @@ class history_t {
history_item_t item_at_index_assume_locked(size_t idx); history_item_t item_at_index_assume_locked(size_t idx);
public: public:
explicit history_t(const wcstring &); // constructor explicit history_t(wcstring ); // constructor
// Returns history with the given name, creating it if necessary. // Returns history with the given name, creating it if necessary.
static history_t &history_with_name(const wcstring &name); static history_t &history_with_name(const wcstring &name);

View file

@ -45,9 +45,9 @@ struct input_mapping_t {
/// New mode that should be switched to after command evaluation. /// New mode that should be switched to after command evaluation.
wcstring sets_mode; wcstring sets_mode;
input_mapping_t(const wcstring &s, const std::vector<wcstring> &c, const wcstring &m, input_mapping_t(wcstring s, std::vector<wcstring> c, wcstring m,
const wcstring &sm) wcstring sm)
: seq(s), commands(c), mode(m), sets_mode(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; static unsigned int s_last_input_map_spec_order = 0;
specification_order = ++s_last_input_map_spec_order; specification_order = ++s_last_input_map_spec_order;
} }

View file

@ -17,6 +17,7 @@
#include <list> #include <list>
#include <memory> #include <memory>
#include <type_traits> #include <type_traits>
#include <utility>
#include "common.h" #include "common.h"
#include "env.h" #include "env.h"

View file

@ -6,6 +6,7 @@
#include <algorithm> #include <algorithm>
#include <memory> #include <memory>
#include <utility>
#include "common.h" #include "common.h"
#include "env.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) {} 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) 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(n) {} : 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) {} source_block_t::source_block_t(const wchar_t *src) : block_t(SOURCE), source_file(src) {}

View file

@ -111,7 +111,7 @@ struct event_block_t : public block_t {
struct function_block_t : public block_t { struct function_block_t : public block_t {
const process_t *process; const process_t *process;
wcstring name; 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 { struct source_block_t : public block_t {

View file

@ -32,6 +32,7 @@
#include <algorithm> // IWYU pragma: keep #include <algorithm> // IWYU pragma: keep
#include <memory> #include <memory>
#include <utility>
#include <vector> #include <vector>
#include "common.h" #include "common.h"
@ -351,8 +352,8 @@ process_t::process_t() {}
/// the Linux kernel will use it for kernel processes. /// the Linux kernel will use it for kernel processes.
/// -1 should not be used; it is a possible return value of the getpgid() /// -1 should not be used; it is a possible return value of the getpgid()
/// function /// function
job_t::job_t(job_id_t jobid, const io_chain_t &bio) job_t::job_t(job_id_t jobid, io_chain_t bio)
: block_io(bio), pgid(-2), tmodes(), job_id(jobid), flags(0) {} : block_io(std::move(bio)), pgid(-2), tmodes(), job_id(jobid), flags(0) {}
job_t::~job_t() { release_job_id(job_id); } job_t::~job_t() { release_job_id(job_id); }

View file

@ -177,7 +177,7 @@ class job_t {
void operator=(const job_t &) = delete; void operator=(const job_t &) = delete;
public: public:
job_t(job_id_t jobid, const io_chain_t &bio); job_t(job_id_t jobid, io_chain_t bio);
~job_t(); ~job_t();
/// Returns whether the command is empty. /// Returns whether the command is empty.

View file

@ -611,8 +611,8 @@ class wildcard_expander_t {
} }
public: public:
wildcard_expander_t(const wcstring &wd, expand_flags_t f, std::vector<completion_t> *r) wildcard_expander_t(wcstring wd, expand_flags_t f, std::vector<completion_t> *r)
: working_directory(wd), : working_directory(std::move(wd)),
flags(f), flags(f),
resolved_completions(r), resolved_completions(r),
did_interrupt(false), did_interrupt(false),