Debian Codegraph @

src/Base/Factory.cpp

raw
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2
3 /***************************************************************************
4 * Copyright (c) 2002 Jürgen Riegel <[email protected]> *
5 * *
6 * This file is part of the FreeCAD CAx development system. *
7 * *
8 * This program is free software; you can redistribute it and/or modify *
9 * it under the terms of the GNU Library General Public License (LGPL) *
10 * as published by the Free Software Foundation; either version 2 of *
11 * the License, or (at your option) any later version. *
12 * for detail see the LICENCE text file. *
13 * *
14 * FreeCAD is distributed in the hope that it will be useful, *
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
17 * GNU Library General Public License for more details. *
18 * *
19 * You should have received a copy of the GNU Library General Public *
20 * License along with FreeCAD; if not, write to the Free Software *
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 *
22 * USA *
23 * *
24 ***************************************************************************/
25
26 #include <list>
27
28 #include "Factory.h"
29 #include "Console.h"
30
31 using namespace Base;
32
33
34 Factory::~Factory()
35 {
36 for (auto& it : _mpcProducers) {
37 delete it.second;
38 }
39 }
40
41 void* Factory::Produce(const char* sClassName) const
42 {
43 std::map<const std::string, AbstractProducer*>::const_iterator pProd;
44
45 pProd = _mpcProducers.find(sClassName);
46 if (pProd != _mpcProducers.end()) {
47 return pProd->second->Produce();
48 }
49
50 return nullptr;
51 }
52
53 void Factory::AddProducer(const char* sClassName, AbstractProducer* pcProducer)
54 {
55 _mpcProducers[sClassName] = pcProducer;
56 }
57
58 bool Factory::CanProduce(const char* sClassName) const
59 {
60 return (_mpcProducers.find(sClassName) != _mpcProducers.end());
61 }
62
63 std::list<std::string> Factory::CanProduce() const
64 {
65 std::list<std::string> lObjects;
66
67 for (const auto& it : _mpcProducers) {
68 lObjects.push_back(it.first);
69 }
70
71 return lObjects;
72 }
73
74 // ----------------------------------------------------
75
76 ScriptFactorySingleton* ScriptFactorySingleton::_pcSingleton = nullptr; // NOLINT
77
78
79 ScriptFactorySingleton& ScriptFactorySingleton::Instance()
80 {
81 if (!_pcSingleton) {
82 _pcSingleton = new ScriptFactorySingleton;
83 }
84 return *_pcSingleton;
85 }
86
87 void ScriptFactorySingleton::Destruct()
88 {
89 delete _pcSingleton;
90 _pcSingleton = nullptr;
91 }
92
93 const char* ScriptFactorySingleton::ProduceScript(const char* sScriptName) const
94 {
95 const char* script = static_cast<const char*>(Produce(sScriptName));
96
97 if (!script) {
98 #ifdef FC_DEBUG
99 Console().warning("\"%s\" is not registered\n", sScriptName);
100 #endif
101 return ""; // no data
102 }
103
104 return script;
105 }