ROOT  6.06/09
Reference Guide
RooNameReg.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 // RooNameReg is a registry for 'const char*' name. For each unique
21 // name (which is not necessarily a unique pointer in the C++ standard),
22 // a unique pointer to a TNamed object is return that can be used for
23 // fast searches and comparisons.
24 // END_HTML
25 //
26 
27 #include "RooFit.h"
28 #include "RooSentinel.h"
29 
30 #include "RooNameReg.h"
31 #include "RooNameReg.h"
32 #include <iostream>
33 using namespace std ;
34 
36 ;
37 
38 RooNameReg* RooNameReg::_instance = 0 ;
39 
40 
41 RooNameReg::RooNameReg(Int_t hashSize) : TNamed("RooNameReg","RooFit Name Registry"), _htable(hashSize) {}
42 
43 ////////////////////////////////////////////////////////////////////////////////
44 /// Destructor
45 
47 {
48  _list.Delete() ;
49 }
50 
51 
52 ////////////////////////////////////////////////////////////////////////////////
53 /// Copy constructor
54 
56 {
57 }
58 
59 
60 ////////////////////////////////////////////////////////////////////////////////
61 /// Return reference to singleton instance
62 
64 {
65  if (_instance==0) {
66  _instance = new RooNameReg(100000) ; // there's only one of these, so we can afford to make it large
68  }
69  return *_instance ;
70 }
71 
72 
73 ////////////////////////////////////////////////////////////////////////////////
74 /// Cleanup function called by atexit() handler installed by RooSentinel
75 /// to delete global objects on heap at end of program
76 
78 {
79  if(_instance) {
80  delete _instance ;
81  _instance = 0 ;
82  }
83 }
84 
85 
86 
87 ////////////////////////////////////////////////////////////////////////////////
88 /// Return a unique TNamed pointer for given C++ string
89 
90 const TNamed* RooNameReg::constPtr(const char* inStr)
91 {
92  // Handle null pointer case explicitly
93  if (inStr==0) return 0 ;
94 
95 // cout << "RooNameReg::constPtr(inStr=" << inStr << ") _htable entries = " << _htable.entries() << endl ;
96 
97  // See if name is already registered ;
98  TNamed* t = (TNamed*) _htable.find(inStr) ;
99  if (t) return t ;
100 
101  // If not, register now
102  t = new TNamed(inStr,inStr) ;
103  _htable.add(t) ;
104  _list.Add(t) ;
105 
106  return t ;
107 }
108 
109 
110 
111 ////////////////////////////////////////////////////////////////////////////////
112 /// Return C++ string corresponding to given TNamed pointer
113 
114 const char* RooNameReg::constStr(const TNamed* namePtr)
115 {
116  if (namePtr) return namePtr->GetName() ;
117  return 0 ;
118 }
119 
120 
121 ////////////////////////////////////////////////////////////////////////////////
122 /// Return a unique TNamed pointer for given C++ string
123 
124 const TNamed* RooNameReg::ptr(const char* stringPtr)
125 {
126  if (stringPtr==0) return 0 ;
127  return instance().constPtr(stringPtr) ;
128 }
129 
130 
131 ////////////////////////////////////////////////////////////////////////////////
132 /// Return C++ string corresponding to given TNamed pointer
133 
134 const char* RooNameReg::str(const TNamed* ptr)
135 {
136  if (ptr==0) return 0 ;
137  return instance().constStr(ptr) ;
138 }
139 
140 
141 ////////////////////////////////////////////////////////////////////////////////
142 /// If the name is already known, return its TNamed pointer. Otherwise return 0 (don't register the name).
143 
144 const TNamed* RooNameReg::known(const char* inStr)
145 {
146  // Handle null pointer case explicitly
147  if (inStr==0) return 0 ;
148  if (_instance==0) return 0;
149  return (const TNamed*) _instance->_htable.find(inStr) ;
150 }
TObject * find(const char *name) const
Return the object with given name from the table.
static const char * str(const TNamed *ptr)
Return C++ string corresponding to given TNamed pointer.
Definition: RooNameReg.cxx:134
int Int_t
Definition: RtypesCore.h:41
STL namespace.
static void cleanup()
Cleanup function called by atexit() handler installed by RooSentinel to delete global objects on heap...
Definition: RooNameReg.cxx:77
The TNamed class is the base class for all named ROOT classes.
Definition: TNamed.h:33
RooLinkedList _list
Definition: RooNameReg.h:48
ClassImp(RooNameReg)
static const TNamed * known(const char *stringPtr)
If the name is already known, return its TNamed pointer. Otherwise return 0 (don't register the name)...
Definition: RooNameReg.cxx:144
static RooNameReg & instance()
Return reference to singleton instance.
Definition: RooNameReg.cxx:63
virtual void Add(TObject *arg)
Definition: RooLinkedList.h:62
void add(TObject *arg, TObject *hashArg=0)
Add given object to table.
static const TNamed * ptr(const char *stringPtr)
Return a unique TNamed pointer for given C++ string.
Definition: RooNameReg.cxx:124
virtual ~RooNameReg()
Destructor.
Definition: RooNameReg.cxx:46
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:51
static void activate()
Install atexit handler that calls CleanupRooFitAtExit() on program termination.
Definition: RooSentinel.cxx:70
void Delete(Option_t *o=0)
Remove all elements in collection and delete all elements NB: Collection does not own elements...
TNamed()
Definition: TNamed.h:40
RooNameReg(Int_t hashSize=31)
Definition: RooNameReg.cxx:41
RooHashTable _htable
Definition: RooNameReg.h:47
const TNamed * constPtr(const char *stringPtr)
Return a unique TNamed pointer for given C++ string.
Definition: RooNameReg.cxx:90
static RooNameReg * _instance
Definition: RooNameReg.h:42
const char * constStr(const TNamed *namePtr)
Return C++ string corresponding to given TNamed pointer.
Definition: RooNameReg.cxx:114