1 // Module:  Log4CPLUS
  2 // File:    factory.h
  3 // Created: 2/2002
  4 // Author:  Tad E. Smith
  5 //
  6 //
  7 // Copyright (C) Tad E. Smith  All rights reserved.
  8 //
  9 // This software is published under the terms of the Apache Software
 10 // License version 1.1, a copy of which has been included with this
 11 // distribution in the LICENSE.APL file.
 12 //
 13 
 14 /** @file */
 15 
 16 #ifndef LOG4CPLUS_SPI_FACTORY_HEADER_
 17 #define LOG4CPLUS_SPI_FACTORY_HEADER_
 18 
 19 #include <log4cplus/config.h>
 20 #include <log4cplus/appender.h>
 21 #include <log4cplus/layout.h>
 22 #include <log4cplus/tstring.h>
 23 #include <log4cplus/helpers/property.h>
 24 #include <log4cplus/helpers/threads.h>
 25 #include <log4cplus/spi/filter.h>
 26 #include <log4cplus/spi/objectregistry.h>
 27 #include <map>
 28 #include <memory>
 29 #include <vector>
 30 
 31 
 32 namespace log4cplus {
 33     namespace spi {
 34 
 35         /**
 36          * This is the base class for all factories.
 37          */
 38         class LOG4CPLUS_EXPORT BaseFactory {
 39         public:
 40             virtual ~BaseFactory() {}
 41 
 42             virtual log4cplus::tstring getTypeName() = 0;
 43         };
 44 
 45 
 46         /**
 47          * This abstract class defines the "Factory" interface to create "Appender"
 48          * objects.
 49          */
 50         class LOG4CPLUS_EXPORT AppenderFactory : public BaseFactory {
 51         public:
 52             AppenderFactory(){}
 53             virtual ~AppenderFactory(){}
 54 
 55             /**
 56              * Create an "Appender" object.
 57              */
 58             virtual SharedAppenderPtr createObject(const log4cplus::helpers::Properties& props) = 0;
 59 
 60             /**
 61              * Returns the typename of the "Appender" objects this factory creates.
 62              */
 63             virtual log4cplus::tstring getTypeName() = 0;
 64         };
 65 
 66 
 67 
 68         /**
 69          * This abstract class defines the "Factory" interface to create "Layout"
 70          * objects.
 71          */
 72         class LOG4CPLUS_EXPORT LayoutFactory : public BaseFactory {
 73         public:
 74             LayoutFactory(){}
 75             virtual ~LayoutFactory(){}
 76 
 77             /**
 78              * Create a "Layout" object.
 79              */
 80             virtual std::auto_ptr<Layout> createObject(const log4cplus::helpers::Properties& props) = 0;
 81 
 82             /**
 83              * Returns the typename of the "Layout" objects this factory creates.
 84              */
 85             virtual log4cplus::tstring getTypeName() = 0;
 86         };
 87 
 88 
 89 
 90         /**
 91          * This abstract class defines the "Factory" interface to create "Appender"
 92          * objects.
 93          */
 94         class LOG4CPLUS_EXPORT FilterFactory : public BaseFactory {
 95         public:
 96             FilterFactory(){}
 97             virtual ~FilterFactory(){}
 98 
 99             /**
100              * Create a "Filter" object.
101              */
102             virtual FilterPtr createObject(const log4cplus::helpers::Properties& props) = 0;
103 
104             /**
105              * Returns the typename of the "Filter" objects this factory creates.
106              */
107             virtual log4cplus::tstring getTypeName() = 0;
108         };
109 
110 
111 
112         /**
113          * This template class is used as a "Factory Registry".  Objects are
114          * "entered" into the registry with a "name" using the 
115          * <code>put()</code> method.  (The registry then owns the object.)  
116          * These object can then be retrieved using the <code>get()</code> 
117          * method.
118          * <p>
119          * <b>Note:</b>  This class is Thread-safe.
120          */
121         template<class T>
122         class LOG4CPLUS_EXPORT FactoryRegistry : ObjectRegistryBase {
123         public:
124             virtual ~FactoryRegistry() {
125                 clear();
126             }
127 
128           // public methods
129             /**
130              * Used to enter an object into the registry.  (The registry now
131              *  owns <code>object</code>.)
132              */
133             bool put(std::auto_ptr<T> object) {
134                  bool putValResult = putVal(object->getTypeName(), object.get());
135                  object.release();
136                  return putValResult; 
137             }
138 
139             /**
140              * Used to retrieve an object from the registry.  (The registry
141              * owns the returned pointer.)
142              */
143             T* get(const log4cplus::tstring& name) const {
144                 return static_cast<T*>(getVal(name));
145             }
146 
147         protected:
148             virtual void deleteObject(void *object) const {
149                 delete static_cast<T*>(object);
150             }
151         };
152 
153 
154         typedef FactoryRegistry<AppenderFactory> AppenderFactoryRegistry;
155         typedef FactoryRegistry<LayoutFactory> LayoutFactoryRegistry;
156         typedef FactoryRegistry<FilterFactory> FilterFactoryRegistry;
157 
158 
159         /**
160          * Returns the "singleton" <code>AppenderFactoryRegistry</code>.
161          */
162         LOG4CPLUS_EXPORT AppenderFactoryRegistry& getAppenderFactoryRegistry();
163 
164         /**
165          * Returns the "singleton" <code>LayoutFactoryRegistry</code>.
166          */
167         LOG4CPLUS_EXPORT LayoutFactoryRegistry& getLayoutFactoryRegistry();
168 
169         /**
170          * Returns the "singleton" <code>FilterFactoryRegistry</code>.
171          */
172         LOG4CPLUS_EXPORT FilterFactoryRegistry& getFilterFactoryRegistry();
173 
174     }
175 }
176 
177 
178 #endif // LOG4CPLUS_SPI_FACTORY_HEADER_


syntax highlighted by Code2HTML, v. 0.9.1