Logo ROOT   6.18/05
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*` names. For each unique
23name (which is not necessarily a unique pointer in the C++ standard),
24a unique pointer to a TNamed object is returned that can be used for
25fast searches and comparisons.
26**/
27
28#include "RooNameReg.h"
29
30#include "RooFit.h"
31#include "RooSentinel.h"
32#include "ROOT/RMakeUnique.hxx"
33#include <iostream>
34using namespace std ;
35
36
38 TNamed("RooNameReg","RooFit Name Registry")
39{}
40
41////////////////////////////////////////////////////////////////////////////////
42/// Destructor
43
45{
46}
47
48
49////////////////////////////////////////////////////////////////////////////////
50/// Return reference to singleton instance
51
53{
54 static RooNameReg instance;
55 return instance;
56}
57
58
59////////////////////////////////////////////////////////////////////////////////
60/// Cleanup function called by atexit() handler installed by RooSentinel
61/// to delete global objects on heap at end of program
62
64{
65}
66
67
68
69////////////////////////////////////////////////////////////////////////////////
70/// Return a unique TNamed pointer for given C++ string
71
72const TNamed* RooNameReg::constPtr(const char* inStr)
73{
74 // Handle null pointer case explicitly
75 if (inStr==0) return 0 ;
76
77 // See if name is already registered ;
78 auto elm = _map.find(inStr) ;
79 if (elm != _map.end()) return elm->second.get();
80
81 // If not, register now
82 auto t = make_unique<TNamed>(inStr,inStr);
83 auto ret = t.get();
84 _map.emplace(std::string(inStr), std::move(t));
85
86 return ret;
87}
88
89
90
91////////////////////////////////////////////////////////////////////////////////
92/// Return C++ string corresponding to given TNamed pointer
93
94const char* RooNameReg::constStr(const TNamed* namePtr)
95{
96 if (namePtr) return namePtr->GetName() ;
97 return 0 ;
98}
99
100
101////////////////////////////////////////////////////////////////////////////////
102/// Return a unique TNamed pointer for given C++ string
103
104const TNamed* RooNameReg::ptr(const char* stringPtr)
105{
106 if (stringPtr==0) return 0 ;
107 return instance().constPtr(stringPtr) ;
108}
109
110
111////////////////////////////////////////////////////////////////////////////////
112/// Return C++ string corresponding to given TNamed pointer
113
114const char* RooNameReg::str(const TNamed* ptr)
115{
116 if (ptr==0) return 0 ;
117 return instance().constStr(ptr) ;
118}
119
120
121////////////////////////////////////////////////////////////////////////////////
122/// If the name is already known, return its TNamed pointer. Otherwise return 0 (don't register the name).
123
124const TNamed* RooNameReg::known(const char* inStr)
125{
126 // Handle null pointer case explicitly
127 if (inStr==0) return 0 ;
128 RooNameReg& reg = instance();
129 const auto elm = reg._map.find(inStr);
130 return elm != reg._map.end() ? elm->second.get() : nullptr;
131}
RooNameReg is a registry for const char* names.
Definition: RooNameReg.h:24
std::unordered_map< std::string, std::unique_ptr< TNamed > > _map
Definition: RooNameReg.h:46
static const char * str(const TNamed *ptr)
Return C++ string corresponding to given TNamed pointer.
Definition: RooNameReg.cxx:114
static const TNamed * ptr(const char *stringPtr)
Return a unique TNamed pointer for given C++ string.
Definition: RooNameReg.cxx:104
virtual ~RooNameReg()
Destructor.
Definition: RooNameReg.cxx:44
const TNamed * constPtr(const char *stringPtr)
Return a unique TNamed pointer for given C++ string.
Definition: RooNameReg.cxx:72
static RooNameReg & instance()
Return reference to singleton instance.
Definition: RooNameReg.cxx:52
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:124
const char * constStr(const TNamed *namePtr)
Return C++ string corresponding to given TNamed pointer.
Definition: RooNameReg.cxx:94
static void cleanup()
Cleanup function called by atexit() handler installed by RooSentinel to delete global objects on heap...
Definition: RooNameReg.cxx:63
The TNamed class is the base class for all named ROOT classes.
Definition: TNamed.h:29
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:47