【Poco笔记】如何创建一个应用
2018.12.28 16:49:47字数 388阅读 804
在windows或linux用c++写一个应用其实很简单,直接在main里写个hello world就可以了。
那么使用Poco该如何写呢?:
#使用宏POCO_APP_MAIN
POCO_APP_MAIN(App) ; // App为继承Poco::Util::Application的子类
48
Application封装了main,并提供了一个虚函数main用于子类将自身逻辑实现写在这里。
main.PNG
除了main以外,还需要实现的函数有:
-
initialize()
每次执行之运行 -
uninitialize()
每次执行结束运行 -
reinitialize()
重新初始化,一般用于重新加载配置 -
defineOptions()
定义参数,在构造函数中运行。
Poco的参数定义十分方便,可以从下面的例子里看到。 -
handleOption()
处理参数,一般不用重载;在构造函数中运行 -
main()
运行主逻辑,在initialize之后,uninitialize之前
看一下具体例子
class MyApplication : public Poco::Util::Application {
public:
const char* name() const override {
return "MyApplication";
}
void initialize(Application& app) override {
loadConfiguration();
Application::initialize(app); // call base method
// your code here
std::cout<< "**initialize" << std::endl;
}
void uninitialize() override {
// your code here
std::cout<< "**uninitialize" << std::endl;
Application::uninitialize();
}
void reinitialize(Application& app) override {
Application::reinitialize(app);
// your code here
std::cout<< "**reinitialize" << std::endl;
}
void defineOptions(Poco::Util::OptionSet& options) override {
Application::defineOptions(options);
// help帮助
options.addOption(
Poco::Util::Option("help", "h", "display help information on command line arguments", false)
.repeatable(false)
.callback(Poco::Util::OptionCallback<MyApplication>(this, &MyApplication::handleOptionHelp)));
// 通过命令行的方式增加一个配置
options.addOption(
Poco::Util::Option("define", "d", "define a configuration property", false)
.repeatable(true)
.argument("name=value")
.callback(Poco::Util::OptionCallback<MyApplication>(this, &MyApplication::handleDefine)));
// 从文件中导入配置
options.addOption(
Poco::Util::Option("config-file", "f", "load configuration data from a file", false)
.repeatable(true)
.argument("file")
.callback(Poco::Util::OptionCallback<MyApplication>(this, &MyApplication::handleConfig)));
// b输入后面的value直接定义为bind.property = value
options.addOption(
Poco::Util::Option("bind", "b", "bind option value to test.property", false)
.repeatable(false)
.argument("value")
.binding("bind.property"));
}
int main(const std::vector<std::string>& args) override {
std::cout << "Commnad line:";
for (ArgVec::const_iterator it = argv().begin(); it != argv().end(); ++it)
{
std::cout << *it << ' ';
}
std::cout << std::endl;
std::cout << "Arguments to main():";
for (ArgVec::const_iterator it = args.begin(); it != args.end(); ++it)
{
std::cout << *it;
}
std::cout << std::endl;
std::cout << "=========print properties========"<<std::endl;
printProperties("");
return Application::EXIT_OK;
}
void handleOptionHelp(const std::string& name, const std::string& value) {
Poco::Util::HelpFormatter helpFormatter(options());
helpFormatter.setCommand(commandName());
helpFormatter.setUsage("OPTIONS");
helpFormatter.setHeader("A sample application that demonstrates some of the features of the Poco::Util::Application class.");
helpFormatter.format(std::cout);
}
void handleDefine(const std::string& name, const std::string& value)
{
defineProperty(value);
}
void handleConfig(const std::string& name, const std::string& value)
{
loadConfiguration(value);
}
void printProperties(const std::string& base)
{
Poco::Util::AbstractConfiguration::Keys keys;
config().keys(base, keys);
if (keys.empty())
{
if (config().hasProperty(base))
{
std::string msg;
msg.append(base);
msg.append(" = ");
msg.append(config().getString(base));
logger().information(msg);
}
}
else
{
for (Poco::Util::AbstractConfiguration::Keys::const_iterator it = keys.begin(); it != keys.end(); ++it)
{
std::string fullKey = base;
if (!fullKey.empty()) fullKey += '.';
fullKey.append(*it);
printProperties(fullKey);
}
}
}
void defineProperty(const std::string& def)
{
std::string name;
std::string value;
std::string::size_type pos = def.find('=');
if (pos != std::string::npos)
{
name.assign(def, 0, pos);
value.assign(def, pos + 1, def.length() - pos);
}
else name = def;
config().setString(name, value);
}
};
// Poco的宏定义了main,以及异常处理
POCO_APP_MAIN(MyApplication);
4812162024283236404448525660646872768084889296100104108112116120124128132136140144148152156160164168172176180184188192196200204208212216220224228232236240244248252256260264268272276280284288292296300304308312316320324328332336340344348352356360364368372376380384388392396400404408412416420424428432436440444448452456460464468472476480484488492496500504508512516520524528532536540544548552556560564568
执行命令application_demo -d Demo=1
运行结果.JPG
可以发现几个需要注意的问题。
-
main中args并不是这里的参数;argv()函数得到的才是;
那main中的args是什么呢? 它其实是除了defineOptions中定义的参数,而且不包含函数自己。如果我执行命令application_demo -d Demo=1 outofdefination 那么args里会有outofdefination
- Poco框架自动帮你存储了需要环境配置项,这些都不是你在配置。如果你在-d中输入这些配置是可以覆盖的。
-
配置的参数后面可以加空格也可以不加空格。
如命令application_demo -d Demo=1和application_demo -dDemo=1是一样的
0人点赞
更多精彩内容,就在简书APP
“你的支持是我努力的动力,再小的力量也是一种支持,谢谢。”
还没有人赞赏,支持一下
总资产37共写了3.9W字获得49个赞共54个粉丝
请登录后查看评论内容