Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
TProtoClass.cxx
Go to the documentation of this file.
1 // @(#)root/meta:$
2 // Author: Axel Naumann 2014-05-02
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2014, 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 /** \class TProtoClass
13 Persistent version of a TClass.
14 */
15 
16 #include "TProtoClass.h"
17 
18 #include "TBaseClass.h"
19 #include "TClass.h"
20 #include "TClassEdit.h"
21 #include "TDataMember.h"
22 #include "TEnum.h"
23 #include "TInterpreter.h"
24 #include "TList.h"
25 #include "TListOfDataMembers.h"
26 #include "TListOfEnums.h"
27 #include "TRealData.h"
28 #include "TError.h"
29 
30 #include <cassert>
31 
32 ////////////////////////////////////////////////////////////////////////////////
33 /// Initialize a TProtoClass from a TClass.
34 
35 TProtoClass::TProtoClass(TClass* cl):
36  TNamed(*cl), fBase(cl->GetListOfBases()),
37  fEnums(cl->GetListOfEnums()), fSizeof(cl->Size()), fCheckSum(cl->fCheckSum),
38  fCanSplit(cl->fCanSplit), fStreamerType(cl->fStreamerType), fProperty(cl->fProperty),
39  fClassProperty(cl->fClassProperty)
40 {
41  if (cl->Property() & kIsNamespace){
42  //fData=new TListOfDataMembers();
43  fEnums=nullptr;
44  //fPRealData=nullptr;
45  fOffsetStreamer=0;
46  return;
47  }
48  TListOfEnums *enums = dynamic_cast<TListOfEnums*>(fEnums);
49  if (enums && !enums->fIsLoaded) {
50  // Make sure all the enum information is loaded
51  enums->Load();
52  }
53  // initialize list of data members (fData)
54  TList * dataMembers = cl->GetListOfDataMembers();
55  if (dataMembers && dataMembers->GetSize() > 0) {
56  fData.reserve(dataMembers->GetSize() );
57  for (auto * obj : *dataMembers) {
58  TDataMember * dm = dynamic_cast<TDataMember*>(obj);
59  fData.push_back(dm);
60  }
61  }
62 
63  fPRealData.reserve(100);
64 
65  if (!cl->GetCollectionProxy()) {
66  // Build the list of RealData before we access it:
67  cl->BuildRealData(0, true /*isTransient*/);
68  // The data members are ordered as follows:
69  // - this class's data members,
70  // - foreach base: base class's data members.
71  // fPRealData encodes all TProtoRealData objects with a
72  // TObjString to signal a new class.
73  TClass* clCurrent = cl;
74  fDepClasses.push_back(cl->GetName() );
75  TRealData* precRd = nullptr;
76  for (auto realDataObj: *cl->GetListOfRealData()) {
77  TRealData *rd = (TRealData*)realDataObj;
78  if (!precRd) precRd = rd;
79  TClass* clRD = rd->GetDataMember()->GetClass();
80  TProtoRealData protoRealData(rd);
81  if (clRD != clCurrent) {
82  // here I have a new class
83  fDepClasses.push_back(clRD->GetName() );
84  clCurrent = clRD;
85  protoRealData.fClassIndex = fDepClasses.size()-1;
86  //protoRealData.fClass = clRD->GetName();
87  //TObjString *clstr = new TObjString(clRD->GetName());
88  if (rd->TestBit(TRealData::kTransient)) {
89  //clstr->SetBit(TRealData::kTransient);
90  protoRealData.SetFlag(TProtoRealData::kIsTransient,true);
91  }
92  else
93  protoRealData.SetFlag(TProtoRealData::kIsTransient,false);
94 
95  // fPRealData->AddLast(clstr);
96  precRd = rd;
97  }
98  //fPRealData->AddLast(new TProtoRealData(rd));
99  fPRealData.push_back(protoRealData);
100  }
101 
102  // if (gDebug > 2) {
103  // for (const auto &data : fPRealData) {
104  // const auto classType = dataPtr->IsA();
105  // const auto dataName = data.fName;
106  // const auto dataClass = data.fClass;
107  // Info("TProtoClass","Data is a protorealdata: %s - class %s - transient %d", dataName.Data(),dataClass.Data(),data.fIsTransient);
108  //if (!dataClass.IsNull()
109  // if (classType == TProtoRealData::Class())
110  // Info("TProtoClass","Data is a protorealdata: %s", dataPtrName);
111  // if (classType == TObjString::Class())
112  // Info("TProtoClass","Data is a objectstring: %s", dataPtrName);
113  // if (dataPtr->TestBit(TRealData::kTransient))
114  // Info("TProtoClass","And is transient");
115  // }
116  // }
117  }
118 
119  // this crashes
120  cl->CalculateStreamerOffset();
121  fOffsetStreamer = cl->fOffsetStreamer;
122 }
123 
124 // // conversion of a new TProtoClass from an old TProtoClass
125 // //______________________________________________________________________________
126 // TProtoClass::TProtoClass(TProtoClassOld * pc):
127 // TNamed(pc->GetName(),pc->GetTitle()), fBase(pc->fBase),
128 // fEnums(pc->fEnums), fSizeof(pc->fSizeof), fCanSplit(pc->fCanSplit),
129 // fStreamerType(pc->fStreamerType), fProperty(pc->fProperty),
130 // fClassProperty(pc->fClassProperty), fOffsetStreamer( pc->fOffsetStreamer)
131 // {
132 
133 // fBase = (pc->fBase) ? (TList*) pc->fBase->Clone() : 0;
134 // //fData = (pc->fData) ? (TList*) pc->fData->Clone() : 0;
135 // fEnums = (pc->fEnums) ? (TList*) pc->fEnums->Clone() : 0;
136 
137 // // initialize list of data members (fData)
138 // TList * dataMembers = pc->fData;
139 // if (dataMembers && dataMembers->GetSize() > 0) {
140 // fData.reserve(dataMembers->GetSize() );
141 // for (auto * obj : *dataMembers) {
142 // TDataMember * dm = dynamic_cast<TDataMember*>(obj);
143 // if (dm) {
144 // TDataMember * dm2 = (TDataMember *) dm->Clone();
145 // if (dm2) fData.push_back(dm2);
146 // }
147 // }
148 // }
149 
150 // fPRealData.reserve(100);
151 
152 // TString className;
153 // for (auto dataPtr : *(pc->fPRealData) ) {
154 
155 // const auto classType = dataPtr->IsA();
156 // if (classType == TObjString::Class()) {
157 // className = dataPtr->GetName();
158 // }
159 // else if (classType == TProtoClass::TProtoRealData::Class()) {
160 // TProtoRealData protoRealData;
161 // TProtoClass::TProtoRealData * oldData= ( TProtoClass::TProtoRealData * )dataPtr;
162 // TClass * cl = TClass::GetClass(className);
163 // //protoRealData.fName = dataPtr->GetName();
164 // //TObject * obj = cl->GetListOfDataMembers()->FindObject( );
165 // protoRealData.fDMIndex = DataMemberIndex(cl, dataPtr->GetName() );
166 // // protoRealData.fTitle = dataPtr->GetTitle();
167 // //protoRealData.fClass = className;
168 // className.Clear();
169 // protoRealData.fIsTransient = dataPtr->TestBit(TRealData::kTransient);
170 // protoRealData.fOffset = oldData->GetOffset();
171 // protoRealData.fIsObject = dataPtr->TestBit(BIT(15));
172 // fPRealData.push_back(protoRealData);
173 // }
174 // }
175 // }
176 
177 ////////////////////////////////////////////////////////////////////////////////
178 /// Destructor.
179 
180 TProtoClass::~TProtoClass()
181 {
182  Delete();
183 }
184 
185 ////////////////////////////////////////////////////////////////////////////////
186 /// Delete the containers that are usually owned by their TClass.
187 /// if (fPRealData) fPRealData->Delete(opt);
188 /// delete fPRealData; fPRealData = 0;
189 
190 void TProtoClass::Delete(Option_t* opt /*= ""*/) {
191  if (fBase) fBase->Delete(opt);
192  delete fBase; fBase = 0;
193 
194  for (auto dm: fData) {
195  delete dm;
196  }
197 
198  if (fEnums) fEnums->Delete(opt);
199  delete fEnums; fEnums = 0;
200 
201  if (gErrorIgnoreLevel==-2) printf("Delete the protoClass %s \n",GetName());
202 }
203 
204 ////////////////////////////////////////////////////////////////////////////////
205 /// Move data from this TProtoClass into cl.
206 /// Returns 'false' if nothing was done. This can happen in the case where
207 /// there is more than one dictionary for the same entity. Note having
208 /// duplicate dictionary is acceptable for namespace or STL collections.
209 
210 Bool_t TProtoClass::FillTClass(TClass* cl) {
211  if (cl->fRealData || cl->fBase.load() || cl->fData || cl->fEnums.load() || cl->fSizeof != -1 || cl->fCanSplit >= 0 ||
212  cl->fProperty != (-1)) {
213 
214  if (cl->GetCollectionType() != ROOT::kNotSTL) {
215  // We are in the case of collection, duplicate dictionary are allowed
216  // (and even somewhat excepted since they can be auto asked for).
217  // They do not always have a TProtoClass for them. In particular
218  // the one that are pre-generated in the ROOT build (in what was once
219  // called the cintdlls) do not have a pcms, neither does vector<string>
220  // which is part of libCore proper.
221  if (gDebug > 0)
222  Info("FillTClass", "Returning w/o doing anything. %s is a STL collection.",cl->GetName());
223  return kFALSE;
224  }
225  if (cl->Property() & kIsNamespace) {
226  if (gDebug > 0)
227  Info("FillTClass", "Returning w/o doing anything. %s is a namespace.",cl->GetName());
228  return kFALSE;
229  }
230  Error("FillTClass", "TClass %s already initialized!", cl->GetName());
231  return kFALSE;
232  }
233  if (gDebug > 1) Info("FillTClass","Loading TProtoClass for %s - %s",cl->GetName(),GetName());
234 
235  if (fPRealData.size() > 0) {
236 
237  // A first loop to retrieve the mother classes before starting to
238  // fill this TClass instance. This is done in order to avoid recursions
239  // for example in presence of daughter and mother class present in two
240  // dictionaries compiled in two different libraries which are not linked
241  // one with each other.
242  for (auto element: fPRealData) {
243  //if (element->IsA() == TObjString::Class()) {
244  if (element.IsAClass() ) {
245  if (gDebug > 1) Info("","Treating beforehand mother class %s",GetClassName(element.fClassIndex));
246  TInterpreter::SuspendAutoParsing autoParseRaii(gInterpreter);
247 
248  TClass::GetClass(GetClassName(element.fClassIndex));
249  }
250  }
251  }
252 
253 
254  //this->Dump();
255 
256  // Copy only the TClass bits.
257  // not bit 13 and below and not bit 24 and above, just Bits 14 - 23
258  UInt_t newbits = TestBits(0x00ffc000);
259  cl->ResetBit(0x00ffc000);
260  cl->SetBit(newbits);
261 
262  cl->fName = this->fName;
263  cl->fTitle = this->fTitle;
264  cl->fBase = fBase;
265 
266  // fill list of data members in TClass
267  //if (cl->fData) { cl->fData->Delete(); delete cl->fData; }
268  cl->fData = new TListOfDataMembers(fData);
269  // for (auto * dataMember : fData) {
270  // //printf("add data member for class %s - member %s \n",GetName(), dataMember->GetName() );
271  // cl->fData->Add(dataMember);
272  // }
273  // // set loaded bit to true to avoid re-loading the data members
274  // cl->fData->SetIsLoaded();*
275 
276  //cl->fData = (TListOfDataMembers*)fData;
277 
278  // The TDataMember were passed along.
279  fData.clear();
280 
281  // We need to fill enums one by one to initialise the internal map which is
282  // transient
283  {
284  auto temp = new TListOfEnums();
285  if (fEnums) {
286  for (TObject* enumAsTObj : *fEnums){
287  temp->Add((TEnum*) enumAsTObj);
288  }
289  // We did not transfer the container itself, let remove it from memory without deleting its content.
290  fEnums->Clear();
291  delete fEnums;
292  fEnums = nullptr;
293  }
294  cl->fEnums = temp;
295  }
296 
297  cl->fSizeof = fSizeof;
298  cl->fCheckSum = fCheckSum;
299  cl->fCanSplit = fCanSplit;
300  cl->fProperty = fProperty;
301  cl->fClassProperty = fClassProperty;
302  cl->fStreamerType = fStreamerType;
303 
304  // Update pointers to TClass
305  if (cl->fBase.load()) {
306  for (auto base: *cl->fBase) {
307  ((TBaseClass*)base)->SetClass(cl);
308  }
309  }
310  if (cl->fData) {
311  for (auto dm: *cl->fData) {
312  ((TDataMember*)dm)->SetClass(cl);
313  }
314  ((TListOfDataMembers*)cl->fData)->SetClass(cl);
315  }
316  if (cl->fEnums.load()) {
317  for (auto en: *cl->fEnums) {
318  ((TEnum*)en)->SetClass(cl);
319  }
320  ((TListOfEnums*)cl->fEnums)->SetClass(cl);
321  }
322 
323 
324  TClass* currentRDClass = cl;
325  TRealData * prevRealData = 0;
326  int prevLevel = 0;
327  bool first = true;
328  if (fPRealData.size() > 0) {
329  for (auto element: fPRealData) {
330  //if (element->IsA() == TObjString::Class()) {
331  if (element.IsAClass() ) {
332  // We now check for the TClass entry, w/o loading. Indeed we did that above.
333  // If the class is not found, it means that really it was not selected and we
334  // replace it with an empty placeholder with the status of kForwardDeclared.
335  // Interactivity will be of course possible but if IO is attempted, a warning
336  // will be issued.
337  TInterpreter::SuspendAutoParsing autoParseRaii(gInterpreter);
338 
339  // Disable autoparsing which might be triggered by the use of ResolvedTypedef
340  // and the fallback new TClass() below.
341  currentRDClass = TClass::GetClass(GetClassName(element.fClassIndex), false /* Load */ );
342  //printf("element is a class - name %s - index %d %s \n ",currentRDClass->GetName(), element.fClassIndex, GetClassName(element.fClassIndex) );
343  if (!currentRDClass && !element.TestFlag(TProtoRealData::kIsTransient)) {
344  if (gDebug>1)
345  Info("FillTClass()",
346  "Cannot find TClass for %s; Creating an empty one in the kForwardDeclared state.",
347  GetClassName(element.fClassIndex));
348  currentRDClass = new TClass(GetClassName(element.fClassIndex),1,TClass::kForwardDeclared, true /*silent*/);
349  }
350  }
351  //else {
352  if (!currentRDClass) continue;
353  //TProtoRealData* prd = (TProtoRealData*)element;
354  // pass a previous real data only if depth
355 
356  if (TRealData* rd = element.CreateRealData(currentRDClass, cl,prevRealData, prevLevel)) {
357  if (first) {
358  //LM: need to do here because somehow fRealData is destroyed when calling TClass::GetListOfDataMembers()
359  if (cl->fRealData) {
360  Info("FillTClass","Real data for class %s is not empty - make a new one",cl->GetName() );
361  delete cl->fRealData;
362  }
363  cl->fRealData = new TList(); // FIXME: this should really become a THashList!
364  first = false;
365  }
366 
367  cl->fRealData->AddLast(rd);
368  prevRealData = rd;
369  prevLevel = element.fLevel;
370 
371  }
372  //}
373  }
374  }
375  else {
376  if (cl->fRealData) {
377  Info("FillTClas","Real data for class %s is not empty - make a new one. Class has no Proto-realdata",cl->GetName() );
378  delete cl->fRealData;
379  }
380  cl->fRealData = new TList(); // FIXME: this should really become a THashList!
381  }
382 
383  cl->SetStreamerImpl();
384 
385  // set to zero in order not to delete when protoclass is deleted
386  fBase = 0;
387  //fData = 0;
388  fEnums = 0;
389 
390  fPRealData.clear();
391  fPRealData.shrink_to_fit(); // to reset the underlying allocate space
392 
393  // if (fPRealData) fPRealData->Delete();
394  // delete fPRealData;
395  // fPRealData = 0;
396 
397  return kTRUE;
398 }
399 
400 ////////////////////////////////////////////////////////////////////////////////
401 
402 TProtoClass::TProtoRealData::TProtoRealData(const TRealData* rd):
403  //TNamed(rd->GetDataMember()->GetName(), rd->GetName()),
404  //TNamed(),
405  //fName(rd->GetDataMember()->GetName()),
406  //fTitle(rd->GetName()),
407  fOffset(rd->GetThisOffset()),
408  fDMIndex(-1),
409  fLevel(0),
410  fClassIndex(-1),
411  fStatusFlag(0)
412 {
413  TDataMember * dm = rd->GetDataMember();
414  TClass * cl = dm->GetClass();
415  assert(cl != NULL);
416  fDMIndex = DataMemberIndex(cl,dm->GetName());
417  //printf("Index of data member %s for class %s is %d \n",dm->GetName(), cl->GetName() , fDMIndex);
418  TString fullDataMemberName = rd->GetName(); // full data member name (e.g. fXaxis.fNbins)
419  fLevel = fullDataMemberName.CountChar('.');
420 
421  if (fullDataMemberName.Contains("*") ) SetFlag(kIsPointer);
422 
423  // Initialize this from a TRealData object.
424  SetFlag(kIsObject, rd->IsObject());
425  SetFlag(kIsTransient, rd->TestBit(TRealData::kTransient) );
426 }
427 
428 ////////////////////////////////////////////////////////////////////////////////
429 /// Destructor to pin vtable.
430 ///if (gErrorIgnoreLevel==-2) printf("destroy real data %s - ",GetName());
431 
432 TProtoClass::TProtoRealData::~TProtoRealData()
433 {
434 }
435 
436 ////////////////////////////////////////////////////////////////////////////////
437 /// Create a TRealData from this, with its data member coming from dmClass.
438 /// find data member from protoclass
439 
440 TRealData* TProtoClass::TProtoRealData::CreateRealData(TClass* dmClass,
441  TClass* parent, TRealData *prevData, int prevLevel) const
442 {
443 
444  //TDataMember* dm = (TDataMember*)dmClass->GetListOfDataMembers()->FindObject(fName);
445  TDataMember* dm = TProtoClass::FindDataMember(dmClass, fDMIndex);
446 
447  if (!dm && dmClass->GetState()!=TClass::kForwardDeclared) {
448  ::Error("CreateRealData",
449  "Cannot find data member # %d of class %s for parent %s!", fDMIndex, dmClass->GetName(),
450  parent->GetName());
451  return nullptr;
452  }
453 
454  // here I need to re-construct the realdata full name (e.g. fAxis.fNbins)
455 
456  TString realMemberName;
457  // keep an empty name if data member is not found
458  if (dm) realMemberName = dm->GetName();
459  if (TestFlag(kIsPointer) )
460  realMemberName = TString("*")+realMemberName;
461  else if (dm){
462  if (dm->GetArrayDim() > 0) {
463  // in case of array (like fMatrix[2][2] we need to add max index )
464  // this only in case of it os not a pointer
465  for (int idim = 0; idim < dm->GetArrayDim(); ++idim)
466  realMemberName += TString::Format("[%d]",dm->GetMaxIndex(idim) );
467  } else if (TClassEdit::IsStdArray(dm->GetTypeName())) {
468  std::string typeNameBuf;
469  Int_t ndim = dm->GetArrayDim();
470  std::array<Int_t, 5> maxIndices; // 5 is the maximum supported in TStreamerElement::SetMaxIndex
471  TClassEdit::GetStdArrayProperties(dm->GetTypeName(),
472  typeNameBuf,
473  maxIndices,
474  ndim);
475  for (Int_t idim = 0; idim < ndim; ++idim) {
476  realMemberName += TString::Format("[%d]",maxIndices[idim] );
477  }
478  }
479  }
480 
481  if (prevData && fLevel > 0 ) {
482  if (fLevel-prevLevel == 1) // I am going down 1 level
483  realMemberName = TString::Format("%s.%s",prevData->GetName(), realMemberName.Data() );
484  else if (fLevel <= prevLevel) { // I am at the same level
485  // need to strip out prev name
486  std::string prevName = prevData->GetName();
487  // we strip the prev data member name from the full name
488  std::string parentName;
489  for (int i = 0; i < prevLevel-fLevel+1; ++i) {
490  parentName = prevName.substr(0, prevName.find_last_of(".") );
491  prevName = parentName;
492  }
493 
494  // now we need to add the current name
495  realMemberName = TString::Format("%s.%s",parentName.c_str(), realMemberName.Data() );
496  }
497  }
498 
499  //printf("adding new realdata for class %s : %s - %s %d %d \n",dmClass->GetName(), realMemberName.Data(), dm->GetName(),fLevel, fDMIndex );
500 
501  TRealData* rd = new TRealData(realMemberName, fOffset, dm);
502  if (TestFlag(kIsTransient)) {
503  rd->SetBit(TRealData::kTransient);
504  }
505  rd->SetIsObject(TestFlag(kIsObject) );
506  return rd;
507 }
508 
509 ////////////////////////////////////////////////////////////////////////////////
510 
511 Int_t TProtoClass::DataMemberIndex(TClass * cl, const char * name)
512 {
513  TList * dmList = cl->GetListOfDataMembers();
514 
515  // we cannot use IndexOf because order is guaranteed only for non-static data member
516  Int_t index = 0;
517  for ( auto * obj : *dmList) {
518  TDataMember * dm = (TDataMember *) obj;
519  if (!dm ) continue;
520  if (dm->Property() & kIsStatic) continue;
521  if ( TString(dm->GetName()) == TString(name) )
522  return index;
523  index++;
524  }
525  ::Error("TProtoClass::DataMemberIndex","data member %s is not found in class %s",name, cl->GetName());
526  dmList->ls();
527  return -1;
528 }
529 ////////////////////////////////////////////////////////////////////////////////
530 
531 TDataMember * TProtoClass::FindDataMember(TClass * cl, Int_t index)
532 {
533  TList * dmList = cl->GetListOfDataMembers(false);
534 
535  // we cannot use IndexOf because order is guaranteed only for non-static data member
536  Int_t i = 0;
537  for ( auto * obj : *dmList) {
538  TDataMember * dm = (TDataMember *) obj;
539  if (!dm ) continue;
540  if (dm->Property() & kIsStatic) continue;
541  if (i == index)
542  return dm;
543  i++;
544  }
545  if (cl->GetState()!=TClass::kForwardDeclared)
546  ::Error("TProtoClass::FindDataMember","data member with index %d is not found in class %s",index,cl->GetName());
547  return nullptr;
548 }