KaliVeda  1.12/06
Heavy-Ion Analysis Toolkit
KVSeqCollection.cpp
Go to the documentation of this file.
1 //Created by KVClassFactory on Fri Jun 19 18:51:28 2009
2 //Author: John Frankland,,,
3 
4 #include "KVSeqCollection.h"
5 #include "TClass.h"
6 #include "TROOT.h"
7 #include "KVBase.h"
8 #include "TMethodCall.h"
9 #include "Riostream.h"
10 #include "TKey.h"
11 
12 #include <THashList.h>
13 #include <TObjString.h>
14 
15 using namespace std;
16 
18 
19 
20 
24 
25 
30 
32 {
33  // Default initialisation called by ctors
34  // Make sure all lists have individual names to ensure rapid look-up
35  // in fgCleanups which is a THashList
36 
37  SetName(Form("KVSeqCollection_%lld", fSCCounter));
38  fSCCounter++;//always increases, so names are different
39  ++fgCounter;//decreased by dtor, counts instances
40  if (!fgCleanups) {
41  fgCleanups = new THashList;
42  fgCleanups->SetName("KVSeqCollection_Cleanups");
43  gROOT->GetListOfCleanups()->Add(fgCleanups);
44  }
45 }
46 
47 
48 
51 
53 {
54  // Default constructor
55  fCollection = 0;
56  ResetBit(kSignals);
57  init();
58 }
59 
60 
61 
65 
67  : TSeqCollection()
68 {
69  // Copy constructor
70  // See KVSeqCollection::Copy
71 
73  col.Copy(*this);
74  init();
76 }
77 
78 
79 
83 
84 KVSeqCollection::KVSeqCollection(const Char_t* collection_classname)
85 {
86  // Create new extended collection of class "collection_classname".
87  // Must be the name of a class derived from TSeqCollection.
88 
89  fCollection = 0;
90  SetCollection(collection_classname);
91  if (!fCollection) MakeZombie();
93  init();
95 }
96 
97 
98 
102 
103 void KVSeqCollection::SetCollection(const Char_t* class_name)
104 {
105  // Create TSeqCollection-derived object of class 'class_name'
106  // and set as the embedded collection fCollection.
107  TClass* cl = TClass::GetClass(class_name);
108  if (!cl) {
109  Error("SetCollection(const Char_t*)", "Called for unknown class: %s",
110  class_name);
111  return;
112  }
113  if (!cl->InheritsFrom("TSeqCollection")) {
114  Error("SetCollection(const Char_t*)",
115  "Called for class %s which does not inherit from TSeqCollection",
116  class_name);
117  return;
118  }
119  fCollection = (TSeqCollection*)cl->New();
120  // if name of KVSeqCollection has already been set (i.e. if this is not
121  // being called by one of the constructors), we set the name of the
122  // embedded TSeqCollection object
123  if (strcmp(GetName(), "KVSeqCollection"))
125 }
126 
127 
128 
133 
135 {
136  // Destructor
137  // If the cleanup mechanism is in use, we first remove the list from
138  // the list of cleanups
139 
140  if (IsCleanup()) {
141  while (fgCleanups->Remove(this))
142  ;
143  }
144  if (fCollection && !IsOwner()) {
145  // ROOT6: when Clear() is called for a TList/THashList containing invalid pointers
146  // (i.e. addresses of previously deleted objects), even if the list is not the owner
147  // of the objects (i.e. the Clear() should not attempt to delete anything), then
148  // calling Clear() without the option "nodelete" leads to false-positive warnings
149  // like this: Error in <***::Clear>: A list is accessing an object (0x5632b51c08b0) already deleted...
150  // As the TList and THashList destructors contain a call to their Clear() method,
151  // here we pre-emptively Clear("nodelete") the collection before deleting it
152  fCollection->Clear("nodelete");
153  }
155  --fgCounter;//decrease instance count
156  if (fgCounter == 0 && fgCleanups) {
157  // delete cleanups list if this is the last KVSeqCollection
158  while (gROOT->GetListOfCleanups()->Remove(fgCleanups))
159  ;
160  fgCleanups->Clear();
161  delete fgCleanups;
162  fgCleanups = NULL;
163  }
164 }
165 
166 
167 
175 
177 {
178  // Copy a list of objects, including the name of the list.
179  // If this list owns its objects, we make new Clones of all objects in the list
180  // (N.B. the Clone() method must work correctly for the objects in question)
181  // and put them in the copy list, the copy will own these new objects.
182  // Copy will have same IsOwner and IsCleanup status as this list.
183  // If this list sends Modified() signal, the copy will do too.
184 
185  TSeqCollection::Copy(obj); //in fact this calls TObject::Copy, no Copy method defined for collection classes
186  KVSeqCollection& copy = (KVSeqCollection&) obj;
187  copy.SetName(GetName());
188 
189  //clear any pre-existing objects in copy list
190  if (copy.IsOwner()) copy.Delete();
191  else copy.Clear();
192 
193  //set ownership
194  copy.SetOwner(IsOwner());
195  //set cleanup status
196  copy.SetCleanup(IsCleanup());
197  //set signal&slot status
199 
200  //copy or clone list members
201  TObject* b;
202  TIter next(fCollection);
203  while ((b = next())) {
204  if (IsOwner())
205  copy.Add(b->Clone());
206  else
207  copy.Add(b);
208  }
209 }
210 
211 
212 
214 
216 {
217  if (&c != this) { // check for self-assignment
218  c.Copy(*this);
219  }
220  return (*this);
221 }
222 
223 
224 
233 
235 {
236  // PROTECTED method
237  // Creates and returns pointer to a new (empty) KVSeqCollection
238  // (or derived class) with the same characteristics
239  // as this one :
240  // - class of embedded collection
241  // - collection is owner of objects ?
242  // - objects are in cleanup list ?
243 
244  KVSeqCollection* newCol = (KVSeqCollection*)IsA()->New();
245  if (!newCol->fCollection) newCol->SetCollection(fCollection->ClassName());
246  newCol->SetOwner(IsOwner());
247  newCol->SetCleanup(IsCleanup());
248  return newCol;
249 }
250 
251 
252 
258 
260 {
261  // Clear the list of objects.
262  // If the cleanup mechanism is in use, and the objects belong to the list
263  // (i.e. Clear() will in fact delete all the objects) we first remove this list
264  // from the list of cleanups in order to avoid recursive deletes
265 
266  Bool_t cleaner = kFALSE;
267  if (IsCleanup()) {
268  if (IsOwner()) {
269  cleaner = kTRUE;
271  }
272  }
273  if (!IsOwner()) {
274  // ROOT6: when Clear() is called for a TList/THashList containing invalid pointers
275  // (i.e. addresses of previously deleted objects), even if the list is not the owner
276  // of the objects (i.e. the Clear() should not attempt to delete anything), then
277  // calling Clear() without the option "nodelete" leads to false-positive warnings
278  // like this: Error in <***::Clear>: A list is accessing an object (0x5632b51c08b0) already deleted...
279  fCollection->Clear("nodelete");
280  }
281  else
282  fCollection->Clear(option);
283  if (cleaner) SetCleanup();
284  Changed();
285 }
286 
287 
288 
293 
295 {
296  // Delete all heap-based objects in the list.
297  // If the cleanup mechanism is in use we first remove this list
298  // from the list of cleanups in order to avoid recursive deletes
299 
300  Bool_t cleaner = kFALSE;
301  if (IsCleanup()) {
302  if (IsOwner()) {
303  cleaner = kTRUE;
305  }
306  }
307  fCollection->Delete(option);
308  if (cleaner) SetCleanup();
309  Changed();
310 }
311 
312 
313 
316 
318 {
319  // Return reference to object.
320  return fCollection->GetObjectRef(obj);
321 }
322 
323 
324 
327 
329 {
330  // Make and return iterator for the list.
331  return fCollection->MakeIterator(dir);
332 }
333 
334 
335 
338 
340 {
341  // Remove object from list.
342 
343  TObject* result = fCollection->Remove(obj);
344  if (result) Changed();
345  return result;
346 }
347 
348 
349 
358 
360 {
361  // Remove object from this collection and recursively remove the object
362  // from all other objects (and collections).
363  //
364  // NOTE: lists which are 'cleanup' and 'sendmodifiedsignals':
365  // the list will not emit 'Modified()' when objects in the
366  // list are deleted elsewhere, they are then removed by the cleanup mechanism
367  // by calling this method.
368 
370 }
371 
372 
373 
376 
378 {
379  // Overrides TCollection::PrintCollectionHeader to show the class name of the embedded list
381  printf("Collection name='%s', embedded list class='%s', size=%d\n", GetName(),
383 }
384 
385 
386 
391 
393 {
394  // Will return object with given type (value of KVBase::GetType() method).
395  // Objects in list which do not inherit KVBase do not have GetType() method,
396  // and are ignored.
397 
398  TIter next(fCollection);
399  TObject* obj;
400  while ((obj = next())) {
402  if (((KVBase*)obj)->IsType(type))
403  return obj;
404  }
405  }
406  return nullptr;
407 }
408 
409 
410 
413 
415 {
416  // Will return object with given title (value of TObject::GetTitle() method).
417 
418  TIter next(fCollection);
419  TObject* obj;
420  while ((obj = next())) {
421  if (!strcmp(obj->GetTitle(), title))
422  return obj;
423  }
424  return nullptr;
425 }
426 
427 
428 
431 
433 {
434  // Return (first) object in embedded list with given class.
435 
436  TIter next(fCollection);
437  TObject* obj;
438  while ((obj = next())) {
439  if (obj->IsA() == cl) return obj;
440  }
441  return nullptr;
442 }
443 
444 
445 
448 
450 {
451  // Return (first) object in embedded list with given class.
452 
454 }
455 
456 
457 
462 
464 {
465  // Will return object with given label (value of KVBase::GetLabel() method).
466  // Objects in list which do not inherit KVBase do not have GetLabel() method,
467  // and are ignored.
468 
469  TIter next(fCollection);
470  TObject* obj;
471  while ((obj = next())) {
473  if (!strcmp(((KVBase*)obj)->GetLabel(), label)) return obj;
474  }
475  }
476  return nullptr;
477 }
478 
479 
480 
481 
486 
488 {
489  // Will return object with given number (value of KVBase::GetNumber() method).
490  // Objects in list which do not inherit KVBase do not have GetNumber() method,
491  // and are ignored.
492 
493  TIter next(fCollection);
494  TObject* obj;
495  while ((obj = next())) {
497  if (((KVBase*)obj)->GetNumber() == num) return obj;
498  }
499  }
500  return nullptr;
501 }
502 
503 
504 
509 
511 {
512  // Return object with specified name AND type (value of KVBase::GetType() method).
513  // Objects in list which do not inherit KVBase do not have GetType() method,
514  // and are ignored.
515 
516  TIter next(fCollection);
517  TObject* obj;
518  while ((obj = next())) {
519  if (obj->TestBit(KVBase::kIsKaliVedaObject) && !strcmp(obj->GetName(), name)) {
520  if (!strcmp(((KVBase*)obj)->GetType(), type)) return obj;
521  }
522  }
523  return nullptr;
524 }
525 
526 
527 
536 
537 TObject* KVSeqCollection::FindObjectWithMethod(const Char_t* retvalue, const Char_t* method) const
538 {
539  // Find the first object in the list for which the given method returns the given return value:
540  // e.g. if method = "GetName" and retvalue = "john", we return the
541  // first object in this list for which GetName() returns "john".
542  //
543  // For each object of the list, the existence of the given method is checked using TMethodCall::IsValid()
544  // if the method is valid and the return value is equal to the input one (retvalue) object is returned
545  // Supported return types are those defined in TMethodCall::ReturnType()
546 
547  if (retvalue && method) {
548  KVString RV(retvalue);
549  KVString MTH(method);
550  Bool_t wildcard = RV.Contains("*");
551  TIter next(fCollection);
552  TObject* obj;
553  while ((obj = next())) {
554  TMethodCall mt;
555  mt.InitWithPrototype(obj->IsA(), MTH.Data(), "");
556  if (mt.IsValid()) {
557  if (mt.ReturnType() == TMethodCall::kString) {
558  Char_t* ret;
559  mt.Execute(obj, "", &ret);
560  if (ret != nullptr) {
561  if (!wildcard) {
562  if (RV == ret) {
563  return obj;
564  }
565  }
566  else {
567  if (KVString(ret).Match(RV)) {
568  return obj;
569  }
570  }
571  }
572  }
573  else if (mt.ReturnType() == TMethodCall::kLong) {
574  Long_t ret;
575  mt.Execute(obj, "", ret);
576  if (ret == RV.Atoi()) {
577  return obj;
578  }
579  }
580  else if (mt.ReturnType() == TMethodCall::kDouble) {
581  Double_t ret;
582  mt.Execute(obj, "", ret);
583  if (ret == RV.Atof()) {
584  return obj;
585  }
586  }
587  else Error("FindObjectWithMethod", "Return type %d is not supported", (int)mt.ReturnType());
588  }
589  }
590  }
591  return nullptr;
592 
593 }
594 
595 
596 
601 
602 void KVSeqCollection::Execute(const char* method, const char* params, Int_t* error)
603 {
604  //Redefinition of TObject::Execute(const char *, const char *, Int_t *) method.
605  //TObject::Execute is called for each object in the embedded list in order, meaning that for each
606  //object the method "method" is executed with arguments "params".
607 
608  TIter next(fCollection);
609  TObject* obj;
610  while ((obj = next())) {
611  obj->Execute(method, params, error);
612  }
613 }
614 
615 
616 
617 
622 
623 void KVSeqCollection::Execute(TMethod* method, TObjArray* params, Int_t* error)
624 {
625  //Redefinition of TObject::Execute(TMethod *, TObjArray *, Int_t *) method.
626  //TObject::Execute is called for each object in the embedded list in order, meaning that for each
627  //object the method "method" is executed with arguments "params".
628 
629  TIter next(fCollection);
630  TObject* obj;
631  while ((obj = next())) {
632  obj->Execute(method, params, error);
633  }
634 }
635 
636 
637 
638 
653 
654 TObject* KVSeqCollection::FindObjectAny(const Char_t* att, const Char_t* keys, Bool_t contains_all, Bool_t case_sensitive) const
655 {
656  //Find an object in the list, if one of its characteristics 'att' contains any or all of
657  //the keywords contained in the string 'keys'
658  //
659  // att = "name", "title", "class", "type", "label",
660  // WARNING: when using "type" or "label", any objects in list which do
661  // not inherit from KVBase are ignored
662  // keys = list of keywords, separated by spaces
663  //
664  // contains_all = kFALSE (default) : object found if any keyword occurs in the characteristic 'att'
665  // contains_all = kTRUE : object found if all keywords occur in the characteristic 'att'
666  //
667  // case_sensitive = kTRUE (default) : case-sensitive comparison
668  // case_sensitive = kFALSE : ignore case of keywords
669 
670  int char_test = -1;
671  enum { kName, kTitle, kClass, kType, kLabel };
672  if (!strcmp(att, "name")) char_test = kName;
673  else if (!strcmp(att, "title")) char_test = kTitle;
674  else if (!strcmp(att, "type")) char_test = kType;
675  else if (!strcmp(att, "label")) char_test = kLabel;
676  else if (!strcmp(att, "class")) char_test = kClass;
677  else return nullptr;
678 
679  TString::ECaseCompare casecmp;
680  if (case_sensitive) casecmp = TString::kExact;
681  else casecmp = TString::kIgnoreCase;
682 
683  TString _keys(keys);
684  unique_ptr<TObjArray> keywords(_keys.Tokenize(' '));
685  if (!keywords.get()) return nullptr;
686  int nkeys;
687  if (!(nkeys = keywords->GetEntries())) {
688  return nullptr;
689  }
690 
691  int nmatches;
692  TIter next(fCollection);
693  TString _att;
694  TObject* obj(nullptr);
695  while ((obj = next())) {
696 
697  if (char_test > kClass && !obj->TestBit(KVBase::kIsKaliVedaObject)) {
698  continue;
699  }
700  KVBase* kvobj = dynamic_cast<KVBase*>(obj);
701  switch (char_test) {
702  case kName:
703  _att = obj->GetName();
704  break;
705  case kTitle:
706  _att = obj->GetTitle();
707  break;
708  case kClass:
709  _att = obj->ClassName();
710  break;
711  case kLabel:
712  _att = kvobj->GetLabel();
713  break;
714  case kType:
715  _att = kvobj->GetType();
716  break;
717  }
718  nmatches = 0;
719  for (int i = 0; i < nkeys; i++) {
720  nmatches += (_att.Contains(((TObjString*)keywords->At(i))->String(), casecmp));
721  }
722  if ((nmatches && !contains_all) || ((nmatches == nkeys) && contains_all)) {
723  return obj;
724  }
725  }
726  return nullptr;
727 }
728 
729 
730 
739 
741 {
742  // Create and fill a (sub)list with objects in this list of the given class.
743  // This new list will be of the same kind as this one.
744  // The objects in the sublist do not belong to the sublist.
745  //
746  // *** WARNING *** : DELETE the KVSeqCollection returned by this method after using it !!!
747  // *** RECOMMENDED *** : store the returned value in a std::unique_ptr
748  // unique_ptr<KVSeqCollection> ptr(GetSubListWithClass(...));
749 
751  sublist->SetOwner(kFALSE);
752  _GetSubListWithClass(sublist, fCollection, _class);
753  return sublist;
754 }
755 
756 
757 
759 
760 void KVSeqCollection::_GetSubListWithClass(KVSeqCollection* outputList, TCollection* Col, const TClass* _class) const
761 {
762  if (_class) {
763  TIter next(Col);
764  TObject* ob;
765  while ((ob = next())) {
766  if (_class == ob->IsA()) outputList->Add(ob);
767  // if we are looking for objects of class "TList" or some other TCollection, they will all be included!
768  if (ob->InheritsFrom("TCollection")) _GetSubListWithClass(outputList, (TCollection*)ob, _class);
769  }
770  }
771 }
772 
773 
774 
783 
785 {
786  // Recursively create and fill a (sub)list with objects in this list (and any sublists) of the given class.
787  // This new list will be of the same kind as this one.
788  // The objects in the sublist do not belong to the sublist.
789  //
790  // *** WARNING *** : DELETE the KVList returned by this method after using it !!!
791  // *** RECOMMENDED *** : store the returned value in a std::unique_ptr
792  // unique_ptr<KVSeqCollection> ptr(GetSubListWithClass(...));
793 
794  if (class_name) {
795  return GetSubListWithClass(TClass::GetClass(class_name));
796  }
797  else return nullptr;
798 }
799 
800 
801 
817 
819 {
820  // Recursively create and fill a (sub)list with objects in this list (and any sublists) for which the
821  // given method returns the given return value:
822  // e.g. if method = "GetName" and retvalue = "john", we return the
823  // (sub)list of objects in this list for which GetName() returns "john".
824  //
825  // This new list will be of the same kind as this one.
826  // The objects in the sublist do not belong to the sublist.
827  // *** WARNING *** : DELETE the list returned by this method after using it !!!
828  // *** RECOMMENDED *** : store the returned value in a std::unique_ptr
829  // unique_ptr<KVSeqCollection> ptr(GetSubListWithMethod(...));
830  //
831  // For each object of the list, the existence of the given method is checked using TMethodCall::IsValid()
832  // if the method is valid and the return value is equal to the input one (retvalue) object is added to the subKVList
833  // return type supported are those defined in TMethodCall::ReturnType()
834 
836  sublist->SetOwner(kFALSE);
837  _GetSubListWithMethod(sublist, fCollection, retvalue, method);
838  return sublist;
839 }
840 
841 
842 
844 
845 void KVSeqCollection::_GetSubListWithMethod(KVSeqCollection* outputList, TCollection* Col, const Char_t* retvalue, const Char_t* method) const
846 {
847  if (retvalue && method) {
848  KVString RV(retvalue);
849  KVString MTH(method);
850  Bool_t wildcard = RV.Contains("*");
851  TIter next(Col);
852  TObject* ob;
853  while ((ob = next())) {
854  // recursive search in subidrectories
855  if (ob->InheritsFrom("TCollection")) {
856  _GetSubListWithMethod(outputList, (TCollection*)ob, retvalue, method);
857  continue;
858  }
859  TMethodCall mt;
860  mt.InitWithPrototype(ob->IsA(), MTH.Data(), "");
861  if (mt.IsValid()) {
862  //cout << "it is valid" << endl;
863  if (mt.ReturnType() == TMethodCall::kString) {
864  Char_t* ret;
865  mt.Execute(ob, "", &ret);
866  if (!wildcard) {
867  if (RV == ret) outputList->Add(ob);
868  }
869  else {
870  if (KVString(ret).Match(RV)) outputList->Add(ob);
871  }
872  }
873  else if (mt.ReturnType() == TMethodCall::kLong) {
874  Long_t ret;
875  mt.Execute(ob, "", ret);
876  if (ret == RV.Atoi()) outputList->Add(ob);
877  }
878  else if (mt.ReturnType() == TMethodCall::kDouble) {
879  Double_t ret;
880  mt.Execute(ob, "", ret);
881  if (ret == RV.Atof()) outputList->Add(ob);
882  }
883  else std::cout << "this type is not supported " << (int)mt.ReturnType() << std::endl;
884  }
885  }
886  }
887 }
888 
889 
890 
900 
902 {
903  // Create and fill a (sub)list with all objects in this list whose name
904  // (i.e. string returned by GetName()) is "retvalue"
905  // This new list will be of the same kind as this one.
906  // The objects in the sublist do not belong to the sublist.
907  //
908  // *** WARNING *** : DELETE the KVList returned by this method after using it !!!
909  // *** RECOMMENDED *** : store the returned value in a std::unique_ptr
910  // unique_ptr<KVSeqCollection> ptr(GetSubListWithName(...));
911 
912  return GetSubListWithMethod(retvalue, "GetName");
913 }
914 
915 
916 
926 
928 {
929  // Create and fill a (sub)list with all objects in this list whose label
930  // (i.e. string returned by GetLabel()) is "retvalue"
931  // This new list will be of the same kind as this one.
932  // The objects in the sublist do not belong to the sublist.
933  //
934  // *** WARNING *** : DELETE the KVList returned by this method after using it !!!
935  // *** RECOMMENDED *** : store the returned value in a std::unique_ptr
936  // unique_ptr<KVSeqCollection> ptr(GetSubListWithLabel(...));
937 
938  return GetSubListWithMethod(retvalue, "GetLabel");
939 }
940 
941 
942 
952 
954 {
955  // Create and fill a (sub)list with all objects in this list whose type
956  // (i.e. string returned by GetType()) is "retvalue"
957  // This new list will be of the same kind as this one.
958  // The objects in the sublist do not belong to the sublist.
959  //
960  // *** WARNING *** : DELETE the KVList returned by this method after using it !!!
961  // *** RECOMMENDED *** : store the returned value in a std::unique_ptr
962  // unique_ptr<KVSeqCollection> ptr(GetSubListWithType(...));
963 
964  return GetSubListWithMethod(retvalue, "GetType");
965 }
966 
967 
968 
978 
980 {
981  //Static method create a list containing all objects contain of a file
982  //The file can be closed after this method, objects stored in the
983  //list still remains valid
984  //if file=NULL, the current directory is considered
985  //
986  // *** WARNING *** : DELETE the KVSeqCollection returned by this method after using it !!!
987  // *** RECOMMENDED *** : store the returned value in a std::unique_ptr
988  // unique_ptr<KVSeqCollection> ptr(MakeListFromFile(...));
989 
990  KVSeqCollection* ll = new KVSeqCollection("TList");
991  ll->SetOwner(kFALSE);
992 
993  TKey* key = 0;
994  if (!file) {
995  TIter next_ps(gDirectory->GetListOfKeys());
996  while ((key = (TKey*) next_ps())) ll->Add(key->ReadObj());
997  }
998  else {
999  TIter next_ps(file->GetListOfKeys());
1000  while ((key = (TKey*) next_ps())) ll->Add(key->ReadObj());
1001 
1002  }
1003  return ll;
1004 }
1005 
1006 
1007 
1016 
1018 {
1019  //Static method create a list containing all objects whose "method" returns "retvalue" in a file
1020  //WARNING list has to be empty with KVSeqCollection::Clear() method before closing file
1021  //if file=NULL, the current directory is considered
1022  //
1023  // *** WARNING *** : DELETE the KVList returned by this method after using it !!!
1024  // *** RECOMMENDED *** : store the returned value in a std::unique_ptr
1025  // unique_ptr<KVSeqCollection> ptr(MakeListFromFileWithMethod(...));
1026 
1027  unique_ptr<KVSeqCollection> l1(MakeListFromFile(file));
1028  KVSeqCollection* l2 = l1->GetSubListWithMethod(retvalue, method);
1029  l1->Clear();
1030  return l2;
1031 }
1032 
1033 
1034 
1043 
1045 {
1046  //Static method create a list containing all objects of given class in a file
1047  //WARNING list has to be empty with KVList::Clear() method before closing file
1048  //if file=NULL, the current directory is considered
1049  //
1050  // *** WARNING *** : DELETE the KVList returned by this method after using it !!!
1051  // *** RECOMMENDED *** : store the returned value in a std::unique_ptr
1052  // unique_ptr<KVSeqCollection> ptr(MakeListFromFileWithClass(...));
1053 
1054  unique_ptr<KVSeqCollection> l1(MakeListFromFile(file));
1055  KVSeqCollection* l2 = l1->GetSubListWithClass(_class);
1056  l1->Clear();
1057  return l2;
1058 }
1059 
1060 
1061 
1070 
1072 {
1073  //Static method create a list containing all objects of given class in a file
1074  //WARNING list has to be empty with KVList::Clear() method before closing file
1075  //if file=NULL, the current directory is considered
1076  //
1077  // *** WARNING *** : DELETE the KVList returned by this method after using it !!!
1078  // *** RECOMMENDED *** : store the returned value in a std::unique_ptr
1079  // unique_ptr<KVSeqCollection> ptr(MakeListFromFileWithClass(...));
1080 
1081  unique_ptr<KVSeqCollection> l1(MakeListFromFile(file));
1082  KVSeqCollection* l2 = l1->GetSubListWithClass(class_name);
1083  l1->Clear();
1084  return l2;
1085 }
1086 
1087 
1088 
1089 
1094 
1096 {
1097  // To use the ROOT cleanup mechanism to ensure that any objects in the list which get
1098  // deleted elsewhere are removed from this list, call SetCleanup(kTRUE)
1099 
1100  //if(enable && IsOwner()) Warning("SetCleanup","List %s will be both owner & cleanup",GetName());
1101  SetBit(kCleanup, enable);
1102  if (enable) {
1103  fgCleanups->Add(this);
1104  fCollection->R__FOR_EACH(TObject, SetBit)(kMustCleanup);
1105  }
1106  else {
1107  fgCleanups->Remove(this);
1108  }
1109 }
1110 
1111 
1112 
1114 
1116 {
1117  ((THashList*)fgCleanups)->Rehash(fgCleanups->GetSize());
1118 }
1119 
1120 
1121 
1124 
1125 void KVSeqCollection::Streamer(TBuffer& R__b)
1126 {
1127  // Stream an object of class KVSeqCollection.
1128 
1129  UInt_t R__s, R__c;
1130  if (R__b.IsReading()) {
1131  Version_t R__v = R__b.ReadVersion(&R__s, &R__c);
1132  TSeqCollection::Streamer(R__b);
1133  if (R__v < 3) {
1134  // correct legacy BIT(16) used for fCleanup
1135  SetCleanup(TestBit(BIT(16)));
1136 #if ROOT_VERSION_CODE >= ROOT_VERSION(6,0,0)
1137  // for ROOT6 we reset BIT(16) if it was set, assuming that
1138  // default is 0 (used by TCollection::IsUsingRWLock())
1139  ResetBit(BIT(16));
1140 #endif
1141  }
1142  fQObject.Streamer(R__b);
1143  if (fCollection) {
1144  Bool_t owns = fCollection->IsOwner();
1145  fCollection->SetOwner(kFALSE);
1146  fCollection->Streamer(R__b);
1147  fCollection->SetOwner(owns);
1148  }
1149  else R__b >> fCollection;
1150  R__b.CheckByteCount(R__s, R__c, KVSeqCollection::IsA());
1151  }
1152  else {
1153  R__c = R__b.WriteVersion(KVSeqCollection::IsA(), kTRUE);
1154  TSeqCollection::Streamer(R__b);
1155  fQObject.Streamer(R__b);
1156  if (fCollection) {
1157  fCollection->Streamer(R__b);
1158  }
1159  else R__b << fCollection;
1160  R__b.SetByteCount(R__c, kTRUE);
1161  }
1162 }
1163 
1164 
int Int_t
unsigned int UInt_t
long Long_t
ClassImp(KVPartitionList) void KVPartitionList
Initialisation.
#define SafeDelete(p)
#define b(i)
#define c(i)
short Version_t
char Char_t
const Bool_t kFALSE
bool Bool_t
double Double_t
const Bool_t kTRUE
const char Option_t
#define BIT(n)
#define gDirectory
int type
#define gROOT
char * Form(const char *fmt,...)
kClass
kName
kTitle
Base class for KaliVeda framework.
Definition: KVBase.h:135
const Char_t * GetLabel() const
Definition: KVBase.h:192
const Char_t * GetType() const
Definition: KVBase.h:170
@ kIsKaliVedaObject
Definition: KVBase.h:156
KaliVeda extensions to ROOT collection classes.
virtual TObject * FindObjectByLabel(const Char_t *) const
static Long64_t fSCCounter
counter used to give unique names to all lists
virtual ~KVSeqCollection()
virtual void Copy(TObject &obj) const
KVSeqCollection * GetSubListWithName(const Char_t *retvalue) const
KVSeqCollection * GetSubListWithMethod(const Char_t *retvalue, const Char_t *method) const
KVSeqCollection & operator=(const KVSeqCollection &)
virtual TObject ** GetObjectRef(const TObject *obj) const
Return reference to object.
static TSeqCollection * fgCleanups
regroup all lists which are to be cleaned up
virtual void SendModifiedSignals(Bool_t yes=kTRUE)
virtual KVSeqCollection * NewCollectionLikeThisOne() const
virtual Bool_t IsCleanup() const
virtual void SetCollection(const Char_t *)
KVSeqCollection * GetSubListWithClass(const TClass *_class) const
TSeqCollection * fCollection
Pointer to embedded ROOT collection.
virtual void SetOwner(Bool_t enable=kTRUE)
virtual void Clear(Option_t *option="")
virtual Int_t GetSize() const
virtual TObject * FindObjectAny(const Char_t *att, const Char_t *keys, Bool_t contains_all=kFALSE, Bool_t case_sensitive=kTRUE) const
TObject * FindObjectByClass(const Char_t *) const
Return (first) object in embedded list with given class.
KVSeqCollection * GetSubListWithType(const Char_t *retvalue) const
virtual void _GetSubListWithMethod(KVSeqCollection *, TCollection *, const Char_t *, const Char_t *) const
virtual void Execute(const char *method, const char *params, Int_t *error=0)
KVSeqCollection()
Default constructor.
virtual Bool_t IsSendingModifiedSignals() const
static void RehashCleanupList()
virtual void Changed()
static Int_t fgCounter
counts instances
virtual void PrintCollectionHeader(Option_t *option) const
Overrides TCollection::PrintCollectionHeader to show the class name of the embedded list.
virtual void SetCleanup(Bool_t enable=kTRUE)
static KVSeqCollection * MakeListFromFileWithClass(TFile *file, const TClass *_class)
virtual TObject * FindObjectByNumber(UInt_t num) const
virtual TObject * FindObjectWithMethod(const Char_t *retvalue, const Char_t *method) const
static KVSeqCollection * MakeListFromFile(TFile *file)
virtual TObject * FindObjectByType(const Char_t *) const
virtual TObject * FindObjectByTitle(const Char_t *) const
Will return object with given title (value of TObject::GetTitle() method).
virtual void Add(TObject *obj)
virtual TObject * FindObjectWithNameAndType(const Char_t *name, const Char_t *type) const
virtual TIterator * MakeIterator(Bool_t dir=kIterForward) const
Make and return iterator for the list.
KVSeqCollection * GetSubListWithLabel(const Char_t *retvalue) const
virtual TObject * Remove(TObject *obj)
Remove object from list.
virtual void Delete(Option_t *option="")
static KVSeqCollection * MakeListFromFileWithMethod(TFile *file, const Char_t *retvalue, const Char_t *method)
@ kCleanup
in ROOT v6 BIT(16) is used by TCollection - without changing the class version
void _GetSubListWithClass(KVSeqCollection *, TCollection *, const TClass *) const
virtual void RecursiveRemove(TObject *obj)
Extension of ROOT TString class which allows backwards compatibility with ROOT v3....
Definition: KVString.h:72
virtual void SetByteCount(UInt_t cntpos, Bool_t packInVersion=kFALSE)=0
virtual Version_t ReadVersion(UInt_t *start=0, UInt_t *bcnt=0, const TClass *cl=0)=0
virtual Int_t CheckByteCount(UInt_t startpos, UInt_t bcnt, const char *classname)=0
Bool_t IsReading() const
virtual UInt_t WriteVersion(const TClass *cl, Bool_t useBcnt=kFALSE)=0
static TClass * GetClass(Bool_t load=kTRUE, Bool_t silent=kFALSE)
virtual TIterator * MakeIterator(Bool_t dir=kIterForward) const=0
virtual const char * GetName() const
virtual void Clear(Option_t *option="")=0
void SetName(const char *name)
virtual void RecursiveRemove(TObject *obj)
virtual void Delete(Option_t *option="")=0
virtual TObject ** GetObjectRef(const TObject *obj) const=0
virtual void SetOwner(Bool_t enable=kTRUE)
Bool_t IsOwner() const
virtual TObject * Remove(TObject *obj)=0
virtual Int_t GetSize() const
virtual TObject * ReadObj()
EReturnType ReturnType()
static const EReturnType kLong
void InitWithPrototype(const char *function, const char *proto, ROOT::EFunctionMatchMode mode=ROOT::kConversionMatch)
static const EReturnType kString
Bool_t IsValid() const
static const EReturnType kDouble
void Execute()
virtual const char * GetName() const
void SetBit(UInt_t f)
R__ALWAYS_INLINE Bool_t TestBit(UInt_t f) const
virtual void Execute(const char *method, const char *params, Int_t *error=0)
virtual const char * ClassName() const
virtual Bool_t InheritsFrom(const char *classname) const
virtual void Copy(TObject &object) const
virtual void Error(const char *method, const char *msgfmt,...) const
virtual const char * GetTitle() const
void MakeZombie()
void ResetBit(UInt_t f)
static void IndentLevel()
virtual void Add(TObject *obj)
Int_t Atoi() const
Double_t Atof() const
TObjArray * Tokenize(const TString &delim) const
const char * Data() const
Bool_t Contains(const char *pat, ECaseCompare cmp=kExact) const
long long Long64_t
gr SetName("gr")
const long double cl
Definition: KVUnits.h:85
Type GetType(const std::string &Name)