【C++】POCO学习总结(十九):哈希、URL、UUID、配置文件、日志配置、动态库加载

本文主要是介绍【C++】POCO学习总结(十九):哈希、URL、UUID、配置文件、日志配置、动态库加载,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

【C++】郭老二博文之:C++目录

1、哈希

1.1 说明

std::map和std::set 的性能是:O(log n)
POCO哈希的性能比STL容器更好,大约快两;
POCO中对应std::map的是:Poco::HashMap;
POCO中对应std::set的是 Poco::HashSet;
使用方法、迭代器都和STL类似。

POCO哈希在执行插入或者删除操作时不会导致性能下降(当数据不足时不需要重新哈希)

HashMap 和 HashSet 使用线性哈希表LinearHashTable作为基础数据结构。
使用时还必须提供一个哈希函数:Poco/Hash.h中预定义了关于整数和std::string的函数

namespace Poco {
std::size_t hash(Int8 n);
std::size_t hash(UInt8 n);
std::size_t hash(Int16 n);
std::size_t hash(UInt16 n);
std::size_t hash(Int32 n);
std::size_t hash(UInt32 n);
std::size_t hash(Int64 n);
std::size_t hash(UInt64 n);
std::size_t hash(const std::string& str);
}
Poco::LinearHashTable<Key, Hash = Poco::Hash<Key>

1.2 示例

#include "Poco/LinearHashTable.h"
#include "Poco/HashMap.h"
#include <iterator>
#include <iostream>
using namespace Poco;
int main()
{const int N = 20;LinearHashTable<int, Hash<int> > ht;for (int i = 0; i < N; ++i)ht.insert(i);LinearHashTable<int, Hash<int> >::Iterator it = ht.begin();while (it != ht.end()){std::cout << "[" << *it << "]";++it;}
}

编译:

g++ hash.cpp -I ~/git/poco/install/include -L ~/git/poco/install/lib -lPocoFoundationd

输出

[0][17][2][19][4][5][6][7][8][9][10][11][12][13][14][15][16][1][18][3]

2、POCO::URI

2.1 说明

POCO提供了POCO::URI类,该类可用于构建和存储URI,并且解析、拆分URI。
URI结构:协议(scheme)、主机名(host)、用户(user)、端口号(port)、路径(path)、查询(query)、资源(Fragment)等
在这里插入图片描述
在这里插入图片描述

2.2 示例

#include "Poco/URI.h"
#include <iostream>
int main(int argc, char** argv)
{Poco::URI uri1("http://www.appinf.com:88/sample?example-query#frag");std::string scheme(uri1.getScheme()); // "http"std::string auth(uri1.getAuthority()); // "www.appinf.com:88"std::string host(uri1.getHost()); // "www.appinf.com"unsigned short port = uri1.getPort(); // 88std::string path(uri1.getPath()); // "/sample"std::string query(uri1.getQuery()); // "example-query"std::string frag(uri1.getFragment()); // "frag"std::string pathEtc(uri1.getPathEtc()); // "/sample?examplequery#frag"Poco::URI uri2;uri2.setScheme("https");uri2.setAuthority("www.appinf.com");uri2.setPath("/another sample");std::string s(uri2.toString()); // "https://www.appinf.com/another%20sample"std::string uri3("http://www.appinf.com");uri3.resolve("/poco/info/index.html");s = uri3.toString(); // "http://www.appinf.com/poco/info/index.html"uri3.resolve("support.html");s = uri3.toString(); // "http://www.appinf.com/poco/info/support.html"uri3.resolve("http://sourceforge.net/projects/poco");s = uri3.toString(); // "http://sourceforge.net/projects/poco"return 0;
}

3、UUID

3.1 说明

UUID(通用唯一标识符)是一种标识符,它在空间和时间上相对于所有UUID的空间都是唯一的。
Poco::UUID支持包括所有关系操作符在内的全值语义,和字符串之间进行转换。

3.2 示例

#include "Poco/UUID.h"
#include "Poco/UUIDGenerator.h"
#include <iostream>
using Poco::UUID;
using Poco::UUIDGenerator;
int main(int argc, char** argv)
{UUIDGenerator& generator = UUIDGenerator::defaultGenerator();UUID uuid1(generator.create()); // time basedUUID uuid2(generator.createRandom());UUID uuid3(generator.createFromName(UUID::uri(), "http://appinf.com");std::cout << uuid1.toString() << std::endl;std::cout << uuid2.toString() << std::endl;std::cout << uuid3.toString() << std::endl;return 0;
}

4、配置文件

4.1 说明

Poco::Util::AbstractConfiguration提供了一个公共接口,用于访问来自不同来源的配置信息。
配置设置基本上是键/值对,其中键和值都是字符串。
键具有层次结构,由以句点分隔的名称组成。
值可以转换为整数、双精度和布尔值。
一个可选的默认值可以在getter函数中指定。

4.2 用法

  • bool hasProperty(const std::string& key)
  • std::string getString(const std::string& key [, const std::string& default])
  • int getInt(const std::string& key [, int default])
  • getDouble()
  • getBool()
  • setString(),
  • setInt()
  • setDouble()
  • setBool()
  • keys()

4.3 Poco::Util::IniFileConfiguration ini配置文件

Poco::Util::IniFileConfiguration支持普通的旧INI格式文件,主要用于Windows。

  • 键名不区分大小写。
  • 从键和值中删除前导和尾随空格。
  • 只读

格式:

; comment
[MyApplication]
somePath = C:\test.dat
someValue = 123

解析:

using Poco::AutoPtr;
using Poco::Util::IniFileConfiguration;
AutoPtr<IniFileConfiguration> pConf(new IniFileConfiguration("test.ini"));
std::string path = pConf->getString("MyApplication.somePath");
int value = pConf->getInt("MyApplication.someValue");
value = pConf->getInt("myapplication.SomeValue");
value = pConf->getInt("myapplication.SomeOtherValue", 456);

4.4 Poco::Util::PropertyFileConfiguration 属性文件

格式:

# a comment
! another comment
key1 = value1
key2: 123
key3.longValue = this is a very \
long value
path = c:\\test.dat

解析:

using Poco::AutoPtr;
using Poco::Util::PropertyFileConfiguration;
AutoPtr<PropertyFileConfiguration> pConf;
pConf = new PropertyFileConfiguration("test.properties");
std::string key1 = pConf->getString("key1");
int value = pConf->getInt("key2");
std::string longVal = pConf->getString("key3.longValue");

4.5 Poco::Util::XMLConfiguration XML配置文件

格式:

<config><prop1>value1</prop1><prop2>123</prop2><prop3><prop4 attr="value3"/><prop4 attr="value4"/></prop3>
</config>

解析

using Poco::AutoPtr;
using Poco::Util::XMLConfiguration;
AutoPtr<XMLConfiguration> pConf(new XMLConfiguration("test.xml"));
std::string prop1 = pConf->getString("prop1"); 
int prop2 = pConf->getInt("prop2");
std::string prop3 = pConf->getString("prop3"); // ""
std::string prop4 = pConf->getString("prop3.prop4"); // ""
prop4 = pConf->getString("prop3.prop4[@attr]"); // "value3"
prop4 = pConf->getString("prop3.prop4[1][@attr]"); // "value4"

5、日志配置

5.1 说明

Poco::Util::LoggingConfigurator类使用来自Poco::Util::AbstractConfiguration的配置信息来设置和连接日志格式化、通道和记录器。
Poco::Util::Application自动初始化一个LoggingConfigurator及其配置。
所有用于日志记录的配置属性都是以“logging”为键值。

5.2 格式化配置

格式化配置以“logging.formatters”开头;
每个格式化都有一个内部名称,该名称仅用于配置目的,用于将格式化程序连接到通道。
该名称成为属性名称的一部分。其中class属性是必须的,它指定实现格式化程序的类。

logging.formatters.f1.class = PatternFormatter
logging.formatters.f1.pattern = %s: [%p] %t
logging.formatters.f1.times = UTC

5.3 通道配置

通道配置以“logging.channels”开头;class属性是必须的
“formatter”属性既可以用来引用已经定义的格式化,也可以用来指定“内联”格式化定义。在这两种情况下,当存在"formatter"属性时,通道将自动被"包装"在FormattingChannel对象

# External Formatter
logging.channels.c1.class = ConsoleChannel
logging.channels.c1.formatter = f1
# Inline Formatter
logging.channels.c2.class = FileChannel
logging.channels.c2.path = ${system.tempDir}/sample.log
logging.channels.c2.formatter.class = PatternFormatter
logging.channels.c2.formatter.pattern = %Y-%m-%d %H:%M:%S %s: [%p] %t
# Inline PatternFormatter
logging.channels.c3.class = ConsoleChannel
logging.channels.c3.pattern = %s: [%p] %t

5.4 日志记录器

日志记录器使用“logging.loggers”
与通道 channels 和格式化 formatters 一样,每个日志记录器都有一个内部名称,但是,该名称仅用于确保属性名称的唯一性。请注意,此名称与记录器的全名不同,后者用于在运行时访问记录器。
除了根记录器之外,每个记录器都有一个强制性的“name”属性,用于指定记录器的全名。

# External Channel
logging.loggers.root.channel = c1
logging.loggers.root.level = warning
# Inline Channel with PatternFormatter
logging.loggers.l1.name = logger1
logging.loggers.l1.channel.class = ConsoleChannel
logging.loggers.l1.channel.pattern = %s: [%p] %t
logging.loggers.l1.level = information
# SplitterChannel
logging.channels.splitter.class = SplitterChannel
logging.channels.splitter.channels = l1,l2
logging.loggers.l2.name = logger2
logging.loggers.l2.channel = splitter

6、动态库加载

6.1 说明

大多数现代平台都提供了在运行时以共享库(动态链接库)的形式加载程序模块的功能。
Windows提供了LoadLibrary()函数,大多数Unix平台都有dopen()。

6.2 用法

头文件: #include “Poco/SharedLibrary.h”
Poco::SharedLibrary是Poco与操作系统动态链接器/加载器的接口。
Poco::SharedLibrary提供了加载共享库、查找符号地址和卸载共享库的底层函数。

  • void load(const std::string& path):从给定的路径加载共享库
  • void unload():卸载共享库
  • bool hasSymbol(const std::string& name):如果库中包含具有给定名称的符号,则返回true
  • void* getSymbol(const std::string& name):返回给定名称的符号的地址。对于函数,这是函数的入口点。要调用函数,请强制转换为函数指针并通过它调用

6.3 示例

1)动态库:TestLibrary.cpp

#include <iostream>
#if defined(_WIN32)
#define LIBRARY_API __declspec(dllexport)
#else
#define LIBRARY_API
#endif
extern "C" void LIBRARY_API hello();
void hello()
{std::cout << "Hello, world!" << std::endl;
}

2)加载动态库:LibraryLoaderTest.cpp

#include "Poco/SharedLibrary.h"
using Poco::SharedLibrary;
typedef void (*HelloFunc)(); // function pointer type
int main(int argc, char** argv)
{std::string path("TestLibrary");path.append(SharedLibrary::suffix()); // adds ".dll" or ".so"SharedLibrary library(path); // will also load the libraryHelloFunc func = (HelloFunc) library.getSymbol("hello");func();library.unload();return 0;
}

6.4 Poco::ClassLoader 从共享库加载类

Poco::ClassLoader是Poco的高级接口,用于从共享库加载类。它非常适合实现典型的插件架构。
头文件: #include “Poco/ClassLoader.h”
Poco::ClassLoader所有类必须是公共基类的子类。Poco::ClassLoader是一个类模板,必须为基类实例化。

6.5 元对象

Manifest库维护一个包含在动态可加载类库中的所有类的列表。
它将这些信息作为元对象的集合进行管理。
MetaObject管理给定类的对象的生命周期。它用于创建类的实例,并删除它们。
作为一个特殊的特性,类库可以导出单例。

这篇关于【C++】POCO学习总结(十九):哈希、URL、UUID、配置文件、日志配置、动态库加载的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/510676

相关文章

关于C++中的虚拟继承的一些总结(虚拟继承,覆盖,派生,隐藏)

1.为什么要引入虚拟继承 虚拟继承是多重继承中特有的概念。虚拟基类是为解决多重继承而出现的。如:类D继承自类B1、B2,而类B1、B2都继承自类A,因此在类D中两次出现类A中的变量和函数。为了节省内存空间,可以将B1、B2对A的继承定义为虚拟继承,而A就成了虚拟基类。实现的代码如下: class A class B1:public virtual A; class B2:pu

C++对象布局及多态实现探索之内存布局(整理的很多链接)

本文通过观察对象的内存布局,跟踪函数调用的汇编代码。分析了C++对象内存的布局情况,虚函数的执行方式,以及虚继承,等等 文章链接:http://dev.yesky.com/254/2191254.shtml      论C/C++函数间动态内存的传递 (2005-07-30)   当你涉及到C/C++的核心编程的时候,你会无止境地与内存管理打交道。 文章链接:http://dev.yesky

51单片机学习记录———定时器

文章目录 前言一、定时器介绍二、STC89C52定时器资源三、定时器框图四、定时器模式五、定时器相关寄存器六、定时器练习 前言 一个学习嵌入式的小白~ 有问题评论区或私信指出~ 提示:以下是本篇文章正文内容,下面案例可供参考 一、定时器介绍 定时器介绍:51单片机的定时器属于单片机的内部资源,其电路的连接和运转均在单片机内部完成。 定时器作用: 1.用于计数系统,可

C++的模板(八):子系统

平常所见的大部分模板代码,模板所传的参数类型,到了模板里面,或实例化为对象,或嵌入模板内部结构中,或在模板内又派生了子类。不管怎样,最终他们在模板内,直接或间接,都实例化成对象了。 但这不是唯一的用法。试想一下。如果在模板内限制调用参数类型的构造函数会发生什么?参数类的对象在模板内无法构造。他们只能从模板的成员函数传入。模板不保存这些对象或者只保存他们的指针。因为构造函数被分离,这些指针在模板外

问题:第一次世界大战的起止时间是 #其他#学习方法#微信

问题:第一次世界大战的起止时间是 A.1913 ~1918 年 B.1913 ~1918 年 C.1914 ~1918 年 D.1914 ~1919 年 参考答案如图所示

[word] word设置上标快捷键 #学习方法#其他#媒体

word设置上标快捷键 办公中,少不了使用word,这个是大家必备的软件,今天给大家分享word设置上标快捷键,希望在办公中能帮到您! 1、添加上标 在录入一些公式,或者是化学产品时,需要添加上标内容,按下快捷键Ctrl+shift++就能将需要的内容设置为上标符号。 word设置上标快捷键的方法就是以上内容了,需要的小伙伴都可以试一试呢!

Linux 安装、配置Tomcat 的HTTPS

Linux 安装 、配置Tomcat的HTTPS 安装Tomcat 这里选择的是 tomcat 10.X ,需要Java 11及更高版本 Binary Distributions ->Core->选择 tar.gz包 下载、上传到内网服务器 /opt 目录tar -xzf 解压将解压的根目录改名为 tomat-10 并移动到 /opt 下, 形成个人习惯的路径 /opt/tomcat-10

AssetBundle学习笔记

AssetBundle是unity自定义的资源格式,通过调用引擎的资源打包接口对资源进行打包成.assetbundle格式的资源包。本文介绍了AssetBundle的生成,使用,加载,卸载以及Unity资源更新的一个基本步骤。 目录 1.定义: 2.AssetBundle的生成: 1)设置AssetBundle包的属性——通过编辑器界面 补充:分组策略 2)调用引擎接口API

C++工程编译链接错误汇总VisualStudio

目录 一些小的知识点 make工具 可以使用windows下的事件查看器崩溃的地方 dumpbin工具查看dll是32位还是64位的 _MSC_VER .cc 和.cpp 【VC++目录中的包含目录】 vs 【C/C++常规中的附加包含目录】——头文件所在目录如何怎么添加,添加了以后搜索头文件就会到这些个路径下搜索了 include<> 和 include"" WinMain 和

Javascript高级程序设计(第四版)--学习记录之变量、内存

原始值与引用值 原始值:简单的数据即基础数据类型,按值访问。 引用值:由多个值构成的对象即复杂数据类型,按引用访问。 动态属性 对于引用值而言,可以随时添加、修改和删除其属性和方法。 let person = new Object();person.name = 'Jason';person.age = 42;console.log(person.name,person.age);//'J