Diana Software
QGlobalRWFactory.cc
Go to the documentation of this file.
1 #include "QGlobalRWFactory.hh"
2 #include "QMessage.hh"
3 #include "QDll.hh"
4 #include "QGlobalReader.hh"
5 #include "QGlobalWriter.hh"
6 
7 using namespace Diana;
8 
10 {
11  static QGlobalRWFactory factory;
12  return factory;
13 }
14 
16 {
17  fDLLLoader = &Diana::QDLLLoader::GetInstance();
18  std::string pluginListFile = "plugins.list";
19  QError err = fDLLLoader->Init(pluginListFile,std::string(Q_PLUGIN_PREFIX_STR));
20  if(err!=QERR_SUCCESS) QMessageHandler::Panic(GetName(), std::string(err.ToString()) + " - " + err.GetDescription());
21 
22  // load external plugins
23  pluginListFile = "diana_plugins_external.list";
24  err = fDLLLoader->Init(pluginListFile,std::string(Q_PLUGIN_PREFIX_STR));
25  if(err!=QERR_SUCCESS) QMessageHandler::Debug(GetName(), std::string(err.ToString()) + " - " + err.GetDescription());
26 }
27 
29 {
30  std::map<std::string, QGlobalReaderFactory*>::iterator GlobalReadersIter;
31  std::map<std::string, QGlobalWriterFactory*>::iterator GlobalWritersIter;
32  for(GlobalReadersIter = fGlobalReaders.begin(); GlobalReadersIter != fGlobalReaders.end(); GlobalReadersIter++)
33  if(GlobalReadersIter->second) delete GlobalReadersIter->second;
34 
35  for(GlobalWritersIter = fGlobalWriters.begin(); GlobalWritersIter != fGlobalWriters.end(); GlobalWritersIter++)
36  if(GlobalWritersIter->second) delete GlobalWritersIter->second;
37 
38 }
39 
40 void QGlobalRWFactory::LoadPlugin(const std::string& name)
41 {
42  std::string fullName = name;
43  QError err = fDLLLoader->LoadPlugin(fullName);
44  if(err!=QERR_SUCCESS) QMessageHandler::Error(GetName(), std::string(err.ToString()) + " - " + err.GetDescription());
45 }
46 
47 // Global readers
49 {
50  std::map<std::string, QGlobalReaderFactory*>::iterator iter;
51  iter = fGlobalReaders.find(GRF->GetName());
52  if(iter == fGlobalReaders.end()) {
53  fGlobalReaders[GRF->GetName()] = GRF;
54  QMessageHandler::Debug(GetName(), std::string("Registered g-reader: " + GRF->GetName()));
55  } else {
56  QMessageHandler::Debug(GetName(), std::string("Already registered: " + GRF->GetName()));
57  delete GRF;
58  }
59 }
60 
61 
62 std::list<const QGlobalReaderFactory*> QGlobalRWFactory::GetListOfGlobalReaders() const
63 {
64  std::list<const QGlobalReaderFactory*> readersList;
65  std::map<std::string, QGlobalReaderFactory*>::const_iterator iter;
66  for(iter = fGlobalReaders.begin(); iter!=fGlobalReaders.end(); iter++)
67  readersList.push_back(iter->second);
68  return readersList;
69 }
70 
72 {
73  std::map<std::string, QGlobalReaderFactory*>::iterator iter;
74  iter = fGlobalReaders.find(name);
75  if(iter!=fGlobalReaders.end() && iter->second != NULL)
76  return iter->second->Create();
77  return NULL;
78 }
79 
81 {
82  std::map<std::string, QGlobalWriterFactory*>::iterator iter;
83  iter = fGlobalWriters.find(GRF->GetName());
84  if(iter == fGlobalWriters.end()) {
85  fGlobalWriters[GRF->GetName()] = GRF;
86  QMessageHandler::Debug(GetName(), std::string("Registered g-writer: ") + GRF->GetName());
87  } else {
88  QMessageHandler::Debug(GetName(), std::string("Already registered: ") + GRF->GetName());
89  delete GRF;
90  }
91 }
92 
93 // Global Writers
94 std::list<const QGlobalWriterFactory*> QGlobalRWFactory::GetListOfGlobalWriters() const
95 {
96  std::list<const QGlobalWriterFactory*> readersList;
97  std::map<std::string, QGlobalWriterFactory*>::const_iterator iter;
98  for(iter = fGlobalWriters.begin(); iter!=fGlobalWriters.end(); iter++)
99  readersList.push_back(iter->second);
100  return readersList;
101 }
102 
104 {
105  std::map<std::string, QGlobalWriterFactory*>::iterator iter;
106  iter = fGlobalWriters.find(name);
107  if(iter!=fGlobalWriters.end() && iter->second != NULL)
108  return iter->second->Create();
109  return NULL;
110 }
err
Definition: CheckOF.C:114
@ QERR_SUCCESS
Definition: QError.hh:27
#define Q_PLUGIN_PREFIX_STR
error class with error type and description
Definition: QError.hh:115
This class take care of plugin registration and creation.
std::list< const QGlobalReaderFactory * > GetListOfGlobalReaders() const
std::list< const QGlobalWriterFactory * > GetListOfGlobalWriters() const
std::map< std::string, QGlobalReaderFactory * > fGlobalReaders
QGlobalReader * CreateGlobalReader(const std::string &name)
Diana::QDLLLoader * fDLLLoader
static QGlobalRWFactory & GetInstance()
virtual ~QGlobalRWFactory()
std::map< std::string, QGlobalWriterFactory * > fGlobalWriters
void RegisterGlobalReader(QGlobalReaderFactory *GRF)
void LoadPlugin(const std::string &name)
QGlobalWriter * CreateGlobalWriter(const std::string &name)
void RegisterGlobalWriter(QGlobalWriterFactory *GRF)
abstract factory for QGlobalReader.
const std::string & GetName() const
Abstract class for global readers.
abstract factory for QGlobalWriter.
const std::string & GetName() const
Abstract class for global writers.
static void Debug(const std::string &sender, const std::string &msg)
Definition: QMessage.hh:41
static void Panic(const std::string &sender, const std::string &msg)
Definition: QMessage.hh:43
static void Error(const std::string &sender, const std::string &msg)
Definition: QMessage.hh:42
base class for anything that has a name
Definition: QNamed.hh:14
const std::string & GetName() const
Definition: QNamed.hh:19
the Diana namespace is needed because sometimes we use Qt libraries, that use same class names of our...