定义自定义输出工具 [英] defining a custom output facility

查看:71
本文介绍了定义自定义输出工具的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个自定义输出工具。换句话说,我希望

一个对象,其使用类似于std :: cout / std :: cerr,但提供更多

的灵活性。而不是简单地将参数写入stdout / stderr,

我希望它写入stdout,写入文件和/或调用日志

函数。


所以我的输出函数可能如下所示:


OutputFacility& OutputFacility :: put(const std :: string& s){

cout<< S; //打印到stdout

write_to_logfile(s); //也调用记录功能

返回* this;

}


现在我可以定义<<运算符是put()的别名。


我的问题是:我是否必须为每种类型的输入定义put()我希望
打印?即,我希望这适用于所有原始类型,

int,char,double,float等。


现在我''我认为OutputFacility类可以有一个

std :: ostringstream作为私有成员;每个版本的

OutputFacility :: put()只会依次调用ostringstream put()

方法,然后调用实际的输出函数(例如write_to_logfile())。


似乎应该有一个更简单的方法来定义我的put

方法,即不是重载它并多次定义它,

如果我可以用魔法定义它一次参数意味着

取任何可以转换为字符串的东西。


感谢您的任何建议!

Matt


-

Matt Garman

发送电子邮件至: http://raw-sewage.net/index.php?file=email

I''d like to create a "custom output facility". In other words, I want
an object whose use is similar to std::cout/std::cerr, but offers more
flexibility. Instead of simply writing the parameter to stdout/stderr,
I''d like it to write to stdout, to a file, and/or call a logging
function.

So my output function might look something like this:

OutputFacility& OutputFacility::put(const std::string& s) {
cout << s; // print to stdout
write_to_logfile(s); // also calling logging function
return *this;
}

Now I can define the << operator to be an alias for put().

My question is: do I have to define put() for every type of input I
expect to print? I.e., I''d like this to work with all primitive types,
int, char, double, float, etc.

Right now I''m thinking that the OutputFacility class could have a
std::ostringstream as a private member; every version of
OutputFacility::put() would just in turn call the ostringstream put()
method, THEN call the actual output functions (e.g. write_to_logfile()).

It seems like there should be a simpler method of defining my put
method, i.e. instead of overloading it and defining it multiple times,
if I could just define it once with a "magical" parameter that means
"take anything that can be converted to a string".

Thanks for any advice!
Matt

--
Matt Garman
email at: http://raw-sewage.net/index.php?file=email

推荐答案

2004年1月13日星期二20:11:51 GMT,Matt Garman< fa ** @ not-real.bogus>写道:
On Tue, 13 Jan 2004 20:11:51 GMT, Matt Garman <fa**@not-real.bogus> wrote:
OutputFacility& OutputFacility :: put(const std :: string& s){
cout<< S; //打印到stdout
write_to_logfile(s); //也调用日志记录功能
return * this;
}
我的问题是:我是否必须为每种类型的输入定义put()我希望打印?即,我希望这适用于所有原始类型,
int,char,double,float等。
OutputFacility& OutputFacility::put(const std::string& s) {
cout << s; // print to stdout
write_to_logfile(s); // also calling logging function
return *this;
}

My question is: do I have to define put() for every type of input I
expect to print? I.e., I''d like this to work with all primitive types,
int, char, double, float, etc.




看起来我可以豁免以下内容:


模板< class T> OutputFacility&安培; OutputFacility :: put(const T& x){

std :: ostringstring os;

os<< x;

cout<< os.str();

write_to_logfile(os.str());

返回* this;

}


这似乎让我接近我想做什么。它适用于

字符串,c样式字符串(字符数组)和整数。但它并没有
识别std :: endl并且它会截断浮点数的一小部分。


有什么想法吗?


再次感谢!

Matt


-

Matt Garman

电子邮件地址: http://raw-sewage.net/index.php ?file = email


Matt Garman写道:
Matt Garman wrote:
我想创建一个自定义输出工具。换句话说,我想要一个使用类似于std :: cout / std :: cerr的对象,但提供更多的灵活性。而不是简单地将参数写入stdout / stderr,
我希望它写入stdout,文件和/或调用日志
函数。

所以我的输出函数可能看起来像这样:

OutputFacility& OutputFacility :: put(const std :: string& s){
cout<< S; //打印到stdout
write_to_logfile(s); //也调用日志功能
return * this;
}
现在我可以定义<< operator是put()的别名。

我的问题是:我是否必须为我希望打印的每种输入定义put()?即,我希望这适用于所有原始类型,
int,char,double,float等。

现在我正在考虑OutputFacility类可能有
std :: ostringstream作为私人成员;每个版本的OutputFacility :: put()都会依次调用ostringstream put()
方法,然后调用实际的输出函数(例如write_to_logfile())。

似乎应该有一个更简单的方法来定义我的put
方法,即不是重载它并多次定义它,如果我可以用魔法定义它一次。参数意味着取出任何可以转换成字符串的东西。

感谢您的任何建议!
Matt
I''d like to create a "custom output facility". In other words, I want
an object whose use is similar to std::cout/std::cerr, but offers more
flexibility. Instead of simply writing the parameter to stdout/stderr,
I''d like it to write to stdout, to a file, and/or call a logging
function.

So my output function might look something like this:

OutputFacility& OutputFacility::put(const std::string& s) {
cout << s; // print to stdout
write_to_logfile(s); // also calling logging function
return *this;
}

Now I can define the << operator to be an alias for put().

My question is: do I have to define put() for every type of input I
expect to print? I.e., I''d like this to work with all primitive types,
int, char, double, float, etc.

Right now I''m thinking that the OutputFacility class could have a
std::ostringstream as a private member; every version of
OutputFacility::put() would just in turn call the ostringstream put()
method, THEN call the actual output functions (e.g. write_to_logfile()).

It seems like there should be a simpler method of defining my put
method, i.e. instead of overloading it and defining it multiple times,
if I could just define it once with a "magical" parameter that means
"take anything that can be converted to a string".

Thanks for any advice!
Matt



模板< typename T>

OutputFacility&

operator<< (OutputFacility& out,T const& out)

{

return out.put(lexical_cast< std :: string>(s));

}

http: //www.boost.org/libs/conversion...m#lexical_cast


template< typename T >
OutputFacility&
operator << ( OutputFacility& out, T const& out )
{
return out.put( lexical_cast< std::string >( s ) );
}

http://www.boost.org/libs/conversion...m#lexical_cast


Matt Garman写道:
Matt Garman wrote:
这似乎让我接近我想做什么。它适用于
字符串,c样式字符串(字符数组)和整数。但它并没有识别std :: endl并且它会截断浮动的小数部分。
This seems to get me "close" to what I want to do. It works for
strings, c-style strings (char arrays), and integers. But it doesn''t
recognize std::endl and it truncates the fraction part of a float.




而是写一个换行符的函数,然后刷新ostream。

因为你的OutputFacility类不是一个ostream,它不行。


我没看到浮动截断,它对我有用。发布一个可编辑的

例子。


-

Andrew



std::endl is specific to ostreams. It is not declared as a newline, but
rather a function that writes a newline, then flushes the ostream.
Since your OutputFacility class isn''t an ostream, it won''t work.

I don''t see the float truncation, it works for me. Post a compilable
example.

--
Andrew


这篇关于定义自定义输出工具的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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