自定义流操纵器

时间:2014-07-05 16:22:30

标签: c++ stream ostream manipulators

我有一个继承自CFileManagerOStream的自定义流std::ostream,它从网络流类CTcpStream获取Unicode UTF16或UTF32字符串,并将它们作为UTF8字符串存储在磁盘上。字符串可能太大(可能是多个terrabytes)要转换为内存中的UTF8,所以我认为我需要为这个作业实现一个C ++流操作器。我找到的所有操纵器示例都采用整个字符串并对其进行处理,但由于内存要求较低,这在我的情况下无法完成。我准备好了所有的Unicode转换代码,我试图解决的问题是使用尽可能少的内部存储器进行转换。

我希望像这样使用操纵器;

CFileManagerOStream outFile("MultipleUtf8Strings.dat"); // Custom std::ostream
...
#ifdef _WINDOWS
CTcpStreamUtf16 largeBlobUtf16Stream;
...
outFile << ToUTF8FromUtf16 << largeBlobUtf16Stream;
#else
CTcpStreamUtf32 largeBlobUtf32Stream;
...
outFile << ToUTF8FromUtf32 << largeBlobUtf32Stream;
#endif

这是可能的还是我错误地接近了这个?

1 个答案:

答案 0 :(得分:1)

我发现使用std::ios_base::iword存储请求的字符编码是解决问题的最佳解决方案:

#include <iostream>

/*!
\brief Unicode encoding
*/
enum EUnicodeEnc
{
    /** UTF-8 character encoding */
    EUnicodeEnc_UTF8 = 1,

    /** UTF-16 character encoding */
    EUnicodeEnc_UTF16 = 2,

    /** UTF-32 character encoding */
    EUnicodeEnc_UTF32 = 3
};

/** Allocate the \c std::ios_base::iword storage for use with \c SourceStreamEncoding object instances */
int SourceStreamEncoding::sourceEnc_xalloc = std::ios_base::xalloc();

/*!
\brief Stream I/O manipulator changes the source character encoding to UTF-8
*/
std::ios_base& FromUtf8(std::ios_base& os) {
    os.iword(SourceStreamEncoding::sourceEnc_xalloc) = EUnicodeEnc_UTF8;
    return os;
}

/*!
\brief Stream I/O manipulator changes the source character encoding to UTF-16
*/
std::ios_base& FromUtf16(std::ios_base& os) {
    os.iword(SourceStreamEncoding::sourceEnc_xalloc) = EUnicodeEnc_UTF16;
    return os;
}

/*!
\brief Stream I/O manipulator changes the source character encoding to UTF-32
*/
std::ios_base& FromUtf32(std::ios_base& os) {
    os.iword(SourceStreamEncoding::sourceEnc_xalloc) = EUnicodeEnc_UTF32;
    return os;
}

/*!
\brief Overrides \c std::ostream::flush()
\details Converts the buffer to the correct character encoding then flushes buffer
after writing its content to a storage device
*/
std::ostream &CFileManagerOStream::flush()
{
    switch (os.iword(SourceStreamEncoding::sourceEnc_xalloc))
    {
        case EUnicodeEnc_UTF8:
            characterEncoder.FromUTF8(...);
        break;
        case EUnicodeEnc_UTF16:
            characterEncoder.FromUTF16(...);
        break;
        case EUnicodeEnc_UTF32:
            characterEncoder.FromUTF32(...);
        break;
    }
    return (*this);
}

// Now I can do as follows:
int main()
{
    CFileManagerOStream outFile("MultipleUtf8Strings.dat"); // Custom std::ostream
    ...
#ifdef _WINDOWS
    CTcpStreamUtf16 largeBlobUtf16Stream;
    ...
    outFile << FromUtf16 << largeBlobUtf16Stream;
#else
    CTcpStreamUtf32 largeBlobUtf32Stream;
    ...
    outFile << FromUtf32 << largeBlobUtf32Stream;
#endif
}

此外,我添加了以下操纵器,它只需要一个参数:

class FromEnc
{
    public:
        explicit FromEnc(int i) : i_(i) {}
        int i_;
    private:
        template <class charT, class Traits>
        friend std::basic_ostream<charT, Traits>& operator<<(std::basic_ostream<charT, Traits>& os, const FromEnc& w) {
            os.iword(SourceStreamEncoding::sourceEnc_xalloc) = w.i_;
            return os;
        }
};

,现在我也可以这样做:

outFile << FromEnc(EUnicodeEnc_UTF16) << largeBlobUtf16Stream;