ROOT  6.06/09
Reference Guide
RooRefCountList.cxx
Go to the documentation of this file.
1 /*****************************************************************************
2  * Project: RooFit *
3  * Package: RooFitCore *
4  * @(#)root/roofitcore:$Id$
5  * Authors: *
6  * WV, Wouter Verkerke, UC Santa Barbara, verkerke@slac.stanford.edu *
7  * DK, David Kirkby, UC Irvine, dkirkby@uci.edu *
8  * *
9  * Copyright (c) 2000-2005, Regents of the University of California *
10  * and Stanford University. All rights reserved. *
11  * *
12  * Redistribution and use in source and binary forms, *
13  * with or without modification, are permitted according to the terms *
14  * listed in LICENSE (http://roofit.sourceforge.net/license.txt) *
15  *****************************************************************************/
16 
17 //////////////////////////////////////////////////////////////////////////////
18 //
19 // BEGIN_HTML
20 // A RooRefCountList is a RooLinkedList that keeps a reference counter
21 // with each added node. Multiple Add()s of the same object will increase
22 // the counter instead of adding multiple copies. Remove() decrements the
23 // reference count until zero, when the object is actually removed.
24 // END_HTML
25 //
26 
27 #include "RooFit.h"
28 
29 #include "RooRefCountList.h"
30 #include "RooRefCountList.h"
31 
32 #include "Riostream.h"
33 #include <stdlib.h>
34 
35 using namespace std;
36 
38  ;
39 
40 
41 
42 ////////////////////////////////////////////////////////////////////////////////
43 /// Default constructor construct lists with initial hash table size of 17
44 
46  : RooLinkedList(0)
47 {
48 }
49 
50 
51 
52 ////////////////////////////////////////////////////////////////////////////////
53 /// Add object to list with given reference count increment
54 /// List takes ownership of object.
55 
57 {
58  // Check if we already have it
59  TObject* listObj = FindObject(obj) ;
60  if (!listObj) {
61  // Add to list with reference count
62  RooLinkedList::Add(obj, count) ;
63  //cout << "RooRefCountList::AddLast(" << obj << ") adding object" << endl ;
64  } else {
66  if(link) {
67  while(count--) link->incRefCount() ;
68  }
69  //cout << "RooRefCountList::AddLast(" << obj << ") incremented reference count to " << link->refCount() << endl ;
70  }
71 
72 }
73 
74 
75 
76 ////////////////////////////////////////////////////////////////////////////////
77 /// Remove object from list and if reference count
78 /// reaches zero delete object itself as well.
79 
81 {
83  if (!link) {
84  return 0 ;
85  } else {
86  if (link->decRefCount()==0) {
87  //cout << "RooRefCountList::AddLast(" << obj << ") removed object" << endl ;
88  return RooLinkedList::Remove(obj) ;
89  }
90  //cout << "RooRefCountList::AddLast(" << obj << ") decremented reference count to " << link->refCount() << endl ;
91  }
92  return 0 ;
93 }
94 
95 
96 
97 ////////////////////////////////////////////////////////////////////////////////
98 /// Remove object from list and delete object itself
99 /// regardless of reference count
100 
102 {
103  return RooLinkedList::Remove(obj) ;
104 }
105 
106 
107 
108 ////////////////////////////////////////////////////////////////////////////////
109 /// Return reference count associated with 'obj'
110 
112 {
114  if (!link) {
115  return 0 ;
116  } else {
117  return link->refCount() ;
118  }
119 }
virtual void Add(TObject *arg)
virtual Bool_t RemoveAll(TObject *obj)
Remove object from list and delete object itself regardless of reference count.
virtual Bool_t Remove(TObject *arg)
Remove object from collection.
Int_t refCount(TObject *obj)
Return reference count associated with 'obj'.
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
STL namespace.
ClassImp(RooList) TObjOptLink *RooList TObjLink * link
Find the link corresponding to the named object in this list.
Definition: RooList.cxx:48
RooRefCountList()
Default constructor construct lists with initial hash table size of 17.
virtual void Add(TObject *arg)
Definition: RooLinkedList.h:62
ClassImp(RooRefCountList)
RooLinkedListElem * findLink(const TObject *arg) const
Find the element link containing the given object.
TObject * FindObject(const char *name) const
Return pointer to obejct with given name.
virtual Bool_t Remove(TObject *obj)
Remove object from list and if reference count reaches zero delete object itself as well...
Int_t refCount() const
Mother of all ROOT objects.
Definition: TObject.h:58
TObject * obj