2014-08-27 18:31:31 +05:30
|
|
|
#ifndef BASE_APPLICATION_H
|
|
|
|
#define BASE_APPLICATION_H
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
#include <list>
|
2020-04-30 19:32:43 +05:30
|
|
|
#include <memory>
|
2014-08-27 18:31:31 +05:30
|
|
|
#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:
|
2020-04-30 19:32:43 +05:30
|
|
|
typedef std::shared_ptr<command> ptr;
|
2016-01-08 18:21:52 +05:30
|
|
|
|
|
|
|
command(std::string const &name);
|
|
|
|
virtual ~command() {}
|
|
|
|
|
|
|
|
void die(std::string const &msg);
|
2021-06-02 10:38:47 +05:30
|
|
|
uint64_t parse_uint64(char const *str, char 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) {
|
|
|
|
}
|
|
|
|
|
2020-04-08 16:49:06 +05:30
|
|
|
int run(int argc, char **argv) {
|
2014-08-27 18:31:31 +05:30
|
|
|
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
|