00001 // Module: Log4CPLUS 00002 // File: factory.h 00003 // Created: 2/2002 00004 // Author: Tad E. Smith 00005 // 00006 // 00007 // Copyright 2002-2009 Tad E. Smith 00008 // 00009 // Licensed under the Apache License, Version 2.0 (the "License"); 00010 // you may not use this file except in compliance with the License. 00011 // You may obtain a copy of the License at 00012 // 00013 // http://www.apache.org/licenses/LICENSE-2.0 00014 // 00015 // Unless required by applicable law or agreed to in writing, software 00016 // distributed under the License is distributed on an "AS IS" BASIS, 00017 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00018 // See the License for the specific language governing permissions and 00019 // limitations under the License. 00020 00023 #ifndef LOG4CPLUS_SPI_FACTORY_HEADER_ 00024 #define LOG4CPLUS_SPI_FACTORY_HEADER_ 00025 00026 #include "dcmtk/oflog/config.h" 00027 #include "dcmtk/oflog/appender.h" 00028 #include "dcmtk/oflog/layout.h" 00029 #include "dcmtk/oflog/tstring.h" 00030 #include "dcmtk/oflog/helpers/property.h" 00031 #include "dcmtk/oflog/helpers/threads.h" 00032 #include "dcmtk/oflog/spi/filter.h" 00033 #include "dcmtk/oflog/spi/objreg.h" 00034 //#include <map> 00035 //#include <memory> 00036 //#include <vector> 00037 00038 00039 namespace log4cplus { 00040 namespace spi { 00041 00045 class LOG4CPLUS_EXPORT BaseFactory { 00046 public: 00047 virtual ~BaseFactory() = 0; 00048 00052 virtual log4cplus::tstring getTypeName() = 0; 00053 }; 00054 00055 00060 class LOG4CPLUS_EXPORT AppenderFactory : public BaseFactory { 00061 public: 00062 typedef Appender ProductType; 00063 typedef SharedAppenderPtr ProductPtr; 00064 00065 AppenderFactory(); 00066 virtual ~AppenderFactory() = 0; 00067 00071 virtual SharedAppenderPtr createObject(const log4cplus::helpers::Properties& props, log4cplus::tstring& error) = 0; 00072 }; 00073 00074 00075 00080 class LOG4CPLUS_EXPORT LayoutFactory : public BaseFactory { 00081 public: 00082 typedef Layout ProductType; 00083 typedef OFauto_ptr<Layout> ProductPtr; 00084 00085 LayoutFactory(); 00086 virtual ~LayoutFactory() = 0; 00087 00091 virtual OFauto_ptr<Layout> createObject(const log4cplus::helpers::Properties& props, log4cplus::tstring& error) = 0; 00092 }; 00093 00094 00095 00100 class LOG4CPLUS_EXPORT FilterFactory : public BaseFactory { 00101 public: 00102 typedef Filter ProductType; 00103 typedef FilterPtr ProductPtr; 00104 00105 FilterFactory(); 00106 virtual ~FilterFactory() = 0; 00107 00111 virtual FilterPtr createObject(const log4cplus::helpers::Properties& props, log4cplus::tstring& error) = 0; 00112 }; 00113 00114 00115 00125 template<class T> 00126 class LOG4CPLUS_EXPORT FactoryRegistry : private ObjectRegistryBase { 00127 public: 00128 typedef T product_type; 00129 00130 virtual ~FactoryRegistry() { 00131 clear(); 00132 } 00133 00134 // public methods 00139 bool put(OFauto_ptr<T> object) { 00140 bool putValResult = putVal(object->getTypeName(), object.get()); 00141 object.release(); 00142 return putValResult; 00143 } 00144 00149 T* get(const log4cplus::tstring& name) const { 00150 return OFstatic_cast(T*, getVal(name)); 00151 } 00152 00153 protected: 00154 virtual void deleteObject(void *object) const { 00155 delete OFstatic_cast(T*, object); 00156 } 00157 }; 00158 00159 00160 typedef FactoryRegistry<AppenderFactory> AppenderFactoryRegistry; 00161 typedef FactoryRegistry<LayoutFactory> LayoutFactoryRegistry; 00162 typedef FactoryRegistry<FilterFactory> FilterFactoryRegistry; 00163 00164 00168 LOG4CPLUS_EXPORT AppenderFactoryRegistry& getAppenderFactoryRegistry(); 00169 00173 LOG4CPLUS_EXPORT LayoutFactoryRegistry& getLayoutFactoryRegistry(); 00174 00178 LOG4CPLUS_EXPORT FilterFactoryRegistry& getFilterFactoryRegistry(); 00179 00180 } 00181 } 00182 00183 00184 #endif // LOG4CPLUS_SPI_FACTORY_HEADER_ 00185