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