A
aaragon
Hi everybody,
I was wondering if there is an easy way that one could create a custom
class that inherits from std:stream to override the functionality of
operator<<.
Since I work with several processes, I want to avoid some of them to
print information to standard output (or enable that to print
information concurrently).
I tried the following but it didn't work:
class Output_strem : public std:stream {
typedef std:stream base_type;
public:
Output_strem() : base_type() {}
template <typename T>
std:stream& operator<<(T t) {
#ifdef MPI
std:stream& os = *this;
os<<"["<<Parallel_base::rank_<<"] ";
#endif
return *this;
}
};
As you see, nothing is telling the ostream object to print to standard
output. I saw the definition of the cout in the standard library and
it looks like this:
ios_base::Init::Init()
{
if (__gnu_cxx::__exchange_and_add_dispatch(&_S_refcount, 1) == 0)
{
// Standard streams default to synced with "C" operations.
_S_synced_with_stdio = true;
new (&buf_cout_sync) stdio_sync_filebuf<char>(stdout);
// other created objects
// The standard streams are constructed once only and never
// destroyed.
new (&cout) ostream(&buf_cout_sync);
// other created objects
// NB: Have to set refcount above one, so that standard
// streams are not re-initialized with uses of ios_base::Init
// besides <iostream> static object, ie just using <ios> with
// ios_base::Init objects.
__gnu_cxx::__atomic_add_dispatch(&_S_refcount, 1);
}
}
Can someone point me in the right direction?
Thank you all,
aa
I was wondering if there is an easy way that one could create a custom
class that inherits from std:stream to override the functionality of
operator<<.
Since I work with several processes, I want to avoid some of them to
print information to standard output (or enable that to print
information concurrently).
I tried the following but it didn't work:
class Output_strem : public std:stream {
typedef std:stream base_type;
public:
Output_strem() : base_type() {}
template <typename T>
std:stream& operator<<(T t) {
#ifdef MPI
std:stream& os = *this;
os<<"["<<Parallel_base::rank_<<"] ";
#endif
return *this;
}
};
As you see, nothing is telling the ostream object to print to standard
output. I saw the definition of the cout in the standard library and
it looks like this:
ios_base::Init::Init()
{
if (__gnu_cxx::__exchange_and_add_dispatch(&_S_refcount, 1) == 0)
{
// Standard streams default to synced with "C" operations.
_S_synced_with_stdio = true;
new (&buf_cout_sync) stdio_sync_filebuf<char>(stdout);
// other created objects
// The standard streams are constructed once only and never
// destroyed.
new (&cout) ostream(&buf_cout_sync);
// other created objects
// NB: Have to set refcount above one, so that standard
// streams are not re-initialized with uses of ios_base::Init
// besides <iostream> static object, ie just using <ios> with
// ios_base::Init objects.
__gnu_cxx::__atomic_add_dispatch(&_S_refcount, 1);
}
}
Can someone point me in the right direction?
Thank you all,
aa