CBSTRStream - 一个简单的 BSTR 流实现





0/5 (0投票)
2001年4月13日
2分钟阅读

70256

519
CBSTRStream 是一个简单的 BSTR 流实现,包含一些有用的数据类型转换函数。
- 下载源代码文件 - 3 Kb
此文件还包含 CEnBSTR 类。
引言
有几个类可以处理 BSTR
字符串,但是至今我还没有找到一个提供基本缓冲支持的类。CBSTRStream
类旨在提供此功能以及一些有用的数据类型转换函数。
这个类不是对流行的 _bstr_t
类的替代品,但它被设计成可以与之互操作。
用法
这个类非常易于使用(如下面的示例所示)。您只需使用 << 运算符将数据附加到流中。文章末尾的列表提供了更详细的概述。
示例 1(基本用法)
_bstr_t bstrData(L"somedata"); _bstr_t bstrData2(L"some other data"); GUID guid = //someguid.. long nData = 3; CBSTRStream bsData; bsData << bstrData << L"TEST" << nData << L" " << bstrData2 << L" " << guid;
示例 2(组合 SQL 查询)
long nItemId = 1; CBSTRStream bsSelect; bsSelect << L"SELECT It_Name, It_Desc FROM It_Item WHERE It_Id = " << nItemId;
示例 3(使用分配提示)
long nData = 14; long nData2 = 18; long nData3 = 25; CBSTRStream bsData(10, 5); bsData << L"Numbers: " << nData << L", " << nData2; bsData << L", " << nData3;
构造函数
CBSTRStream(const CBSTRStream& bsSrc, long nInitialBufferSize = 50, long nChunkSize = 50) CBSTRStream(long nInitialBufferSize = 50, long nChunkSize = 50) |
|
这些构造函数为您提供了设置流分配器分配提示的机会。nInitialBufferSize 设置首次缓冲区分配期间使用的尺寸,nChunkSize 设置下一次缓冲区重新分配期间附加的尺寸。 |
运算符
CBSTRStream& operator << (const _bstr_t& bstrData) CBSTRStream& operator << (wchar_t* pstr) CBSTRStream& operator << (REFGUID src) CBSTRStream& operator << (long nData) CBSTRStream& operator << (unsigned long nData) CBSTRStream& operator << (int nData) CBSTRStream& operator << (double dValue) CBSTRStream& operator << (const CBSTRStream& bsSrc) |
|
这些运算符提供基本的流支持以及数据类型转换。它们会将各自数据类型的字符串表示形式附加到流中。 | |
operator == |
|
比较两个 CBSTRStream 对象。 |
|
operator = |
|
将新值分配给现有的 CBSTRStream 对象。 |
|
operator BSTR() operator _bstr_t() |
|
提取封装的 BSTR 对象的指针/对象。 |
操作
void reset | |
重置字符串(但保留缓冲区)。 | |
long length const | |
返回封装的 BSTR 的长度。 |
限制
- 这个类不是线程安全的。
历史
v1.0 (2001-04-13)
- 初始发布