如何使用IOStream存储格式设置?
How to store formatting settings with an IOStream?
为用户定义类型创建格式化输出时,通常需要定义自定义格式标志。例如,如果自定义字符串类可以选择在字符串周围添加引号,那就太好了:
String str("example");
std::cout << str << ' ' << squotes << str << << ' ' << dquotes << str << 'n';
应该产生
example 'example' "example"
创建操纵器来更改格式标志本身非常简单:
std::ostream& squotes(std::ostream& out) {
// what magic goes here?
return out;
}
std::ostream& dquotes(std::ostream& out) {
// similar magic as above
return out;
}
std::ostream& operator<< (std::ostream& out, String const& str) {
char quote = ????;
return quote? out << quote << str.c_str() << quote: str.c_str();
}
。但是,操纵器如何存储哪些引号应该与流一起使用,然后让输出运算符检索值?
流类被设计为可扩展的,包括存储附加信息的能力: 流对象(实际上是公共基类std::ios_base
)提供了几个管理与流关联的数据的函数:
iword()
,它将int
作为键并产生一个以0
开头的int&
。int
作为键并产生一个以0
开头的void*&
。xalloc()
一个static
函数,它在每次调用时产生不同的int
以"分配"唯一密钥(无法释放密钥)。register_callback()
注册在流被销毁、调用copyfmt()
或imbue()
新std::locale
时调用的函数。
pword()
,它将为了存储简单的格式信息,如String
示例中所示,分配一个int
并在iword()
中存储合适的值就足够了:
int stringFormatIndex() {
static int rc = std::ios_base::xalloc();
return rc;
}
std::ostream& squote(std::ostream& out) {
out.iword(stringFormatIndex()) = ''';
return out;
}
std::ostream& dquote(std::ostream& out) {
out.iword(stringFormatIndex()) = '"';
return out;
}
std::ostream& operator<< (std::ostream& out, String const& str) {
char quote(out.iword(stringFormatIndex()));
return quote? out << quote << str.c_str() << quote: out << str.c_str();
}
该实现使用stringFormatIndex()
函数来确保在首次调用函数时初始化rc
只分配一个索引。由于iword()
在没有为流设置值时返回0
,因此此值用于默认格式(在本例中不使用引号)。如果应该使用报价,则报价的char
值仅存储在iword()
中。
使用iword()
相当简单,因为不需要任何资源管理。例如,假设String
也应该使用字符串前缀打印:前缀的长度不应受到限制,即它不适合int
。设置前缀已经有点复杂了,因为相应的操纵器需要是一个类类型:
class prefix {
std::string value;
public:
prefix(std::string value): value(value) {}
std::string const& str() const { return this->value; }
static void callback(std::ios_base::event ev, std::ios_base& s, int idx) {
switch (ev) {
case std::ios_base::erase_event: // clean up
delete static_cast<std::string*>(s.pword(idx));
s.pword(idx) = 0;
break;
case std::ios_base::copyfmt_event: // turn shallow copy into a deep copy!
s.pword(idx) = new std::string(*static_cast<std::string*>(s.pword(idx)));
break;
default: // there is nothing to do on imbue_event
break;
}
}
};
std::ostream& operator<< (std::ostream& out, prefix const& p) {
void*& pword(out.pword(stringFormatIndex()));
if (pword) {
*static_cast<std::string*>(pword) = p.str();
}
else {
out.register_callback(&prefix::callback, stringFormatIndex());
pword = new std::string(p.str());
}
return out;
}
要创建带有参数的操纵器,将创建一个对象,该对象捕获要用作前缀的std::string
,并实现"输出运算符"以在pword()
中实际设置前缀。由于只能存储void*
,因此有必要分配内存并维护可能存在的内存:如果已经存储了一些东西,则必须是std::string
并将其更改为新前缀。否则,将注册一个回调,用于维护pword()
的内容,一旦注册了回调,就会分配一个新的std::string
并将其存储在pword()
中。
棘手的业务是回调:在三个条件下调用它:
- 当流
s
被销毁或调用s.copyfmt(other)
时,将调用每个已注册的回调,s
作为std::ios_base&
参数,并将事件std::ios_base::erase_event
。此标志的目标是释放任何资源。为避免意外双重释放数据,pword()
设置为删除std::string
后0
。 - 调用
s.copyfmt(other)
时,将在复制所有回调和内容后std::ios_base::copyfmt_event
事件一起调用回调。但是,pword()
将只包含原始副本的浅拷贝,即回调需要制作pword()
的深层副本。由于回调是在之前用std::ios_base::erase_event
调用的,因此不需要清理任何内容(无论如何,此时都会被覆盖)。 - 调用
s.imbue()
后,使用std::ios_base::imbue_event
调用回调。此调用的主要用途是更新可能为流缓存std::locale
特定值。对于前缀维护,这些调用将被忽略。
上面的代码应该是描述数据如何与流关联的大纲。该方法允许存储任意数据和多个独立的数据项。值得注意的是,xalloc()
仅返回一个唯一整数序列。如果iword()
或pword()
的用户不使用xalloc()
则索引可能会发生冲突。因此,使用xalloc()
使不同的代码很好地协同工作非常重要。
这是一个活生生的例子。
- 如何以叮当格式设置评论的行长?
- Vulkan 中的动态顶点缓冲区格式设置
- 如何将多种语言设置放在单个 .clang 格式文件中
- 如何设置叮当格式的注释编译指示,以免触及多行doxygen注释?
- 如何禁用影响宏的clang格式设置
- 保证最大输出长度为双精度,并设置默认输出格式的精度
- 如何编辑格式设置以增加换行长度?
- 如何设计对象以不同方式设置文本格式
- 如何为输入设置变量编号和__android_log_print格式
- 使用无符号字符设置文本格式
- 如何设置数字格式以显示C 中的所有小数位数
- MinGW中的数字格式设置更改
- C++库以人类可读的格式存储设置
- CLANG 格式:禁用宏的格式设置
- 设置返回指向包含类的指针的函数的格式
- 实现区域设置格式的时间输出
- 各种操作系统的区域设置格式
- 提升区域设置格式
- Boost区域设置格式百分比
- 在 c++ 中设置格式 SQL 查询