00001 /***************************************************************************** 00002 * Project: RooFit * 00003 * Package: RooFitCore * 00004 * @(#)root/roofitcore:$Id: RooNameReg.cxx 24285 2008-06-16 15:05:15Z wouter $ 00005 * Authors: * 00006 * WV, Wouter Verkerke, UC Santa Barbara, verkerke@slac.stanford.edu * 00007 * DK, David Kirkby, UC Irvine, dkirkby@uci.edu * 00008 * * 00009 * Copyright (c) 2000-2005, Regents of the University of California * 00010 * and Stanford University. All rights reserved. * 00011 * * 00012 * Redistribution and use in source and binary forms, * 00013 * with or without modification, are permitted according to the terms * 00014 * listed in LICENSE (http://roofit.sourceforge.net/license.txt) * 00015 *****************************************************************************/ 00016 00017 ////////////////////////////////////////////////////////////////////////////// 00018 // 00019 // BEGIN_HTML 00020 // RooNameReg is a registry for 'const char*' name. For each unique 00021 // name (which is not necessarily a unique pointer in the C++ standard), 00022 // a unique pointer to a TNamed object is return that can be used for 00023 // fast searches and comparisons. 00024 // END_HTML 00025 // 00026 00027 #include "RooFit.h" 00028 #include "RooSentinel.h" 00029 00030 #include "RooNameReg.h" 00031 #include "RooNameReg.h" 00032 00033 ClassImp(RooNameReg) 00034 ; 00035 00036 RooNameReg* RooNameReg::_instance = 0 ; 00037 00038 00039 00040 //_____________________________________________________________________________ 00041 RooNameReg::~RooNameReg() 00042 { 00043 // Destructor 00044 00045 _list.Delete() ; 00046 } 00047 00048 00049 //_____________________________________________________________________________ 00050 RooNameReg::RooNameReg(const RooNameReg& other) : TNamed(other) 00051 { 00052 // Copy constructor 00053 } 00054 00055 00056 //_____________________________________________________________________________ 00057 RooNameReg& RooNameReg::instance() 00058 { 00059 // Return reference to singleton instance 00060 00061 if (_instance==0) { 00062 _instance = new RooNameReg ; 00063 RooSentinel::activate() ; 00064 } 00065 return *_instance ; 00066 } 00067 00068 00069 //_____________________________________________________________________________ 00070 void RooNameReg::cleanup() 00071 { 00072 // Cleanup function called by atexit() handler installed by RooSentinel 00073 // to delete global objects on heap at end of program 00074 if(_instance) { 00075 delete _instance ; 00076 _instance = 0 ; 00077 } 00078 } 00079 00080 00081 00082 //_____________________________________________________________________________ 00083 const TNamed* RooNameReg::constPtr(const char* inStr) 00084 { 00085 // Return a unique TNamed pointer for given C++ string 00086 00087 // Handle null pointer case explicitly 00088 if (inStr==0) return 0 ; 00089 00090 // See if name is already registered ; 00091 TNamed* t = (TNamed*) _htable.find(inStr) ; 00092 if (t) return t ; 00093 00094 // If not, register now 00095 t = new TNamed(inStr,inStr) ; 00096 _htable.add(t) ; 00097 _list.Add(t) ; 00098 00099 return t ; 00100 } 00101 00102 00103 00104 //_____________________________________________________________________________ 00105 const char* RooNameReg::constStr(const TNamed* namePtr) 00106 { 00107 // Return C++ string corresponding to given TNamed pointer 00108 00109 if (namePtr) return namePtr->GetName() ; 00110 return 0 ; 00111 } 00112 00113 00114 //_____________________________________________________________________________ 00115 const TNamed* RooNameReg::ptr(const char* stringPtr) 00116 { 00117 // Return a unique TNamed pointer for given C++ string 00118 00119 return instance().constPtr(stringPtr) ; 00120 } 00121 00122 00123 //_____________________________________________________________________________ 00124 const char* RooNameReg::str(const TNamed* ptr) 00125 { 00126 // Return C++ string corresponding to given TNamed pointer 00127 00128 return instance().constStr(ptr) ; 00129 }