Permalink
Browse files

No indent for namespaces

  • Loading branch information...
petya2164 committed Sep 24, 2016
1 parent 65fed88 commit 516dde8b49f3a8096b48d27990a751732acad76b
Showing with 221 additions and 221 deletions.
  1. +21 −21 src/skipper/any.hpp
  2. +143 −143 src/skipper/program.hpp
  3. +30 −30 src/skipper/range.hpp
  4. +27 −27 src/skipper/set.hpp
View
@@ -11,27 +11,27 @@
namespace skipper
{
/// validates true no matter what value provided
template<typename Type>
struct any
/// validates true no matter what value provided
template<typename Type>
struct any
{
/// check if a value is ok to use
/// @param value the value to check
/// @return wether the provided value checked
bool operator()(const Type value) const
{
/// check if a value is ok to use
/// @param value the value to check
/// @return wether the provided value checked
bool operator()(const Type value) const
{
(void) value;
return true;
}
(void) value;
return true;
}
/// print help text by overloading the << operator
/// @param os an output stream
/// @param s a set
/// @return friendly help stream
friend std::ostream& operator<<(std::ostream& os, const any& a)
{
(void) a;
return os << "of type " << typeid(Type).name() << " of any value";
}
};
/// print help text by overloading the << operator
/// @param os an output stream
/// @param s a set
/// @return friendly help stream
friend std::ostream& operator<<(std::ostream& os, const any& a)
{
(void) a;
return os << "of type " << typeid(Type).name() << " of any value";
}
};
}
View
@@ -17,184 +17,184 @@
namespace skipper
{
/// generic converter
template<typename Type>
const Type default_convert(std::istream& in)
/// generic converter
template<typename Type>
const Type default_convert(std::istream& in)
{
Type value;
if (!(in >> value))
{
Type value;
if (!(in >> value))
{
// reset the input stream error state and read all inputs
in.clear();
std::string temp;
in >> temp;
throw std::string("Could not convert the input");
}
// reset the input stream error state and read all inputs
in.clear();
std::string temp;
in >> temp;
return value;
throw std::string("Could not convert the input");
}
/// defines a number of commands that can be executed by the user making
/// various function calls optinally providing an argument
class program
{
return value;
}
public:
/// defines a number of commands that can be executed by the user making
/// various function calls optinally providing an argument
class program
{
/// the command type
using command_type = std::function<void()>;
public:
/// storage for a command
using command_storage = std::tuple<command_type, std::string>;
/// the command type
using command_type = std::function<void()>;
/// @param description
/// @param in the input stream to use
/// @param out the output stream to use
program(std::string description,
std::istream& in = std::cin,
std::ostream& out = std::cout) :
m_description(description), m_in(in), m_out(out)
{
add_command("h", "print this help", [&]() {m_out << *this;});
}
/// storage for a command
using command_storage = std::tuple<command_type, std::string>;
/// add a command to the program
/// @param key what to press to pick the command
/// @param description help text for the command
/// @param call the function that will be called by the command
void add_command(const std::string key,
std::string description,
command_type call)
{
assert(!m_commands.count(key));
/// @param description
/// @param in the input stream to use
/// @param out the output stream to use
program(std::string description,
std::istream& in = std::cin,
std::ostream& out = std::cout) :
m_description(description), m_in(in), m_out(out)
{
add_command("h", "print this help", [&]() {m_out << *this;});
}
m_commands[key] = std::make_tuple(call, description);
}
/// add a command to the program
/// @param key what to press to pick the command
/// @param description help text for the command
/// @param call the function that will be called by the command
void add_command(const std::string key,
std::string description,
command_type call)
{
assert(!m_commands.count(key));
/// @copydoc add_command
/// @param validate Predicate that validates the input for the command
/// @param convert function that converts the input string
template<typename Type, typename Predicate = any<Type>>
void add_command(const std::string key,
std::string description,
const std::function<void(const Type)>& call,
const Predicate& validate = any<Type>(),
std::function<const Type(std::istream&)> convert =
default_convert<Type>)
{
assert(!m_commands.count(key) && "Duplicated command");
m_commands[key] = std::make_tuple(call, description);
}
auto set_command = [=]()
{
Type value;
try
{
value = convert(m_in);
}
catch (std::string str)
{
m_out << str << std::endl;
return;
}
if (validate(value))
call(value);
else
m_out << "Invalid input, press 'h' for help" << std::endl;
};
std::ostringstream help;
help << description << ", " << validate;
add_command(key, description, set_command);
}
/// @copydoc add_command
/// @param validate Predicate that validates the input for the command
/// @param convert function that converts the input string
template<typename Type, typename Predicate = any<Type>>
void add_command(const std::string key,
std::string description,
const std::function<void(const Type)>& call,
const Predicate& validate = any<Type>(),
std::function<const Type(std::istream&)> convert =
default_convert<Type>)
{
assert(!m_commands.count(key) && "Duplicated command");
/// run the program
int run()
auto set_command = [=]()
{
if (m_print_help)
m_out << *this;
Type value;
try
{
value = convert(m_in);
}
catch (std::string str)
{
m_out << str << std::endl;
return;
}
std::string in_key;
if (validate(value))
call(value);
else
m_out << "Invalid input, press 'h' for help" << std::endl;
};
m_out << m_ready_indicator;
while (m_in >> in_key)
{
if (in_key == m_exit_key)
return 0;
std::ostringstream help;
help << description << ", " << validate;
add_command(key, description, set_command);
}
if (m_commands.count(in_key))
std::get<0>(m_commands[in_key])();
else
m_out << "Invalid command, press 'h' for help" << std::endl;
/// run the program
int run()
{
if (m_print_help)
m_out << *this;
m_out << m_ready_indicator;
}
return 0;
}
std::string in_key;
/// Set wether help should be printed on run
/// @param help_on print help at run
void set_print_help(bool help_on)
m_out << m_ready_indicator;
while (m_in >> in_key)
{
m_print_help = help_on;
}
if (in_key == m_exit_key)
return 0;
/// set the ready indicator, use "" for no indicator
/// @param indicator the ready indicator
void set_ready_indicator(const std::string& indicator)
{
m_ready_indicator = indicator;
}
if (m_commands.count(in_key))
std::get<0>(m_commands[in_key])();
else
m_out << "Invalid command, press 'h' for help" << std::endl;
/// Set the quit / escape key
/// @param exit_key the key to exit the program
void set_exit_key(const std::string& exit_key)
{
m_exit_key = exit_key;
m_out << m_ready_indicator;
}
return 0;
}
/// Set wether help should be printed on run
/// @param help_on print help at run
void set_print_help(bool help_on)
{
m_print_help = help_on;
}
private:
/// set the ready indicator, use "" for no indicator
/// @param indicator the ready indicator
void set_ready_indicator(const std::string& indicator)
{
m_ready_indicator = indicator;
}
/// print help text by overloading the << operator
/// @param os an output stream
/// @param s a set
/// @return friendly help stream
friend std::ostream& operator<<(std::ostream& os, const program& p)
{
os << std::endl << p.m_description << std::endl << std::endl
<< "The following commands are accepted:" << std::endl;
/// Set the quit / escape key
/// @param exit_key the key to exit the program
void set_exit_key(const std::string& exit_key)
{
m_exit_key = exit_key;
}
for (auto i = p.m_commands.begin(); i != p.m_commands.end(); i++)
os << i->first << " " << std::get<1>(i->second) << std::endl;
private:
os << p.m_exit_key << " exit the program" << std::endl << std::endl;
/// print help text by overloading the << operator
/// @param os an output stream
/// @param s a set
/// @return friendly help stream
friend std::ostream& operator<<(std::ostream& os, const program& p)
{
os << std::endl << p.m_description << std::endl << std::endl
<< "The following commands are accepted:" << std::endl;
return os;
}
for (auto i = p.m_commands.begin(); i != p.m_commands.end(); i++)
os << i->first << " " << std::get<1>(i->second) << std::endl;
os << p.m_exit_key << " exit the program" << std::endl << std::endl;
return os;
}
private:
private:
/// help text for the program
std::string m_description;
/// help text for the program
std::string m_description;
/// the input stream used
std::istream& m_in;
/// the input stream used
std::istream& m_in;
/// the output stream used
std::ostream& m_out;
/// the output stream used
std::ostream& m_out;
/// the parameters support by the program
std::map<std::string, command_storage> m_commands;
/// the parameters support by the program
std::map<std::string, command_storage> m_commands;
/// Print help on run
bool m_print_help = true;
/// Print help on run
bool m_print_help = true;
/// Ready indicator
std::string m_ready_indicator = "> ";
/// Ready indicator
std::string m_ready_indicator = "> ";
/// the string that terminates the program
std::string m_exit_key = "q";
};
/// the string that terminates the program
std::string m_exit_key = "q";
};
}
Oops, something went wrong.

0 comments on commit 516dde8

Please sign in to comment.