poco例子及代码阅读笔记

xiaoxiao2021-02-27  224

 

1、uuidgenerator类

UUID uuid;

if (arg == "-random")

   uuid = UUIDGenerator::defaultGenerator().createRandom();

  else if (arg.empty())

   uuid = UUIDGenerator::defaultGenerator().create();

  else

   uuid = UUIDGenerator::defaultGenerator().createFromName(UUID::uri(), arg);

分random,默认和根据字符串三种方式

2、uri类

URI uri1("http://www.appinf.com:81/sample?example-query#somewhere");

 std::cout << "Scheme:    " << uri1.getScheme() << std::endl

           << "Authority: " << uri1.getAuthority() << std::endl

           << "Path:      " << uri1.getPath() << std::endl

           << "Query:     " << uri1.getQuery() << std::endl

           << "Fragment:  " << uri1.getFragment() << std::endl;

构造uri对象,获取属性

3、timer类

 TimerExample example;

 Timer timer(250, 500);

 timer.start(TimerCallback<TimerExample>(example, &TimerExample::onTimer));

通过注册模板回调类和类函数实现timer的handler

模板类定义了handler类

template <class C>class TimerCallback: public AbstractTimerCallback{}

模板成员声明了handler类的处理函数

typedef void (C::*Callback)(Timer&);

模板类的构造函数传入handler类的处理函数地址

TimerCallback(C& object, Callback method): _pObject(&object), _method(method)

4、stringtokenizer类

 std::string tokens = "white; black; magenta, blue, green; yellow";

 StringTokenizer tokenizer(tokens, ";,", StringTokenizer::TOK_TRIM);

 for (StringTokenizer::Iterator it = tokenizer.begin(); it != tokenizer.end(); ++it)

 {

  std::cout << *it << std::endl;

 }

定义分隔符,设定分割字符去空格、忽略空字符串两个选项

5、notificationqueue类

 NotificationQueue queue;

  // create some worker threads

 Worker worker1("Worker 1", queue);

 Worker worker2("Worker 2", queue);

 Worker worker3("Worker 3", queue);

 // start worker threads

 ThreadPool::defaultPool().start(worker1);

 ThreadPool::defaultPool().start(worker2);

 ThreadPool::defaultPool().start(worker3);

 // distribute some work

 for (int i = 0; i < 50; ++i)

 {

  queue.enqueueNotification(new WorkNotification(i));

 }

客户程序实现runnable接口和notification接口,前者为线程池类threadpool的接口,后者为线程池类的通知执行类notificationqueue接口,通过notificationqueue::enqueuenotification函数来通知threadpool调度执行

6、md5engine类

 MD5Engine md5;

 DigestOutputStream dos(md5);

  StreamCopier::copyStream(istr, dos);

 dos.close();

md5engine为md5实现类,digestoutputstream实现ofstream接口,streamcopier::copystream实现ifstream到ofstream的流拷贝

7、logrotation

 AutoPtr<SplitterChannel> splitterChannel(new SplitterChannel());

 AutoPtr<Channel> consoleChannel(new ConsoleChannel());

 AutoPtr<Channel> fileChannel(new FileChannel("test.log"));

 AutoPtr<FileChannel> rotatedFileChannel(new FileChannel("rotated.log"));

 rotatedFileChannel->setProperty("rotation", "100");

 rotatedFileChannel->setProperty("archive", "timestamp");

 splitterChannel->addChannel(consoleChannel);

 splitterChannel->addChannel(fileChannel);

 splitterChannel->addChannel(rotatedFileChannel);

 AutoPtr<Formatter> formatter(new PatternFormatter("%h-%M-%S.%i: %t"));

 AutoPtr<Channel> formattingChannel(new FormattingChannel(formatter, splitterChannel));

 Logger& logger = Logger::create("TestLog", formattingChannel, Message::PRIO_TRACE);

 for (int i = 0; i < 10; ++i)

 {

 std::ostringstream oss;

  oss << "Value of i: " << i;

  logger.fatal(oss.str());

 }

splitterchannel类为channel接口的容器类,输出log到channel队列,channel队列通过addchannel、removechannel维护

channel接口定义了log核心方法和open、close、setproperty、getproperty方法

channel接口的实现有consolechannel,filechannel,formattingchannel类

formatter接口定义了format核心方法,是formattingchannel的辅助接口

logger::create构造实例,调用fetal输出log

8、logger

 // set up two channel chains - one to the

 // console and the other one to a log file.

 FormattingChannel* pFCConsole = new FormattingChannel(new PatternFormatter("%s: %p: %t"));

 pFCConsole->setChannel(new ConsoleChannel);

 pFCConsole->open();

 FormattingChannel* pFCFile = new FormattingChannel(new PatternFormatter("%Y-%m-%d %H:%M:%S.%c %N[%P]:%s:%q:%t"));

 pFCFile->setChannel(new FileChannel("sample.log"));

 pFCFile->open();

 // create two Logger objects - one for

 // each channel chain.

 Logger& consoleLogger = Logger::create("ConsoleLogger", pFCConsole, Message::PRIO_INFORMATION);

 Logger& fileLogger    = Logger::create("FileLogger", pFCFile, Message::PRIO_WARNING);

 // log some messages

 consoleLogger.error("An error message");

 fileLogger.error("An error message");

 consoleLogger.warning("A warning message");

 fileLogger.error("A warning message");

 consoleLogger.information("An information message");

 fileLogger.information("An information message");

 Logger::get("ConsoleLogger").error("Another error message");

构造两个实现channel接口的channel类实例consolechannel和filechannel

构造formattingchannel实例,传入上面构造的channel实例对象,通过设置过滤格式,输出规范的log记录

logger::get可以获取channel队列的命名channel,单独输出log

9、inflate

10、hmacmd5

11、grep

12、dir

13、deflate

14、datetime

15、binaryreaderwriter

16、base64encode

17、activity

18、activemethod

转载请注明原文地址: https://www.6miu.com/read-10081.html

最新回复(0)