libconfig C++ 学习笔记
1. C++API 头文件 #include <libconfig.h++> ,命名空间:using namespace libconfig;
2.多线程使用问题:
(1)libconfig是完全可重入的,库中函数不使用全局变量和不保持成功调用间的状态。所以两个独立配置文件在两个不同线程间并发操作可以是安全的;
(2)libconfig是线程不安全,libconfig不考虑系统线程模型。所以一个配置文件的实例被多个线程获取,必须使用同步机制(例如读写锁或互斥锁)来保护。
(3)libconfig不是异步安全,不能在信号句柄中调用
(4)libconfig不能保证取消安全。因为它不知道主机系统的线程模型,库不包含任何线程取消点。在大多数情况下这将不是多线程程序的问题。但是,请注意一些库中的例程(即那些从/到文件读取/写入配置的程序)流)使用可能会阻止的C库例程执行I/O;是否这些C库例程是取消安全的,取决于主机系统。
3.C++API
C++API使用Config类和Setting类,因为不提供公有的copy constructor or assignment operator,所以在函数参数中传递时使用引用或指针方式
4.使用方法(使用官方example):
配置文件:
// An example configuration file that stores information about a store. // Basic store information:
name = "Books, Movies & More"; // Store inventory:
inventory =
{
books = ( { title = "Treasure Island";
author = "Robert Louis Stevenson";
price = 29.99;
qty = ; },
{ title = "Snow Crash";
author = "Neal Stephenson";
price = 9.99;
qty = ; }
); movies = ( { title = "Brazil";
media = "DVD";
price = 19.99;
qty = ; },
{ title = "The City of Lost Children";
media = "DVD";
price = 18.99;
qty = ; },
{ title = "Memento";
media = "Blu-Ray";
price = 24.99;
qty = ;
},
{ title = "Howard the Duck"; }
);
}; // Store hours:
hours =
{
mon = { open = ; close = ; };
tue = { open = ; close = ; };
wed = { open = ; close = ; };
thu = { open = ; close = ; };
fri = { open = ; close = ; };
sat = { open = ; close = ; };
sun = { open = ; close = ; };
};
C++读取配置程序:
#include <cstdlib>
#include <libconfig.h++>
#include <iostream>
#include <iomanip> using namespace std;
using namespace libconfig; int main(){
Config cfg; //1.声明 Config对象 cfg.readFile("example.cfg"); //读取配置文件 // Get the store name.
try
{
string name = cfg.lookup("name");
cout << "Store name: " << name << endl << endl;
}
catch(const SettingNotFoundException &nfex) //配置没找到会有SettingNotFoundException 异常
{
cerr << "No 'name' setting in configuration file." << endl;
}
// Get the store name.
try
{
string name = cfg.lookup("name");
cout << "Store name: " << name << endl << endl;
}
catch(const SettingNotFoundException &nfex)
{
cerr << "No 'name' setting in configuration file." << endl;
} const Setting& root = cfg.getRoot(); // Output a list of all books in the inventory.
try
{
const Setting &books = root["inventory"]["books"];
int count = books.getLength(); cout << setw() << left << "TITLE" << " "
<< setw() << left << "AUTHOR" << " "
<< setw() << left << "PRICE" << " "
<< "QTY"
<< endl; for(int i = ; i < count; ++i)
{
const Setting &book = books[i]; // Only output the record if all of the expected fields are present.
string title, author;
double price;
int qty; if(!(book.lookupValue("title", title)
&& book.lookupValue("author", author)
&& book.lookupValue("price", price)
&& book.lookupValue("qty", qty)))
continue; cout << setw() << left << title << " "
<< setw() << left << author << " "
<< '$' << setw() << right << price << " "
<< qty
<< endl;
}
cout << endl;
}
catch(const SettingNotFoundException &nfex)
{
// Ignore.
} // Output a list of all books in the inventory.
try
{
const Setting &movies = root["inventory"]["movies"];
int count = movies.getLength(); cout << setw() << left << "TITLE" << " "
<< setw() << left << "MEDIA" << " "
<< setw() << left << "PRICE" << " "
<< "QTY"
<< endl; for(int i = ; i < count; ++i)
{
const Setting &movie = movies[i]; // Only output the record if all of the expected fields are present.
string title, media;
double price;
int qty; if(!(movie.lookupValue("title", title)
&& movie.lookupValue("media", media)
&& movie.lookupValue("price", price)
&& movie.lookupValue("qty", qty)))
continue; cout << setw() << left << title << " "
<< setw() << left << media << " "
<< '$' << setw() << right << price << " "
<< qty
<< endl;
}
cout << endl;
}
catch(const SettingNotFoundException &nfex)
{
// Ignore.
}
return ;
}
5.常用Config类方法:
(1)Method on Config: Setting & getRoot () const
This method returns the root setting for the configuration, which is a group.
(2)
Method on Config: Setting & lookup (const std::string &path) constMethod on Config: Setting & lookup (const char * path) const
These methods locate the setting specified by the path path. If the requested setting is not found, a SettingNotFoundException
is thrown.
(3)
Method on Config: bool lookupValue (const char *path, bool &value) constMethod on Config: bool lookupValue (const std::string &path, bool &value) constMethod on Config: bool lookupValue (const char *path, int &value) constMethod on Config: bool lookupValue (const std::string &path, int &value) constMethod on Config: bool lookupValue (const char *path, unsigned int &value) constMethod on Config: bool lookupValue (const std::string &path, unsigned int &value) constMethod on Config: bool lookupValue (const char *path, long long &value) constMethod on Config: bool lookupValue (const std::string &path, long long &value) constMethod on Config: bool lookupValue (const char *path, float &value) constMethod on Config: bool lookupValue (const std::string &path, float &value) constMethod on Config: bool lookupValue (const char *path, double &value) constMethod on Config: bool lookupValue (const std::string &path, double &value) constMethod on Config: bool lookupValue (const char *path, const char *&value) constMethod on Config: bool lookupValue (const std::string &path, const char *&value) constMethod on Config: bool lookupValue (const char *path, std::string &value) constMethod on Config: bool lookupValue (const std::string &path, std::string &value) const
libconfig C++ 学习笔记的更多相关文章
- kvm虚拟化学习笔记(一)之kvm虚拟化环境安装
平时一直玩RHEL/CentOS/OEL系列的操作,玩虚拟化也是采这一类系统,kvm在RHEL6系列操作系统支持比较好,本文采用采用OEL6.3操作系统,网上所有文章都说KVM比xen简单,我怎么感觉 ...
- js学习笔记:webpack基础入门(一)
之前听说过webpack,今天想正式的接触一下,先跟着webpack的官方用户指南走: 在这里有: 如何安装webpack 如何使用webpack 如何使用loader 如何使用webpack的开发者 ...
- PHP-自定义模板-学习笔记
1. 开始 这几天,看了李炎恢老师的<PHP第二季度视频>中的“章节7:创建TPL自定义模板”,做一个学习笔记,通过绘制架构图.UML类图和思维导图,来对加深理解. 2. 整体架构图 ...
- PHP-会员登录与注册例子解析-学习笔记
1.开始 最近开始学习李炎恢老师的<PHP第二季度视频>中的“章节5:使用OOP注册会员”,做一个学习笔记,通过绘制基本页面流程和UML类图,来对加深理解. 2.基本页面流程 3.通过UM ...
- 2014年暑假c#学习笔记目录
2014年暑假c#学习笔记 一.C#编程基础 1. c#编程基础之枚举 2. c#编程基础之函数可变参数 3. c#编程基础之字符串基础 4. c#编程基础之字符串函数 5.c#编程基础之ref.ou ...
- JAVA GUI编程学习笔记目录
2014年暑假JAVA GUI编程学习笔记目录 1.JAVA之GUI编程概述 2.JAVA之GUI编程布局 3.JAVA之GUI编程Frame窗口 4.JAVA之GUI编程事件监听机制 5.JAVA之 ...
- seaJs学习笔记2 – seaJs组建库的使用
原文地址:seaJs学习笔记2 – seaJs组建库的使用 我觉得学习新东西并不是会使用它就够了的,会使用仅仅代表你看懂了,理解了,二不代表你深入了,彻悟了它的精髓. 所以不断的学习将是源源不断. 最 ...
- CSS学习笔记
CSS学习笔记 2016年12月15日整理 CSS基础 Chapter1 在console输入escape("宋体") ENTER 就会出现unicode编码 显示"%u ...
- HTML学习笔记
HTML学习笔记 2016年12月15日整理 Chapter1 URL(scheme://host.domain:port/path/filename) scheme: 定义因特网服务的类型,常见的为 ...
随机推荐
- java中四种引用类型(对象的强、软、弱和虚引用)
对象的强.软.弱和虚引用在JDK 1.2以前的版本中,若一个对象不被任何变量引用,那么程序就无法再使用这个对象.也就是说,只有对象处于可触及(reachable)状态,程序才能使用它.从JDK 1.2 ...
- Web开发:Bootstrap的应用
- Coherence 简介
Coherence是Oracle为了建立一种高可靠和高扩展集群计算的一个关键部件. 典型的使用Coherence的架构图是: Coherence被放在应用服务器和数据库服务器之间,从而解决通常应用 ...
- 第八章.Java集合
Java集合类是一种特别有用的工具类,可用于存储数量不等的对象.Java集合大致可分为Set.List.Queue和Map四种体系 Set代表无序.不可重复的集合 List代表有序.重复的集合 Map ...
- 纯css画三角形,勾等形状
//三角形 .money-ul li.active:after { content: ""; position: absolute; bottom: 0; right: 0; bo ...
- webpack-loader原理
loader loader 是导出为一个函数的 node 模块.该函数在 loader 转换资源的时候调用.给定的函数将调用 loader API,并通过 this 上下文访问. loader配置 { ...
- 小白学flask之路由,反向路由,路由参数
# -*- coding: utf-8 -*- from flask import Flask, request, url_for app = Flask(__name__) @app.route(& ...
- IntelliJ IDEA 16创建Web项目
首先要理解一个概念:在IntelliJ IDEA中“new Project”相当于eclipse中的工作空间(Workspace),而“new Module”相当于eclipse中的工程(Projec ...
- JS十进制转二进制(控制位数)
主要需求:十进制转二进制,可以控制指定的位数. 转化显示后的二进制数为bin-bit中输入的数字宽度.dec-number为5,bin-bit为5,则转化后数字为00101.如果bin-bit小于转化 ...
- 移动web中的幻灯片切换效果
百度或者谷歌下类似的插件有很多,原理都差不多,关键适合自己的项目,如果移动端要引入jquery这么大的插件,只能呵呵了.... 下面是工作中针对webkit内核的浏览器写的,html很简单: < ...