Logo ROOT  
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\file RooRefCountList.cxx
19\class RooRefCountList
20\ingroup Roofitcore
21
22A RooRefCountList is a RooLinkedList that keeps a reference counter
23with each added node. Multiple Add()s of the same object will increase
24the counter instead of adding multiple copies. Remove() decrements the
25reference count until zero, when the object is actually removed.
26**/
27
28#include "RooFit.h"
29
30#include "RooRefCountList.h"
31
32#include "Riostream.h"
33#include <stdlib.h>
34
35using 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 {
65 RooLinkedListElem* link = findLink(obj) ;
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{
82 RooLinkedListElem* link = findLink(obj) ;
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{
113 RooLinkedListElem* link = findLink(obj) ;
114 if (!link) {
115 return 0 ;
116 } else {
117 return link->refCount() ;
118 }
119}
int Int_t
Definition: RtypesCore.h:45
bool Bool_t
Definition: RtypesCore.h:63
#define ClassImp(name)
Definition: Rtypes.h:364
RooLinkedListElem is an link element for the RooLinkedList class.
Int_t refCount() const
RooLinkedList is an collection class for internal use, storing a collection of RooAbsArg pointers in ...
Definition: RooLinkedList.h:38
TObject * FindObject(const char *name) const
Return pointer to obejct with given name.
RooLinkedListElem * findLink(const TObject *arg) const
Find the element link containing the given object.
virtual void Add(TObject *arg)
Definition: RooLinkedList.h:67
virtual Bool_t Remove(TObject *arg)
Remove object from collection.
A RooRefCountList is a RooLinkedList that keeps a reference counter with each added node.
Int_t refCount(TObject *obj) const
Return reference count associated with 'obj'.
RooRefCountList()
Default constructor construct lists with initial hash table size of 17.
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 *obj)
Remove object from list and if reference count reaches zero delete object itself as well.
Mother of all ROOT objects.
Definition: TObject.h:37