过载运算符<<用于文本呈现

时间:2013-02-19 03:19:22

标签: c++ templates operator-overloading

我想通过使用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()的字符串,但我不太清楚如何实现它。

2 个答案:

答案 0 :(得分:2)

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;
}

答案 1 :(得分:0)

我喜欢从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";
}