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<<(ostr eam_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 6 3596 ja**@yankeeboys oftware.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<<(ostr eam_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<Man ipT>(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 ja**@yankeeboys oftware.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.co m> <http://www.dietmar-kuehl.de/>
<http://www.eai-systems.com> - Efficient Artificial Intelligence
On 20 Dec 2005 13:52:54 -0800 in comp.lang.c++, ja**@yankeeboys oftware.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.
> 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 ? ja**@yankeeboys oftware.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 ja**@yankeeboys oftware.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.co m> <http://www.dietmar-kuehl.de/>
<http://www.eai-systems.com> - Efficient Artificial Intelligence This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 which is compiled and works fine.
############################################
|
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 (with a full
Ref<Object> specialization) that works like a smart pointer to the
Object-rooted hierarchy. The only way to create Object instances is
via a static factory that returns a Ref<Object> to a heap allocated
object.
|
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)> myFunctor = SampleFunction;
string result = myFunctor(7, true);
Works great thanks to the help from this group. Here's the guts so far
for two-arity:
|
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 methods. I don't show the Integer class because it
will not add any information to my problem. Main is using some STL function.
In main you put in nodes in the STL list and you can display the STL list
and other things such as erase some value from...
|
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 that I am instantiating UI
before any call to out.operator<<(). Is there a better way to accomplish this?
What I'd like is something I could simply place in the output call between '<<'
operators without having to instantiate it first. The...
| |
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 a named namespace
namespace foo
{
class Foo
|
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 *enemydata; // Holds information about an enemy (in a game)
// Its a double linked list node
|
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:
double operator+ ( char* left
, char* right)
{
// some code here
}
|
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 need to read in a text file... shown below
H
H,E,L
E,B,F
B,A,C
A,null,null
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |