如何为一般情况编写流插入运算符? (也就是说,对于`char`和`wchar_t`流?)

时间:2010-12-28 19:27:07

标签: c++ templates unicode iostream

我正在为我的一类实施stream insertion operator。我希望我的班级可以同时使用窄流和宽流。我正在使用模板来允许这种行为 - 除了字符文字之外,所有内容都与实际使用的流类型无关。如果它是一个宽字符串,字符文字需要L前置于文字,否则它们不会。

有没有办法将这种事情键入模板参数,这样我就不需要复制这么多代码了?

(如果可能的话,我宁愿避免在运行时执行从窄到宽的字符或从宽到窄的字符转换。)

我目前拥有的示例 - 它是一个模板,但由于字符文字宽,它不适用于窄字符流:

template <typename charT, typename traits>
std::basic_ostream<charT, traits>& operator<<(
    std::basic_ostream<charT, traits>& lhs,
    const Process& rhs
    )
{
    lhs << L"Process (0x" << std::setw(8) << std::hex
        << std::setfill(L'0') << rhs.GetId() << L") ";
    lhs << rhs.GetName() << std::endl;
    lhs << L"Command Line: " << rhs.GetCmdLine() << std::endl;
    const std::vector<Thread>& threads = rhs.GetThreads();
    for (std::vector<Thread>::const_iterator it = threads.begin(); 
        it != threads.end(); ++it)
    {
        lhs << L" --> " << *it << std::endl;
    }
    const std::map<void *, Module>& modules = rhs.GetModules();
    for (std::map<void *, Module>::const_iterator it = modules.begin(); 
        it != modules.end(); ++it)
    {
        lhs << L" --> " << it->second << std::endl;
    }
    return lhs;
}

2 个答案:

答案 0 :(得分:8)

如果您不想要运行时开销,我认为,虽然丑陋,但在这种情况下,宏可以帮助您。

template <typename T>
inline const T* select(const char* narrow, const wchar_t* wide);

template <>
inline const char* select<char>(const char* narrow, const wchar_t* /*wide*/)
{
    return narrow;
}

template <>
inline const wchar_t* select<wchar_t>(const char* /*narrow*/, const wchar_t* wide)
{
    return wide;
}

#define doselect(T, str) select<T>(str, L ## str)

template <typename charT, typename traits>
std::basic_ostream<charT, traits>& operator<<(
    std::basic_ostream<charT, traits>& lhs,
    const Process& rhs
    )
{
    lhs << doselect(charT, "Process (0x") << std::setw(8) << std::hex
        << std::setfill(charT('0')) << rhs.GetId() << doselect(charT, ") ");
    lhs << rhs.GetName() << std::endl;
    lhs << doselect(charT, "Command Line: ") << rhs.GetCmdLine() << std::endl;
    const std::vector<Thread>& threads = rhs.GetThreads();
    for (std::vector<Thread>::const_iterator it = threads.begin(); 
        it != threads.end(); ++it)
    {
        lhs << doselect(charT, " --> ") << *it << std::endl;
    }
    const std::map<void *, Module>& modules = rhs.GetModules();
    for (std::map<void *, Module>::const_iterator it = modules.begin(); 
        it != modules.end(); ++it)
    {
        lhs << doselect(charT, " --> ") << it->second << std::endl;
    }
    return lhs;
}

您可以使用另一个漂亮的宏扩展doselect,以进一步减少代码重复。即doselect2(" --> ")会自动扩展为doselect(charT, " --> ")

答案 1 :(得分:1)

我所做的只是插入char,并将它们转换为宽字符或窄字符。转换在编译期间完成,如果您只使用宽和窄编码相同的字符,它确实有效。这很烦人,但确实有效。

template <typename charT, typename traits>
std::basic_ostream<charT, traits>& operator<<(
    std::basic_ostream<charT, traits>& lhs,
    const Process& rhs
    )
{
    lhs << charT('P') << charT('r') << charT('o') << charT('c') << charT('e')
        << charT('s') << charT('s') << charT(' ') << charT('(') << charT('0')
        << charT('x') << std::setw(8) << std::hex << std::setfill(charT('0'))
        << rhs.GetId() << charT(')') << charT(' ');
    lhs << rhs.GetName() << std::endl;
    lhs << charT('C') << charT('o') << charT('m') << charT('m') << charT('a')
        << charT('n') << charT('d') << charT(' ') << charT('L') << charT('i')
        << charT('n') << charT('e') << charT(':') << charT(' ')
        << rhs.GetCmdLine() << std::endl;
    const std::vector<Thread>& threads = rhs.GetThreads();
    for (std::vector<Thread>::const_iterator it = threads.begin(); 
        it != threads.end(); ++it)
    {
        lhs << charT(' ') << charT('-') << charT('-') << charT('>') << charT(' ')
            << *it << std::endl;
    }
    const std::map<void *, Module>& modules = rhs.GetModules();
    for (std::map<void *, Module>::const_iterator it = modules.begin(); 
        it != modules.end(); ++it)
    {
        lhs << charT(' ') << charT('-') << charT('-') << charT('>') << charT(' ')
            << it->second << std::endl;
    }
    return lhs;
}

如果您有许多字符串,则可以使用另一个模板,您将专注于宽字符或窄字符类型并用于存储字符串。然而,这将迫使你复制你的字符串(你将失败DRY原则)。

template <typename charT>
struct ProcessInsertionOperatorHelper
{
    static const charT* const String1;
    static const charT* const String2;
    static const charT* const String3;
    static const charT* const String4;
};

template <>
const wchar_t* const ProcessInsertionOperatorHelper<wchar_t>::String1 = L"Process (0x";
template <>
const wchar_t* const ProcessInsertionOperatorHelper<wchar_t>::String2 = L") ";
template <>
const wchar_t* const ProcessInsertionOperatorHelper<wchar_t>::String3 = L"Command Line: ";
template <>
const wchar_t* const ProcessInsertionOperatorHelper<wchar_t>::String4 = L" --> ";

template <>
struct ProcessInsertionOperatorHelper<char>
{
};

template <typename charT, typename traits>
std::basic_ostream<charT, traits>& operator<<(
    std::basic_ostream<charT, traits>& lhs,
    const Process& rhs
    )
{
    lhs << ProcessInsertionOperatorHelper<charT>::String1 << std::setw(8)
        << std::hex << std::setfill(L'0') << rhs.GetId()
        << ProcessInsertionOperatorHelper<charT>::String2;
    lhs << rhs.GetName() << std::endl;
    lhs << ProcessInsertionOperatorHelper<charT>::String3
        << rhs.GetCmdLine() << std::endl;
    const std::vector<Thread>& threads = rhs.GetThreads();
    for (std::vector<Thread>::const_iterator it = threads.begin(); 
        it != threads.end(); ++it)
    {
        lhs << ProcessInsertionOperatorHelper<charT>::String4
            << *it << std::endl;
    }
    const std::map<void *, Module>& modules = rhs.GetModules();
    for (std::map<void *, Module>::const_iterator it = modules.begin(); 
        it != modules.end(); ++it)
    {
        lhs << ProcessInsertionOperatorHelper<charT>::String4
            << it->second << std::endl;
    }
    return lhs;
}