# factory.h

Go to the documentation of this file.

    00001 // Module:  Log4CPLUS
    00002 // File:    factory.h
    00003 // Created: 2/2002
    00004 // Author:  Tad E. Smith
    00005 //
    00006 //
    00007 // Copyright (C) Tad E. Smith  All rights reserved.
    00008 //
    00009 // This software is published under the terms of the Apache Software
    00010 // License version 1.1, a copy of which has been included with this
    00011 // distribution in the LICENSE.APL file.
    00012 //
    00013
    00016 #ifndef LOG4CPLUS_SPI_FACTORY_HEADER_
    00017 #define LOG4CPLUS_SPI_FACTORY_HEADER_
    00018
    00019 #include <log4cplus/config.h>
    00020 #include <log4cplus/appender.h>
    00021 #include <log4cplus/layout.h>
    00022 #include <log4cplus/tstring.h>
    00023 #include <log4cplus/helpers/property.h>
    00024 #include <log4cplus/helpers/threads.h>
    00025 #include <log4cplus/spi/filter.h>
    00026 #include <log4cplus/spi/objectregistry.h>
    00027 #include <map>
    00028 #include <memory>
    00029 #include <vector>
    00030
    00031
    00032 namespace log4cplus {
    00033     namespace spi {
    00034
    00038         class LOG4CPLUS_EXPORT BaseFactory {
    00039         public:
    00040             virtual ~BaseFactory() {}
    00041
    00042             virtual log4cplus::tstring getTypeName() = 0;
    00043         };
    00044
    00045
    00050         class LOG4CPLUS_EXPORT AppenderFactory : public BaseFactory {
    00051         public:
    00052             AppenderFactory(){}
    00053             virtual ~AppenderFactory(){}
    00054
    00058             virtual SharedAppenderPtr createObject(const log4cplus::helpers::Properties& props) = 0;
    00059
    00063             virtual log4cplus::tstring getTypeName() = 0;
    00064         };
    00065
    00066
    00067
    00072         class LOG4CPLUS_EXPORT LayoutFactory : public BaseFactory {
    00073         public:
    00074             LayoutFactory(){}
    00075             virtual ~LayoutFactory(){}
    00076
    00080             virtual std::auto_ptr<Layout> createObject(const log4cplus::helpers::Properties& props) = 0;
    00081
    00085             virtual log4cplus::tstring getTypeName() = 0;
    00086         };
    00087
    00088
    00089
    00094         class LOG4CPLUS_EXPORT FilterFactory : public BaseFactory {
    00095         public:
    00096             FilterFactory(){}
    00097             virtual ~FilterFactory(){}
    00098
    00102             virtual FilterPtr createObject(const log4cplus::helpers::Properties& props) = 0;
    00103
    00107             virtual log4cplus::tstring getTypeName() = 0;
    00108         };
    00109
    00110
    00111
    00121         template<class T>
    00122         class LOG4CPLUS_EXPORT FactoryRegistry : ObjectRegistryBase {
    00123         public:
    00124             virtual ~FactoryRegistry() {
    00125                 clear();
    00126             }
    00127
    00128           // public methods
    00133             bool put(std::auto_ptr<T> object) {
    00134                  bool putValResult = putVal(object->getTypeName(), object.get());
    00135                  object.release();
    00136                  return putValResult;
    00137             }
    00138
    00143             T* get(const log4cplus::tstring& name) const {
    00144                 return static_cast<T*>(getVal(name));
    00145             }
    00146
    00147         protected:
    00148             virtual void deleteObject(void *object) const {
    00149                 delete static_cast<T*>(object);
    00150             }
    00151         };
    00152
    00153
    00154         typedef FactoryRegistry<AppenderFactory> AppenderFactoryRegistry;
    00155         typedef FactoryRegistry<LayoutFactory> LayoutFactoryRegistry;
    00156         typedef FactoryRegistry<FilterFactory> FilterFactoryRegistry;
    00157
    00158
    00162         LOG4CPLUS_EXPORT AppenderFactoryRegistry& getAppenderFactoryRegistry();
    00163
    00167         LOG4CPLUS_EXPORT LayoutFactoryRegistry& getLayoutFactoryRegistry();
    00168
    00172         LOG4CPLUS_EXPORT FilterFactoryRegistry& getFilterFactoryRegistry();
    00173
    00174     }
    00175 }
    00176
    00177
    00178 #endif // LOG4CPLUS_SPI_FACTORY_HEADER_
    00179