| 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 |
|
| 27 |
#ifndef BASE_FACTORY_H |
| 28 |
#define BASE_FACTORY_H |
| 29 |
|
| 30 |
#include <list> |
| 31 |
#include <map> |
| 32 |
#include <string> |
| 33 |
#ifndef FC_GLOBAL_H |
| 34 |
# include <FCGlobal.h> |
| 35 |
#endif |
| 36 |
|
| 37 |
|
| 38 |
namespace Base |
| 39 |
{ |
| 40 |
|
| 41 |
/// Abstract base class of all producers |
| 42 |
class BaseExport AbstractProducer |
| 43 |
{ |
| 44 |
public: |
| 45 |
AbstractProducer() = default; |
| 46 |
virtual ~AbstractProducer() = default; |
| 47 |
/// overwritten by a concrete producer to produce the needed object |
| 48 |
virtual void* Produce() const = 0; |
| 49 |
|
| 50 |
FC_DISABLE_COPY_MOVE(AbstractProducer) |
| 51 |
}; |
| 52 |
|
| 53 |
|
| 54 |
/** Base class of all factories |
| 55 |
* This class has the purpose to produce instances of classes at runtime |
| 56 |
* that are unknown at compile time. It holds a map of so called |
| 57 |
* producers which are able to produce an instance of a special class. |
| 58 |
* Producer can be registered at runtime through e.g. application modules |
| 59 |
*/ |
| 60 |
class BaseExport Factory |
| 61 |
{ |
| 62 |
public: |
| 63 |
/// Adds a new prducer instance |
| 64 |
void AddProducer(const char* sClassName, AbstractProducer* pcProducer); |
| 65 |
/// returns true if there is a producer for this class registered |
| 66 |
bool CanProduce(const char* sClassName) const; |
| 67 |
/// returns a list of all registered producer |
| 68 |
std::list<std::string> CanProduce() const; |
| 69 |
/// destruction |
| 70 |
virtual ~Factory(); |
| 71 |
|
| 72 |
FC_DISABLE_COPY_MOVE(Factory) |
| 73 |
|
| 74 |
protected: |
| 75 |
/// produce a class with the given name |
| 76 |
void* Produce(const char* sClassName) const; |
| 77 |
/// construction |
| 78 |
Factory() = default; |
| 79 |
|
| 80 |
std::map<const std::string, AbstractProducer*> _mpcProducers; |
| 81 |
}; |
| 82 |
|
| 83 |
// -------------------------------------------------------------------- |
| 84 |
|
| 85 |
/** The ScriptFactorySingleton singleton |
| 86 |
*/ |
| 87 |
class BaseExport ScriptFactorySingleton: public Factory // NOLINT |
| 88 |
{ |
| 89 |
public: |
| 90 |
static ScriptFactorySingleton& Instance(); |
| 91 |
static void Destruct(); |
| 92 |
|
| 93 |
const char* ProduceScript(const char* sScriptName) const; |
| 94 |
|
| 95 |
FC_DISABLE_COPY_MOVE(ScriptFactorySingleton) |
| 96 |
|
| 97 |
private: |
| 98 |
static ScriptFactorySingleton* _pcSingleton; // NOLINT |
| 99 |
|
| 100 |
protected: |
| 101 |
ScriptFactorySingleton() = default; |
| 102 |
~ScriptFactorySingleton() override = default; |
| 103 |
}; |
| 104 |
|
| 105 |
inline ScriptFactorySingleton& ScriptFactory() |
| 106 |
{ |
| 107 |
return ScriptFactorySingleton::Instance(); |
| 108 |
} |
| 109 |
|
| 110 |
// -------------------------------------------------------------------- |
| 111 |
|
| 112 |
/** Script Factory |
| 113 |
* This class produce Scripts. |
| 114 |
* @see Factory |
| 115 |
*/ |
| 116 |
class BaseExport ScriptProducer: public AbstractProducer |
| 117 |
{ |
| 118 |
public: |
| 119 |
/// Constructor |
| 120 |
ScriptProducer(const char* name, const char* script) |
| 121 |
: mScript(script) |
| 122 |
{ |
| 123 |
ScriptFactorySingleton::Instance().AddProducer(name, this); |
| 124 |
} |
| 125 |
|
| 126 |
~ScriptProducer() override = default; |
| 127 |
|
| 128 |
/// Produce an instance |
| 129 |
void* Produce() const override |
| 130 |
{ |
| 131 |
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast) |
| 132 |
return const_cast<char*>(mScript); |
| 133 |
} |
| 134 |
|
| 135 |
FC_DISABLE_COPY_MOVE(ScriptProducer) |
| 136 |
|
| 137 |
private: |
| 138 |
const char* mScript; |
| 139 |
}; |
| 140 |
|
| 141 |
} // namespace Base |
| 142 |
|
| 143 |
|
| 144 |
#endif |