Useing a TGTextView as an ostream object

Hi all!

first of all root 5.17/05 x86_64 GNU/Linux g++ 3.4.5

I’m writing a gui to make it simpler for other people than me to use some software I’ve written to do some tests on detectors. In the test software there is a class with methods whose output is given in a cout-like fashion but to any ostream (I have a
"std::ostream &aostream=std::cout" in the declaration of each method). What I would like to do is to give as an “ostream” a TGTextView that I use as output in my GUI. One why of doing this is using a ostringstream object and then copy the content line by line, but this does not work if I want the output statements in my methods to appear in the TGTextView as if they were written to std::cout in a terminal. Is the question clear?
Any ideas?

kind regards

Joa

Hi Joa,

There are methods LoadBuffer(const char* txtbuf) and LoadFile((const char* fname, long startpos = 0, long length = -1) that may help you to get what you want. It will be better if you provide a simple running example which shows what you have and what you need (please make it as simple as possible).

Cheers, Ilka

Hi again,

A small script that illustrates my question. When the button is pressed the numbers 0-9 is first written to std::cout with a 700ms delay between the numbers. Then they are written in the TextView. Note the difference, when written to the TextView one has to wait ~7s then is it all written at the same time. This is not what I want, I would like the same behaviour in both cases, i.e. a number is written to textview, one waits 700ms the next is written and so on.

It is not an option to let the help-class in my example see the TGTextView object: The only interface I have is an ostream-object.

Is the question clearer? Is it doable?

cheers

Joa
buttonTest.C (2.62 KB)

Hi Joa,
Please find your macro modified in a way I understood your needs.
Cheers, Ilka
buttonTest1.C (2.76 KB)

Hi Ilka

the modified script you sent does not quite work in my application, I’m really limited to using only an ostream object as communication! So I tried to implement something that works the way I would like it to:

class TGTextViewStream : public TGTextView, public std::ostream
{
.
}

This implementation of mine almost works, but not quite, and I’m sort on the edge om my c++ knowledge here. Maybe someone can help? I include the a small set of files to test what I have done. Test it like

root > .L TGTextViewStream.cpp+
root> .L testButton.C+
root> testButton()

Notice that my TextViewStream works if used directly but not if passad as an argument to a class method (which is what I would like to do). In this test the result is that nothing is written to the TGText buffer. In my real application I get a crash: Stack strace:

0x00000039d478eb44 in waitpid () from /lib64/tls/libc.so.6
#1 0x00000039d47398df in do_system () from /lib64/tls/libc.so.6
#2 0x0000002a957913d7 in TUnixSystem::StackTrace ()
from /home/jljungva/root/lib/root/libCore.so
#3 0x0000002a9578e198 in TUnixSystem::DispatchSignals ()
from /home/jljungva/root/lib/root/libCore.so
#4
#5 0x00000039d708552d in std::operator<< <std::char_traits > () <- ! I think
from /usr/lib64/libstdc++.so.6
#6 0x0000002a97c7083b in TNT2::tnt2::ReadCalibration (this=0xb761b8,
inputstream=Variable “inputstream” is not available.
) at tnt2.cpp:855

cheers

Joa
TextViewStream.tar (20 KB)

Hi,

the stream operators are non-virtual, so you cannot overload them in a derived class and have them called through the base class pointer / reference. Instead you’ll have to work on the buffer; see e.g. http://spec.winprog.org/streams/ Make yourself a cup of coffee before reading that :slight_smile: Good luck!

Cheers, Axel.

Hi,

Thanks, I sort of expected that I was bit too optimistic;( Luckly I got coffee and the weather in the Paris region is really bad! Maybe I get around to make a std::istream-object as well…

Anyway, I post the code for TGTextViewStream when it works, maybe someone else would like something like it.

cheers

Joa

Hi all

Problems problems…

How do I solve this?

Generating dictionary /home/jljungva/tmp/obj/TGTextViewStreamDict.cpp…
Limitation: Can’t instantiate precompiled template basic_streambuf<char,std::char_traits > TGTextViewStream.h:15:
Limitation: Can’t instantiate precompiled template basic_ostream<char,std::char_traits > TGTextViewStream.h:42:
Warning: Error occurred during reading source files
Warning: Error occurred during dictionary source generation
!!!Removing /home/jljungva/tmp/obj/TGTextViewStreamDict.cpp /home/jljungva/tmp/obj/TGTextViewStreamDict.h !!!
Error: rootcint: error loading headers…
make: *** [/home/jljungva/tmp/obj/TGTextViewStreamDict.cpp] Error 1

There are some more issues with the code, but neither mind them, as soon as I can test what I’m doing I’ll tackel them

Source files included…

cheers

Joa
tgstream.tar (10 KB)

Hi,

As a work around (if you really need to use the basic_ostream rather than a one of derived class) use #ifdef __MAKECINT__ class TGTextViewStreamBuf #else class TGTextViewStreamBuf : public TGTextView, public std::basic_streambuf<char, std::char_traits<char> > #endif

Cheers,
Philippe

Hi again.

Now I got something that works like I want it too, so as promised, here it is. Thanks for the help.

cheers
Joa
tgtextviewstream.tar (10 KB)

Hi Joa,

It is now implemented in 5-34-00-patches and soon in the master. Could you send me your full info (name, email) in order to add your contribution in the CREDITS file? Thanks a lot for you contribution! (and sorry for the loooong delay :blush: )

Cheers, Bertrand.