Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
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 \file RooRefCountList.cxx
19 \class RooRefCountList
20 \ingroup Roofitcore
21 
22 A RooRefCountList is a RooLinkedList that keeps a reference counter
23 with each added node. Multiple Add()s of the same object will increase
24 the counter instead of adding multiple copies. Remove() decrements the
25 reference count until zero, when the object is actually removed.
26 **/
27 
28 #include "RooFit.h"
29 
30 #include "RooRefCountList.h"
31 #include "RooRefCountList.h"
32 
33 #include "Riostream.h"
34 #include <stdlib.h>
35 
36 using namespace std;
37 
38 ClassImp(RooRefCountList);
39  ;
40 
41 
42 
43 ////////////////////////////////////////////////////////////////////////////////
44 /// Default constructor construct lists with initial hash table size of 17
45 
46 RooRefCountList::RooRefCountList()
47  : RooLinkedList(0)
48 {
49 }
50 
51 
52 
53 ////////////////////////////////////////////////////////////////////////////////
54 /// Add object to list with given reference count increment
55 /// List takes ownership of object.
56 
57 void RooRefCountList::Add(TObject* obj, Int_t count)
58 {
59  // Check if we already have it
60  TObject* listObj = FindObject(obj) ;
61  if (!listObj) {
62  // Add to list with reference count
63  RooLinkedList::Add(obj, count) ;
64  //cout << "RooRefCountList::AddLast(" << obj << ") adding object" << endl ;
65  } else {
66  RooLinkedListElem* link = findLink(obj) ;
67  if(link) {
68  while(count--) link->incRefCount() ;
69  }
70  //cout << "RooRefCountList::AddLast(" << obj << ") incremented reference count to " << link->refCount() << endl ;
71  }
72 
73 }
74 
75 
76 
77 ////////////////////////////////////////////////////////////////////////////////
78 /// Remove object from list and if reference count
79 /// reaches zero delete object itself as well.
80 
81 Bool_t RooRefCountList::Remove(TObject* obj)
82 {
83  RooLinkedListElem* link = findLink(obj) ;
84  if (!link) {
85  return 0 ;
86  } else {
87  if (link->decRefCount()==0) {
88  //cout << "RooRefCountList::AddLast(" << obj << ") removed object" << endl ;
89  return RooLinkedList::Remove(obj) ;
90  }
91  //cout << "RooRefCountList::AddLast(" << obj << ") decremented reference count to " << link->refCount() << endl ;
92  }
93  return 0 ;
94 }
95 
96 
97 
98 ////////////////////////////////////////////////////////////////////////////////
99 /// Remove object from list and delete object itself
100 /// regardless of reference count
101 
102 Bool_t RooRefCountList::RemoveAll(TObject* obj)
103 {
104  return RooLinkedList::Remove(obj) ;
105 }
106 
107 
108 
109 ////////////////////////////////////////////////////////////////////////////////
110 /// Return reference count associated with 'obj'
111 
112 Int_t RooRefCountList::refCount(TObject* obj) const
113 {
114  RooLinkedListElem* link = findLink(obj) ;
115  if (!link) {
116  return 0 ;
117  } else {
118  return link->refCount() ;
119  }
120 }