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