Console Library 8.0.0
A header-only library that makes C++ simple
Loading...
Searching...
No Matches
console::BasicINIConfig< CharT > Class Template Reference

INI 配置文件操作类。 More...

#include <config.h>

Classes

class  Item
 配置项代理类,支持隐式类型转换。 More...

Public Member Functions

 BasicINIConfig ()=default
 默认构造函数,创建空的配置对象。
 BasicINIConfig (const std::string &filename)
 从指定文件加载配置的构造函数。
 BasicINIConfig (const Path &filename)
 从指定文件加载配置的构造函数。
void load (const Path &filename)
 从文件加载 INI 配置。
void save (const std::string &filename)
 将配置保存到文件。
void save (const Path &filename) const
 将配置保存到文件。
Item get (const string_type &section_and_key) const
 获取配置项的值。
template<class T>
T get (const string_type &section_and_key) const
template<class T>
T get (const string_type &section_and_key, const T &default_value) const
 获取配置项的值,若不存在则返回默认值。
void set (const string_type &section_and_key, const string_type &value)
 设置配置项的值。
bool has (const string_type &section_and_key) const
 检查配置项或节是否存在。
bool remove (const string_type &section_and_key)
 删除配置项或节。
const std::map< string_type, std::map< string_type, string_type > > & data () const
 获取原始数据结构的只读引用。

Private Types

using string_type = std::basic_string<CharT>

Private Member Functions

void write_to_stream (std::ostream &os) const
void read_from_stream (std::istream &is)

Static Private Member Functions

static std::string to_narrow (const string_type &s)

Private Attributes

std::map< string_type, std::map< string_type, string_type > > data_

Friends

std::basic_ostream< CharT > & operator<< (std::basic_ostream< CharT > &os, const BasicINIConfig &config)
 配置对象的输出流运算符,按 INI 格式输出配置内容。
std::basic_istream< CharT > & operator>> (std::basic_istream< CharT > &is, BasicINIConfig &config)
 配置对象的输入流运算符,按 INI 格式输入配置内容。

Detailed Description

template<class CharT = char>
class console::BasicINIConfig< CharT >

INI 配置文件操作类。

Template Parameters
CharT字符类型,支持 char 和 wchar_t。

支持加载、保存、读取、修改 INI 格式的配置文件,本类的 INI 变种支持空节头和空键。

Member Typedef Documentation

◆ string_type

template<class CharT = char>
using console::BasicINIConfig< CharT >::string_type = std::basic_string<CharT>
private

Constructor & Destructor Documentation

◆ BasicINIConfig() [1/3]

template<class CharT = char>
console::BasicINIConfig< CharT >::BasicINIConfig ( )
default

默认构造函数,创建空的配置对象。

◆ BasicINIConfig() [2/3]

template<class CharT = char>
console::BasicINIConfig< CharT >::BasicINIConfig ( const std::string & filename)
inline

从指定文件加载配置的构造函数。

Parameters
filename配置文件路径。

◆ BasicINIConfig() [3/3]

template<class CharT = char>
console::BasicINIConfig< CharT >::BasicINIConfig ( const Path & filename)
inline

从指定文件加载配置的构造函数。

Parameters
filename配置文件路径。

Member Function Documentation

◆ data()

template<class CharT = char>
const std::map< string_type, std::map< string_type, string_type > > & console::BasicINIConfig< CharT >::data ( ) const
inline

获取原始数据结构的只读引用。

◆ get() [1/3]

template<class CharT = char>
template<class T>
T console::BasicINIConfig< CharT >::get ( const string_type & section_and_key) const
inline

◆ get() [2/3]

template<class CharT = char>
Item console::BasicINIConfig< CharT >::get ( const string_type & section_and_key) const
inline

获取配置项的值。

Parameters
section_and_key节和键,格式为 "节名.键名"。
Returns
Item 配置项代理对象,可隐式转换为目标类型。
Exceptions
IndexError格式错误、节不存在或键不存在时抛出。

◆ get() [3/3]

template<class CharT = char>
template<class T>
T console::BasicINIConfig< CharT >::get ( const string_type & section_and_key,
const T & default_value ) const
inline

获取配置项的值,若不存在则返回默认值。

Template Parameters
T目标类型。
Parameters
section_and_key节和键,格式为 "节名.键名"。
default_value默认值,当配置项不存在时返回。
Returns
T 配置值或默认值。

◆ has()

template<class CharT = char>
bool console::BasicINIConfig< CharT >::has ( const string_type & section_and_key) const
inline

检查配置项或节是否存在。

Parameters
section_and_key节和键,格式为 "节名.键名" 或仅 "节名"。
Returns
bool 存在返回 true,否则返回 false。

◆ load()

template<class CharT = char>
void console::BasicINIConfig< CharT >::load ( const Path & filename)
inline

从文件加载 INI 配置。

Parameters
filename配置文件路径。

支持行首注释(; 或 #),支持节头 [section]。

◆ read_from_stream()

template<class CharT = char>
void console::BasicINIConfig< CharT >::read_from_stream ( std::istream & is)
inlineprivate

◆ remove()

template<class CharT = char>
bool console::BasicINIConfig< CharT >::remove ( const string_type & section_and_key)
inline

删除配置项或节。

Parameters
section_and_key节和键,格式为 "节名.键名" 或仅 "节名"。
Returns
bool 成功删除返回 true,未找到返回 false。

◆ save() [1/2]

template<class CharT = char>
void console::BasicINIConfig< CharT >::save ( const Path & filename) const
inline

将配置保存到文件。

Parameters
filename目标文件路径。
Exceptions
FileError文件打开失败或写入流异常时抛出。

◆ save() [2/2]

template<class CharT = char>
void console::BasicINIConfig< CharT >::save ( const std::string & filename)
inline

将配置保存到文件。

Parameters
filename目标文件路径。
Exceptions
FileError文件打开失败或写入流异常时抛出。

◆ set()

template<class CharT = char>
void console::BasicINIConfig< CharT >::set ( const string_type & section_and_key,
const string_type & value )
inline

设置配置项的值。

Parameters
section_and_key节和键,格式为 "节名.键名"。
value要设置的值。
Exceptions
IndexError格式错误时抛出。
Note
若节或键不存在,会自动创建。

◆ to_narrow()

template<class CharT = char>
std::string console::BasicINIConfig< CharT >::to_narrow ( const string_type & s)
inlinestaticprivate

◆ write_to_stream()

template<class CharT = char>
void console::BasicINIConfig< CharT >::write_to_stream ( std::ostream & os) const
inlineprivate

◆ operator<<

template<class CharT = char>
std::basic_ostream< CharT > & operator<< ( std::basic_ostream< CharT > & os,
const BasicINIConfig< CharT > & config )
friend

配置对象的输出流运算符,按 INI 格式输出配置内容。

◆ operator>>

template<class CharT = char>
std::basic_istream< CharT > & operator>> ( std::basic_istream< CharT > & is,
BasicINIConfig< CharT > & config )
friend

配置对象的输入流运算符,按 INI 格式输入配置内容。

Member Data Documentation

◆ data_

template<class CharT = char>
std::map<string_type, std::map<string_type, string_type> > console::BasicINIConfig< CharT >::data_
private

The documentation for this class was generated from the following file: