ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
TEmulatedMapProxy.cxx
Go to the documentation of this file.
1 // @(#)root/io:$Id$
2 // Author: Markus Frank 28/10/04
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers. *
6  * All rights reserved. *
7  * *
8  * For the licensing terms see $ROOTSYS/LICENSE. *
9  * For the list of contributors see $ROOTSYS/README/CREDITS. *
10  *************************************************************************/
11 
12 //////////////////////////////////////////////////////////////////////////
13 // //
14 // TEmulatedMapProxy
15 //
16 // Streamer around an arbitrary container, which implements basic
17 // functionality and iteration.
18 //
19 // In particular this is used to implement splitting and abstract
20 // element access of any container. Access to compiled code is necessary
21 // to implement the abstract iteration sequence and functionality like
22 // size(), clear(), resize(). resize() may be a void operation.
23 //
24 //////////////////////////////////////////////////////////////////////////
25 
26 /**
27  \class TEmulatedMapProxy
28  \ingroup IO
29 
30 Streamer around a map container. For optimization reasons this functionality
31 is separated from the TEmulatedProxy.
32 */
33 
34 #include "TEmulatedMapProxy.h"
35 #include "TClassEdit.h"
36 #include "TStreamerInfo.h"
37 #include "TError.h"
38 
41 {
42  // copy constructor
44  Fatal("TEmulatedMapProxy","Class %s is not a map-type!",fName.c_str());
45  }
46 }
47 
48 TEmulatedMapProxy::TEmulatedMapProxy(const char* cl_name, Bool_t silent)
49  : TEmulatedCollectionProxy(cl_name, silent)
50 {
51  // Build a Streamer for an emulated vector whose type is 'name'.
53  Fatal("TEmulatedMapProxy","Class %s is not a map-type!",fName.c_str());
54  }
55 }
56 
58 {
59  // Standard destructor.
60 }
61 
63 {
64  // Virtual copy constructor.
65  if ( !fClass ) Initialize(kFALSE);
66  return new TEmulatedMapProxy(*this);
67 }
68 
70 {
71  // Return the address of the value at index 'idx'.
72  if ( fEnv && fEnv->fObject ) {
74  return idx<(c->size()/fValDiff) ? ((char*)&(*c->begin())) + idx*fValDiff : 0;
75  }
76  Fatal("TEmulatedMapProxy","At> Logic error - no proxy object set.");
77  return 0;
78 }
79 
81 {
82  // Return the current size of the container.
83  if ( fEnv && fEnv->fObject ) {
85  return fEnv->fSize = (c->size()/fValDiff);
86  }
87  Fatal("TEmulatedMapProxy","Size> Logic error - no proxy object set.");
88  return 0;
89 }
90 
92 {
93  // Map input streamer.
94  Bool_t vsn3 = b.GetInfo() && b.GetInfo()->GetOldVersion()<=3;
95  UInt_t idx, loop;
96  Int_t off[2] = {0, fValOffset };
97  Value *v, *val[2] = { fKey, fVal };
98  StreamHelper* helper;
99  float f;
100  char* addr = 0;
101  char* temp = (char*)At(0);
102  for ( idx = 0; idx < nElements; ++idx ) {
103  addr = temp + idx*fValDiff;
104  for ( loop=0; loop<2; loop++) {
105  addr += off[loop];
106  helper = (StreamHelper*)addr;
107  v = val[loop];
108  switch (v->fCase) {
109  case kIsFundamental: // Only handle primitives this way
110  case kIsEnum:
111  switch( int(v->fKind) ) {
112  case kBool_t: b >> helper->boolean; break;
113  case kChar_t: b >> helper->s_char; break;
114  case kShort_t: b >> helper->s_short; break;
115  case kInt_t: b >> helper->s_int; break;
116  case kLong_t: b >> helper->s_long; break;
117  case kLong64_t: b >> helper->s_longlong; break;
118  case kFloat_t: b >> helper->flt; break;
119  case kFloat16_t: b >> f;
120  helper->flt = float(f); break;
121  case kDouble_t: b >> helper->dbl; break;
122  case kBOOL_t: b >> helper->boolean; break;
123  case kUChar_t: b >> helper->u_char; break;
124  case kUShort_t: b >> helper->u_short; break;
125  case kUInt_t: b >> helper->u_int; break;
126  case kULong_t: b >> helper->u_long; break;
127  case kULong64_t: b >> helper->u_longlong; break;
128  case kDouble32_t:b >> f;
129  helper->dbl = double(f); break;
130  case kchar:
131  case kNoType_t:
132  case kOther_t:
133  Error("TEmulatedMapProxy","fType %d is not supported yet!\n",v->fKind);
134  }
135  break;
136  case kIsClass:
137  b.StreamObject(helper,v->fType);
138  break;
139  case kBIT_ISSTRING:
140  helper->read_std_string(b);
141  break;
142  case kIsPointer|kIsClass:
143  helper->set(b.ReadObjectAny(v->fType));
144  break;
146  helper->read_std_string_pointer(b);
147  break;
149  helper->read_tstring_pointer(vsn3,b);
150  break;
151  }
152  }
153  }
154 }
155 
157 {
158  // Map output streamer.
159  Value *v, *val[2] = { fKey, fVal };
160  int off[2] = { 0, fValOffset };
161  StreamHelper* i;
162  char* addr = 0;
163  char* temp = (char*)At(0);
164  for (UInt_t loop, idx = 0; idx < nElements; ++idx ) {
165  addr = temp + idx*fValDiff;
166  for ( loop = 0; loop<2; ++loop ) {
167  addr += off[loop];
168  i = (StreamHelper*)addr;
169  v = val[loop];
170  switch (v->fCase) {
171  case kIsFundamental: // Only handle primitives this way
172  case kIsEnum:
173  switch( int(v->fKind) ) {
174  case kBool_t: b << i->boolean; break;
175  case kChar_t: b << i->s_char; break;
176  case kShort_t: b << i->s_short; break;
177  case kInt_t: b << i->s_int; break;
178  case kLong_t: b << i->s_long; break;
179  case kLong64_t: b << i->s_longlong; break;
180  case kFloat_t: b << i->flt; break;
181  case kFloat16_t: b << float(i->flt); break;
182  case kDouble_t: b << i->dbl; break;
183  case kBOOL_t: b << i->boolean; break;
184  case kUChar_t: b << i->u_char; break;
185  case kUShort_t: b << i->u_short; break;
186  case kUInt_t: b << i->u_int; break;
187  case kULong_t: b << i->u_long; break;
188  case kULong64_t: b << i->u_longlong; break;
189  case kDouble32_t:b << float(i->dbl); break;
190  case kchar:
191  case kNoType_t:
192  case kOther_t:
193  Error("TEmulatedMapProxy","fType %d is not supported yet!\n",v->fKind);
194  }
195  break;
196  case kIsClass:
197  b.StreamObject(i,v->fType);
198  break;
199  case kBIT_ISSTRING:
200  TString(i->c_str()).Streamer(b);
201  break;
202  case kIsPointer|kIsClass:
203  b.WriteObjectAny(i->ptr(),v->fType);
204  break;
207  break;
209  i->write_tstring_pointer(b);
210  break;
211  }
212  }
213  }
214 }
215 
216 void TEmulatedMapProxy::ReadBuffer(TBuffer &b, void *obj, const TClass *onfileClass)
217 {
218  // Read portion of the streamer.
219 
220  SetOnFileClass((TClass*)onfileClass);
221  ReadBuffer(b,obj);
222 }
223 
225 {
226  // Read portion of the streamer.
227 
228  TPushPop env(this,obj);
229  int nElements = 0;
230  b >> nElements;
231  if ( fEnv->fObject ) {
232  Resize(nElements,true);
233  }
234  if ( nElements > 0 ) {
235  ReadMap(nElements, b);
236  }
237 }
238 
240 {
241  // TClassStreamer IO overload.
242  if ( b.IsReading() ) { //Read mode
243  UInt_t nElements = 0;
244  b >> nElements;
245  if ( fEnv->fObject ) {
246  Resize(nElements,true);
247  }
248  if ( nElements > 0 ) {
249  ReadMap(nElements, b);
250  }
251  }
252  else { // Write case
253  UInt_t nElements = fEnv->fObject ? Size() : 0;
254  b << nElements;
255  if ( nElements > 0 ) {
256  WriteMap(nElements, b);
257  }
258  }
259 }
virtual TVirtualCollectionProxy * Generate() const
Virtual copy constructor.
virtual void Streamer(TBuffer &refBuffer)
Streamer Function.
Bool_t IsReading() const
Definition: TBuffer.h:83
void Fatal(const char *location, const char *msgfmt,...)
TGenCollectionProxy * Initialize(Bool_t silent) const
Proxy initializer.
return c
virtual void StreamObject(void *obj, const type_info &typeinfo, const TClass *onFileClass=0)=0
std::string fName
Name of the class being proxied.
void ReadMap(UInt_t nElements, TBuffer &b)
virtual Int_t WriteObjectAny(const void *obj, const TClass *ptrClass)=0
Buffer base class used for serializing objects.
Definition: TBuffer.h:42
virtual UInt_t Size() const
Return the current size of the container.
Basic string class.
Definition: TString.h:137
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
const Bool_t kFALSE
Definition: Rtypes.h:92
Definition: drr.cxx:518
UInt_t fCase
type of data of Value_type
Streamer around an arbitrary STL like container, which implements basic container functionality...
virtual TVirtualStreamerInfo * GetInfo()=0
TFile * f
virtual void ReadBuffer(TBuffer &buff, void *pObj)
Streamer around a map container.
void Error(const char *location, const char *msgfmt,...)
EDataType fKind
kind of ROOT-fundamental type
virtual void Resize(UInt_t n, Bool_t force_delete)
Resize the container.
int fValOffset
Offset from key to value (in maps)
int fSTL_type
STL container type.
SVector< double, 2 > v
Definition: Dict.h:5
unsigned int UInt_t
Definition: RtypesCore.h:42
The ROOT global object gROOT contains a list of all defined classes.
Definition: TClass.h:81
int fValDiff
Offset between two consecutive value_types (memory layout).
virtual void * ReadObjectAny(const TClass *cast)=0
virtual Version_t GetOldVersion() const =0
TClassRef fType
TClass reference of Value_type in collection.
virtual void SetOnFileClass(TClass *cl)
Value * fKey
Descriptor of the key_type.
Helper class to facilitate I/O.
EnvironBase_t * fEnv
Address of the currently proxied object.
TEmulatedMapProxy(const TEmulatedMapProxy &copy)
void WriteMap(UInt_t nElements, TBuffer &b)
TObject * obj
virtual void * At(UInt_t idx)
Return the address of the value at index 'idx'.
Small helper to describe the Value_type or the key_type of an STL container.
void read_tstring_pointer(Bool_t vsn3, TBuffer &b)
Value * fVal
Descriptor of the Value_type.