CBuilder - FormatMessage API 的 STL“替代品”
这是“MakeMessage - FormatMessage API 的 STL '替代品'”的一个替代方案
引言
本文介绍了一种替代我之前的 'MakeMessage' 类的方法。
本文中介绍的 pja::CBuilder
类使用位置参数来格式化文本字符串。 参数可以是任何类型,包括整型或用户自定义类型,只要该类型定义了 friend std::ostream& operator <<()
,因为所有参数都通过将它们传递给 std::ostringstream
对象来格式化为字符串。 然后将这些字符串插入到原始格式字符串的指定位置。
Example usage
示例 1 - 一个基本示例
#include "builder.h" std::string format = "The first parameter is {0}, the second is {1}."; pja::CBuilder<> output(format, 15, 25.45); std::cout << output << std::endl;
此示例的结果是
The first parameter is 15, the second is 25.45.
示例 2 - 使用不同的插入标记
#include "builder.h" typedef pja::CBuilder<'%', '#'> MyBuilder; std::string format = "The second parameter is %1#, the first is %0#."; MyBuilder output(format, 15, 25.45); std::cout << output << std::endl;
此示例的结果是
The second parameter is 25.45, the first is 15.
示例 3 - 使用 % 运算符添加参数
#include "builder.h" std::string format = "The first parameter is {0}, the second is {1}."; pja::CBuilder<> output(format); output % 15; output % 25.45; std::cout << output << std::endl;
此示例的结果是
The first parameter is 15, the second is 25.45.
示例 4 - 使用用户自定义的修饰符类
#include "builder.h" class Hex // Hexadecimal formatted output { private: unsigned int h; public: Hex(unsigned int _h) : h(_h) {} friend std::tostream& operator << (std::tostream &os, const Hex& me) { std::ios_base::fmtflags flags = os.flags(); // get the current format flags os.unsetf(std::ios::dec); // clear the decimal flag os.setf(std::ios::hex | std::ios::uppercase); // set the hexadecimal and uppercase flags os.fill('0'); // have leading zeroes os.width(2); // and at least two digits os << "0x" << me.h; // output the hexadecimal number os.setf(flags); // reset the former format flags return os; } }; pja::CBuilder<> output("{0} in Hexadecimal is {1}."); output % 164 % Hex(164); std::cout << output << std::endl;
此示例的结果是
164 in Hexadecimal is 0xA4.
CBuilder 类
CBuilder 类是一个模板类,其中用于分隔插入标记的字符在声明 CBuilder 对象时在模板参数中声明。 默认情况下,分隔字符是左右大括号 ( { 和 } )。 请参阅示例 2 以获取使用不同分隔符字符的示例代码。
格式化字符串在声明 CBuilder 对象时在类构造函数中指定。 声明后它不能被更改,每个格式化字符串都必须构造一个新的 CBuilder 对象。 但是,参数可以根据需要重置。 只需使用 ClearParameters
函数清除旧参数,然后使用 %
参数添加运算符添加新参数即可。
CBuilder 类成员
构造函数
有两种类型的构造函数。第一种类型接受一个显式的格式化字符串,第二种类型从字符串资源加载格式化字符串。 两种类型最多可以接受十个可选参数,这些参数是要插入到格式化字符串中的任何类型的参数。 如果需要超过十个参数,可以使用 % 参数添加运算符添加它们。
语法
CBuilder(const std::tstring &Format [, P0 ... P9] )
CBuilder(HMODULE hModule, unsigned long FormatStringID [, P0 ... P9] )
参数
格式
- [in] 格式化字符串。
hModule
- [in] 模块的句柄,其可执行文件包含字符串资源。 使用
NULL
指定当前应用程序。
- [in] 模块的句柄,其可执行文件包含字符串资源。 使用
FormatStringID
- [in] 要加载的资源字符串的标识符。
P0 .. P9
- [opt] [in] 最多 10 个任何类型的可选参数。
参数管理
可以使用两个函数和一个运算符来管理传递给 CBuilder 对象的参数。
ClearParameters
清除此 CBuilder 对象中的所有参数。
语法
void ClearParameters(void)
GetParameterCount
返回此 CBuilder 对象中当前参数的数量。
语法
unsigned int GetParameterCount(void)
operator %
参数添加运算符。 用于将新的/更多参数添加到 CBuilder 对象。 此运算符可以链接在一起以在一个语句中添加多个参数。
语法
template <class T> CBuilder<Left, Right>& operator % (const T& Parameter)
参数
参数
- [in] 要添加的参数,可以是任何类型。
格式化输出
使用一个函数和两个运算符从 CBuilder 对象中获取格式化的文本。
c_str
返回一个 C 风格的指针,指向以 NULL 结尾的格式化文本字符串。
语法
const TCHAR *c_str()
operator std::tstring
返回一个包含格式化文本字符串的 std::tstring。
语法
operator const std::tstring()
operator <<
流输出运算符。 将格式化的输出发送到输出流。
语法
friend std::tostream& operator << (std::tostream &os, pja::CBuilder<Left, Right> &Builder)
更新
2012 年 9 月 26 日 - 更改文章标题
2024 年 3 月 4 日 - 修复了 Hex 类示例代码中的一个小错误。