ICE的属性配置 C++版

本文深入探讨了Ice框架中命令行参数如何通过初始化函数抽取出专有参数,并将其存入属性表中。详细介绍了属性表的常用方法,包括获取属性值、设置属性值等操作。并通过实例展示了如何利用属性表改进程序,实现更灵活的配置管理。同时,文章还阐述了如何使用属性表加载配置文件和解析命令行参数,提供了将命令行参数转化为属性表的方法,以及如何使用属性表的parseCommandLineOptions方法解析指定前缀的参数。最后,文章讨论了配置文件的使用场景,以及如何通过属性表进行自定义配置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 
(2011-09-13 20:57:05)
标签:

杂谈

 

Ice的初始化函数initialize的输入参数正好是主函数的输入参数,也就是命令行参数(注:辅助类Ice::Application也调用了initialize函数)。

Ice的初始化函数得到命令行参数后,抽取出Ice专有参数,并把分析后的配置存入到Ice的属性表中。

假设命令行为:

MyProg.exe --myoption --Ice.Config=config\ -x a --Ice.Trace.Network=3 -y opt file

其中MyProg.exe是我们用Ice编写的程序,当Ice::initialize返回时,Ice的两个专用参数就会被抽去,argc由原来的9变成7,argv为:

MyProg.exe --myoption -x a -y opt file

抽去的两个参数则存放于属性表里。属性表的类型为Ice::Properties,它的常用方法有:

std::string getProperty(const std::string& prop); std::string getPropertyWithDefault(const std::string& prop, const std::string&); Ice::Int getPropertyAsInt(const std::string& prop); Ice::Int getPropertyAsIntWithDefault(const std::string& prop, Ice::Int);

取得prop属性的值

Ice::StringSeq getPropertyAsList(const ::std::string& prop); Ice::StringSeq getPropertyAsListWithDefault(const ::std::string& prop, const ::Ice::StringSeq&);

取得prop属性的值,并返回按逗号分割成的一组值。这里的Ice::StringSeq就是std::vector<std::string>(注:集合类型可通过配置改变,默认是vector)。
比如:有命令行参数: --MyProp.Test=abc,def,aaa,sde,s 则返回
abc defaaasdes

Ice::PropertyDict getPropertiesForPrefix(const ::std::string& prefix);

返回所有含有prefix前缀的所有属性,PropertyDict是一个std::map<std::string, std::string>类型的数据表。

void setProperty(const ::std::string& prop, const ::std::string&);

设置prop属性的值

Ice::StringSeq getCommandLineOptions();

把属性表中的所有数据转化成字符串列表

Ice::StringSeq parseCommandLineOptions(     const std::string& prefix, const Ice::StringSeq& args);

分析命令行参数,抽取出所有prefix前缀的参数,返回剩余的命令行参数。由于main函数输入的是argc和argv,我们可以使用Ice::StringSeq argsToStringSeq(int, char*[])void stringSeqToArgs(const StringSeq&, int&, char*[])函数在StringSeq与argc/argv之间互相转换。

Ice::StringSeq parseIceCommandLineOptions(const Ice::StringSeq&);

分析Ice专有命令行参数

void load(const std::string& file);

载入file指定的配置文件,关于配置文件后文还会讲到

Ice::PropertiesPtr clone()

返回一个属性表的拷贝
下面是获得属性表的两个常用方式:

Ice::PropertiesPtr createProperties();

生成一个全新的属性表

Ice::PropertiesPtr Ice::Communicator::getProperties();

从连接器中取得当前使用的属性表 利用属性表改进我们的程序

比如,我们前面的客户端的例子:

class MyApp: public Ice::Application{ public:     virtual int run(int, char*[])     {         Ice::CommunicatorPtr ic communicator();         Ice::ObjectPrx base              ic->stringToProxy("SimplePrinter:default -p 10000");         PrinterPrx printer  PrinterPrx::checkedCast(base);         if(!printer) throw "Invalid Proxy!";         printer->printString("Hello World!");         return 0;     } };

ic->stringToProxy("SimplePrinter:default -p 10000");这句把代理名、协议、端口以及主机名都固化在了程序中。有时,我们需要更大的灵活性,比如由命令行参数或配置文件指定它们,上面提到的Properties接口就可以帮我们:

class MyApp: public Ice::Application{ public:     virtual int run(int, char*[])     {         Ice::CommunicatorPtr ic communicator();         Ice::PropertiesPtr prop ic->getProperties();         string = prop->getPropertyWithDefault(             "Ice.Printer","SimplePrinter:default -p 10000");         Ice::ObjectPrx base ic->stringToProxy(s);         PrinterPrx printer  PrinterPrx::checkedCast(base);         if(!printer) throw "Invalid Proxy!";         printer->printString("Hello World!");         return 0;     } };

这里,代理字符串从属性表(Ice::Properties,Ice::PropertiesPtr是Ice::Properties的智能指针)中得到,我们只要改变命令行参数就可以改变这个字符串,如:

MyProg.exe --Ice.Printer="SimplePrinter:tcp -p 10000" MyProg.exe --Ice.Printer="SimplePrinter:tcp -p 1234 -h 192.168.1.5"...

可能你已经猜到,如果命令行里没有指定Ice.Printer,则使用默认的"SimplePrinter:default -p 10000"

另外,上面的:

Ice::PropertiesPtr prop ic->getProperties(); string prop->getPropertyWithDefault(     "Ice.Printer","SimplePrinter:default -p 10000"); Ice::ObjectPrx base ic->stringToProxy(s);

三条指令可以用一条指令代替:

Ice::ObjectPrx base ic->propertyToProxy("Ice.Printer");

多写几行有时还是有用的,我们可以利用它来读取我们自定义的一些参数^_^(当然还有默认参数功能

上面的命令行里我们取的是"Ice.Printer"属性,注意它的前缀是"Ice.",如果我们改成其它前缀,如"MyProp.",那么Ice初始化时就不认为这是Ice专用参数而不理睬它了。

我们想自定义一些属性用于“私用”的话,使用"Ice."前缀就显得不合理了,这时我们可以使用属性集的parseCommandLineOptions方法来解析指定前缀的参数:

class MyApp: public Ice::Application{ public:     virtual int run(int argc, char*argv[])     {         Ice::CommunicatorPtr ic communicator();         Ice::PropertiesPtr prop Ice::createProperties();         prop->parseCommandLineOptions("MyProp",              Ice::argsToStringSeq(argc,argv));         string prop->getPropertyWithDefault(             "MyProp.Printer","SimplePrinter:default -p 10000");         Ice::ObjectPrx base ic->stringToProxy(s);         PrinterPrx printer  PrinterPrx::checkedCast(base);         if(!printer) throw "Invalid Proxy!";         printer->printString("Hello World!");         return 0;     } };

现在,程序可以解析MyProp.Printer属性了。

配置文件

对于少量的属性,使用命令行参数确实没什么问题,一旦自定义属性增加,使用命令行参数就显得不太合适了。

Ice允许我们把所有的属性参数写到一个文本文件里,每个参数一行,比如有:

MyProg.exe \ --MyProp.Printer="SimplePrinter:tcp -p 1234 -h 192.168.1.5" \ --MyProp.Test="abc,def,aaa,sde,s"

我们可以生成一个文本文件,内容:

MyProp.Printer="SimplePrinter:tcp -p 1234 -h 192.168.1.5" MyProp.Test="abc,def,aaa,sde,s"

然后,把上面的命令行参数改成:

MyProg.exe --Ice.Config=文件名

或者使用Ice::Properties::load方法载入文件,

或者使用Ice::Properties::parseIceCommandLineOptions方法解析文本内容。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值