Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
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*` names. 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 returned that can be used for
25 fast searches and comparisons.
26 **/
27 
28 #include "RooNameReg.h"
29 
30 #include "RooFit.h"
31 #include "ROOT/RMakeUnique.hxx"
32 #include <iostream>
33 using namespace std ;
34 
35 
36 RooNameReg::RooNameReg() :
37  TNamed("RooNameReg","RooFit Name Registry")
38 {}
39 
40 ////////////////////////////////////////////////////////////////////////////////
41 /// Destructor
42 
43 RooNameReg::~RooNameReg()
44 {
45 }
46 
47 
48 ////////////////////////////////////////////////////////////////////////////////
49 /// Return reference to singleton instance
50 
51 RooNameReg& RooNameReg::instance()
52 {
53  static RooNameReg instance;
54  return instance;
55 }
56 
57 
58 ////////////////////////////////////////////////////////////////////////////////
59 /// Return a unique TNamed pointer for given C++ string
60 
61 const 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 
83 const 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 
93 const 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 
103 const 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 
113 const 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 }