class Appl reworked.
This commit is contained in:
+147
-61
@@ -25,93 +25,179 @@
|
||||
#include <map>
|
||||
#include <vector>
|
||||
|
||||
/** the available data types. */
|
||||
enum DataType {
|
||||
dt_none, // default value
|
||||
dt_bool, //
|
||||
dt_int, //
|
||||
dt_long, //
|
||||
dt_float, //
|
||||
dt_string, //
|
||||
};
|
||||
|
||||
/** option types. */
|
||||
enum OptionType {
|
||||
ot_none, // default value
|
||||
ot_optional, // a value is optional
|
||||
ot_mandatory, // a value is mandatory
|
||||
};
|
||||
|
||||
/** structure for defining application options */
|
||||
typedef struct opt {
|
||||
const char* name;
|
||||
const char* shortname;
|
||||
const char* description;
|
||||
DataType datatype;
|
||||
OptionType optiontype;
|
||||
} opt_t;
|
||||
|
||||
/** union for option values */
|
||||
union OptVal {
|
||||
bool b;
|
||||
int i;
|
||||
long l;
|
||||
float f;
|
||||
const char* c;
|
||||
|
||||
OptVal() { memset(this, 0, sizeof(OptVal)); }
|
||||
OptVal(bool _b) : b(_b) {}
|
||||
OptVal(int _i) : i(_i) {}
|
||||
OptVal(long _l) : l(_l) {}
|
||||
OptVal(float _f) : f(_f) {}
|
||||
OptVal(const char* _c) : c(_c) {}
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @brief class for all kinds of application parameters.
|
||||
*/
|
||||
class Appl
|
||||
{
|
||||
|
||||
private:
|
||||
struct Option;
|
||||
|
||||
public:
|
||||
enum Datatype { type_none, type_bool, type_int, type_long, type_float, type_string };
|
||||
enum Optiontype { opt_none, opt_optional, opt_mandatory };
|
||||
|
||||
union Param {
|
||||
bool b;
|
||||
int i;
|
||||
long l;
|
||||
float f;
|
||||
const char* c;
|
||||
|
||||
Param() { memset(this, 0, sizeof(Param)); }
|
||||
Param(bool _b) : b(_b) {}
|
||||
Param(int _i) : i(_i) {}
|
||||
Param(long _l) : l(_l) {}
|
||||
Param(float _f) : f(_f) {}
|
||||
Param(const char* _c) : c(_c) {}
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
T getParam(const char* name)
|
||||
{
|
||||
p_it = m_params.find(name);
|
||||
return (reinterpret_cast<T&>(p_it->second));
|
||||
}
|
||||
|
||||
static Appl& Instance();
|
||||
/**
|
||||
* @brief create an Appl instance and return the reference.
|
||||
* @param command is true if an command is needed.
|
||||
* @return the reference to instance.
|
||||
*/
|
||||
static Appl& Instance(const bool command=false);
|
||||
|
||||
/**
|
||||
* @brief destructor.
|
||||
*/
|
||||
~Appl();
|
||||
|
||||
void addArgs(const std::string argTxt, const int argNum);
|
||||
size_t numArg() const { return m_argValues.size(); }
|
||||
std::string getArg(const int argNum) const { return m_argValues[argNum]; }
|
||||
/**
|
||||
* @brief save application version string.
|
||||
* @param version string
|
||||
*/
|
||||
void setVersion(const char* version) { m_version = version; }
|
||||
|
||||
void addItem(const char* name, Param param, const char* shortname,
|
||||
const char* longname, const char* description,
|
||||
Datatype datatype, Optiontype optiontype);
|
||||
/**
|
||||
* @brief create new entry of application option only for help page.
|
||||
* @param text string to print
|
||||
*/
|
||||
void addText(const char* text);
|
||||
|
||||
void addVersion(const char* version);
|
||||
/**
|
||||
* @brief create new entry of application option.
|
||||
* @param name the long name
|
||||
* @param shortname optional short name
|
||||
* @param optval value of option
|
||||
* @param datatype data type of option value
|
||||
* @param optiontype type of given option
|
||||
* @param description hint text for help page
|
||||
*/
|
||||
void addOption(const char* name, const char* shortname, OptVal optval,
|
||||
DataType datatype, OptionType optiontype, const char* description);
|
||||
|
||||
/**
|
||||
* @brief returns the value of the interested option
|
||||
* @param the interested option
|
||||
* @return casted value
|
||||
*/
|
||||
template <typename T>
|
||||
T getOptVal(const char* name)
|
||||
{
|
||||
ov_it = m_optvals.find(name);
|
||||
return (reinterpret_cast<T&>(ov_it->second));
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief parse application arguments.
|
||||
* @param argc the number of options
|
||||
* @param argv the given options
|
||||
*/
|
||||
void parseArgs(int argc, char* argv[]);
|
||||
void printSettings();
|
||||
|
||||
/**
|
||||
* @brief returns the number of saved commands and arguments
|
||||
* @return number of commands and arguments
|
||||
*/
|
||||
int numArgs() const { return m_arguments.size(); }
|
||||
|
||||
/**
|
||||
* @brief returns the string of an interested argument (0 = command)
|
||||
* @param num number of interested argument
|
||||
* @return string value
|
||||
*/
|
||||
std::string getArg(const int num) const { return m_arguments[num]; }
|
||||
|
||||
private:
|
||||
Appl() {}
|
||||
/** private constructor - singleton pattern*/
|
||||
Appl(const bool command) : m_needCommand(command) {}
|
||||
Appl(const Appl&);
|
||||
Appl& operator= (const Appl&);
|
||||
Appl& operator=(const Appl&);
|
||||
|
||||
struct Arg {
|
||||
const char* name;
|
||||
const char* shortname;
|
||||
const char* longname;
|
||||
const char* description;
|
||||
Datatype datatype;
|
||||
Optiontype optiontype;
|
||||
};
|
||||
/** application options */
|
||||
std::vector<opt_t> m_opts;
|
||||
std::vector<opt_t>::const_iterator o_it;
|
||||
|
||||
size_t m_argc;
|
||||
/** map option - value */
|
||||
std::map<const char*, OptVal> m_optvals;
|
||||
std::map<const char*, OptVal>::iterator ov_it;
|
||||
|
||||
/** given arguments */
|
||||
std::vector<std::string> m_argv;
|
||||
|
||||
std::vector<Arg> m_args;
|
||||
std::vector<Arg>::const_iterator a_it;
|
||||
|
||||
std::map<const char*, Param> m_params;
|
||||
std::map<const char*, Param>::iterator p_it;
|
||||
|
||||
std::string m_argTxt;
|
||||
size_t m_argNum;
|
||||
|
||||
/** application version string */
|
||||
const char* m_version;
|
||||
|
||||
std::vector<std::string> m_argValues;
|
||||
/** true if the application need a command */
|
||||
bool m_needCommand;
|
||||
|
||||
bool checkArg(const std::string& name, const std::string& arg);
|
||||
/** arguments (argument 0 = command) string */
|
||||
std::vector<std::string> m_arguments;
|
||||
|
||||
void addParam(const char* name, Param param) { m_params[name] = param; }
|
||||
/**
|
||||
* @brief checks the passed parameter if this is a valid option.
|
||||
* @param option to check
|
||||
* @param value to save if paramter is a valid option
|
||||
*/
|
||||
bool checkOption(const std::string& option, const std::string& value);
|
||||
|
||||
void addParam(const char* name, const std::string arg, Datatype datatype);
|
||||
/**
|
||||
* @brief save the passed value to option.
|
||||
* @param option name
|
||||
* @param value to save
|
||||
* @param datatype of given option
|
||||
*/
|
||||
void setOptVal(const char* option, const std::string value, DataType datatype);
|
||||
|
||||
/**
|
||||
* @brief print application version.
|
||||
*/
|
||||
void printVersion();
|
||||
|
||||
/**
|
||||
* @brief print help page.
|
||||
*/
|
||||
void printHelp();
|
||||
|
||||
/**
|
||||
* @brief print used option settings.
|
||||
*/
|
||||
void printSettings();
|
||||
};
|
||||
|
||||
#endif // LIBUTILS_APPL_H_
|
||||
|
||||
Reference in New Issue
Block a user