Logo ROOT  
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 <iostream>
32#include <memory>
33using namespace std ;
34
35
37 TNamed("RooNameReg","RooFit Name Registry")
38{}
39
40////////////////////////////////////////////////////////////////////////////////
41/// Destructor
42
44{
45}
46
47
48////////////////////////////////////////////////////////////////////////////////
49/// Return reference to singleton instance
50
52{
53 static RooNameReg instance;
54 return instance;
55}
56
57
58////////////////////////////////////////////////////////////////////////////////
59/// Return a unique TNamed pointer for given C++ string
60
61const TNamed* RooNameReg::constPtr(const char* inStr)
62{
63 // Handle null pointer case explicitly
64 if (inStr==0) return 0 ;
65
66 // See if name is already registered ;
67 auto elm = _map.find(inStr) ;
68 if (elm != _map.end()) return elm->second.get();
69
70 // If not, register now
71 auto t = make_unique<TNamed>(inStr,inStr);
72 auto ret = t.get();
73 _map.emplace(std::string(inStr), std::move(t));
74
75 return ret;
76}
77
78
79
80////////////////////////////////////////////////////////////////////////////////
81/// Return C++ string corresponding to given TNamed pointer
82
83const char* RooNameReg::constStr(const TNamed* namePtr)
84{
85 if (namePtr) return namePtr->GetName() ;
86 return 0 ;
87}
88
89
90////////////////////////////////////////////////////////////////////////////////
91/// Return a unique TNamed pointer for given C++ string
92
93const TNamed* RooNameReg::ptr(const char* stringPtr)
94{
95 if (stringPtr==0) return 0 ;
96 return instance().constPtr(stringPtr) ;
97}
98
99
100////////////////////////////////////////////////////////////////////////////////
101/// Return C++ string corresponding to given TNamed pointer
102
103const char* RooNameReg::str(const TNamed* ptr)
104{
105 if (ptr==0) return 0 ;
106 return instance().constStr(ptr) ;
107}
108
109
110////////////////////////////////////////////////////////////////////////////////
111/// If the name is already known, return its TNamed pointer. Otherwise return 0 (don't register the name).
112
113const TNamed* RooNameReg::known(const char* inStr)
114{
115 // Handle null pointer case explicitly
116 if (inStr==0) return 0 ;
117 RooNameReg& reg = instance();
118 const auto elm = reg._map.find(inStr);
119 return elm != reg._map.end() ? elm->second.get() : nullptr;
120}
121
122
123////////////////////////////////////////////////////////////////////////////////
124/// The renaming counter has to be incremented every time a RooAbsArg is
125/// renamed. This is a protected function, and only the friend class RooAbsArg
126/// should call it when it gets renamed.
127
130}
131
132
133////////////////////////////////////////////////////////////////////////////////
134// Return a reference to a counter that keeps track how often a RooAbsArg was
135/// renamed in this RooFit process.
136
137const std::size_t& RooNameReg::renameCounter() {
138 return instance()._renameCounter;
139}
RooNameReg is a registry for const char* names.
Definition: RooNameReg.h:25
std::unordered_map< std::string, std::unique_ptr< TNamed > > _map
Definition: RooNameReg.h:50
static const char * str(const TNamed *ptr)
Return C++ string corresponding to given TNamed pointer.
Definition: RooNameReg.cxx:103
static const TNamed * ptr(const char *stringPtr)
Return a unique TNamed pointer for given C++ string.
Definition: RooNameReg.cxx:93
std::size_t _renameCounter
Definition: RooNameReg.h:51
virtual ~RooNameReg()
Destructor.
Definition: RooNameReg.cxx:43
const TNamed * constPtr(const char *stringPtr)
Return a unique TNamed pointer for given C++ string.
Definition: RooNameReg.cxx:61
static RooNameReg & instance()
Return reference to singleton instance.
Definition: RooNameReg.cxx:51
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:113
const char * constStr(const TNamed *namePtr)
Return C++ string corresponding to given TNamed pointer.
Definition: RooNameReg.cxx:83
static void incrementRenameCounter()
The renaming counter has to be incremented every time a RooAbsArg is renamed.
Definition: RooNameReg.cxx:128
static const std::size_t & renameCounter()
renamed in this RooFit process.
Definition: RooNameReg.cxx:137
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