Eliminate moved_ref

Use real rvalue references instead
This commit is contained in:
ridiculousfish 2017-01-26 15:36:12 -08:00
parent 1634c9df78
commit fec83fa975
8 changed files with 16 additions and 29 deletions

View File

@ -479,15 +479,6 @@ inline wcstring to_string(const int &x) {
return to_string(static_cast<long>(x)); return to_string(static_cast<long>(x));
} }
// A hackish thing to simulate rvalue references in C++98. The idea is that you can define a
// constructor to take a moved_ref<T> and then swap() out of it.
template <typename T>
struct moved_ref {
T &val;
explicit moved_ref(T &v) : val(v) {}
};
wchar_t **make_null_terminated_array(const wcstring_list_t &lst); wchar_t **make_null_terminated_array(const wcstring_list_t &lst);
char **make_null_terminated_array(const std::vector<std::string> &lst); char **make_null_terminated_array(const std::vector<std::string> &lst);

View File

@ -75,10 +75,10 @@ static wcstring profiling_cmd_name_for_redirectable_block(const parse_node_t &no
return result; return result;
} }
parse_execution_context_t::parse_execution_context_t(moved_ref<parse_node_tree_t> t, parse_execution_context_t::parse_execution_context_t(parse_node_tree_t t,
const wcstring &s, parser_t *p, const wcstring &s, parser_t *p,
int initial_eval_level) int initial_eval_level)
: tree(t), : tree(std::move(t)),
src(s), src(s),
parser(p), parser(p),
eval_level(initial_eval_level), eval_level(initial_eval_level),

View File

@ -127,7 +127,7 @@ class parse_execution_context_t {
int line_offset_of_character_at_offset(size_t char_idx); int line_offset_of_character_at_offset(size_t char_idx);
public: public:
parse_execution_context_t(moved_ref<parse_node_tree_t> t, const wcstring &s, parser_t *p, parse_execution_context_t(parse_node_tree_t t, const wcstring &s, parser_t *p,
int initial_eval_level); int initial_eval_level);
/// Returns the current eval level. /// Returns the current eval level.

View File

@ -603,15 +603,11 @@ void parse_ll_t::determine_node_ranges(void) {
void parse_ll_t::acquire_output(parse_node_tree_t *output, parse_error_list_t *errors) { void parse_ll_t::acquire_output(parse_node_tree_t *output, parse_error_list_t *errors) {
if (output != NULL) { if (output != NULL) {
output->swap(this->nodes); *output = std::move(this->nodes);
} }
this->nodes.clear();
if (errors != NULL) { if (errors != NULL) {
errors->swap(this->errors); *errors = std::move(this->errors);
} }
this->errors.clear();
this->symbol_stack.clear();
} }
void parse_ll_t::parse_error(parse_token_t token, parse_error_code_t code, const wchar_t *fmt, void parse_ll_t::parse_error(parse_token_t token, parse_error_code_t code, const wchar_t *fmt,

View File

@ -140,8 +140,10 @@ class parse_node_t {
class parse_node_tree_t : public std::vector<parse_node_t> { class parse_node_tree_t : public std::vector<parse_node_t> {
public: public:
parse_node_tree_t() {} parse_node_tree_t() {}
parse_node_tree_t(parse_node_tree_t &&) = default;
parse_node_tree_t(moved_ref<parse_node_tree_t> t) { this->swap(t.val); } parse_node_tree_t &operator=(parse_node_tree_t &&) = default;
parse_node_tree_t(const parse_node_tree_t &) = delete; // no copying
parse_node_tree_t &operator=(const parse_node_tree_t &) = delete; // no copying
// Get the node corresponding to a child of the given node, or NULL if there is no such child. // Get the node corresponding to a child of the given node, or NULL if there is no such child.
// If expected_type is provided, assert that the node has that type. // If expected_type is provided, assert that the node has that type.

View File

@ -593,15 +593,14 @@ int parser_t::eval(const wcstring &cmd, const io_chain_t &io, enum block_type_t
fwprintf(stderr, L"%ls\n", backtrace_and_desc.c_str()); fwprintf(stderr, L"%ls\n", backtrace_and_desc.c_str());
return 1; return 1;
} }
return this->eval_acquiring_tree(cmd, io, block_type, moved_ref<parse_node_tree_t>(tree)); return this->eval(cmd, io, block_type, std::move(tree));
} }
int parser_t::eval_acquiring_tree(const wcstring &cmd, const io_chain_t &io, int parser_t::eval(const wcstring &cmd, const io_chain_t &io, enum block_type_t block_type, parse_node_tree_t tree) {
enum block_type_t block_type, moved_ref<parse_node_tree_t> tree) {
CHECK_BLOCK(1); CHECK_BLOCK(1);
assert(block_type == TOP || block_type == SUBST); assert(block_type == TOP || block_type == SUBST);
if (tree.val.empty()) { if (tree.empty()) {
return 0; return 0;
} }
@ -613,7 +612,8 @@ int parser_t::eval_acquiring_tree(const wcstring &cmd, const io_chain_t &io,
(execution_contexts.empty() ? -1 : execution_contexts.back()->current_eval_level()); (execution_contexts.empty() ? -1 : execution_contexts.back()->current_eval_level());
// Append to the execution context stack. // Append to the execution context stack.
execution_contexts.push_back(make_unique<parse_execution_context_t>(tree, cmd, this, exec_eval_level)); execution_contexts.push_back(make_unique<parse_execution_context_t>(std::move(tree), cmd,
this, exec_eval_level));
const parse_execution_context_t *ctx = execution_contexts.back().get(); const parse_execution_context_t *ctx = execution_contexts.back().get();
// Execute the first node. // Execute the first node.

View File

@ -244,9 +244,7 @@ class parser_t {
int eval(const wcstring &cmd, const io_chain_t &io, enum block_type_t block_type); int eval(const wcstring &cmd, const io_chain_t &io, enum block_type_t block_type);
/// Evaluate the expressions contained in cmd, which has been parsed into the given parse tree. /// Evaluate the expressions contained in cmd, which has been parsed into the given parse tree.
/// This takes ownership of the tree. int eval(const wcstring &cmd, const io_chain_t &io, enum block_type_t block_type, parse_node_tree_t t);
int eval_acquiring_tree(const wcstring &cmd, const io_chain_t &io, enum block_type_t block_type,
moved_ref<parse_node_tree_t> t);
/// Evaluates a block node at the given node offset in the topmost execution context. /// Evaluates a block node at the given node offset in the topmost execution context.
int eval_block_node(node_offset_t node_idx, const io_chain_t &io, enum block_type_t block_type); int eval_block_node(node_offset_t node_idx, const io_chain_t &io, enum block_type_t block_type);

View File

@ -3305,7 +3305,7 @@ static int read_ni(int fd, const io_chain_t &io) {
parse_error_list_t errors; parse_error_list_t errors;
parse_node_tree_t tree; parse_node_tree_t tree;
if (!parse_util_detect_errors(str, &errors, false /* do not accept incomplete */, &tree)) { if (!parse_util_detect_errors(str, &errors, false /* do not accept incomplete */, &tree)) {
parser.eval_acquiring_tree(str, io, TOP, moved_ref<parse_node_tree_t>(tree)); parser.eval(str, io, TOP, std::move(tree));
} else { } else {
wcstring sb; wcstring sb;
parser.get_backtrace(str, errors, &sb); parser.get_backtrace(str, errors, &sb);