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