做一个包装的cout? [英] Make a wrapper for cout?

查看:211
本文介绍了做一个包装的cout?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以这里有一个有趣的问题,我该如何做一些有趣的包装?
我想把它添加到一个dll,所以我可以把它放入我的程序。但它的基本语法应该是

  Mything :: mesage<< 我是一些文字<< im_an_int<< someclass<< mything :: ENDL; 

  Mything :: mesageandlog<<< 我要打印到控制台,还有一个文件! << mything :: ENDL; 

我可以处理大部分的内部逻辑,但是我应该怎么做甚至这样做。



可能在我的类中创建一个静态流成员称为消息,然后当它的写入也会通过一个方法运行时发生事件?



Idk,我环顾四周,发现一些类似的东西,但是把它扔到一个dll里,我感到很失落。 (如何编写函数因为这需要我使用extern和一个变量,而是如何使它变得静态,所以我可以直接调用它而不创建一个变量? p>

澄清的一点,像这样:
mydll.h

  #include< iostream> 
命名空间mynamespace {

extern struct LogMessage {};

模板< typename T>
LogMessage&运营商LT;< (LogMessage& s,const T& x){
SetStdHandle(STD_OUTPUT_HANDLE,GetStdHandle(STD_OUTPUT_HANDLE));
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_BLUE);
std :: cout<<< [IF];
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_WHITE);
// LogTimestamp(); - 这意味着。
std :: cout<<< x< ENDL;
// writeStreamToLogfile(s); - 不好处理这个。
return s;
}
}

driverprogram.h

  #include< mydll.h> 
#include< iostream>
int _tmain(int argc,_TCHAR * argv [])
{
mynamespace :: LogMessage<< 事情:< std :: endl;
}

期望输出: / p>

 [IF] [00:00:00]某事


您可以创建一个具有<<运算符

  struct OutputThing 
{

template<类T>
OutputThing& operator<(T val)
{
std :: cout<< val;
return * this;
}
};

现在,只要您想要登录,就必须对该对象进行实例。

  OutputThing()<x =<< x; 

如果你想避免重复的构造和破坏的对象,你可以使它成为一个单身。

  struct OutputThingSingleton 
{
static OutputThingSingleton& GetThing()
{
static OutputThingSingleton OutputThing;
return OutputThing;
}

模板<类T>
OutputThingSingleton& operator<(T val)
{
std :: cout<< val;
return * this;
}

private:
OutputThingSingleton()
{};

};

所以调用现在看起来像

  OutputThingSingleton :: GetThing()<x =<< x; 

您可以使用宏来缩短。



这可以跨多个DLL,但是根据它的使用方式,您可以有多个单例的实例存在。只要您不想在您的单身人士中保持任何状态,这可以正常工作。如果您确实需要确保单个实例,则可以在自己的dll中进行编译。使用这个DLL的任何其他二进制文件将共享dll中的单个实例拥有。


So here's an interesting question, How would I make something kinda like a wrapper for cout? I want to be able to add it into a dll so I can throw it into my programs. but the basic syntax of it should be

    Mything::mesage << "I'm some text" << im_an_int << someclass << mything::endl;

or

    Mything::mesageandlog << "I'm going to print to console, and to a file!" << mything::endl;

I can handle most of the internal logic but as to what I should put to even do this. kinda stumped.

Possibly make a static stream member in my class called message, then have an event fire when its written too that runs it through a method?

Idk, I looked around and found something sortA similar, but as for throwing it into a dll I'm at a loss. (How to write a function wrapper for cout that allows for expressive syntax?) because this requires me to use extern and a variable, but how would I make it static so I can just straight call it without creating a variable?

Bit of clarification, something like this: mydll.h

#include <iostream>
namespace mynamespace {

    extern struct LogMessage{};

    template <typename T>
    LogMessage& operator<< (LogMessage &s, const T &x) {
        SetStdHandle(STD_OUTPUT_HANDLE, GetStdHandle(STD_OUTPUT_HANDLE));
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_BLUE);
        std::cout << "[IF] ";
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_WHITE);
        //LogTimestamp(); --ill impliment this.
        std::cout << x << endl;
        //writeStreamToLogfile(s); --and ill handle this.
        return s;
    }
}

driverprogram.h

#include <mydll.h>
#include <iostream>
int _tmain(int argc, _TCHAR* argv[])
{
    mynamespace::LogMessage << "Something: << std::endl;
}

expected output:

"[IF] [00:00:00] Something

解决方案

You can create a struct, that has a << operator

struct OutputThing
{

  template< class T >
  OutputThing &operator<<( T val )
  {
    std::cout<<val;
    return *this;
  }
};

Now whenever you want to log, you will have to instance the object.

OutputThing()<<"x ="<<x;

If you want to avoid the repeated construction and destruction of the object, you can make it a singleton.

struct OutputThingSingleton
{
  static OutputThingSingleton& GetThing()
  {
    static OutputThingSingleton OutputThing;
    return OutputThing;
  }

  template< class T >
  OutputThingSingleton &operator<<( T val )
  {
    std::cout<<val;
    return *this;
  }

private:
  OutputThingSingleton()
  {};

};

So the call now looks like

OutputThingSingleton::GetThing()<<"x ="<<x;

Which you could shorten using a macro.

This will work across multiple dlls, however depending on how it is used you can have multiple instances of the singleton existing. This would work fine as long as you don't want to maintain any state in your singleton. If you do need to ensure a single instance, you can compile it in its own dll. Any other binary that uses this dll will share the single instance 'owned' by the dll.

这篇关于做一个包装的cout?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆