内容目录

介绍

Foundation库

XML库

Util库

Net库

将这些东西组合到一起

 

介绍

POCO C++库是一组开源C++类库的集合,它们简化及加速了用C++来开发以网络功能为核心的可移植程序的过程。这些库,完美地与C++标准库结合到一起,并且填补了它所留下的那些空缺。它们具有模块化、高效的设计与实现,使得POCO C++库特别适合于进行嵌入式开发。而这是C++编程语言正在变得越来越流行的领域,因为,它既能进行底层(设备I/O、中断处理,等等)的开发,也能进行高级的面向对象的开发。当然,POCO也已经准备好面对企业级开发的挑战了。

 

POCO由4个核心库及若干个附加库组成。核心库是:Foundation、XML、Util和Net。其中的两个附加库是:NetSSL,为Net 库中的网络类提供SSL 支持;Data,用来以统一的形式访问不同的SQL 数据库。POCO致力于进行以网络功能为核心的跨平台C++软件的开发,可以类比于Ruby on Rails对于Web开发的作用——一个功能强大而又简单易用的平台,用来构建妳自己的应用程序。POCO是严格围绕标准ANSI/ISO C++来开发的,并且支持标准库。贡献者们努力地达到以下要素之间的平衡:使用高级的C++特性;保持那些类的可理解性;保持代码的干净、一致及易维护性。

)为基础的正则表达式支持。

POCO提供 了一些类,用于处理多种变种中的日期及时间。对于文件系统访问功能 , POCO提供 了 Poco::File 和 Poco::Path 类,以及 Poco::DirectoryIterator 类。 在狠多程序中,某个部分需要向其它部分告知某些事件发生了 。 POCO提供 了 Poco::NotificationCenter 、 Poco::NotificationQueue 和事件 (类似 于 C#事件) 来简化这个过程。 以下示例展示了 POCO事件 的用法。 在这个示例中, 类 Source 拥有 一个公有事件,名为 theEvent , 它有一个参数,类型为 int 。订阅 者可以通过调用 operator +=来订阅,调用 operator -= 来取消订阅,并且 要传入两个参数:指向某个对象 的一个指针;以及,指向某个成员函数 的指针。事件 可通用调用 operator () 来发射, Source::fireEvent() 中就是这么做的。

#include "Poco/BasicEvent.h"

#include "Poco/Delegate.h"

#include <iostream>

 

using Poco::BasicEvent;

using Poco::Delegate;

 

class Source

{

public:

    BasicEvent<int> theEvent;

 

void fireEvent(int n)

    {

        theEvent(this, n);

    }

};

 

class Target

{

public:

void onEvent(const void* pSender, int& arg)

    {

std::cout << "onEvent: " << arg << std::endl;

    }

};

 

int main(int argc, char** argv)

{

    Source source;

    Target target;

 

    source.theEvent += Delegate<Target, int>(

        &target, &Target::onEvent);

 

    source.fireEvent(42);

 

    source.theEvent -= Delegate<Target, int>(

        &target, &Target::onEvent);

 

return 0;

}

 

POCO 中的那些流式操作类,已经介绍过了。 为了对它们进行增强,还提供了 Poco::BinaryReader 和 Poco::BinaryWriter ,用于从流中读取及写入二进制数据,并且会自动、透明地处理字节序问题。

在复杂的多线程程序中,找出问题及缺陷的唯一手段就是输出详尽的日志信息。POCO提供了一个功能强大且可扩展的日志框架,它支持:过滤;路由到不同的频道;以及,日志消息的格式化。日志信息可被写入到以下目标:终端;文件;syslog守护进程;或者,发送到网络。如果POCO 提供的这些频道还不能满足妳,那么,还可以轻易地使用新的类来扩展日志框架。

对于 在运行时 载入( 及卸载 )共享 库的任务, POCO提供 了一个低层的 Poco::SharedLibrary 类。 在它之上,是 Poco::ClassLoader 类模板,以及对应的支持框架,使 得妳可以在运行时动态地载入及卸载 C++ 类,这就类似于 Java 和 .NET 中的功能。 类载入器框架,也使得, 妳可以轻易地以平台无关的方式来 为程序加入插件支持功能。

最后 , POCO Foundation 中包含了对于多线程编程的不同层次的抽象。 有 Poco::Thread 类,以及常用的同步原语 Poco::Mutex 、 Poco::ScopedLock 、 Poco::Event Poco::Semaphore 、 Poco::RWLock , 一个 Poco::ThreadPool 类, 以及对于线程本地存储的支持, 也有高级的抽象,例如活跃对象(active objects)。简单 来说,活跃对象,指的就是, 它有某些方法,是在独立的线程中执行的。 这样,就可以进行异步 的成员函数调用——调用 一个成员函数, 在它正在执行的过程中, 去干一些其它的事,最后,获取 该函数的返回值。下面 的示例中展示了在POCO 中如何做到这一点。 在 ActiveAdder 类定义了一个活跃方法(active method) add() , 该方法是由 addImpl() 这个成员函数来实现的。 在 main() 中调用该活跃方法,就会产生出一个 Poco::ActiveResult  ( 也被称作未来对象( future ) ),最终会通过它来获取到该函数的返回值。

#include "Poco/ActiveMethod.h"

#include "Poco/ActiveResult.h"

#include <utility>

#include <iostream>

 

using Poco::ActiveMethod;

using Poco::ActiveResult;

 

class ActiveAdder

{

public:

    ActiveAdder(): add(this, &ActiveAdder::addImpl)

    {

    }

 

    ActiveMethod<int, std::pair<int, int>, ActiveAdder> add;

 

private:

int addImpl(const std::pair<int, int>& args)

    {

return args.first + args.second;

    }

};

 

int main(int argc, char** argv)

{

    ActiveAdder adder;

 

    ActiveResult<int> sum = adder.add(std::make_pair(1, 2));

// 做些别的事情

    sum.wait();

std::cout << sum.data() << std::endl;

 

return 0;

}

 

在未来的版本中将支持XPath 和XSLT。

Util库的名字可能会让妳产生误解,实际上,它主要是一个用来创建命令行程序和服务器程序的框架。包含的功能:处理命令行参数(验证、绑定到配置属性,等等);以及,管理配置信息。支持不同的配置文件格式——Java风格的属性文件、XML文件

对于服务器程序,这个框架提供了对于Unix 守护进程的透明支持。当然,所有的服务器程序也都可以直接从命令行启动,这样便于测试及调试。

POCO的Net库使得妳能够轻易地写出基于网络通信的应用程序。无论妳只是想使用普通的TCP 套接字来发送数据,还是想要一个内置的完整功能的HTTP 服务器,Net 库都能满足妳。

从POP3 服务器接收邮件。

将这些东西组合到一起

)读取默认的程序配置文件,并且取得某些配置属性的值。

#include "Poco/Net/HTTPServer.h"

#include "Poco/Net/HTTPRequestHandler.h"

#include "Poco/Net/HTTPRequestHandlerFactory.h"

#include "Poco/Net/HTTPServerParams.h"

#include "Poco/Net/HTTPServerRequest.h"

#include "Poco/Net/HTTPServerResponse.h"

#include "Poco/Net/HTTPServerParams.h"

#include "Poco/Net/ServerSocket.h"

#include "Poco/Timestamp.h"

#include "Poco/DateTimeFormatter.h"

#include "Poco/DateTimeFormat.h"

#include "Poco/Exception.h"

#include "Poco/ThreadPool.h"

#include "Poco/Util/ServerApplication.h"

#include "Poco/Util/Option.h"

#include "Poco/Util/OptionSet.h"

#include "Poco/Util/HelpFormatter.h"

#include <iostream>

 

using Poco::Net::ServerSocket;

using Poco::Net::HTTPRequestHandler;

using Poco::Net::HTTPRequestHandlerFactory;

using Poco::Net::HTTPServer;

using Poco::Net::HTTPServerRequest;

using Poco::Net::HTTPServerResponse;

using Poco::Net::HTTPServerParams;

using Poco::Timestamp;

using Poco::DateTimeFormatter;

using Poco::DateTimeFormat;

using Poco::ThreadPool;

using Poco::Util::ServerApplication;

using Poco::Util::Application;

using Poco::Util::Option;

using Poco::Util::OptionSet;

using Poco::Util::OptionCallback;

using Poco::Util::HelpFormatter;

 

class TimeRequestHandler: public HTTPRequestHandler

{

public:

    TimeRequestHandler(const std::string& format): _format(format)

    {

    }

 

void handleRequest(HTTPServerRequest& request,

                       HTTPServerResponse& response)

    {

        Application& app = Application::instance();

        app.logger().information("Request from "

            + request.clientAddress().toString());

 

        Timestamp now;

std::string dt(DateTimeFormatter::format(now, _format));

 

        response.setChunkedTransferEncoding(true);

        response.setContentType("text/html");

 

std::ostream& ostr = response.send();

        ostr << "<html><head><title>HTTPTimeServer powered by "

"POCO C++ Libraries</title>";

        ostr << "<meta http-equiv= \" refresh \" content= \" \" ></head>";

        ostr << "<body><p style= \" text-align: center; "

"font-size: 48px; \" >";

        ostr << dt;

        ostr << "</p></body></html>";

    }

 

private:

std::string _format;

};

 

class TimeRequestHandlerFactory: public HTTPRequestHandlerFactory

{

public:

    TimeRequestHandlerFactory(const std::string& format):

_format(format)

    {

    }

 

    HTTPRequestHandler* createRequestHandler(

const HTTPServerRequest& request)

    {

if (request.getURI() == "/")

return new TimeRequestHandler(_format);

else

return 0;

    }

 

private:

std::string _format;

};

 

class HTTPTimeServer: public Poco::Util::ServerApplication

{

public:

    HTTPTimeServer(): _helpRequested(false)

    {

    }

 

    ~HTTPTimeServer()

    {

    }

 

protected:

void initialize(Application& self)

    {

        loadConfiguration();

        ServerApplication::initialize(self);

    }

 

void uninitialize()

    {

        ServerApplication::uninitialize();

    }

 

void defineOptions(OptionSet& options)

    {

        ServerApplication::defineOptions(options);

 

        options.addOption(

        Option("help", "h", "display argument help information")

            .required(false)

            .repeatable(false)

            .callback(OptionCallback<HTTPTimeServer>(

this, &HTTPTimeServer::handleHelp)));

    }

 

void handleHelp(const std::string& name,

const std::string& value)

    {

        HelpFormatter helpFormatter(options());

        helpFormatter.setCommand(commandName());

        helpFormatter.setUsage("OPTIONS");

        helpFormatter.setHeader(

"A web server that serves the current date and time.");

        helpFormatter.format(std::cout);

        stopOptionsProcessing();

_helpRequested = true;

    }

 

int main(const std::vector<std::string>& args)

    {

if (!_helpRequested)

        {

unsigned short port = (unsigned short)

                config().getInt("HTTPTimeServer.port", 9980);

std::string format(

                config().getString("HTTPTimeServer.format",

                                   DateTimeFormat::SORTABLE_FORMAT));

 

            ServerSocket svs(port);

            HTTPServer srv(new TimeRequestHandlerFactory(format),

                svs, new HTTPServerParams);

            srv.start();

            waitForTerminationRequest();

            srv.stop();

        }

return Application::EXIT_OK;

    }

 

private:

bool _helpRequested;

};

 

int main(int argc, char** argv)

{

    HTTPTimeServer app;

return app.run(argc, argv);

}

http://www.stupidbeauty.com/Blog/article/1648/POCO%E6%96%87%E6%A1%A3%E7%BF%BB%E8%AF%91%EF%BC%9APOCO%20C++%E5%BA%93%E5%85%A5%E9%97%A8%E6%8C%87%E5%8D%97,A%20Guided%20Tour%20Of%20The%20POCO%20C++%20Libraries

相关文章: