Logo ROOT  
Reference Guide
Types.cxx
Go to the documentation of this file.
1 // @(#)root/tmva $Id$
2 // Author: Andreas Hoecker, Joerg Stelzer, Helge Voss
3 
4 /**********************************************************************************
5  * Project: TMVA - a Root-integrated toolkit for multivariate data analysis *
6  * Package: TMVA *
7  * Class : Types *
8  * Web : http://tmva.sourceforge.net *
9  * *
10  * Description: *
11  * Implementation *
12  * *
13  * Authors (alphabetical): *
14  * Andreas Hoecker <Andreas.Hocker@cern.ch> - CERN, Switzerland *
15  * Joerg Stelzer <Joerg.Stelzer@cern.ch> - CERN, Switzerland *
16  * Helge Voss <Helge.Voss@cern.ch> - MPI-K Heidelberg, Germany *
17  * *
18  * Copyright (c) 2005: *
19  * CERN, Switzerland *
20  * U. of Victoria, Canada *
21  * MPI-K Heidelberg, Germany *
22  * *
23  * Redistribution and use in source and binary forms, with or without *
24  * modification, are permitted according to the terms listed in LICENSE *
25  * (http://mva.sourceforge.net/license.txt) *
26  **********************************************************************************/
27 
28 /*! \class TMVA::Types
29 \ingroup TMVA
30 Singleton class for Global types used by TMVA
31 */
32 
33 #include "TMVA/Types.h"
34 
35 #include "TMVA/MsgLogger.h"
36 
37 #include "RtypesCore.h"
38 #include "TString.h"
39 
40 #include <map>
41 #if __cplusplus > 199711L && !defined _MSC_VER
42 #include <mutex>
43 #endif
44 
45 #if __cplusplus > 199711L && !defined _MSC_VER
46 std::atomic<TMVA::Types*> TMVA::Types::fgTypesPtr{0};
47 static std::mutex gTypesMutex;
48 #else
50 #endif
51 
52 ////////////////////////////////////////////////////////////////////////////////
53 /// constructor
54 
56  : fLogger( new MsgLogger("Types") )
57 {
58 }
59 
61 {
62  // destructor
63  delete fLogger;
64 }
65 
66 ////////////////////////////////////////////////////////////////////////////////
67 /// the the single instance of "Types" if existing already, or create it (Singleton)
68 
70 {
71 #if __cplusplus > 199711L && !defined _MSC_VER
72  if(!fgTypesPtr) {
73  Types* tmp = new Types();
74  Types* expected = 0;
75  if(!fgTypesPtr.compare_exchange_strong(expected,tmp)) {
76  //Another thread already did it
77  delete tmp;
78  }
79  }
80  return *fgTypesPtr;
81 #else
82  return fgTypesPtr ? *fgTypesPtr : *(fgTypesPtr = new Types());
83 #endif
84 }
85 
86 ////////////////////////////////////////////////////////////////////////////////
87 /// "destructor" of the single instance
88 
90 {
91 #if __cplusplus > 199711L && !defined _MSC_VER
92  if (fgTypesPtr != 0) { delete fgTypesPtr.load(); fgTypesPtr = 0; }
93 #else
94  if (fgTypesPtr != 0) { delete fgTypesPtr; fgTypesPtr = 0; }
95 #endif
96 }
97 
98 ////////////////////////////////////////////////////////////////////////////////
99 
101 {
102 #if __cplusplus > 199711L && !defined _MSC_VER
103  std::lock_guard<std::mutex> guard(gTypesMutex);
104 #endif
105  std::map<TString, EMVA>::const_iterator it = fStr2type.find( methodname );
106  if (it != fStr2type.end()) {
107  Log() << kFATAL
108  << "Cannot add method " << methodname
109  << " to the name->type map because it exists already" << Endl;
110  return kFALSE;
111  }
112 
113  fStr2type[methodname] = method;
114  return kTRUE;
115 }
116 
117 ////////////////////////////////////////////////////////////////////////////////
118 /// returns the method type (enum) for a given method (string)
119 
121 {
122 #if __cplusplus > 199711L && !defined _MSC_VER
123  std::lock_guard<std::mutex> guard(gTypesMutex);
124 #endif
125  std::map<TString, EMVA>::const_iterator it = fStr2type.find( method );
126  if (it == fStr2type.end()) {
127  Log() << kFATAL << "Unknown method in map: " << method << Endl;
128  return kVariable; // Inserted to get rid of GCC warning...
129  }
130  else return it->second;
131 }
132 
133 ////////////////////////////////////////////////////////////////////////////////
134 
136 {
137 #if __cplusplus > 199711L && !defined _MSC_VER
138  std::lock_guard<std::mutex> guard(gTypesMutex);
139 #endif
140  std::map<TString, EMVA>::const_iterator it = fStr2type.begin();
141  for (; it!=fStr2type.end(); ++it) if (it->second == method) return it->first;
142  Log() << kFATAL << "Unknown method index in map: " << method << Endl;
143  return "";
144 }
TMVA::Types::Types
Types()
constructor
Definition: Types.cxx:55
TMVA::Types::~Types
~Types()
Definition: Types.cxx:60
kTRUE
const Bool_t kTRUE
Definition: RtypesCore.h:91
TMath::Log
Double_t Log(Double_t x)
Definition: TMath.h:760
TMVA::Types::GetMethodType
Types::EMVA GetMethodType(const TString &method) const
returns the method type (enum) for a given method (string)
Definition: Types.cxx:120
TMVA::Types::DestroyInstance
static void DestroyInstance()
"destructor" of the single instance
Definition: Types.cxx:89
TMVA::Types::AddTypeMapping
Bool_t AddTypeMapping(Types::EMVA method, const TString &methodname)
Definition: Types.cxx:100
TString
Basic string class.
Definition: TString.h:136
TMVA::Types::GetMethodName
TString GetMethodName(Types::EMVA method) const
Definition: Types.cxx:135
TString.h
bool
TMVA::Types::fgTypesPtr
static Types * fgTypesPtr
Definition: Types.h:177
MsgLogger.h
kFALSE
const Bool_t kFALSE
Definition: RtypesCore.h:92
TMVA::Types
Singleton class for Global types used by TMVA.
Definition: Types.h:73
Types.h
TMVA::Endl
MsgLogger & Endl(MsgLogger &ml)
Definition: MsgLogger.h:158
RtypesCore.h
TMVA::MsgLogger
ostringstream derivative to redirect and format output
Definition: MsgLogger.h:59
TMVA::Types::Instance
static Types & Instance()
the the single instance of "Types" if existing already, or create it (Singleton)
Definition: Types.cxx:69
TMVA::Types::EMVA
EMVA
Definition: Types.h:78