Commit dab3627d authored by Samuel Newbold's avatar Samuel Newbold

Argm is no longer privately an std::vector

parent 41af4942
......@@ -24,19 +24,47 @@
Argm::Argm(Variable_map* parent_map_i, Rwsh_istream_p input_i,
Rwsh_ostream_p output_i, Rwsh_ostream_p error_i) :
argc_v(0), argfunction_v(0),
input(input_i), output(output_i), error(error_i),
argfunction_v(0), input(input_i), output(output_i), error(error_i),
parent_map_v(parent_map_i) {}
Argm::Argm(const Argm& src) : Base(src), argc_v(src.argc()),
Argm::Argm(const Argm& src) : argv_v(src.argv_v),
argfunction_v(src.argfunction()->copy_pointer()),
input(src.input), output(src.output), error(src.error),
parent_map_v(src.parent_map()) {}
// constructor of Argm from a subrange with streams
Argm::Argm(const Argv& args,
Command_block* argfunction_i, Variable_map* parent_map_i,
Rwsh_istream_p input_i, Rwsh_ostream_p output_i,
Rwsh_ostream_p error_i) :
argv_v(args.begin(), args.end()),
argfunction_v(argfunction_i->copy_pointer()),
input(input_i), output(output_i), error(error_i),
parent_map_v(parent_map_i) {}
// constructor of Argm from a subrange with default streams
Argm::Argm(const Argv& args,
Command_block* argfunction_i, Variable_map* parent_map_i) :
argv_v(args.begin(), args.end()),
argfunction_v(argfunction_i->copy_pointer()),
input(default_input), output(default_output), error(default_error),
parent_map_v(parent_map_i) {}
// constructor of Argm from an initial argument and a pair of iterators
Argm::Argm(const std::string& first_string,
const Argv& subsequent_args,
Command_block* argfunction_i, Variable_map* parent_map_i,
Rwsh_istream_p input_i, Rwsh_ostream_p output_i,
Rwsh_ostream_p error_i) :
argv_v(subsequent_args.begin(), subsequent_args.end()),
argfunction_v(argfunction_i->copy_pointer()),
input(input_i), output(output_i), error(error_i),
parent_map_v(parent_map_i) {
argv_v.insert(argv_v.begin(), first_string);}
Argm& Argm::operator=(const Argm& src) {
Base::clear();
std::copy(src.begin(), src.end(), std::back_inserter(*this));
argc_v = src.argc_v;
argv_v.clear();
std::copy(src.begin(), src.end(), std::back_inserter(argv_v));
delete argfunction_v;
argfunction_v = src.argfunction()->copy_pointer();
parent_map_v = src.parent_map();
......@@ -66,12 +94,12 @@ std::string Argm::get_var(const std::string& key) const {
switch (key[0]) {
case '#': {
std::ostringstream str;
str <<size();
str <<argv_v.size();
return str.str();}
case '1': case '2': case '3': case '4': case '5': case '6': case '7':
case '8': case '9': case '0': {
int n = std::atoi(key.c_str());
if (size() > n) return (*this)[n];
if (argv_v.size() > n) return argv_v[n];
else return std::string();}
//throw Exception(Argm::Undefined_variable, key);}
default: return parent_map()->get(key);}}
......@@ -85,7 +113,7 @@ bool Argm::var_exists(const std::string& key) const {
case '1': case '2': case '3': case '4': case '5': case '6':
case '7': case '8': case '9': case '0': {
int n = std::atoi(key.c_str());
return size() > n;}
return argv_v.size() > n;}
default: return parent_map()->check(key);}}
int Argm::global(const std::string& key, const std::string& value) const {
......
// Copyright (C) 2005-2017 Samuel Newbold
// Copyright (C) 2005-2018 Samuel Newbold
class Command_block;
class Argm : private std::vector<std::string> {
typedef std::vector<std::string> Base;
unsigned argc_v;
Command_block* argfunction_v;
Variable_map* parent_map_v;
class Argm {
public:
typedef std::vector<std::string> Public_base;
typedef std::vector<std::string> Argv;
Argm(Variable_map* parent_map_i,
Rwsh_istream_p input_i, Rwsh_ostream_p output_i, Rwsh_ostream_p error_i);
template <class String_it>
Argm(String_it first_string, String_it last_string,
Argm(const Argv& args,
Command_block* argfunction_i, Variable_map* parent_map_i,
Rwsh_istream_p input_i, Rwsh_ostream_p output_i, Rwsh_ostream_p error_i);
template <class String_it>
Argm(const std::string& first_string,
String_it second_string, String_it last_string,
Argm(const Argv& args, Command_block* argfunction_i,
Variable_map* parent_map_i);
Argm(const std::string& first_string, const Argv& subsequent_args,
Command_block* argfunction_i, Variable_map* parent_map_i,
Rwsh_istream_p input_i, Rwsh_ostream_p output_i, Rwsh_ostream_p error_i);
Argm(const Argm& src);
......@@ -114,31 +108,37 @@ class Argm : private std::vector<std::string> {
bool var_exists(const std::string& key) const;
// map semantics
typedef Base::size_type size_type;
typedef Base::value_type value_type;
typedef Base::iterator iterator;
typedef Base::const_iterator const_iterator;
typedef Base::const_reference const_reference;
typedef Argv::size_type size_type;
typedef Argv::value_type value_type;
typedef Argv::iterator iterator;
typedef Argv::reference reference;
typedef Argv::const_iterator const_iterator;
typedef Argv::const_reference const_reference;
// vector semantics: to the extent possible this structure should be thought
// of as a map, but it is also a representation of a command line, and so
// there is an assigned order to its members, and some operations should only
// need or want to know that much
const_iterator begin(void) const {return Base::begin();};
const_iterator end(void) const {return Base::end();};
const_reference back(void) const {return (*this)[argc()-1];};
//later//void push_back(const std::string& x) {(*this)[argc_v++] = x;};
void push_back(const std::string& x) {Base::push_back(x); argc_v++;};
void pop_back(void) {Base::pop_back(); --argc_v;};
reference operator[] (int i) {return Base::operator[](i);};
const_reference operator[] (int i) const {return Base::operator[](i);};
const_iterator begin(void) const {return argv_v.begin();};
const_iterator end(void) const {return argv_v.end();};
const_reference back(void) const {return argv_v.back();};
void push_back(const std::string& x) {argv_v.push_back(x);};
void pop_back(void) {argv_v.pop_back();};
reference operator[] (int i) {return argv_v[i];};
const_reference operator[] (int i) const {return argv_v[i];};
// special vector semantics
unsigned argc(void) const {return argc_v;};
Public_base subrange(unsigned start) const {
return Public_base(begin()+start, end());};
Public_base subrange(unsigned start, unsigned before_end) const {
return Public_base(begin()+start, end()-before_end);}; };
const Argv& argv(void) const {return argv_v;};
unsigned argc(void) const {return argv_v.size();};
Argv subrange(unsigned start) const {
return Argv(begin()+start, end());};
Argv subrange(unsigned start, unsigned before_end) const {
return Argv(begin()+start, end()-before_end);};
private:
Argv argv_v;
Command_block* argfunction_v;
Variable_map* parent_map_v; };
struct Error_list : public std::list<Argm> {
void add_error(const Argm& error); };
......
......@@ -4,39 +4,13 @@
//
// Copyright (C) 2006-2018 Samuel Newbold
// constructor of Argm from a pair of iterators
template <class String_it>
inline Argm::Argm(String_it first_string, String_it last_string,
Command_block* argfunction_i, Variable_map* parent_map_i,
Rwsh_istream_p input_i, Rwsh_ostream_p output_i,
Rwsh_ostream_p error_i) :
Base(first_string, last_string),
argfunction_v(argfunction_i->copy_pointer()),
input(input_i), output(output_i), error(error_i),
parent_map_v(parent_map_i) {
argc_v = Base::size();};
// constructor of Argm from an initial argument and a pair of iterators
template <class String_it>
inline Argm::Argm(const std::string& first_string,
String_it second_string, String_it last_string,
Command_block* argfunction_i, Variable_map* parent_map_i,
Rwsh_istream_p input_i, Rwsh_ostream_p output_i,
Rwsh_ostream_p error_i) :
Base(second_string, last_string),
argfunction_v(argfunction_i->copy_pointer()),
input(input_i), output(output_i), error(error_i),
parent_map_v(parent_map_i) {
Base::insert(Base::begin(), first_string);
argc_v = Base::size();};
// write the strings corresponding to $*
template<class Out>
inline Out Argm::star_var(const std::string& key, unsigned reference_level,
Out res) const {
int n = std::atoi(key.c_str());
if (n < 0) n = 0;
else if (n >= size()) n = size();
else if (n >= argv_v.size()) n = argv_v.size();
for (const_iterator i = begin()+n; i != end(); ++i) {
std::string next = *i;
for (unsigned i = 0; i < reference_level; ++i) next = get_var(next);
......
This diff is collapsed.
......@@ -110,7 +110,7 @@ void Base_executable::exception_handler(Error_list& exceptions) {
exceptions.pop_back();
unwind_stack_v = false;}}
if (failed_handlers.find(focus[0]) != failed_handlers.end())
b_fallback_handler(Argm(".fallback_handler", focus.begin(), focus.end(),
b_fallback_handler(Argm(".fallback_handler", focus.argv(),
focus.argfunction(), Variable_map::global_map,
focus.input, focus.output, focus.error),
exceptions);}
......
......@@ -50,7 +50,7 @@ Base_executable* Executable_map::find(const Argm& key) {
else return nullptr;}
bool Executable_map::run_if_exists(const std::string& key, Argm& argm_i) {
Argm temp_argm(key, argm_i.begin(), argm_i.end(), argm_i.argfunction(),
Argm temp_argm(key, argm_i.argv(), argm_i.argfunction(),
argm_i.parent_map(), argm_i.input,argm_i.output, argm_i.error);
Base_executable* i = find(temp_argm);
if (i) {
......@@ -85,7 +85,7 @@ int Executable_map::run(Argm& argm, Error_list& exceptions) {
if (is_function_name(argm[0])) { // try autofunction
if (in_autofunction) return not_found(argm); // nested autofunction
in_autofunction = true;
Argm auto_argm("rwsh.autofunction", argm.begin(), argm.end(),
Argm auto_argm("rwsh.autofunction", argm.argv(),
argm.argfunction(), argm.parent_map(),
argm.input, argm.output, argm.error);
run(auto_argm, exceptions);
......
......@@ -81,7 +81,7 @@ int Command_block::prototype_execute(const Argm& argm,
const Prototype& prototype,
Error_list& exceptions) const {
Variable_map locals(prototype.arg_to_param(argm));
Argm params(argm.begin(), argm.end(), argm.argfunction(), &locals,
Argm params(argm.argv(), argm.argfunction(), &locals,
argm.input, argm.output, argm.error);
try {
int ret = execute(params, exceptions);
......
......@@ -144,8 +144,10 @@ int main(int argc, char *argv[]) {
catch (Exception& exception) {
executable_map.base_run(exception);}
Command_stream command_stream(default_input, true);
Argm init_command(".init", &argv[0], &argv[argc], 0, Variable_map::global_map,
default_input, default_output, default_error);
Argm::Argv std_argv(&argv[0], &argv[argc]);
Argm init_command(".init", std_argv,
nullptr, Variable_map::global_map,
default_input, default_output, default_error);
executable_map.base_run(init_command);
register_signals();
Arg_script script("", 0);
......@@ -164,7 +166,7 @@ int main(int argc, char *argv[]) {
executable_map.base_run(command);
executable_map.run_if_exists("rwsh.after_command", command);}
Argm shutdown_command(Argm::exception_names[Argm::Shutdown],
&argv[0], &argv[argc], 0, Variable_map::global_map,
std_argv, nullptr, Variable_map::global_map,
default_input, default_output, default_error);
executable_map.base_run(shutdown_command);
executable_map.unused_var_check_at_exit();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment