ROOT  6.06/09
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 #include <map>
29 #include <iostream>
30 #if __cplusplus > 199711L
31 #include <mutex>
32 #endif
33 
34 #include "TMVA/Types.h"
35 #include "TMVA/MsgLogger.h"
36 
37 #if __cplusplus > 199711L
38 std::atomic<TMVA::Types*> TMVA::Types::fgTypesPtr{0};
39 static std::mutex gTypesMutex;
40 #else
41 TMVA::Types* TMVA::Types::fgTypesPtr = 0;
42 #endif
43 
44 ////////////////////////////////////////////////////////////////////////////////
45 /// constructor
46 
48  : fLogger( new MsgLogger("Types") )
49 {
50 }
51 
53 {
54  // destructor
55  delete fLogger;
56 }
57 
58 ////////////////////////////////////////////////////////////////////////////////
59 /// the the single instance of "Types" if existin already, or create it (Signleton)
60 
62 {
63 #if __cplusplus > 199711L
64  if(!fgTypesPtr) {
65  Types* tmp = new Types();
66  Types* expected = 0;
67  if(!fgTypesPtr.compare_exchange_strong(expected,tmp)) {
68  //Another thread already did it
69  delete tmp;
70  }
71  }
72  return *fgTypesPtr;
73 #else
74  return fgTypesPtr ? *fgTypesPtr : *(fgTypesPtr = new Types());
75 #endif
76 }
77 ////////////////////////////////////////////////////////////////////////////////
78 /// "destructor" of the single instance
79 
81 {
82 #if __cplusplus > 199711L
83  if (fgTypesPtr != 0) { delete fgTypesPtr.load(); fgTypesPtr = 0; }
84 #else
85  if (fgTypesPtr != 0) { delete fgTypesPtr; fgTypesPtr = 0; }
86 #endif
87 }
88 
89 
90 ////////////////////////////////////////////////////////////////////////////////
91 
93 {
94 #if __cplusplus > 199711L
95  std::lock_guard<std::mutex> guard(gTypesMutex);
96 #endif
97  std::map<TString, EMVA>::const_iterator it = fStr2type.find( methodname );
98  if (it != fStr2type.end()) {
99  Log() << kFATAL
100  << "Cannot add method " << methodname
101  << " to the name->type map because it exists already" << Endl;
102  return kFALSE;
103  }
104 
105  fStr2type[methodname] = method;
106  return kTRUE;
107 }
108 
109 ////////////////////////////////////////////////////////////////////////////////
110 
112 {
113 #if __cplusplus > 199711L
114  std::lock_guard<std::mutex> guard(gTypesMutex);
115 #endif
116  // returns the method type (enum) for a given method (string)
117  std::map<TString, EMVA>::const_iterator it = fStr2type.find( method );
118  if (it == fStr2type.end()) {
119  Log() << kFATAL << "Unknown method in map: " << method << Endl;
120  return kVariable; // Inserted to get rid of GCC warning...
121  }
122  else return it->second;
123 }
124 
125 ////////////////////////////////////////////////////////////////////////////////
126 
128 {
129 #if __cplusplus > 199711L
130  std::lock_guard<std::mutex> guard(gTypesMutex);
131 #endif
132  std::map<TString, EMVA>::const_iterator it = fStr2type.begin();
133  for (; it!=fStr2type.end(); it++) if (it->second == method) return it->first;
134  Log() << kFATAL << "Unknown method index in map: " << method << Endl;
135  return "";
136 }
MsgLogger & Endl(MsgLogger &ml)
Definition: MsgLogger.h:162
Basic string class.
Definition: TString.h:137
bool Bool_t
Definition: RtypesCore.h:59
const Bool_t kFALSE
Definition: Rtypes.h:92
static Types & Instance()
the the single instance of "Types" if existin already, or create it (Signleton)
Definition: Types.cxx:61
Bool_t AddTypeMapping(Types::EMVA method, const TString &methodname)
Definition: Types.cxx:92
Types()
constructor
Definition: Types.cxx:47
TString GetMethodName(Types::EMVA method) const
Definition: Types.cxx:127
Types::EMVA GetMethodType(const TString &method) const
Definition: Types.cxx:111
static void DestroyInstance()
"destructor" of the single instance
Definition: Types.cxx:80
const Bool_t kTRUE
Definition: Rtypes.h:91
Definition: math.cpp:60