LIMA
Libre Multilingual Analyzer — C++ API
Loading...
Searching...
No Matches
MainFactory.h
Go to the documentation of this file.
1// Copyright 2002-2019 CEA LIST
2// SPDX-FileCopyrightText: 2022 CEA LIST <gael.de-chalendar@cea.fr>
3//
4// SPDX-License-Identifier: MIT
5
6/***************************************************************************
7 * Copyright (C) 2004-2019 by CEA LIST *
8 * *
9 ***************************************************************************/
10
11#ifndef LIMA_COMMON_ABSTRACTFACTORYPATTERN_MAINFACTORY_H
12#define LIMA_COMMON_ABSTRACTFACTORYPATTERN_MAINFACTORY_H
13
17
18#include <map>
19#include <deque>
20#include <string>
21#include <typeinfo>
22
23#include "MainFactoriesMap.h"
24
25namespace Lima
26{
27
35 template<typename Factory>
37 {
38
39 public:
40
44 static const MainFactory<Factory>& single();
45
50
54 static void cleanup();
55
67 const std::shared_ptr<Factory> getFactory(const std::string& classId) const;
68
80 std::shared_ptr<Factory> getFactory(const std::string& classId);
81
88 void registerFactory(
89 const std::string& classId,
90 Factory* fact);
91
96 std::deque<std::string> getRegisteredFactories() const;
97
98 virtual ~MainFactory();
99
100 private:
101
102 MainFactory();
103
104 typedef std::map<std::string, std::shared_ptr<Factory>> FactoryMap;
105 typedef typename FactoryMap::const_iterator FactoryMapCItr;
106 FactoryMap m_factories;
107
108 };
109
110 template<typename Factory>
111 MainFactory<Factory>::MainFactory() : m_factories() {}
112
113 template<typename Factory>
115 {
116 typename MainFactoryMap::const_iterator it = MainFactoriesMap::get().find(typeid(Factory).name());
117 if (MainFactoriesMap::get().end() == it)
118 MainFactoriesMap::get().insert(std::pair<std::string, void*>(typeid(Factory).name(), new MainFactory<Factory>()));
119
120 it = MainFactoriesMap::get().find(typeid(Factory).name());
121 if (MainFactoriesMap::get().end() == it)
122 throw LimaException("This can't happen.");
123
124 return *((const MainFactory<Factory>*)(it->second));
125 }
126
127 template<typename Factory>
129 {
130 typename MainFactoryMap::iterator it = MainFactoriesMap::get().find(typeid(Factory).name());
131 if (MainFactoriesMap::get().end() == it)
132 MainFactoriesMap::get().insert(std::pair<std::string, void*>(typeid(Factory).name(), new MainFactory<Factory>()));
133
134 it = MainFactoriesMap::get().find(typeid(Factory).name());
135 if (MainFactoriesMap::get().end() == it)
136 throw LimaException("This can't happen.");
137
138 return *((MainFactory<Factory>*)(it->second));
139 }
140
141
142 template<typename Factory>
146
147 template<typename Factory>
151
152 template<typename Factory>
153 const std::shared_ptr<Factory> MainFactory<Factory>::getFactory(const std::string& classId) const
154 {
155 if (classId.empty())
156 {
157 std::cerr << "Trying to access to factory with empty name!" << std::endl;
158 throw InvalidConfiguration("Trying to access to factory with empty name!");
159 }
160 FactoryMapCItr factItr=m_factories.find(classId);
161 if (factItr==m_factories.end())
162 {
163 std::cerr << "1: No AbstractFactory for classId '" << classId << "' ! " << std::endl;
164 std::cerr << " Should be initialized at library loading time." << std::endl;
165 std::cerr << " Maybe you forgot to link with the library defining this classId ?" << std::endl;
166 std::cerr << " Or you forgot to define it as a plugin using, in its CMakeLists.txt," << std::endl;
167 std::cerr << " DECLARE_LIMA_PLUGIN, instead of add_library ?" << std::endl;
169 QString::fromStdString("No AbstractFactory for classId '%1'").arg(
170 QString::fromStdString(classId)));
171 }
172 return factItr->second;
173 }
174
175 template<typename Factory>
176 std::shared_ptr<Factory> MainFactory<Factory>::getFactory(const std::string& classId)
177 {
178 if (classId.empty())
179 {
180 std::cerr << "Trying to access to factory with empty name!" << std::endl;
181 throw InvalidConfiguration("Trying to access to factory with empty name!");
182 }
183 FactoryMapCItr factItr=m_factories.find(classId);
184 if (factItr==m_factories.end())
185 {
186 std::cerr << "2: No AbstractFactory for classId '" << classId << "' ! " << std::endl;
187 std::cerr << " Should be initialized at library loading time." << std::endl;
188 std::cerr << " Maybe you forgot to link with the library defining this classId ?" << std::endl;
189 std::cerr << " Or you forgot to define it as a plugin using, in its CMakeLists.txt," << std::endl;
190 std::cerr << " DECLARE_LIMA_PLUGIN, instead of add_library ?" << std::endl;
192 QString::fromStdString("No AbstractFactory for classId '%1'").arg(
193 QString::fromStdString(classId)));
194 }
195 return factItr->second;
196 }
197
198 template<typename Factory>
200 const std::string& classId,
201 Factory* fact)
202 {
203 // std::cerr << "register factory for classId '" << classId << "'" << std::endl;
204 if (m_factories.find(classId)!=m_factories.end())
205 {
206 std::cerr << "Factory for classId '" << classId << "' already exists do not replace it." << std::endl;
207 // throw InvalidConfiguration();
208 }
209 else
210 {
211 m_factories[classId]= std::shared_ptr<Factory>(fact);
212 }
213 }
214
215 template<typename Factory>
216 std::deque<std::string> MainFactory<Factory>::getRegisteredFactories() const
217 {
218 std::deque<std::string> result;
219 for (FactoryMapCItr factItr=m_factories.begin();
220 factItr!=m_factories.end();
221 factItr++)
222 {
223 result.push_back(factItr->first);
224 }
225 return result;
226 }
227
228} // Lima
229
230#endif
Use this exception to signal an error in one of the configuration files.
Definition LimaCommon.h:345
The main LIMA exception class.
Definition LimaCommon.h:262
static MainFactoryMap & get()
std::deque< std::string > getRegisteredFactories() const
show registered classId
static const MainFactory< Factory > & single()
const singleton accessor
const std::shared_ptr< Factory > getFactory(const std::string &classId) const
create an Object using the appropriate registered factory.
static MainFactory< Factory > & changeable()
singleton accessor
void registerFactory(const std::string &classId, Factory *fact)
register a factory with an id
virtual ~MainFactory()
static void cleanup()
destroy factory and all registered factories
originally detectGroupConfigurationStructure.h in detectlibraries
NAUTITIA.
launch exception related to the configuration file parsing