Logo ROOT   6.16/01
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
22RooNameReg is a registry for 'const char*' name. For each unique
23name (which is not necessarily a unique pointer in the C++ standard),
24a unique pointer to a TNamed object is return that can be used for
25fast 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>
34using namespace std ;
35
37;
38
40
41
42RooNameReg::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
91const 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
115const 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
125const 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
135const 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
145const 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}
int Int_t
Definition: RtypesCore.h:41
#define ClassImp(name)
Definition: Rtypes.h:363
TObject * find(const char *name) const
Return the object with given name from the table.
void add(TObject *arg, TObject *hashArg=0)
Add given object to table.
void Delete(Option_t *o=0)
Remove all elements in collection and delete all elements NB: Collection does not own elements,...
virtual void Add(TObject *arg)
Definition: RooLinkedList.h:62
RooNameReg is a registry for 'const char*' name.
Definition: RooNameReg.h:23
static RooNameReg * _instance
Definition: RooNameReg.h:42
static const char * str(const TNamed *ptr)
Return C++ string corresponding to given TNamed pointer.
Definition: RooNameReg.cxx:135
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
const TNamed * constPtr(const char *stringPtr)
Return a unique TNamed pointer for given C++ string.
Definition: RooNameReg.cxx:91
static RooNameReg & instance()
Return reference to singleton instance.
Definition: RooNameReg.cxx:64
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:145
RooHashTable _htable
Definition: RooNameReg.h:47
const char * constStr(const TNamed *namePtr)
Return C++ string corresponding to given TNamed pointer.
Definition: RooNameReg.cxx:115
static void cleanup()
Cleanup function called by atexit() handler installed by RooSentinel to delete global objects on heap...
Definition: RooNameReg.cxx:78
RooNameReg(Int_t hashSize=31)
Definition: RooNameReg.cxx:42
RooLinkedList _list
Definition: RooNameReg.h:48
static void activate()
Install atexit handler that calls CleanupRooFitAtExit() on program termination.
Definition: RooSentinel.cxx:73
The TNamed class is the base class for all named ROOT classes.
Definition: TNamed.h:29
TNamed()
Definition: TNamed.h:36
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:47
STL namespace.