00001 /*************************************************************************** 00002 * (c) Jürgen Riegel (juergen.riegel@web.de) 2002 * 00003 * * 00004 * This file is part of the FreeCAD CAx development system. * 00005 * * 00006 * This program is free software; you can redistribute it and/or modify * 00007 * it under the terms of the GNU Library General Public License (LGPL) * 00008 * as published by the Free Software Foundation; either version 2 of * 00009 * the License, or (at your option) any later version. * 00010 * for detail see the LICENCE text file. * 00011 * * 00012 * FreeCAD is distributed in the hope that it will be useful, * 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 00015 * GNU Library General Public License for more details. * 00016 * * 00017 * You should have received a copy of the GNU Library General Public * 00018 * License along with FreeCAD; if not, write to the Free Software * 00019 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 * 00020 * USA * 00021 * * 00022 * Juergen Riegel 2002 * 00023 ***************************************************************************/ 00024 00025 00026 #ifndef BASE_FACTORY_H 00027 #define BASE_FACTORY_H 00028 00029 #include<typeinfo> 00030 #include<string> 00031 #include<map> 00032 #include<list> 00033 #include"../FCConfig.h" 00034 00035 00036 namespace Base 00037 { 00038 00040 class BaseExport AbstractProducer 00041 { 00042 public: 00043 AbstractProducer() {} 00044 virtual ~AbstractProducer() {} 00046 virtual void* Produce (void) const = 0; 00047 }; 00048 00049 00050 00057 class BaseExport Factory 00058 { 00059 public: 00061 void AddProducer (const char* sClassName, AbstractProducer *pcProducer); 00063 bool CanProduce(const char* sClassName) const; 00065 std::list<std::string> CanProduce() const; 00066 00067 protected: 00069 void* Produce (const char* sClassName) const; 00070 std::map<const std::string, AbstractProducer*> _mpcProducers; 00072 Factory (void){} 00074 virtual ~Factory (); 00075 }; 00076 00077 // -------------------------------------------------------------------- 00078 00081 class BaseExport ScriptFactorySingleton : public Factory 00082 { 00083 public: 00084 static ScriptFactorySingleton& Instance(void); 00085 static void Destruct (void); 00086 00087 const char* ProduceScript (const char* sScriptName) const; 00088 00089 private: 00090 static ScriptFactorySingleton* _pcSingleton; 00091 00092 ScriptFactorySingleton(){} 00093 ~ScriptFactorySingleton(){} 00094 }; 00095 00096 inline ScriptFactorySingleton& ScriptFactory(void) 00097 { 00098 return ScriptFactorySingleton::Instance(); 00099 } 00100 00101 // -------------------------------------------------------------------- 00102 00107 class BaseExport ScriptProducer: public AbstractProducer 00108 { 00109 public: 00111 ScriptProducer (const char* name, const char* script) : mScript(script) 00112 { 00113 ScriptFactorySingleton::Instance().AddProducer(name, this); 00114 } 00115 00116 virtual ~ScriptProducer (void){} 00117 00119 virtual void* Produce (void) const 00120 { 00121 return (void*)mScript; 00122 } 00123 00124 private: 00125 const char* mScript; 00126 }; 00127 00128 } //namespace Base 00129 00130 00131 #endif 00132