log4cplus  1.1.0
factory.h
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 // Module:  Log4CPLUS
00003 // File:    factory.h
00004 // Created: 2/2002
00005 // Author:  Tad E. Smith
00006 //
00007 //
00008 // Copyright 2002-2010 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::auto_ptr<Layout> ProductPtr;
00088 
00089             LayoutFactory();
00090             virtual ~LayoutFactory() = 0;
00091 
00095             virtual std::auto_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 : ObjectRegistryBase {
00150         public:
00151             typedef T product_type;
00152 
00153             virtual ~FactoryRegistry() {
00154                 clear();
00155             }
00156 
00157           // public methods
00162             bool put(std::auto_ptr<T> object) {
00163                  bool putValResult = putVal(object->getTypeName(), object.get());
00164                  object.release();
00165                  return putValResult; 
00166             }
00167 
00172             T* get(const log4cplus::tstring& name) const {
00173                 return static_cast<T*>(getVal(name));
00174             }
00175 
00176         protected:
00177             virtual void deleteObject(void *object) const {
00178                 delete static_cast<T*>(object);
00179             }
00180         };
00181 
00182 
00183         typedef FactoryRegistry<AppenderFactory> AppenderFactoryRegistry;
00184         typedef FactoryRegistry<LayoutFactory> LayoutFactoryRegistry;
00185         typedef FactoryRegistry<FilterFactory> FilterFactoryRegistry;
00186         typedef FactoryRegistry<LocaleFactory> LocaleFactoryRegistry;
00187 
00188 
00192         LOG4CPLUS_EXPORT AppenderFactoryRegistry& getAppenderFactoryRegistry();
00193 
00197         LOG4CPLUS_EXPORT LayoutFactoryRegistry& getLayoutFactoryRegistry();
00198 
00202         LOG4CPLUS_EXPORT FilterFactoryRegistry& getFilterFactoryRegistry();
00203 
00207         LOG4CPLUS_EXPORT LocaleFactoryRegistry& getLocaleFactoryRegistry();
00208 
00209 
00210         template <typename ProductFactoryBase>
00211         class LocalFactoryBase
00212             : public ProductFactoryBase
00213         {
00214         public:
00215             LocalFactoryBase (tchar const * n)
00216                 : name (n)
00217             { }
00218 
00219             virtual log4cplus::tstring const & getTypeName() const
00220             {
00221                 return name;
00222             }
00223 
00224         private:
00225             log4cplus::tstring name;
00226         };
00227 
00228 
00229         template <typename LocalProduct, typename ProductFactoryBase>
00230         class FactoryTempl
00231             : public LocalFactoryBase<ProductFactoryBase>
00232         {
00233         public:
00234             typedef typename ProductFactoryBase::ProductPtr ProductPtr;
00235 
00236             FactoryTempl (tchar const * n)
00237                 : LocalFactoryBase<ProductFactoryBase> (n)
00238             { }
00239 
00240             virtual ProductPtr createObject (helpers::Properties const & props)
00241             {
00242                 return ProductPtr (new LocalProduct (props));
00243             }
00244         };
00245 
00246 
00247         #define LOG4CPLUS_REG_PRODUCT(reg, productprefix, productname, productns, productfact) \
00248         reg.put (                                                                              \
00249             std::auto_ptr<productfact> (                                                       \
00250                     new log4cplus::spi::FactoryTempl<productns productname, productfact> (     \
00251                     LOG4CPLUS_TEXT(productprefix)                                              \
00252                     LOG4CPLUS_TEXT(#productname))))
00253 
00254         #define LOG4CPLUS_REG_APPENDER(reg, appendername)                             \
00255         LOG4CPLUS_REG_PRODUCT (reg, "log4cplus::", appendername, log4cplus::,         \
00256             log4cplus::spi::AppenderFactory) 
00257 
00258         #define LOG4CPLUS_REG_LAYOUT(reg, layoutname)                                 \
00259         LOG4CPLUS_REG_PRODUCT (reg, "log4cplus::", layoutname, log4cplus::,           \
00260             log4cplus::spi::LayoutFactory)
00261 
00262         #define LOG4CPLUS_REG_FILTER(reg, filtername)                                 \
00263         LOG4CPLUS_REG_PRODUCT (reg, "log4cplus::spi::", filtername, log4cplus::spi::, \
00264             log4cplus::spi::FilterFactory)
00265 
00266         #define LOG4CPLUS_REG_LOCALE(reg, name, factory)            \
00267             reg.put (std::auto_ptr<log4cplus::spi::LocaleFactory> ( \
00268                     new factory (name)))
00269     } // namespace spi
00270 }
00271 
00272 
00273 #endif // LOG4CPLUS_SPI_FACTORY_HEADER_