Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
TClassTable.cxx
Go to the documentation of this file.
1// @(#)root/cont:$Id$
2// Author: Fons Rademakers 11/08/95
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/** \class TClassTable
13\ingroup Containers
14This class registers for all classes their name, id and dictionary
15function in a hash table. Classes are automatically added by the
16ctor of a special init class when a global of this init class is
17initialized when the program starts (see the ClassImp macro).
18
19All functions in TClassTable are thread-safe.
20*/
21
22#include "TClassTable.h"
23
24#include "TClass.h"
25#include "TClassEdit.h"
26#include "TProtoClass.h"
27#include "TList.h"
28#include "TROOT.h"
29#include "TString.h"
30#include "TError.h"
31#include "TRegexp.h"
32
33#include "TObjString.h"
34#include "TMap.h"
35
36#include "TInterpreter.h"
37
38#include <map>
39#include <memory>
40#include <typeinfo>
41#include <cstdlib>
42#include <string>
43#include <mutex>
44
45using namespace ROOT;
46
48
53std::atomic<UInt_t> TClassTable::fgTally;
57
59
60static std::mutex &GetClassTableMutex()
61{
62 static std::mutex sMutex;
63 return sMutex;
64}
65
66// RAII to first normalize the input classname (operation that
67// both requires the ROOT global lock and might call `TClassTable`
68// resursively) and then acquire a lock on `TClassTable` local
69// mutex.
71 std::string fNormalizedName;
72
73public:
79
81 {
83 return;
84
85 // The recorded name is normalized, let's make sure we convert the
86 // input accordingly. This operation will take the ROOT global lock
87 // and might call recursively `TClassTable`, so this must be done
88 // outside of the `TClassTable` critical section.
90
91 GetClassTableMutex().lock();
92 }
93
95 GetClassTableMutex().unlock();
96 }
97
98 const std::string &GetNormalizedName() const {
99 return fNormalizedName;
100 }
101};
102
103////////////////////////////////////////////////////////////////////////////////
104
105namespace ROOT {
106 class TClassRec {
107 public:
109 fName(nullptr), fId(0), fDict(nullptr), fInfo(nullptr), fProto(nullptr), fNext(next)
110 {}
111
113 // TClassTable::fgIdMap->Remove(r->fInfo->name());
114 delete [] fName;
115 delete fProto;
116 delete fNext;
117 }
118
119 char *fName;
123 const std::type_info *fInfo;
126 };
127
128 class TClassAlt {
129 public:
130 TClassAlt(const char*alternate, const char *normName, TClassAlt *next) :
131 fName(alternate), fNormName(normName), fNext(next)
132 {}
133
135 // Nothing more to delete.
136 }
137
138 const char *fName; // Do not own
139 const char *fNormName; // Do not own
140 std::unique_ptr<TClassAlt> fNext;
141 };
142
143#define R__USE_STD_MAP
145#if defined R__USE_STD_MAP
146 // This wrapper class allow to avoid putting #include <map> in the
147 // TROOT.h header file.
148 public:
149 typedef std::map<std::string, TClassRec*> IdMap_t;
153#ifdef R__WIN32
154 // Window's std::map does NOT defined mapped_type
155 typedef TClassRec* mapped_type;
156#else
158#endif
159
160 private:
162
163 public:
164 void Add(const key_type &key, mapped_type &obj) {
165 fMap[key] = obj;
166 }
167
168 mapped_type Find(const key_type &key) const {
169 IdMap_t::const_iterator iter = fMap.find(key);
170 mapped_type cl = nullptr;
171 if (iter != fMap.end()) cl = iter->second;
172 return cl;
173 }
174
175 void Remove(const key_type &key) { fMap.erase(key); }
176
177 void Print() {
178 Info("TMapTypeToClassRec::Print", "printing the typeinfo map in TClassTable");
179 for (const_iterator iter = fMap.begin(); iter != fMap.end(); ++iter) {
180 printf("Key: %40s 0x%zx\n", iter->first.c_str(), (size_t)iter->second);
181 }
182 }
183#else
184 private:
185 TMap fMap;
186 public:
187#ifdef R__COMPLETE_MEM_TERMINATION
189 TIter next(&fMap);
190 TObjString *key;
191 while((key = (TObjString*)next())) {
192 delete key;
193 }
194 }
195#endif
196
197 void Add(const char *key, TClassRec *&obj)
198 {
199 // Add <key,value> pair to the map.
200
201 TObjString *realkey = new TObjString(key);
202 fMap.Add(realkey, (TObject*)obj);
203 }
204
205 TClassRec *Find(const char *key) const {
206 // Find the value corresponding the key.
207 const TPair *a = (const TPair *)fMap.FindObject(key);
208 if (a) return (TClassRec*) a->Value();
209 return 0;
210 }
211
212 void Remove(const char *key) {
213 // Remove the value corresponding the key.
214 TObjString realkey(key);
215 TObject *actual = fMap.Remove(&realkey);
216 delete actual;
217 }
218
219 void Print() {
220 // Print the content of the map.
221 Info("TMapTypeToClassRec::Print", "printing the typeinfo map in TClassTable");
222 TIter next(&fMap);
223 TObjString *key;
224 while((key = (TObjString*)next())) {
225 printf("Key: %s\n",key->String().Data());
226 TClassRec *data = (TClassRec*)fMap.GetValue(key);
227 if (data) {
228 printf(" class: %s %d\n",data->fName,data->fId);
229 } else {
230 printf(" no class: \n");
231 }
232 }
233 }
234#endif
235 };
236
237 static UInt_t ClassTableHash(const char *name, UInt_t size)
238 {
239 auto p = reinterpret_cast<const unsigned char*>( name );
240 UInt_t slot = 0;
241
242 while (*p) slot = slot<<1 ^ *p++;
243 slot %= size;
244
245 return slot;
246 }
247
248 std::vector<std::unique_ptr<TClassRec>> &GetDelayedAddClass()
249 {
250 static std::vector<std::unique_ptr<TClassRec>> delayedAddClass;
251 return delayedAddClass;
252 }
253
254 std::vector<std::pair<const char *, const char *>> &GetDelayedAddClassAlternate()
255 {
256 static std::vector<std::pair<const char *, const char *>> delayedAddClassAlternate;
257 return delayedAddClassAlternate;
258 }
259}
260
261////////////////////////////////////////////////////////////////////////////////
262/// TClassTable is a singleton (i.e. only one can exist per application).
263
265{
266 if (gClassTable) return;
267
268 fgSize = 1009; //this is the result of (int)TMath::NextPrime(1000);
269 fgTable = new TClassRec* [fgSize];
271 fgIdMap = new IdMap_t;
272 memset(fgTable, 0, fgSize * sizeof(TClassRec*));
273 memset(fgAlternate, 0, fgSize * sizeof(TClassAlt*));
274 gClassTable = this;
275
276 for (auto &&r : GetDelayedAddClass()) {
277 AddClass(r->fName, r->fId, *r->fInfo, r->fDict, r->fBits);
278 };
279 GetDelayedAddClass().clear();
280
281 for (auto &&r : GetDelayedAddClassAlternate()) {
282 AddAlternate(r.first, r.second);
283 }
285}
286
287////////////////////////////////////////////////////////////////////////////////
288/// TClassTable singleton is deleted in Terminate().
289
291{
292 // Try to avoid spurious warning from memory leak checkers.
293 if (gClassTable != this) return;
294
295 for (UInt_t i = 0; i < fgSize; i++) {
296 delete fgTable[i]; // Will delete all the elements in the chain.
297 }
298 delete [] fgTable; fgTable = nullptr;
299 delete [] fgSortedTable; fgSortedTable = nullptr;
300 delete fgIdMap; fgIdMap = nullptr;
301}
302
303////////////////////////////////////////////////////////////////////////////////
304/// Return true fs the table exist.
305/// If the table does not exist but the delayed list does, then
306/// create the table and return true.
307
309{
310 // This will be set at the lastest during TROOT construction, so before
311 // any threading could happen.
312 if (!gClassTable || !fgTable) {
313 if (GetDelayedAddClass().size()) {
314 new TClassTable;
315 return kTRUE;
316 }
317 return kFALSE;
318 }
319 return kTRUE;
320}
321
322////////////////////////////////////////////////////////////////////////////////
323/// Print the class table. Before printing the table is sorted
324/// alphabetically. Only classes specified in option are listed.
325/// The default is to list all classes.
326/// Standard wildcarding notation supported.
327
328void TClassTable::Print(Option_t *option) const
329{
330 std::lock_guard<std::mutex> lock(GetClassTableMutex());
331
332 // This is the very rare case (i.e. called before any dictionary load)
333 // so we don't need to execute this outside of the critical section.
334 if (fgTally == 0 || !fgTable)
335 return;
336
337 SortTable();
338
339 int n = 0, ninit = 0, nl = 0;
340
341 if (!option) option = "";
342 int nch = strlen(option);
343 TRegexp re(option, kTRUE);
344
345 Printf("\nDefined classes");
346 Printf("class version bits initialized");
347 Printf("================================================================");
348 for (UInt_t i = 0; i < fgTally; i++) {
350 if (!r) break;
351 n++;
352 TString s = r->fName;
353 if (nch && strcmp(option,r->fName) && s.Index(re) == kNPOS) continue;
354 nl++;
355 if (TClass::GetClass(r->fName, kFALSE)) {
356 ninit++;
357 Printf("%-35s %6d %7d Yes", r->fName, r->fId, r->fBits);
358 } else
359 Printf("%-35s %6d %7d No", r->fName, r->fId, r->fBits);
360 }
361 Printf("----------------------------------------------------------------");
362 Printf("Listed Classes: %4d Total classes: %4d initialized: %4d",nl, n, ninit);
363 Printf("================================================================\n");
364}
365
366//---- static members --------------------------------------------------------
367
368////////////////////////////////////////////////////////////////////////////////
369/// Returns class at index from sorted class table. Don't use this iterator
370/// while modifying the class table. The class table can be modified
371/// when making calls like TClass::GetClass(), etc.
372/// Returns 0 if index points beyond last class name.
373
375{
376 if (index < fgTally) {
377 std::lock_guard<std::mutex> lock(GetClassTableMutex());
378
379 SortTable();
381 if (r)
382 return r->fName;
383 }
384 return nullptr;
385}
386
387//______________________________________________________________________________
389//______________________________________________________________________________
391
392namespace ROOT { class TForNamespace {}; } // Dummy class to give a typeid to namespace (see also TGenericClassInfo)
393
394////////////////////////////////////////////////////////////////////////////////
395/// Add a class to the class table (this is a static function).
396/// Note that the given cname *must* be already normalized.
397
398void TClassTable::Add(const char *cname, Version_t id, const std::type_info &info,
399 DictFuncPtr_t dict, Int_t pragmabits)
400{
401 if (!cname || *cname == 0)
402 ::Fatal("TClassTable::Add()", "Failed to deduce type for '%s'", info.name());
403
404 // This will be set at the lastest during TROOT construction, so before
405 // any threading could happen.
406 if (!gClassTable)
407 new TClassTable;
408
409 std::unique_lock<std::mutex> lock(GetClassTableMutex());
410
411 // check if already in table, if so return
413 if (r->fName && r->fInfo) {
414 if ( strcmp(r->fInfo->name(), typeid(ROOT::TForNamespace).name()) ==0
415 && strcmp(info.name(), typeid(ROOT::TForNamespace).name()) ==0 ) {
416 // We have a namespace being reloaded.
417 // This okay we just keep the old one.
418 return;
419 }
421 lock.unlock(); // Warning might recursively call TClassTable during gROOT init
422 // Warn only for class that are not STD classes
423 ::Warning("TClassTable::Add", "class %s already in TClassTable", cname);
424 }
425 return;
426 } else if (ROOT::Internal::gROOTLocal && gCling) {
427 TClass *oldcl = (TClass*)gROOT->GetListOfClasses()->FindObject(cname);
428 if (oldcl) { // && oldcl->GetClassInfo()) {
429 // As a work-around to ROOT-6012, we need to register the class even if
430 // it is not a template instance, because a forward declaration in the header
431 // files loaded by the current dictionary wil also de-activate the update
432 // class info mechanism!
433
434 // The TClass exist and already has a class info, so it must
435 // correspond to a class template instantiation which the interpreter
436 // was able to make with the library containing the TClass Init.
437 // Because it is already known to the interpreter, the update class info
438 // will not be triggered, we need to force it.
439 gCling->RegisterTClassUpdate(oldcl, dict);
440 }
441 }
442
443 if (!r->fName)
444 r->fName = StrDup(cname);
445 r->fId = id;
446 r->fBits = pragmabits;
447 r->fDict = dict;
448 r->fInfo = &info;
449
450 fgIdMap->Add(info.name(),r);
451
453}
454
455////////////////////////////////////////////////////////////////////////////////
456/// Add a class to the class table (this is a static function).
457
459{
460 // This will be set at the lastest during TROOT construction, so before
461 // any threading could happen.
462 if (!gClassTable)
463 new TClassTable;
464
465 std::unique_lock<std::mutex> lock(GetClassTableMutex());
466
467 // By definition the name in the TProtoClass is (must be) the normalized
468 // name, so there is no need to tweak it.
469 const char *cname = proto->GetName();
470
471 // check if already in table, if so return
473 if (r->fName) {
474 if (r->fProto) delete r->fProto;
475 r->fProto = proto;
476 TClass *oldcl = (TClass*)gROOT->GetListOfClasses()->FindObject(cname);
477
478 lock.unlock(); // FillTClass might recursively call TClassTable during gROOT init
479 if (oldcl && oldcl->GetState() == TClass::kHasTClassInit)
480 proto->FillTClass(oldcl);
481 return;
482 } else if (ROOT::Internal::gROOTLocal && gCling) {
483 TClass *oldcl = (TClass*)gROOT->GetListOfClasses()->FindObject(cname);
484 if (oldcl) { // && oldcl->GetClassInfo()) {
485 // As a work-around to ROOT-6012, we need to register the class even if
486 // it is not a template instance, because a forward declaration in the header
487 // files loaded by the current dictionary wil also de-activate the update
488 // class info mechanism!
489
490 lock.unlock(); // Warning might recursively call TClassTable during gROOT init
491 ::Warning("TClassTable::Add(TProtoClass*)","Called for existing class without a prior call add the dictionary function.");
492 }
493 }
494
495 r->fName = StrDup(cname);
496 r->fId = 0;
497 r->fBits = 0;
498 r->fDict = nullptr;
499 r->fInfo = nullptr;
500 r->fProto= proto;
501
503}
504
505////////////////////////////////////////////////////////////////////////////////
506
507ROOT::TClassAlt* TClassTable::AddAlternate(const char *normName, const char *alternate)
508{
509 // This will be set at the lastest during TROOT construction, so before
510 // any threading could happen.
511 if (!gClassTable)
512 new TClassTable;
513
514 std::lock_guard<std::mutex> lock(GetClassTableMutex());
515
516 UInt_t slot = ROOT::ClassTableHash(alternate, fgSize);
517
518 for (const TClassAlt *a = fgAlternate[slot]; a; a = a->fNext.get()) {
519 if (strcmp(alternate,a->fName)==0) {
520 if (strcmp(normName,a->fNormName) != 0) {
521 fprintf(stderr,"Error in TClassTable::AddAlternate: "
522 "Second registration of %s with a different normalized name (old: '%s', new: '%s')\n",
523 alternate, a->fNormName, normName);
524 }
525 return nullptr;
526 }
527 }
528
529 fgAlternate[slot] = new TClassAlt(alternate,normName,fgAlternate[slot]);
530 return fgAlternate[slot];
531}
532
533////////////////////////////////////////////////////////////////////////////////
534///
536{
537 if (!alt || !gClassTable)
538 return;
539
540 std::lock_guard<std::mutex> lock(GetClassTableMutex());
541
543
544 if (!fgAlternate[slot])
545 return;
546
547 if (fgAlternate[slot] == alt)
548 fgAlternate[slot] = alt->fNext.release();
549 else {
550 for (TClassAlt *a = fgAlternate[slot]; a; a = a->fNext.get()) {
551 if (a->fNext.get() == alt) {
552 a->fNext.swap( alt->fNext );
553 assert( alt == alt->fNext.get());
554 alt->fNext.release();
555 }
556 }
557 }
558 delete alt;
559}
560
561////////////////////////////////////////////////////////////////////////////////
562
563Bool_t TClassTable::Check(const char *cname, std::string &normname)
564{
565 if (!CheckClassTableInit())
566 return kFALSE;
567
568 std::lock_guard<std::mutex> lock(GetClassTableMutex());
569
571
572 // Check if 'cname' is a known normalized name.
573 for (TClassRec *r = fgTable[slot]; r; r = r->fNext)
574 if (strcmp(cname,r->fName)==0) return kTRUE;
575
576 // See if 'cname' is register in the list of alternate names
577 for (const TClassAlt *a = fgAlternate[slot]; a; a = a->fNext.get()) {
578 if (strcmp(cname,a->fName)==0) {
579 normname = a->fNormName;
580 return kTRUE;
581 }
582 }
583
584 return kFALSE;
585}
586
587////////////////////////////////////////////////////////////////////////////////
588/// Remove a class from the class table. This happens when a shared library
589/// is unloaded (i.e. the dtor's of the global init objects are called).
590
591void TClassTable::Remove(const char *cname)
592{
593 if (!CheckClassTableInit())
594 return;
595
596 std::lock_guard<std::mutex> lock(GetClassTableMutex());
597
599
600 TClassRec *r;
601 TClassRec *prev = nullptr;
602 for (r = fgTable[slot]; r; r = r->fNext) {
603 if (!strcmp(r->fName, cname)) {
604 if (prev)
605 prev->fNext = r->fNext;
606 else
607 fgTable[slot] = r->fNext;
608 fgIdMap->Remove(r->fInfo->name());
609 r->fNext = nullptr; // Do not delete the others.
610 delete r;
611 fgTally--;
613 break;
614 }
615 prev = r;
616 }
617}
618
619////////////////////////////////////////////////////////////////////////////////
620/// Find a class by name in the class table (using hash of name). Returns
621/// 0 if the class is not in the table. Unless arguments insert is true in
622/// which case a new entry is created and returned.
623/// `cname` must be the normalized name of the class.
624
626{
627 // Internal routine, no explicit lock needed here.
628
630
631 for (TClassRec *r = fgTable[slot]; r; r = r->fNext)
632 if (strcmp(cname, r->fName) == 0)
633 return r;
634
635 if (!insert)
636 return nullptr;
637
638 fgTable[slot] = new TClassRec(fgTable[slot]);
639
640 fgTally++;
641 return fgTable[slot];
642}
643
644////////////////////////////////////////////////////////////////////////////////
645/// Returns the ID of a class.
646
648{
650
651 TClassRec *r = FindElement(guard.GetNormalizedName().c_str(), kFALSE);
652 if (r)
653 return r->fId;
654 return -1;
655}
656
657////////////////////////////////////////////////////////////////////////////////
658/// Returns the pragma bits as specified in the LinkDef.h file.
659
661{
663
664 TClassRec *r = FindElement(guard.GetNormalizedName().c_str(), kFALSE);
665 if (r)
666 return r->fBits;
667 return 0;
668}
669
670////////////////////////////////////////////////////////////////////////////////
671/// Given the class name returns the Dictionary() function of a class
672/// (uses hash of name).
673
675{
676 if (gDebug > 9) {
677 ::Info("GetDict", "searches for %s", cname);
678 fgIdMap->Print();
679 }
681
682 TClassRec *r = FindElement(guard.GetNormalizedName().c_str(), kFALSE);
683 if (r)
684 return r->fDict;
685 return nullptr;
686}
687
688////////////////////////////////////////////////////////////////////////////////
689/// Given the std::type_info returns the Dictionary() function of a class
690/// (uses hash of std::type_info::name()).
691
692DictFuncPtr_t TClassTable::GetDict(const std::type_info& info)
693{
694 if (!CheckClassTableInit())
695 return nullptr;
696
697 if (gDebug > 9)
698 ROOT::GetROOT(); // Info might recursively call TClassTable during the gROOT init
699
700 std::lock_guard<std::mutex> lock(GetClassTableMutex());
701
702 if (gDebug > 9) {
703 ::Info("GetDict", "searches for %s at 0x%zx", info.name(), (size_t)&info);
704 fgIdMap->Print();
705 }
706
707 TClassRec *r = fgIdMap->Find(info.name());
708 if (r)
709 return r->fDict;
710 return nullptr;
711}
712
713////////////////////////////////////////////////////////////////////////////////
714/// Given the normalized class name returns the Dictionary() function of a class
715/// (uses hash of name).
716
718{
719 if (!CheckClassTableInit())
720 return nullptr;
721
722 if (gDebug > 9)
723 ROOT::GetROOT(); // Info might recursively call TClassTable during the gROOT init
724
725 std::lock_guard<std::mutex> lock(GetClassTableMutex());
726
727 if (gDebug > 9) {
728 ::Info("GetDict", "searches for %s", cname);
729 fgIdMap->Print();
730 }
731
733 if (r)
734 return r->fDict;
735 return nullptr;
736}
737
738////////////////////////////////////////////////////////////////////////////////
739/// Given the class name returns the TClassProto object for the class.
740/// (uses hash of name).
741
743{
744 if (gDebug > 9) {
745 ::Info("GetDict", "searches for %s", cname);
746 }
747
748 if (!CheckClassTableInit())
749 return nullptr;
750
752
753 if (gDebug > 9) {
754 // Because of the early call to Info, gROOT is already initialized
755 // and thus this will not cause a recursive call to TClassTable.
756 ::Info("GetDict", "searches for %s", cname);
757 fgIdMap->Print();
758 }
759
760 TClassRec *r = FindElement(guard.GetNormalizedName().c_str(), kFALSE);
761 if (r)
762 return r->fProto;
763 return nullptr;
764}
765
766////////////////////////////////////////////////////////////////////////////////
767/// Given the class normalized name returns the TClassProto object for the class.
768/// (uses hash of name).
769
771{
772 if (gDebug > 9) {
773 ::Info("GetDict", "searches for %s", cname);
774 }
775
776 if (!CheckClassTableInit())
777 return nullptr;
778
779 std::lock_guard<std::mutex> lock(GetClassTableMutex());
780
781 if (gDebug > 9) {
782 fgIdMap->Print();
783 }
784
786 if (r)
787 return r->fProto;
788 return nullptr;
789}
790
791////////////////////////////////////////////////////////////////////////////////
792
793extern "C" {
794 static int ClassComp(const void *a, const void *b)
795 {
796 // Function used for sorting classes alphabetically.
797
798 return strcmp((*(TClassRec **)a)->fName, (*(TClassRec **)b)->fName);
799 }
800}
801
802////////////////////////////////////////////////////////////////////////////////
803/// Returns next class from sorted class table. Don't use this iterator
804/// while modifying the class table. The class table can be modified
805/// when making calls like TClass::GetClass(), etc.
806
808{
809 std::lock_guard<std::mutex> lock(GetClassTableMutex());
810
811 if (fgCursor < fgTally) {
813 return r->fName;
814 }
815
816 return nullptr;
817}
818
819////////////////////////////////////////////////////////////////////////////////
820/// Print the class table. Before printing the table is sorted
821/// alphabetically.
822
824{
825 if (fgTally == 0 || !fgTable)
826 return;
827
828 std::lock_guard<std::mutex> lock(GetClassTableMutex());
829
830 SortTable();
831
832 int n = 0, ninit = 0;
833
834 Printf("\nDefined classes");
835 Printf("class version bits initialized");
836 Printf("================================================================");
837 UInt_t last = fgTally;
838 for (UInt_t i = 0; i < last; i++) {
840 if (!r) break;
841 n++;
842 // Do not use TClass::GetClass to avoid any risk of autoloading.
843 if (gROOT->GetListOfClasses()->FindObject(r->fName)) {
844 ninit++;
845 Printf("%-35s %6d %7d Yes", r->fName, r->fId, r->fBits);
846 } else
847 Printf("%-35s %6d %7d No", r->fName, r->fId, r->fBits);
848 }
849 Printf("----------------------------------------------------------------");
850 Printf("Total classes: %4d initialized: %4d", n, ninit);
851 Printf("================================================================\n");
852}
853
854////////////////////////////////////////////////////////////////////////////////
855/// Sort the class table by ascending class ID's.
856
858{
859 // Internal routine.
860
861 if (!fgSorted) {
862 delete [] fgSortedTable;
864
865 int j = 0;
866 for (UInt_t i = 0; i < fgSize; i++)
867 for (TClassRec *r = fgTable[i]; r; r = r->fNext)
868 fgSortedTable[j++] = r;
869
870 ::qsort(fgSortedTable, fgTally, sizeof(TClassRec *), ::ClassComp);
871 fgSorted = kTRUE;
872 }
873}
874
875////////////////////////////////////////////////////////////////////////////////
876/// Deletes the class table (this static class function calls the dtor).
877
879{
880 if (gClassTable) {
881 for (UInt_t i = 0; i < fgSize; i++)
882 delete fgTable[i]; // Will delete all the elements in the chain.
883
884 delete [] fgTable; fgTable = nullptr;
885 delete [] fgSortedTable; fgSortedTable = nullptr;
886 delete fgIdMap; fgIdMap = nullptr;
887 fgSize = 0;
889 }
890}
891
892////////////////////////////////////////////////////////////////////////////////
893/// Global function called by the ctor of a class's init class
894/// (see the ClassImp macro).
895
896void ROOT::AddClass(const char *cname, Version_t id,
897 const std::type_info& info,
898 DictFuncPtr_t dict,
899 Int_t pragmabits)
900{
901 if (!TROOT::Initialized() && !gClassTable) {
902 auto r = std::unique_ptr<TClassRec>(new TClassRec(nullptr));
903 r->fName = StrDup(cname);
904 r->fId = id;
905 r->fBits = pragmabits;
906 r->fDict = dict;
907 r->fInfo = &info;
908 GetDelayedAddClass().emplace_back(std::move(r));
909 } else {
910 TClassTable::Add(cname, id, info, dict, pragmabits);
911 }
912}
913
914////////////////////////////////////////////////////////////////////////////////
915/// Global function called by GenerateInitInstance.
916/// (see the ClassImp macro).
917
918ROOT::TClassAlt* ROOT::AddClassAlternate(const char *normName, const char *alternate)
919{
920 if (!TROOT::Initialized() && !gClassTable) {
921 GetDelayedAddClassAlternate().emplace_back(normName, alternate);
922 // If a library is loaded before gROOT is initialized we can assume
923 // it is hard linked along side libCore (or is libCore) thus can't
924 // really be unloaded.
925 return nullptr;
926 } else {
927 return TClassTable::AddAlternate(normName, alternate);
928 }
929}
930
932{
933 // This routine is meant to be called (indirectly) by dlclose so we
934 // we are guaranteed that the library initialization has completed.
936}
937
938////////////////////////////////////////////////////////////////////////////////
939/// Global function to update the version number.
940/// This is called via the RootClassVersion macro.
941///
942/// if cl!=0 and cname==-1, set the new class version if and only is
943/// greater than the existing one and greater or equal to 2;
944/// and also ignore the request if fVersionUsed is true.
945///
946/// Note on class version number:
947/// - If no class has been specified, TClass::GetVersion will return -1
948/// - The Class Version 0 request the whole object to be transient
949/// - The Class Version 1, unless specify via ClassDef indicates that the
950/// I/O should use the TClass checksum to distinguish the layout of the class
951void ROOT::ResetClassVersion(TClass *cl, const char *cname, Short_t newid)
952{
953 if (cname && cname != (void*)-1 && TClassTable::CheckClassTableInit()) {
956 if (r)
957 r->fId = newid;
958 }
959 if (cl) {
960 if (cl->fVersionUsed) {
961 // Problem, the reset is called after the first usage!
962 if (cname!=(void*)-1)
963 Error("ResetClassVersion","Version number of %s can not be changed after first usage!",
964 cl->GetName());
965 } else {
966 if (newid < 0) {
967 Error("SetClassVersion","The class version (for %s) must be positive (value %d is ignored)",cl->GetName(),newid);
968 }
969 if (cname==(void*)-1) {
970 if (cl->fClassVersion<newid && 2<=newid) {
971 cl->SetClassVersion(newid);
972 }
973 } else {
974 cl->SetClassVersion(newid);
975 }
976 }
977 }
978}
979
980
981////////////////////////////////////////////////////////////////////////////////
982/// Global function called by the dtor of a class's init class
983/// (see the ClassImp macro).
984
985void ROOT::RemoveClass(const char *cname, TClass *oldcl)
986{
987 // don't delete class information since it is needed by the I/O system
988 // to write the StreamerInfo to file
989 if (cname) {
990 // Let's still remove this information to allow reloading later.
991 // Anyway since the shared library has been unloaded, the dictionary
992 // pointer is now invalid ....
993 // We still keep the TClass object around because TFile needs to
994 // get to the TStreamerInfo.
995 if (oldcl)
996 oldcl->SetUnloaded();
998 }
999}
1000
1001////////////////////////////////////////////////////////////////////////////////
1002/// Global function to register the implementation file and line of
1003/// a class template (i.e. NOT a concrete class).
1004
1005TNamed *ROOT::RegisterClassTemplate(const char *name, const char *file,
1006 Int_t line)
1007{
1008 static TList table;
1009 static Bool_t isInit = []() {
1010 table.SetOwner(kTRUE);
1011 table.UseRWLock();
1012 return true;
1013 }();
1014 (void)isInit;
1015
1016 TString classname(name);
1017 Ssiz_t loc = classname.Index("<");
1018 if (loc >= 1)
1019 classname.Remove(loc);
1020 TNamed *reg = (TNamed*)table.FindObject(classname);
1021 if (file) {
1022 if (reg)
1023 reg->SetTitle(file);
1024 else {
1025 reg = new TNamed((const char*)classname, file);
1026 table.Add(reg);
1027 }
1028 reg->SetUniqueID(line);
1029 }
1030 return reg;
1031}
#define SafeDelete(p)
Definition RConfig.hxx:525
#define b(i)
Definition RSha256.hxx:100
#define a(i)
Definition RSha256.hxx:99
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
bool Bool_t
Definition RtypesCore.h:63
short Version_t
Definition RtypesCore.h:65
unsigned int UInt_t
Definition RtypesCore.h:46
short Short_t
Definition RtypesCore.h:39
constexpr Bool_t kFALSE
Definition RtypesCore.h:94
constexpr Ssiz_t kNPOS
Definition RtypesCore.h:117
constexpr Bool_t kTRUE
Definition RtypesCore.h:93
const char Option_t
Definition RtypesCore.h:66
TClass *(* DictFuncPtr_t)()
Definition Rtypes.h:85
#define ClassImp(name)
Definition Rtypes.h:382
static std::mutex & GetClassTableMutex()
TClassTable * gClassTable
static int ClassComp(const void *a, const void *b)
R__EXTERN TClassTable * gClassTable
void Info(const char *location, const char *msgfmt,...)
Use this function for informational messages.
Definition TError.cxx:218
void Error(const char *location, const char *msgfmt,...)
Use this function in case an error occurred.
Definition TError.cxx:185
winID h TVirtualViewer3D TVirtualGLPainter p
Option_t Option_t option
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void data
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t r
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t index
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize id
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char cname
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void reg
char name[80]
Definition TGX11.cxx:110
R__EXTERN TInterpreter * gCling
Int_t gDebug
Definition TROOT.cxx:597
#define gROOT
Definition TROOT.h:406
void Printf(const char *fmt,...)
Formats a string in a circular formatting buffer and prints the string.
Definition TString.cxx:2503
char * StrDup(const char *str)
Duplicate the string str.
Definition TString.cxx:2557
const char * proto
Definition civetweb.c:17535
const char * fName
TClassAlt(const char *alternate, const char *normName, TClassAlt *next)
std::unique_ptr< TClassAlt > fNext
const char * fNormName
DictFuncPtr_t fDict
TClassRec(TClassRec *next)
const std::type_info * fInfo
TClassRec * fNext
TProtoClass * fProto
IdMap_t::size_type size_type
IdMap_t::mapped_type mapped_type
void Add(const key_type &key, mapped_type &obj)
IdMap_t::key_type key_type
std::map< std::string, TClassRec * > IdMap_t
IdMap_t::const_iterator const_iterator
void Remove(const key_type &key)
mapped_type Find(const key_type &key) const
IdMap_t::size_type size_type
Definition TClass.cxx:373
IdMap_t::mapped_type mapped_type
Definition TClass.cxx:378
IdMap_t::const_iterator const_iterator
Definition TClass.cxx:372
IdMap_t::key_type key_type
Definition TClass.cxx:371
const std::string & GetNormalizedName() const
NormalizeThenLock(const NormalizeThenLock &)=delete
NormalizeThenLock(const char *cname)
NormalizeThenLock(NormalizeThenLock &&)=delete
NormalizeThenLock & operator=(const NormalizeThenLock &)=delete
NormalizeThenLock & operator=(NormalizeThenLock &&)=delete
This class registers for all classes their name, id and dictionary function in a hash table.
Definition TClassTable.h:37
static void PrintTable()
Print the class table.
static Int_t GetPragmaBits(const char *name)
Returns the pragma bits as specified in the LinkDef.h file.
static DictFuncPtr_t GetDict(const char *cname)
Given the class name returns the Dictionary() function of a class (uses hash of name).
static Version_t GetID(const char *cname)
Returns the ID of a class.
ROOT::TMapTypeToClassRec IdMap_t
Definition TClassTable.h:43
static void SortTable()
Sort the class table by ascending class ID's.
static TProtoClass * GetProtoNorm(const char *cname)
Given the class normalized name returns the TClassProto object for the class.
static DictFuncPtr_t GetDictNorm(const char *cname)
Given the normalized class name returns the Dictionary() function of a class (uses hash of name).
static ROOT::TClassAlt ** fgAlternate
Definition TClassTable.h:46
void Print(Option_t *option="") const override
Print the class table.
static void Terminate()
Deletes the class table (this static class function calls the dtor).
static TProtoClass * GetProto(const char *cname)
Given the class name returns the TClassProto object for the class.
static std::atomic< UInt_t > fgTally
Definition TClassTable.h:51
TClassTable()
TClassTable is a singleton (i.e. only one can exist per application).
static char * Next()
Returns next class from sorted class table.
static char * At(UInt_t index)
Returns class at index from sorted class table.
static Bool_t Check(const char *cname, std::string &normname)
static void Init()
static void Remove(const char *cname)
Remove a class from the class table.
static ROOT::TClassRec ** fgSortedTable
Definition TClassTable.h:48
static ROOT::TClassRec ** fgTable
Definition TClassTable.h:47
static IdMap_t * fgIdMap
Definition TClassTable.h:49
static Bool_t CheckClassTableInit()
Return true fs the table exist.
static void Add(const char *cname, Version_t id, const std::type_info &info, DictFuncPtr_t dict, Int_t pragmabits)
Add a class to the class table (this is a static function).
~TClassTable()
TClassTable singleton is deleted in Terminate().
static void RemoveAlternate(ROOT::TClassAlt *alt)
static ROOT::TClassRec * FindElement(const char *cname, Bool_t insert)
Find a class by name in the class table (using hash of name).
static Bool_t fgSorted
Definition TClassTable.h:52
static UInt_t fgSize
Definition TClassTable.h:50
static ROOT::TClassAlt * AddAlternate(const char *normname, const char *alternate)
static UInt_t fgCursor
Definition TClassTable.h:53
TClass instances represent classes, structs and namespaces in the ROOT type system.
Definition TClass.h:81
EState GetState() const
Definition TClass.h:488
Version_t fClassVersion
Definition TClass.h:221
void SetUnloaded()
Call this method to indicate that the shared library containing this class's code has been removed (u...
Definition TClass.cxx:6376
void SetClassVersion(Version_t version)
Private function.
Definition TClass.cxx:5742
std::atomic< Bool_t > fVersionUsed
saved remember if fOffsetStreamer has been set.
Definition TClass.h:262
@ kHasTClassInit
Definition TClass.h:127
static TClass * GetClass(const char *name, Bool_t load=kTRUE, Bool_t silent=kFALSE)
Static method returning pointer to TClass of the specified class name.
Definition TClass.cxx:3035
virtual bool UseRWLock(Bool_t enable=true)
Set this collection to use a RW lock upon access, making it thread safe.
virtual void SetOwner(Bool_t enable=kTRUE)
Set whether this collection is the owner (enable==true) of its content.
virtual void RegisterTClassUpdate(TClass *oldcl, DictFuncPtr_t dict)=0
A doubly linked list.
Definition TList.h:38
TObject * FindObject(const char *name) const override
Find an object in this list using its name.
Definition TList.cxx:576
void Add(TObject *obj) override
Definition TList.h:81
TMap implements an associative array of (key,value) pairs using a THashTable for efficient retrieval ...
Definition TMap.h:40
The TNamed class is the base class for all named ROOT classes.
Definition TNamed.h:29
const char * GetName() const override
Returns name of object.
Definition TNamed.h:47
Collectable string class.
Definition TObjString.h:28
TString & String()
Definition TObjString.h:48
Mother of all ROOT objects.
Definition TObject.h:41
virtual void Warning(const char *method, const char *msgfmt,...) const
Issue warning message.
Definition TObject.cxx:991
virtual TObject * FindObject(const char *name) const
Must be redefined in derived classes.
Definition TObject.cxx:420
virtual void Fatal(const char *method, const char *msgfmt,...) const
Issue fatal error message.
Definition TObject.cxx:1033
virtual void Info(const char *method, const char *msgfmt,...) const
Issue info message.
Definition TObject.cxx:979
Class used by TMap to store (key,value) pairs.
Definition TMap.h:102
Persistent version of a TClass.
Definition TProtoClass.h:38
static Bool_t Initialized()
Return kTRUE if the TROOT object has been initialized.
Definition TROOT.cxx:2910
Regular expression class.
Definition TRegexp.h:31
Basic string class.
Definition TString.h:139
const char * Data() const
Definition TString.h:376
TString & Remove(Ssiz_t pos)
Definition TString.h:685
Ssiz_t Index(const char *pat, Ssiz_t i=0, ECaseCompare cmp=kExact) const
Definition TString.h:651
TLine * line
const Int_t n
Definition legend1.C:16
R__EXTERN TROOT * gROOTLocal
Definition TROOT.h:379
tbb::task_arena is an alias of tbb::interface7::task_arena, which doesn't allow to forward declare tb...
std::vector< std::pair< const char *, const char * > > & GetDelayedAddClassAlternate()
ROOT::TClassAlt * AddClassAlternate(const char *normName, const char *alternate)
Global function called by GenerateInitInstance.
std::vector< std::unique_ptr< TClassRec > > & GetDelayedAddClass()
void RemoveClass(const char *cname, TClass *cl)
Global function called by the dtor of a class's init class (see the ClassImp macro).
void AddClass(const char *cname, Version_t id, const std::type_info &info, DictFuncPtr_t dict, Int_t pragmabits)
Global function called by the ctor of a class's init class (see the ClassImp macro).
TNamed * RegisterClassTemplate(const char *name, const char *file, Int_t line)
Global function to register the implementation file and line of a class template (i....
static UInt_t ClassTableHash(const char *name, UInt_t size)
void ResetClassVersion(TClass *, const char *, Short_t)
Global function to update the version number.
TROOT * GetROOT()
Definition TROOT.cxx:472
void RemoveClassAlternate(ROOT::TClassAlt *)
bool IsStdClass(const char *type)
return true if the class belongs to the std namespace
void GetNormalizedName(std::string &norm_name, std::string_view name)
Return the normalized name.