By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,655 Members | 964 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,655 IT Pros & Developers. It's quick & easy.

method to count 'endl' manipulator on insertion operator

P: n/a
I have a class which overloads the insertion operator '<<' for every
type that ostream handles. I do this so that I can use my class a
direct replacement for cout and cerr where the insertion syntax is
used. The reason for this is that I have a log file class that needs
to know how many lines have been written. When the line exceeds a
certain number, I need to close the log file, and open a new one.

I would like to be able to track the number of times that the 'endl'
manipulator is passed to the insertion operator.

I realise that I can't simply compare the pf pointer with endl in this
function

ostream_type&
operator<<(ostream_type& (*pf) (ostream_type&))
{
if(pf == endl)
count++;
}

because it won't compile. But I was wondering if there is a way to get
the address of the concrete implementation of the endl manipulator for
the cout and cerr implementation explicitely.

Thanks

Dec 20 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
ja**@yankeeboysoftware.com wrote:
I have a class which overloads the insertion operator '<<' for every
type that ostream handles. I do this so that I can use my class a
direct replacement for cout and cerr where the insertion syntax is
used. The reason for this is that I have a log file class that needs
to know how many lines have been written. When the line exceeds a
certain number, I need to close the log file, and open a new one.

I would like to be able to track the number of times that the 'endl'
manipulator is passed to the insertion operator.

I realise that I can't simply compare the pf pointer with endl in this
function

ostream_type&
operator<<(ostream_type& (*pf) (ostream_type&))
{
if(pf == endl)
count++;
}

because it won't compile. But I was wondering if there is a way to get
the address of the concrete implementation of the endl manipulator for
the cout and cerr implementation explicitely.


try to explicitely cast the manipulator to the function pointer type:

typedef ostream_type& (*ManipT) (ostream_type&);
if (pf == static_cast<ManipT>(endl))
....

either there is some arcane reason in the standard (anyone?) or this is
just a shortcoming of many current compilers, but using template
function addresses in expressions without casting or storing in a
temporary variable and using that instead does not compile.

-- peter

Dec 21 '05 #2

P: n/a
ja**@yankeeboysoftware.com wrote:
I have a class which overloads the insertion operator '<<' for every
type that ostream handles. I do this so that I can use my class a
direct replacement for cout and cerr where the insertion syntax is
used. The reason for this is that I have a log file class that needs
to know how many lines have been written. When the line exceeds a
certain number, I need to close the log file, and open a new one.


This is the entirely wrong approach to go about the problem! The
correct approach is not to defeat the IOStream classes but to use
them: you shall create a filtering stream buffer and check for
appropriate conditions in its 'overflow()' and 'sync()' methods.
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.eai-systems.com> - Efficient Artificial Intelligence
Dec 21 '05 #3

P: n/a
On 20 Dec 2005 13:52:54 -0800 in comp.lang.c++,
ja**@yankeeboysoftware.com wrote,
I have a class which overloads the insertion operator '<<' for every
type that ostream handles. I do this so that I can use my class a
direct replacement for cout and cerr where the insertion syntax is
used. The reason for this is that I have a log file class that needs
to know how many lines have been written. When the line exceeds a
certain number, I need to close the log file, and open a new one.
Bad idea. Use a custom streambuf derivative and don't redo any of
the formatting operations of the iostream front end. Your class is
not a direct replacement for cout or cerr unless it can be the
object of a std::iostream& reference.

See:
http://www.inf.uni-konstanz.de/~kuehl/c++/iostream/
http://groups.google.com/groups?as_q...p.lang.c%2B%2B
I would like to be able to track the number of times that the 'endl'
manipulator is passed to the insertion operator.


Should probably be never. Use endl only when it is important to
flush the stream after every line. For ordinary purposes use '\n'.

Your streambuf can count instances of '\n' sent to it.

Dec 21 '05 #4

P: n/a
>
Bad idea. Use a custom streambuf derivative and don't redo any of
the formatting operations of the iostream front end.
I guess you are correct here. I kind of knew my solution was a bit
wrong, but couldn't figure out how.
Your class is
not a direct replacement for cout or cerr unless it can be the
object of a std::iostream& reference.
I am not sure I understand what you are saying here. Could you amplify
this.

See:
http://www.inf.uni-konstanz.de/~kuehl/c++/iostream/


That would solve one part of my problem (which I didn't state) in that
I also need to put a timestamp on each new line. That was one of the
reasons for intercepting the endl manip., I cannot see, howvever how I
would use this to open a new file when I reach a certain line count.
What am I missing ?
I would like to be able to track the number of times that the 'endl'
manipulator is passed to the insertion operator.


Should probably be never. Use endl only when it is important to
flush the stream after every line. For ordinary purposes use '\n'.

Your streambuf can count instances of '\n' sent to it.


Understood. I realise that endl caused a flush, but prefer it to the
use of '\n' in my strings. BTW, is there a manipulator which just
inserts a '\n' character and doesn't flush ?

Dec 21 '05 #5

P: n/a

ja**@yankeeboysoftware.com wrote:
I would like to be able to track the number of times that the 'endl'
manipulator is passed to the insertion operator.
Should probably be never. Use endl only when it is important to
flush the stream after every line. For ordinary purposes use '\n'.

Your streambuf can count instances of '\n' sent to it.


Understood. I realise that endl caused a flush, but prefer it to the
use of '\n' in my strings.


Any particular reason?
BTW, is there a manipulator which just
inserts a '\n' character and doesn't flush ?


Errr... no. Because the effect of inserting '\n' is "inserts a '\n'
character and doesn't flush". It does precisely what you want. Why not
use it?

Gavin Deane

Dec 21 '05 #6

P: n/a
ja**@yankeeboysoftware.com wrote:
That would solve one part of my problem (which I didn't state) in that
I also need to put a timestamp on each new line. That was one of the
reasons for intercepting the endl manip., I cannot see, howvever how I
would use this to open a new file when I reach a certain line count.
What am I missing ?
There is no need to use a generic filtering stream buffer which gets
the stream buffer to used passed, e.g. as constructor argument.
Instead, you can use a 'std::filebuf' internally which you open to
refer to the desired file. For example, prior to writing the
timestamp you could check the current line count and, when necessary,
close the file buffer and reopen it to write to a new file.
Understood. I realise that endl caused a flush, but prefer it to the
use of '\n' in my strings.
Just note that relying on 'endl' is somewhat error prone. In a logging
stream buffer I would probably only use an internal buffer and have
each character be intercepted by 'overflow()' such that I can write
the line as soon as I receive a newline. Whether the users remembers
to flush the stream buffer would be irrelevant in this situation.

Personally, I think that 'endl' is grossly overused. It looks like a
bright idea to realize a writing mode similar to C's line buffered
mode (see the '_IOLBF' parameter to C's 'setvbuf()') but it relies on
the user to do the right thing. One thing is granted: if I'm member of
the project I would mess up things in this context! Dealing with
newlines in the stream buffer is much more reliable although, depending
on your needs, it may have a performance impact. If the latter is
indeed problematic, I would still not rely on 'endl' but rather set up
"unit buffering" (see 'std::ios_base::unitbuf') on my stream...
BTW, is there a manipulator which just
inserts a '\n' character and doesn't flush ?


No but it is easy to write:

std::ostream& nl(std::ostream& out) { return out << '\n'; }

.... plus some template stuff for a little bit more generic version.
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.eai-systems.com> - Efficient Artificial Intelligence
Dec 21 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.