c++ stream iword、pword和register_callback函数

pword,stream 的 register_callback函数

#include <iostream>
#include <sstream>
using namespace std;


const int name_index = std::ios::xalloc();

void SetStreamName (std::ios& stream, const char* name) {
   stream.pword(name_index) = const_cast<char*>(name);
}

std::ostream& StreamName (std::ostream& os) {
   const char* name = static_cast<const char*>(os.pword(name_index));
   if (name) os << name;
   else os << "(unknown)";
   return os;
}

void cofmtflags(ios_base::event e,std::ios_base& strm,int arg){
    if(e==ios_base::copyfmt_event){
        cout<<"copy fmt event"<<endl;
        strm.pword(arg)=new char[]{"World"};
    }else if(e==ios_base::erase_event){
        cout<<"erase event"<<endl;
    }else if(e==ios_base::imbue_event){
        cout<<"imbue event"<<endl;
    }
}

int main(){
    ostringstream ostr("Hello");
    SetStreamName(ostr, "String");
    ostr.register_callback(cofmtflags,name_index);
    cout.copyfmt(ostr);
    cout<<StreamName<<"123"<<endl;
}

iword使用

#include <iostream>

using namespace std;
static const int iword_index=ios_base::xalloc();

std::ostream& fraction_spaces(std::ostream& strm){
    strm.iword(iword_index)=true;
    return strm;
}
std::ostream& no_fraction_spaces(std::ostream& strm){
    strm.iword(iword_index)=false;
    return strm;
}

class Fraction{
public:
    Fraction(int num,int den):num(num),den(den){}
    int numerator() const{
        return num;
    }
    int denominator() const{
        return den;
    }

private:
    int num;
    int den;
};

ostream& operator<<(ostream& strm,const Fraction& f){

    if(strm.iword(iword_index)){
        strm<<f.numerator()<< " / "<<f.denominator();
    }else{
        strm<<f.numerator()<<"/"<<f.denominator();
    }
    return strm;
}

int main5(){
    Fraction df(11,22);

    cout<<df<<endl;

    cout<<fraction_spaces<<df<<endl;
    cout<<no_fraction_spaces<<df<<endl;

}
### C语言中的`IWORD``XWORD` 在标准C语言中,并不存在名为 `IWORD` 或者 `XWORD` 的预定义关键字或数据类型。这些术语可能是特定于某些编译器、库或者是自定义宏定义的一部分。 #### 自定义宏定义 如果项目中有使用到这两个名称,则很可能是通过宏定义来创建的特殊用途变量或者常量。例如: ```c #define IWORD int #define XWORD long long ``` 这种情况下,`IWORD` `XWORD` 实际上代表的是不同的基本数据类型,在不同环境中可以根据需求灵活调整其含义[^1]。 #### 使用场景 当开发者希望提高代码可读性维护性时,可能会采用这种方式给常用的数据类型赋予更具描述性的名字。比如在一个嵌入式开发环境里,为了区分字节宽度不同的整型数值,可以这样定义: ```c typedef unsigned char BYTE; typedef unsigned short WORD; typedef unsigned long DWORD; // 假设 IWORD 表示有符号短整形而 XWORD 是扩展版本的长整形 typedef signed short IWORD; typedef signed long long XWORD; ``` 这使得后续编码过程中可以直接利用更直观的名字来进行操作,同时也方便后期修改底层实现而不影响高层逻辑[^2]。 #### 数据类型说明 基于上述假设下的定义方式,以下是两种类型的简单介绍: - **IWORD**: 可能被用来表示较小范围内的带符号整数,通常对应于机器上的自然字大小减半的情况(如16位系统上的16位整数)。适用于存储相对简单的计数器或者其他不需要特别大取值空间的应用场合。 - **XWORD**: 则倾向于指代更大容量的带符号整数形式,能够容纳更加广泛的变化区间。适合处理涉及较大数量级运算的任务,像文件偏移地址计算或是高精度算术表达式的中间结果保存等情形[^3].
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值