#include <map>
#include <iostream>
#if __cplusplus > 199711L
#include <mutex>
#endif
#include "TMVA/Types.h"
#include "TMVA/MsgLogger.h"
#if __cplusplus > 199711L
std::atomic<TMVA::Types*> TMVA::Types::fgTypesPtr{0};
static std::mutex gTypesMutex;
#else
TMVA::Types* TMVA::Types::fgTypesPtr = 0;
#endif
TMVA::Types::Types()
: fLogger( new MsgLogger("Types") )
{
}
TMVA::Types::~Types()
{
delete fLogger;
}
TMVA::Types& TMVA::Types::Instance()
{
#if __cplusplus > 199711L
if(!fgTypesPtr) {
Types* tmp = new Types();
Types* expected = 0;
if(!fgTypesPtr.compare_exchange_strong(expected,tmp)) {
delete tmp;
}
}
return *fgTypesPtr;
#else
return fgTypesPtr ? *fgTypesPtr : *(fgTypesPtr = new Types());
#endif
}
void TMVA::Types::DestroyInstance()
{
#if __cplusplus > 199711L
if (fgTypesPtr != 0) { delete fgTypesPtr.load(); fgTypesPtr = 0; }
#else
if (fgTypesPtr != 0) { delete fgTypesPtr; fgTypesPtr = 0; }
#endif
}
Bool_t TMVA::Types::AddTypeMapping( Types::EMVA method, const TString& methodname )
{
#if __cplusplus > 199711L
std::lock_guard<std::mutex> guard(gTypesMutex);
#endif
std::map<TString, EMVA>::const_iterator it = fStr2type.find( methodname );
if (it != fStr2type.end()) {
Log() << kFATAL
<< "Cannot add method " << methodname
<< " to the name->type map because it exists already" << Endl;
return kFALSE;
}
fStr2type[methodname] = method;
return kTRUE;
}
TMVA::Types::EMVA TMVA::Types::GetMethodType( const TString& method ) const
{
#if __cplusplus > 199711L
std::lock_guard<std::mutex> guard(gTypesMutex);
#endif
std::map<TString, EMVA>::const_iterator it = fStr2type.find( method );
if (it == fStr2type.end()) {
Log() << kFATAL << "Unknown method in map: " << method << Endl;
return kVariable;
}
else return it->second;
}
TString TMVA::Types::GetMethodName( TMVA::Types::EMVA method ) const
{
#if __cplusplus > 199711L
std::lock_guard<std::mutex> guard(gTypesMutex);
#endif
std::map<TString, EMVA>::const_iterator it = fStr2type.begin();
for (; it!=fStr2type.end(); it++) if (it->second == method) return it->first;
Log() << kFATAL << "Unknown method index in map: " << method << Endl;
return "";
}