Logo ROOT   6.10/09
Reference Guide
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
43  if ( !(fSTL_type == ROOT::kSTLmap || fSTL_type == ROOT::kSTLmultimap) ) {
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'.
52  if ( !(fSTL_type == ROOT::kSTLmap || fSTL_type == ROOT::kSTLmultimap) ) {
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 kUChar_t: b >> helper->u_char; break;
123  case kUShort_t: b >> helper->u_short; break;
124  case kUInt_t: b >> helper->u_int; break;
125  case kULong_t: b >> helper->u_long; break;
126  case kULong64_t: b >> helper->u_longlong; break;
127  case kDouble32_t:b >> f;
128  helper->dbl = double(f); break;
129  case kchar:
130  case kNoType_t:
131  case kOther_t:
132  Error("TEmulatedMapProxy","fType %d is not supported yet!\n",v->fKind);
133  }
134  break;
135  case kIsClass:
136  b.StreamObject(helper,v->fType);
137  break;
138  case kBIT_ISSTRING:
139  helper->read_std_string(b);
140  break;
141  case kIsPointer|kIsClass:
142  helper->set(b.ReadObjectAny(v->fType));
143  break;
145  helper->read_std_string_pointer(b);
146  break;
148  helper->read_tstring_pointer(vsn3,b);
149  break;
150  }
151  }
152  }
153 }
154 
156 {
157  // Map output streamer.
158  Value *v, *val[2] = { fKey, fVal };
159  int off[2] = { 0, fValOffset };
160  StreamHelper* i;
161  char* addr = 0;
162  char* temp = (char*)At(0);
163  for (UInt_t loop, idx = 0; idx < nElements; ++idx ) {
164  addr = temp + idx*fValDiff;
165  for ( loop = 0; loop<2; ++loop ) {
166  addr += off[loop];
167  i = (StreamHelper*)addr;
168  v = val[loop];
169  switch (v->fCase) {
170  case kIsFundamental: // Only handle primitives this way
171  case kIsEnum:
172  switch( int(v->fKind) ) {
173  case kBool_t: b << i->boolean; break;
174  case kChar_t: b << i->s_char; break;
175  case kShort_t: b << i->s_short; break;
176  case kInt_t: b << i->s_int; break;
177  case kLong_t: b << i->s_long; break;
178  case kLong64_t: b << i->s_longlong; break;
179  case kFloat_t: b << i->flt; break;
180  case kFloat16_t: b << float(i->flt); break;
181  case kDouble_t: b << i->dbl; break;
182  case kUChar_t: b << i->u_char; break;
183  case kUShort_t: b << i->u_short; break;
184  case kUInt_t: b << i->u_int; break;
185  case kULong_t: b << i->u_long; break;
186  case kULong64_t: b << i->u_longlong; break;
187  case kDouble32_t:b << float(i->dbl); break;
188  case kchar:
189  case kNoType_t:
190  case kOther_t:
191  Error("TEmulatedMapProxy","fType %d is not supported yet!\n",v->fKind);
192  }
193  break;
194  case kIsClass:
195  b.StreamObject(i,v->fType);
196  break;
197  case kBIT_ISSTRING:
198  TString(i->c_str()).Streamer(b);
199  break;
200  case kIsPointer|kIsClass:
201  b.WriteObjectAny(i->ptr(),v->fType);
202  break;
205  break;
207  i->write_tstring_pointer(b);
208  break;
209  }
210  }
211  }
212 }
213 
214 void TEmulatedMapProxy::ReadBuffer(TBuffer &b, void *obj, const TClass *onfileClass)
215 {
216  // Read portion of the streamer.
217 
218  SetOnFileClass((TClass*)onfileClass);
219  ReadBuffer(b,obj);
220 }
221 
223 {
224  // Read portion of the streamer.
225 
226  TPushPop env(this,obj);
227  int nElements = 0;
228  b >> nElements;
229  if ( fEnv->fObject ) {
230  Resize(nElements,true);
231  }
232  if ( nElements > 0 ) {
233  ReadMap(nElements, b);
234  }
235 }
236 
238 {
239  // TClassStreamer IO overload.
240  if ( b.IsReading() ) { //Read mode
241  UInt_t nElements = 0;
242  b >> nElements;
243  if ( fEnv->fObject ) {
244  Resize(nElements,true);
245  }
246  if ( nElements > 0 ) {
247  ReadMap(nElements, b);
248  }
249  }
250  else { // Write case
251  UInt_t nElements = fEnv->fObject ? Size() : 0;
252  b << nElements;
253  if ( nElements > 0 ) {
254  WriteMap(nElements, b);
255  }
256  }
257 }
Bool_t IsReading() const
Definition: TBuffer.h:81
virtual void Streamer(TBuffer &refBuffer)
Streamer Function.
virtual TVirtualCollectionProxy * Generate() const
Virtual copy constructor.
void Fatal(const char *location, const char *msgfmt,...)
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
virtual UInt_t Size() const
Return the current size of the container.
Buffer base class used for serializing objects.
Definition: TBuffer.h:40
Basic string class.
Definition: TString.h:129
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
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
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)
TGenCollectionProxy * Initialize(Bool_t silent) const
Proxy initializer.
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:71
int fValDiff
Offset between two consecutive value_types (memory layout).
virtual void StreamObject(void *obj, const std::type_info &typeinfo, const TClass *onFileClass=0)=0
const Bool_t kFALSE
Definition: RtypesCore.h:92
double f(double x)
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.
you should not use this method at all Int_t Int_t Double_t Double_t Double_t Int_t Double_t Double_t Double_t Double_t b
Definition: TRolke.cxx:630
TEmulatedMapProxy(const TEmulatedMapProxy &copy)
void WriteMap(UInt_t nElements, TBuffer &b)
virtual void * At(UInt_t idx)
Return the address of the value at index &#39;idx&#39;.
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.