473,325 Members | 2,860 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

method to count 'endl' manipulator on insertion operator

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
6 3568
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
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
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
>
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

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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Alex Vinokur | last post by:
========================================= Windows 2000 CYGWIN_NT-5.0 1.3.22(0.78/3/2) GNU gcc version 3.2 20020927 (prerelease) ========================================= Here is some program...
6
by: Gonçalo Rodrigues | last post by:
Hi all, The following (test) program is not working as I expected. Note: The code is a bit long but is easy to understand: There is a class Object implementing ref counting. A Ref<T> template...
4
by: daniel.w.gelder | last post by:
I wrote a template class that takes a function prototype and lets you store and call a C-level function, like this: inline string SampleFunction(int, bool) {..} functor<string (int, bool)>...
0
by: Tony Johansson | last post by:
Hello! I have two classes called Handle which is a template class and a class Integer which is not a template class. The Integer class is just a wrapper class for a primitive int with some...
0
by: Steven T. Hatton | last post by:
I tried to create my own manipulator that would both set the width of the subsequent output field, and cast an unsigned char to unsigned in. I came up with the following rather ugly hack. Notice...
20
by: Patrick Guio | last post by:
Dear all, I have some problem with insertion operator together with namespace. I have a header file foo.h containing declaration of classes, typedefs and insertion operators for the typedefs in...
10
by: Kent | last post by:
Hi! I want to store data (of enemys in a game) as a linked list, each node will look something like the following: struct node { double x,y; // x and y position coordinates struct enemy...
3
by: kvnsmnsn | last post by:
I've been asked to overload the insertion operator. What exactly is the insertion operator in C++, and how would one overload it? I think that in C I could overload the "+" operator like so: ...
2
by: slizorn | last post by:
hi guys, i need to make a tree traversal algorithm that would help me search the tree.. creating a method to search a tree to find the position of node and to return its pointer value basically i...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.