2014-08-27 18:31:31 +05:30
|
|
|
#ifndef BASE_APPLICATION_H
|
|
|
|
#define BASE_APPLICATION_H
|
|
|
|
|
2016-01-08 18:21:52 +05:30
|
|
|
#include <boost/shared_ptr.hpp>
|
2014-08-27 18:31:31 +05:30
|
|
|
#include <iostream>
|
|
|
|
#include <list>
|
|
|
|
#include <string>
|
|
|
|
#include <stdexcept>
|
2016-01-08 18:21:52 +05:30
|
|
|
#include <stdint.h>
|
2014-08-27 18:31:31 +05:30
|
|
|
|
|
|
|
//----------------------------------------------------------------
|
|
|
|
|
|
|
|
namespace base {
|
|
|
|
class command {
|
|
|
|
public:
|
2016-01-08 18:21:52 +05:30
|
|
|
typedef boost::shared_ptr<command> ptr;
|
|
|
|
|
|
|
|
command(std::string const &name);
|
|
|
|
virtual ~command() {}
|
|
|
|
|
|
|
|
void die(std::string const &msg);
|
|
|
|
uint64_t parse_uint64(std::string const &str, std::string const &desc);
|
2014-08-27 18:31:31 +05:30
|
|
|
|
2016-01-08 18:21:52 +05:30
|
|
|
|
|
|
|
virtual void usage(std::ostream &out) const = 0;
|
|
|
|
virtual int run(int argc, char **argv) = 0;
|
2014-08-27 18:31:31 +05:30
|
|
|
|
|
|
|
std::string const &get_name() const {
|
|
|
|
return name_;
|
|
|
|
}
|
|
|
|
|
2016-01-08 18:21:52 +05:30
|
|
|
private:
|
|
|
|
std::string name_;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
class command_old : public command {
|
|
|
|
public:
|
|
|
|
typedef int (*cmd_fn)(int, char **);
|
|
|
|
|
|
|
|
command_old(std::string const &name, cmd_fn fn)
|
|
|
|
: command(name),
|
|
|
|
fn_(fn) {
|
|
|
|
}
|
|
|
|
|
2014-08-27 18:31:31 +05:30
|
|
|
int run(int argc, char **argv) const {
|
|
|
|
return fn_(argc, argv);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
cmd_fn fn_;
|
|
|
|
};
|
|
|
|
|
|
|
|
class application {
|
|
|
|
public:
|
2016-01-08 18:21:52 +05:30
|
|
|
void add_cmd(command::ptr c) {
|
|
|
|
cmds_.push_back(c);
|
2014-08-27 18:31:31 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
int run(int argc, char **argv);
|
|
|
|
|
|
|
|
private:
|
|
|
|
void usage();
|
2015-01-16 16:18:19 +05:30
|
|
|
std::string get_basename(std::string const &path) const;
|
2014-08-27 18:31:31 +05:30
|
|
|
|
2016-01-08 18:21:52 +05:30
|
|
|
std::list<command::ptr> cmds_;
|
2014-08-27 18:31:31 +05:30
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------
|
|
|
|
|
|
|
|
#endif
|