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