ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
TListOfFunctions.cxx
Go to the documentation of this file.
1 // @(#)root/cont
2 // Author: Philippe Canal Aug 2013
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2013, 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 TListOfFunctions
13 A collection of TFunction objects designed for fast access given a
14 DeclId_t and for keep track of TFunction that were described
15 unloaded function.
16 */
17 
18 #include "TListOfFunctions.h"
19 #include "TClass.h"
20 #include "TExMap.h"
21 #include "TFunction.h"
22 #include "TMethod.h"
23 #include "TInterpreter.h"
24 #include "TVirtualMutex.h"
25 
27 
28 ////////////////////////////////////////////////////////////////////////////////
29 /// Constructor.
30 
31 TListOfFunctions::TListOfFunctions(TClass *cl) : fClass(cl),fIds(0),fUnloaded(0),fLastLoadMarker(0)
32 {
33  fIds = new TExMap;
34  fUnloaded = new THashList;
35 }
36 
37 ////////////////////////////////////////////////////////////////////////////////
38 /// Destructor.
39 
41 {
43  delete fIds;
44  fUnloaded->Delete();
45  delete fUnloaded;
46 }
47 
48 ////////////////////////////////////////////////////////////////////////////////
49 /// Add pair<id, object> to the map of functions and their ids.
50 
52 {
53  TFunction *f = dynamic_cast<TFunction*>(obj);
54  if (f) {
55  fIds->Add((Long64_t)f->GetDeclId(),(Long64_t)f);
56  }
57 }
58 
59 ////////////////////////////////////////////////////////////////////////////////
60 /// Add object at the beginning of the list.
61 
63 {
65  MapObject(obj);
66 }
67 
68 ////////////////////////////////////////////////////////////////////////////////
69 /// Add object at the beginning of the list and also store option.
70 /// Storing an option is useful when one wants to change the behaviour
71 /// of an object a little without having to create a complete new
72 /// copy of the object. This feature is used, for example, by the Draw()
73 /// method. It allows the same object to be drawn in different ways.
74 
76 {
77  THashList::AddFirst(obj,opt);
78  MapObject(obj);
79 }
80 
81 ////////////////////////////////////////////////////////////////////////////////
82 /// Add object at the end of the list.
83 
85 {
86  THashList::AddLast(obj);
87  MapObject(obj);
88 }
89 
90 ////////////////////////////////////////////////////////////////////////////////
91 /// Add object at the end of the list and also store option.
92 /// Storing an option is useful when one wants to change the behaviour
93 /// of an object a little without having to create a complete new
94 /// copy of the object. This feature is used, for example, by the Draw()
95 /// method. It allows the same object to be drawn in different ways.
96 
98 {
99  THashList::AddLast(obj, opt);
100  MapObject(obj);
101 }
102 
103 ////////////////////////////////////////////////////////////////////////////////
104 /// Insert object at location idx in the list.
105 
107 {
108  THashList::AddAt(obj, idx);
109  MapObject(obj);
110 }
111 
112 ////////////////////////////////////////////////////////////////////////////////
113 /// Insert object after object after in the list.
114 
116 {
117  THashList::AddAfter(after, obj);
118  MapObject(obj);
119 }
120 
121 ////////////////////////////////////////////////////////////////////////////////
122 /// Insert object after object after in the list.
123 
125 {
126  THashList::AddAfter(after, obj);
127  MapObject(obj);
128 }
129 
130 ////////////////////////////////////////////////////////////////////////////////
131 /// Insert object before object before in the list.
132 
134 {
135  THashList::AddBefore(before, obj);
136  MapObject(obj);
137 }
138 
139 ////////////////////////////////////////////////////////////////////////////////
140 /// Insert object before object before in the list.
141 
143 {
144  THashList::AddBefore(before, obj);
145  MapObject(obj);
146 }
147 
148 ////////////////////////////////////////////////////////////////////////////////
149 /// Remove all objects from the list. Does not delete the objects unless
150 /// the THashList is the owner (set via SetOwner()).
151 
153 {
154  fUnloaded->Clear(option);
155  fIds->Clear();
156  THashList::Clear(option);
157 }
158 
159 ////////////////////////////////////////////////////////////////////////////////
160 /// Delete all TFunction object files.
161 
162 void TListOfFunctions::Delete(Option_t *option /* ="" */)
163 {
164  fUnloaded->Delete(option);
165  fIds->Clear();
166  THashList::Delete(option);
167 }
168 
169 ////////////////////////////////////////////////////////////////////////////////
170 /// Specialize FindObject to do search for the
171 /// a function just by name or create it if its not already in the list
172 
174 {
177  if (!result) {
178 
180  if (fClass) decl = gInterpreter->GetFunction(fClass->GetClassInfo(),name);
181  else decl = gInterpreter->GetFunction(0,name);
182  if (decl) result = const_cast<TListOfFunctions*>(this)->Get(decl);
183  }
184  return result;
185 }
186 
187 ////////////////////////////////////////////////////////////////////////////////
188 /// Return the set of overloads for this name, collecting all available ones.
189 /// Can construct and insert new TFunction-s.
190 
192 {
194 
195  TList* overloads = (TList*)fOverloads.FindObject(name);
196  TExMap overloadsSet;
197  Bool_t wasEmpty = true;
198  if (!overloads) {
199  overloads = new TList();
200  overloads->SetName(name);
201  fOverloads.Add(overloads);
202  } else {
203  TIter iOverload(overloads);
204  while (TFunction* over = (TFunction*)iOverload()) {
205  wasEmpty = false;
206  overloadsSet.Add((Long64_t)(ULong64_t)over->GetDeclId(),
207  (Long64_t)(ULong64_t)over);
208  }
209  }
210 
211  // Update if needed.
212  std::vector<DeclId_t> overloadDecls;
213  ClassInfo_t* ci = fClass ? fClass->GetClassInfo() : 0;
214  gInterpreter->GetFunctionOverloads(ci, name, overloadDecls);
215  for (std::vector<DeclId_t>::const_iterator iD = overloadDecls.begin(),
216  eD = overloadDecls.end(); iD != eD; ++iD) {
217  TFunction* over = Get(*iD);
218  if (wasEmpty || !overloadsSet.GetValue((Long64_t)(ULong64_t)over->GetDeclId())) {
219  overloads->Add(over);
220  }
221  }
222 
223  return overloads;
224 }
225 
226 ////////////////////////////////////////////////////////////////////////////////
227 /// Return the set of overloads for this name, collecting all available ones.
228 /// Can construct and insert new TFunction-s.
229 
231 {
232  return const_cast<TListOfFunctions*>(this)->GetListForObjectNonConst(name);
233 }
234 
235 ////////////////////////////////////////////////////////////////////////////////
236 /// Return the set of overloads for function obj, collecting all available ones.
237 /// Can construct and insert new TFunction-s.
238 
240 {
241  if (!obj) return 0;
242  return const_cast<TListOfFunctions*>(this)
244 }
245 
246 ////////////////////////////////////////////////////////////////////////////////
247 /// Return the TMethod or TFunction describing the function corresponding
248 /// to the Decl 'id'. Return NULL if not found.
249 
251 {
252  if (!id) return 0;
253 
255  return (TFunction*)fIds->GetValue((Long64_t)id);
256 }
257 
258 ////////////////////////////////////////////////////////////////////////////////
259 /// Return (after creating it if necessary) the TMethod or TFunction
260 /// describing the function corresponding to the Decl 'id'.
261 
263 {
264  if (!id) return 0;
265 
266  TFunction *f = Find(id);
267  if (f) return f;
268 
270  if (fClass) {
271  if (!gInterpreter->ClassInfo_Contains(fClass->GetClassInfo(),id)) return 0;
272  } else {
273  if (!gInterpreter->ClassInfo_Contains(0,id)) return 0;
274  }
275 
277 
278  MethodInfo_t *m = gInterpreter->MethodInfo_Factory(id);
279 
280  // Let's see if this is a reload ...
281  const char *name = gInterpreter->MethodInfo_Name(m);
282  if (const TList* bucketForMethod = fUnloaded->GetListForObject(name)) {
283  TString mangledName( gInterpreter->MethodInfo_GetMangledName(m) );
284  TIter next(bucketForMethod);
285  TFunction *uf;
286  while ((uf = (TFunction *) next())) {
287  if (uf->GetMangledName() == mangledName) {
288  // Reuse
289  fUnloaded->Remove(uf);
290 
291  uf->Update(m);
292  f = uf;
293  break;
294  }
295  }
296  }
297  if (!f) {
298  if (fClass) f = new TMethod(m, fClass);
299  else f = new TFunction(m);
300  }
301  // Calling 'just' THahList::Add would turn around and call
302  // TListOfFunctions::AddLast which should *also* do the fIds->Add.
304  fIds->Add((Long64_t)id,(Long64_t)f);
305 
306  return f;
307 }
308 
309 ////////////////////////////////////////////////////////////////////////////////
310 /// Remove a pair<id, object> from the map of functions and their ids.
311 
313 {
314  TFunction *f = dynamic_cast<TFunction*>(obj);
315  if (f) {
316  fIds->Remove((Long64_t)f->GetDeclId());
317  }
318 }
319 
320 ////////////////////////////////////////////////////////////////////////////////
321 /// Remove object from this collection and recursively remove the object
322 /// from all other objects (and collections).
323 /// This function overrides TCollection::RecursiveRemove that calls
324 /// the Remove function. THashList::Remove cannot be called because
325 /// it uses the hash value of the hash table. This hash value
326 /// is not available anymore when RecursiveRemove is called from
327 /// the TObject destructor.
328 
330 {
331  if (!obj) return;
332 
335  UnmapObject(obj);
336 
337 }
338 
339 ////////////////////////////////////////////////////////////////////////////////
340 /// Remove object from the list.
341 
343 {
344  Bool_t found;
345 
346  found = THashList::Remove(obj);
347  if (!found) {
348  found = fUnloaded->Remove(obj);
349  }
350  UnmapObject(obj);
351  if (found) return obj;
352  else return 0;
353 }
354 
355 ////////////////////////////////////////////////////////////////////////////////
356 /// Remove object via its objlink from the list.
357 
359 {
360  if (!lnk) return 0;
361 
362  TObject *obj = lnk->GetObject();
363 
364  THashList::Remove(lnk);
365  fUnloaded->Remove(obj);
366 
367  UnmapObject(obj);
368  return obj;
369 }
370 
371 ////////////////////////////////////////////////////////////////////////////////
372 /// Load all the functions known to the interpreter for the scope 'fClass'
373 /// into this collection.
374 
376 {
377  if (fClass && fClass->GetClassInfo() == 0) return;
378 
380 
381  ULong64_t currentTransaction = gInterpreter->GetInterpreterStateMarker();
382  if (currentTransaction == fLastLoadMarker) {
383  return;
384  }
385  fLastLoadMarker = currentTransaction;
386 
387  ClassInfo_t *info;
388  if (fClass) info = fClass->GetClassInfo();
389  else info = gInterpreter->ClassInfo_Factory();
390 
391  MethodInfo_t *t = gInterpreter->MethodInfo_Factory(info);
392  while (gInterpreter->MethodInfo_Next(t)) {
393  if (gInterpreter->MethodInfo_IsValid(t)) {
394  TDictionary::DeclId_t mid = gInterpreter->GetDeclId(t);
395  // Get will check if there is already there or create a new one
396  // (or re-use a previously unloaded version).
397  Get(mid);
398  }
399  }
400  gInterpreter->MethodInfo_Delete(t);
401  if (!fClass) gInterpreter->ClassInfo_Delete(info);
402 }
403 
404 ////////////////////////////////////////////////////////////////////////////////
405 /// Mark 'all func' as being unloaded.
406 /// After the unload, the function can no longer be found directly,
407 /// until the decl can be found again in the interpreter (in which
408 /// the func object will be reused.
409 
411 {
412  TObjLink *lnk = FirstLink();
413  while (lnk) {
414  TFunction *func = (TFunction*)lnk->GetObject();
415 
416  fIds->Remove((Long64_t)func->GetDeclId());
417  fUnloaded->Add(func);
418 
419  lnk = lnk->Next();
420  }
421 
423 }
424 
425 ////////////////////////////////////////////////////////////////////////////////
426 /// Mark 'func' as being unloaded.
427 /// After the unload, the function can no longer be found directly,
428 /// until the decl can be found again in the interpreter (in which
429 /// the func object will be reused.
430 
432 {
433  if (THashList::Remove(func)) {
434  // We contains the object, let remove it from the other internal
435  // list and move it to the list of unloaded objects.
436 
437  fIds->Remove((Long64_t)func->GetDeclId());
438  fUnloaded->Add(func);
439  }
440 }
441 
442 ////////////////////////////////////////////////////////////////////////////////
443 
445 {
447  return THashList::FindObject(obj);
448 }
449 
450 ////////////////////////////////////////////////////////////////////////////////
451 
453 {
455  return new TListOfFunctionsIter(this,dir);
456 }
457 
458 ////////////////////////////////////////////////////////////////////////////////
459 
461 {
463  return THashList::At(idx);
464 }
465 
466 ////////////////////////////////////////////////////////////////////////////////
467 
469 {
471  return THashList::After(obj);
472 }
473 
474 ////////////////////////////////////////////////////////////////////////////////
475 
477 {
479  return THashList::Before(obj);
480 }
481 
482 ////////////////////////////////////////////////////////////////////////////////
483 
485 {
487  return THashList::First();
488 }
489 
490 ////////////////////////////////////////////////////////////////////////////////
491 
493 {
495  return THashList::FirstLink();
496 }
497 
498 ////////////////////////////////////////////////////////////////////////////////
499 
501 {
503  return THashList::GetObjectRef(obj);
504 }
505 
506 ////////////////////////////////////////////////////////////////////////////////
507 
509 {
511  return THashList::Last();
512 }
513 
514 ////////////////////////////////////////////////////////////////////////////////
515 
517 {
519  return THashList::LastLink();
520 }
521 
522 
523 ////////////////////////////////////////////////////////////////////////////////
524 
526 {
528  return THashList::GetLast();
529 }
530 
531 ////////////////////////////////////////////////////////////////////////////////
532 
534 {
536  return THashList::IndexOf(obj);
537 }
538 
539 
540 ////////////////////////////////////////////////////////////////////////////////
541 
543 {
545  return THashList::GetSize();
546 }
547 
548 /** \class TListOfFunctionsIter
549 Iterator for TListOfFunctions.
550 */
551 
553 
554 ////////////////////////////////////////////////////////////////////////////////
555 
557  TListIter(l,dir) {}
558 
559 ////////////////////////////////////////////////////////////////////////////////
560 
562 {
564  return TListIter::Next();
565 }
566 
virtual TObject * FindObject(const TObject *obj) const
Find object using its hash value (returned by its Hash() member).
virtual void Clear(Option_t *="")
Definition: TObject.h:110
void Add(ULong64_t hash, Long64_t key, Long64_t value)
Add an (key,value) pair to the table. The key should be unique.
Definition: TExMap.cxx:87
void AddFirst(TObject *obj)
Add object at the beginning of the list.
Definition: THashList.cxx:68
void Remove(ULong64_t hash, Long64_t key)
Remove entry with specified key from the TExMap.
Definition: TExMap.cxx:216
virtual TIterator * MakeIterator(Bool_t dir=kIterForward) const
Return a list iterator.
void AddLast(TObject *obj)
Add object at the end of the list.
Definition: THashList.cxx:90
ULong64_t fLastLoadMarker
long long Long64_t
Definition: RtypesCore.h:69
ClassImp(TSeqCollection) Int_t TSeqCollection TIter next(this)
Return index of object in collection.
TObject * Next()
Return next object in the list. Returns 0 when no more objects in list.
TDictionary::DeclId_t DeclId_t
Definition: TInterpreter.h:236
const char Option_t
Definition: RtypesCore.h:62
TObject * FindObject(const char *name) const
Find object using its name.
Definition: THashList.cxx:213
virtual TObject * Before(const TObject *obj) const
Returns the object before object obj.
R__EXTERN TVirtualMutex * gInterpreterMutex
Definition: TInterpreter.h:46
virtual Int_t GetSize() const
virtual TObject ** GetObjectRef(const TObject *obj) const
Return address of pointer to obj.
void Delete(Option_t *option="")
Remove all objects from the list AND delete all heap based objects.
Definition: THashList.cxx:184
Basic string class.
Definition: TString.h:137
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
virtual Int_t GetLast() const
Returns index of last object in collection.
#define gInterpreter
Definition: TInterpreter.h:502
TFunction * Get(DeclId_t id)
Return (after creating it if necessary) the TMethod or TFunction describing the function correspondin...
TObject * Next()
Return next object in the list. Returns 0 when no more objects in list.
Definition: TList.cxx:933
void RecursiveRemove(TObject *obj)
Remove object from this collection and recursively remove the object from all other objects (and coll...
Definition: THashList.cxx:253
virtual TObjLink * LastLink() const
const void * DeclId_t
Definition: TDictionary.h:209
virtual TObject * At(Int_t idx) const
Returns the object at position idx. Returns 0 if idx is out of range.
Definition: TList.cxx:311
TObject * Remove(TObject *obj)
Remove object from the list.
Iterator abstract base class.
Definition: TIterator.h:32
Iterator of linked list.
Definition: TList.h:187
TFile * f
virtual void Clear(Option_t *option)
Remove all objects from the list.
void AddFirst(TObject *obj)
Add object at the beginning of the list.
const TList * GetListForObject(const char *name) const
Return the THashTable's list (bucket) in which obj can be found based on its hash; see THashTable::Ge...
Definition: THashList.cxx:230
virtual TObjLink * FirstLink() const
virtual TObject * After(const TObject *obj) const
Returns the object after object obj.
Definition: TList.cxx:289
THashList implements a hybrid collection class consisting of a hash table and a list to store TObject...
Definition: THashList.h:36
virtual TObjLink * LastLink() const
Definition: TList.h:104
void AddAt(TObject *obj, Int_t idx)
Insert object at location idx in the list.
TClass * fClass
pointer to the foreign object
virtual void Delete(Option_t *option="")
Delete all TFunction object files.
virtual Int_t IndexOf(const TObject *obj) const
XFontStruct * id
Definition: TGX11.cxx:108
virtual TObject * Last() const
Return the last object in the list. Returns 0 when list is empty.
A doubly linked list.
Definition: TList.h:47
Iterator for TListOfFunctions.
TList * GetListForObjectNonConst(const char *name)
Return the set of overloads for this name, collecting all available ones.
virtual TObject * At(Int_t idx) const
Returns the object at position idx. Returns 0 if idx is out of range.
TThread * t[5]
Definition: threadsh1.C:13
void Clear(Option_t *option="")
Remove all objects from the list.
Definition: THashList.cxx:168
Long64_t GetValue(ULong64_t hash, Long64_t key)
Return the value belonging to specified key and hash value.
Definition: TExMap.cxx:173
DeclId_t GetDeclId() const
Definition: TFunction.cxx:198
void AddAt(TObject *obj, Int_t idx)
Insert object at location idx in the list.
Definition: THashList.cxx:148
~TListOfFunctions()
Destructor.
virtual TObject * Before(const TObject *obj) const
Returns the object before object obj.
Definition: TList.cxx:322
ClassInfo_t * GetClassInfo() const
Definition: TClass.h:390
void AddBefore(const TObject *before, TObject *obj)
Insert object before object before in the list.
TObject * Remove(TObject *obj)
Remove object from the list.
Definition: THashList.cxx:285
void AddLast(TObject *obj)
Add object at the end of the list.
TMarker * m
Definition: textangle.C:8
void AddAfter(const TObject *after, TObject *obj)
Insert object after object after in the list.
Definition: THashList.cxx:130
TLine * l
Definition: textangle.C:4
The ROOT global object gROOT contains a list of all defined classes.
Definition: TClass.h:81
virtual TObjLink * FirstLink() const
Definition: TList.h:101
void SetName(const char *name)
Definition: TCollection.h:116
virtual bool Update(MethodInfo_t *info)
Update the TFunction to reflect the new info.
Definition: TFunction.cxx:278
virtual Int_t GetLast() const
Returns index of last object in collection.
virtual TObject * Last() const
Return the last object in the list. Returns 0 when list is empty.
Definition: TList.cxx:581
virtual Int_t GetSize() const
Definition: TCollection.h:95
void MapObject(TObject *obj)
Add pair<id, object> to the map of functions and their ids.
virtual const char * GetName() const
Returns name of object.
Definition: TObject.cxx:415
TFunction * Find(DeclId_t id) const
Return the TMethod or TFunction describing the function corresponding to the Decl 'id'...
void Unload()
Mark 'all func' as being unloaded.
unsigned long long ULong64_t
Definition: RtypesCore.h:70
void dir(char *path=0)
Definition: rootalias.C:30
double func(double *x, double *p)
Definition: stressTF1.cxx:213
void Add(TObject *obj)
Add object to the hash table.
Definition: THashTable.cxx:76
#define R__LOCKGUARD(mutex)
void AddBefore(const TObject *before, TObject *obj)
Insert object before object before in the list.
Definition: THashList.cxx:112
virtual TObject * After(const TObject *obj) const
Returns the object after object obj.
#define name(a, b)
Definition: linkTestLib0.cpp:5
virtual TList * GetListForObject(const char *name) const
Return the set of overloads for this name, collecting all available ones.
Mother of all ROOT objects.
Definition: TObject.h:58
TList()
Definition: TList.h:72
Global functions class (global functions are obtained from CINT).
Definition: TFunction.h:30
virtual TObject * First() const
Return the first object in the list. Returns 0 when list is empty.
Definition: TList.cxx:557
THashTable fOverloads
void Load()
Load all the functions known to the interpreter for the scope 'fClass' into this collection.
TDictionary::DeclId_t DeclId_t
virtual void Add(TObject *obj)
Definition: TList.h:81
void AddAfter(const TObject *after, TObject *obj)
Insert object after object after in the list.
Each ROOT class (see TClass) has a linked list of methods.
Definition: TMethod.h:40
TObject * FindObject(const char *name) const
Find object using its name.
Definition: THashTable.cxx:210
THashList * fUnloaded
virtual TObject ** GetObjectRef(const TObject *obj) const
Return address of pointer to obj.
Definition: TList.cxx:566
double result[121]
void RecursiveRemove(TObject *obj)
Remove object from this collection and recursively remove the object from all other objects (and coll...
A collection of TFunction objects designed for fast access given a DeclId_t and for keep track of TFu...
virtual TObject * First() const
Return the first object in the list. Returns 0 when list is empty.
ClassImp(TListOfFunctions) TListOfFunctions
Constructor.
virtual Int_t IndexOf(const TObject *obj) const
TObject * obj
This class stores a (key,value) pair using an external hash.
Definition: TExMap.h:35
void UnmapObject(TObject *obj)
Remove a pair<id, object> from the map of functions and their ids.
virtual const char * GetMangledName() const
Returns the mangled name as defined by CINT, or 0 in case of error.
Definition: TFunction.cxx:236