【问题标题】:Overload operator<< for text rendering重载操作符<< 用于文本渲染
【发布时间】:2013-02-19 03:19:22
【问题描述】:

我想创建一个类,通过使用 3D 渲染器提供类似 std::cout 或 QDebug 的功能来帮助我进行调试。

我现在正在使用以下渲染器方法

IRenderer::renderText(int posX, int posY, const float* color, const char* text, ...);

// E.g.
int i;
float f;
float color[] = {1, 1, 1, 1};

renderer->renderText(50, 50, color, "Float %f followed by int %i", f, i);

这实际上工作正常,但我想知道是否可以创建一个允许我这样做的类:

debug() << "My variables: " << i << ", " << "f";

我假设会有一个模板函数可以根据输入类型构建要传递给renderText() 的字符串,但我不太确定如何实现它。

【问题讨论】:

    标签: c++ templates operator-overloading


    【解决方案1】:

    Rob 答案的替代方法是在您的自定义记录器类中包含 ostringstream,并使用析构函数进行记录:

    #include <iostream>
    #include <sstream>
    
    class MyLogger
    {
    protected:
        std::ostringstream ss;
    
    public:
        ~MyLogger()
        {
            std::cout << "Hey ma, I'm a custom logger! " << ss.str();
    
            //renderer->renderText(50, 50, color, ss.str());
        }
    
        std::ostringstream& Get()
        {
            return ss;
        }
    };
    
    int main()
    {
        int foo = 12;
        bool bar = false;
        std::string baz = "hello world";
    
        MyLogger().Get() << foo << bar << baz << std::endl;
    
        // less verbose to use a macro:
    #define MY_LOG() MyLogger().Get()
        MY_LOG() << baz << bar << foo << std::endl;
    
        return 0;
    }
    

    【讨论】:

    • 我建议您不要使用宏,而是为 MyLogger 类定义 template&lt;class T&gt; MyLogger &amp; operator &lt;&lt; (T const &amp;),这与 ss 相同
    【解决方案2】:

    我喜欢从 std::ostream 派生我的日志记录类,所以我得到了所有流的优点。诀窍是将所有特定于应用程序的代码放在相关的 streambuf 类中。考虑这个工作示例。要修改它以满足您的需要,只需重写CLogBuf::sync(),如下所示:

    int sync() { 
      renderer->renderText(50, 50, color, "%s", str());
      str("");
      return false;
    }  
    

    例子:

    #include <iostream>
    #include <sstream>
    
    class CLogger : public std::ostream {
    private:
        class CLogBuf : public std::stringbuf {
        private:
            // or whatever you need for your application
            std::string m_marker;
        public:
            CLogBuf(const std::string& marker) : m_marker(marker) { }
            ~CLogBuf() {  pubsync(); }
            int sync() { std::cout << m_marker << ": " << str(); str("");  return !std::cout; }
        };
    
    public:
        // Other constructors could specify filename, etc
        // just remember to pass whatever you need to CLogBuf
        CLogger(const std::string& marker) : std::ostream(new CLogBuf(marker)) {}
        ~CLogger() { delete rdbuf(); }
    };
    
    int main()
    {
        CLogger hi("hello");
        CLogger bye("goodbye");
    
        hi << "hello, world" << std::endl;
        hi << "Oops, forgot to flush.\n";
        bye << "goodbye, cruel world\n" << std::flush;
        bye << "Cough, cough.\n";
    }
    

    【讨论】:

    • 你甚至不需要创建临时记录器:CLogger("hello") &lt;&lt; "hello, world" &lt;&lt; std::endl;
    • @CongXu - 是的,您确实需要命名对象。临时对象不能绑定到像operator&gt;&gt;(ostream&amp;, const char*) 这样的非常量引用。见stackoverflow.com/questions/14381311/…
    • 有什么方法可以在不使用流的情况下做到这一点?
    • 因为我正在处理的代码使用禁用了 iostreams 的 stlport。我现在使用 va_list 有一个可行的解决方案,但仍然想知道如何使用 实现一个
    猜你喜欢
    • 2015-10-09
    • 1970-01-01
    • 2018-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多