[ROOT] operator>>(TBuffer &buf, int*& x) (and << as well)

From: Brett Viren (bv@bnl.gov)
Date: Wed Oct 25 2000 - 16:41:00 MEST


Hi all.

Thanks to folks here on roottalk, I have my templated heterogeneous
container, Registry, (relatively) happily sending basic C++ types as
well as custom and standard ROOT based classes through ROOT I/O.

One thing that I had to do in order to provide a uniform templated
Streamer function was to write the following operators for the basic
C++ types:

	TBuffer& operator>>(TBuffer &buf, int*& xptr);
	TBuffer& operator>>(TBuffer &buf, double*& xptr);
	TBuffer& operator>>(TBuffer &buf, float*& xptr);
	// etc...

	TBuffer& operator<<(TBuffer &buf, int*& xptr);
	TBuffer& operator<<(TBuffer &buf, double*& xptr);
	TBuffer& operator<<(TBuffer &buf, float*& xptr);
	// etc...

For the case of `int' they are defined as:

	TBuffer& operator>>(TBuffer &buf, int*& xptr)
	{
	    int x;
	    buf >> x;
	    xptr = new int(x);	// Caller must own this now!
	    return buf;
	}
	TBuffer& operator<<(TBuffer &buf, int*& xptr)
	{
	    buf << *xptr;
	    return buf;
	}

For other types the operators are defined similarly.

These are needed because ROOT treats ROOT object I/O different than
base type I/O.  The `>>' and `<<' operators take (reference to)
pointers for ROOT objects and cause a ROOT object to ``spring forth''
(be created) when a `>>' operator is called.  OTOH, for base types,
the `>>' and `<<' operators are called by reference to a value, and no
creation is done.

Now, putting these operators inside my Registry container code seems
wrong, since there are not specific to my application.  Should these
be included in ROOT proper (in TBuffer.h)?


BTW, there are two types which cause problems, `char' and `char*',
because ROOT already has an operator>>(char*) and an operator<<(char*)
for reading in strings (and *not* for causing a char to ``spring
forth'' durring a >> operation).  I had to handle this by writing a
specialized Streamer for any char or char* type.  I guess this must
stay a special case.

-Brett.



This archive was generated by hypermail 2b29 : Tue Jan 02 2001 - 11:50:36 MET