log4cplus
2.0.0
|
00001 // -*- C++ -*- 00002 // Module: Log4CPLUS 00003 // File: factory.h 00004 // Created: 2/2002 00005 // Author: Tad E. Smith 00006 // 00007 // 00008 // Copyright 2002-2015 Tad E. Smith 00009 // 00010 // Licensed under the Apache License, Version 2.0 (the "License"); 00011 // you may not use this file except in compliance with the License. 00012 // You may obtain a copy of the License at 00013 // 00014 // http://www.apache.org/licenses/LICENSE-2.0 00015 // 00016 // Unless required by applicable law or agreed to in writing, software 00017 // distributed under the License is distributed on an "AS IS" BASIS, 00018 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00019 // See the License for the specific language governing permissions and 00020 // limitations under the License. 00021 00024 #ifndef LOG4CPLUS_SPI_FACTORY_HEADER_ 00025 #define LOG4CPLUS_SPI_FACTORY_HEADER_ 00026 00027 #include <log4cplus/config.hxx> 00028 00029 #if defined (LOG4CPLUS_HAVE_PRAGMA_ONCE) 00030 #pragma once 00031 #endif 00032 00033 #include <log4cplus/appender.h> 00034 #include <log4cplus/layout.h> 00035 #include <log4cplus/tstring.h> 00036 #include <log4cplus/spi/filter.h> 00037 #include <log4cplus/spi/objectregistry.h> 00038 #include <memory> 00039 #include <vector> 00040 #include <locale> 00041 00042 00043 namespace log4cplus { 00044 namespace spi { 00045 00049 class LOG4CPLUS_EXPORT BaseFactory { 00050 public: 00051 virtual ~BaseFactory() = 0; 00052 00056 virtual log4cplus::tstring const & getTypeName() const = 0; 00057 }; 00058 00059 00064 class LOG4CPLUS_EXPORT AppenderFactory : public BaseFactory { 00065 public: 00066 typedef Appender ProductType; 00067 typedef SharedAppenderPtr ProductPtr; 00068 00069 AppenderFactory(); 00070 virtual ~AppenderFactory() = 0; 00071 00075 virtual SharedAppenderPtr createObject(const log4cplus::helpers::Properties& props) = 0; 00076 }; 00077 00078 00079 00084 class LOG4CPLUS_EXPORT LayoutFactory : public BaseFactory { 00085 public: 00086 typedef Layout ProductType; 00087 typedef std::unique_ptr<Layout> ProductPtr; 00088 00089 LayoutFactory(); 00090 virtual ~LayoutFactory() = 0; 00091 00095 virtual std::unique_ptr<Layout> createObject(const log4cplus::helpers::Properties& props) = 0; 00096 }; 00097 00098 00099 00104 class LOG4CPLUS_EXPORT FilterFactory : public BaseFactory { 00105 public: 00106 typedef Filter ProductType; 00107 typedef FilterPtr ProductPtr; 00108 00109 FilterFactory(); 00110 virtual ~FilterFactory() = 0; 00111 00115 virtual FilterPtr createObject(const log4cplus::helpers::Properties& props) = 0; 00116 }; 00117 00118 00123 class LOG4CPLUS_EXPORT LocaleFactory 00124 : public BaseFactory 00125 { 00126 public: 00127 typedef std::locale ProductType; 00128 typedef std::locale ProductPtr; 00129 00130 LocaleFactory(); 00131 virtual ~LocaleFactory() = 0; 00132 00134 virtual ProductPtr createObject ( 00135 const log4cplus::helpers::Properties & props) = 0; 00136 }; 00137 00138 00148 template<class T> 00149 class LOG4CPLUS_EXPORT FactoryRegistry 00150 : public ObjectRegistryBase 00151 { 00152 public: 00153 typedef T product_type; 00154 00155 virtual ~FactoryRegistry() { 00156 clear(); 00157 } 00158 00159 // public methods 00164 bool put(std::unique_ptr<T> object) { 00165 bool putValResult = putVal(object->getTypeName(), object.get()); 00166 object.release(); 00167 return putValResult; 00168 } 00169 00174 T* get(const log4cplus::tstring& name) const { 00175 return static_cast<T*>(getVal(name)); 00176 } 00177 00178 protected: 00179 virtual void deleteObject(void *object) const { 00180 delete static_cast<T*>(object); 00181 } 00182 }; 00183 00184 00185 typedef FactoryRegistry<AppenderFactory> AppenderFactoryRegistry; 00186 typedef FactoryRegistry<LayoutFactory> LayoutFactoryRegistry; 00187 typedef FactoryRegistry<FilterFactory> FilterFactoryRegistry; 00188 typedef FactoryRegistry<LocaleFactory> LocaleFactoryRegistry; 00189 00190 00194 LOG4CPLUS_EXPORT AppenderFactoryRegistry& getAppenderFactoryRegistry(); 00195 00199 LOG4CPLUS_EXPORT LayoutFactoryRegistry& getLayoutFactoryRegistry(); 00200 00204 LOG4CPLUS_EXPORT FilterFactoryRegistry& getFilterFactoryRegistry(); 00205 00209 LOG4CPLUS_EXPORT LocaleFactoryRegistry& getLocaleFactoryRegistry(); 00210 00211 00212 template <typename ProductFactoryBase> 00213 class LocalFactoryBase 00214 : public ProductFactoryBase 00215 { 00216 public: 00217 LocalFactoryBase (tchar const * n) 00218 : name (n) 00219 { } 00220 00221 virtual log4cplus::tstring const & getTypeName() const 00222 { 00223 return name; 00224 } 00225 00226 private: 00227 log4cplus::tstring name; 00228 }; 00229 00230 00231 template <typename LocalProduct, typename ProductFactoryBase> 00232 class FactoryTempl 00233 : public LocalFactoryBase<ProductFactoryBase> 00234 { 00235 public: 00236 typedef typename ProductFactoryBase::ProductPtr ProductPtr; 00237 00238 FactoryTempl (tchar const * n) 00239 : LocalFactoryBase<ProductFactoryBase> (n) 00240 { } 00241 00242 virtual ProductPtr createObject (helpers::Properties const & props) 00243 { 00244 return ProductPtr (new LocalProduct (props)); 00245 } 00246 }; 00247 00248 00249 #define LOG4CPLUS_REG_PRODUCT(reg, productprefix, productname, productns, productfact) \ 00250 reg.put ( \ 00251 std::unique_ptr<productfact> ( \ 00252 new log4cplus::spi::FactoryTempl<productns productname, productfact> ( \ 00253 LOG4CPLUS_TEXT(productprefix) \ 00254 LOG4CPLUS_TEXT(#productname)))) 00255 00256 #define LOG4CPLUS_REG_APPENDER(reg, appendername) \ 00257 LOG4CPLUS_REG_PRODUCT (reg, "log4cplus::", appendername, log4cplus::, \ 00258 log4cplus::spi::AppenderFactory) 00259 00260 #define LOG4CPLUS_REG_LAYOUT(reg, layoutname) \ 00261 LOG4CPLUS_REG_PRODUCT (reg, "log4cplus::", layoutname, log4cplus::, \ 00262 log4cplus::spi::LayoutFactory) 00263 00264 #define LOG4CPLUS_REG_FILTER(reg, filtername) \ 00265 LOG4CPLUS_REG_PRODUCT (reg, "log4cplus::spi::", filtername, log4cplus::spi::, \ 00266 log4cplus::spi::FilterFactory) 00267 00268 #define LOG4CPLUS_REG_LOCALE(reg, name, factory) \ 00269 reg.put (std::unique_ptr<log4cplus::spi::LocaleFactory> ( \ 00270 new factory (name))) 00271 } // namespace spi 00272 } 00273 00274 00275 #endif // LOG4CPLUS_SPI_FACTORY_HEADER_