ParameterizedAbstractFactory.h
Go to the documentation of this file.
1 /* Copyright (C) 2008 National Institute For Space Research (INPE) - Brazil.
2 
3  This file is part of the TerraLib - a Framework for building GIS enabled applications.
4 
5  TerraLib is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published by
7  the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  TerraLib is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with TerraLib. See COPYING. If not, write to
17  TerraLib Team at <terralib-team@terralib.org>.
18  */
19 
20 /*!
21  \file terralib/common/ParameterizedAbstractFactory.h
22 
23  \brief A class that defines the basic interface of an abstract factory that makes use of parameters to construct the objects.
24  */
25 
26 #ifndef __TERRALIB_COMMON_INTERNAL_PARAMETERIZEDABSTRACTFACTORY_H
27 #define __TERRALIB_COMMON_INTERNAL_PARAMETERIZEDABSTRACTFACTORY_H
28 
29 // TerraLib
30 #include "FactoryDictionary.h"
31 
32 namespace te
33 {
34  namespace common
35  {
36 
37  /*!
38  \class ParameterizedAbstractFactory
39 
40  \brief This class defines the basic interface of an abstract factory that makes use of parameters to construct the objects.
41 
42  The parameterized abstract factory keeps an internal dictionary for
43  accessing its concrete factories. A concrete factory is identified
44  by a key of type TFACTORYKEY. This type must be copy constructible.
45 
46  It will create objects of type TPRODUCT and will pass
47  parameters of type TPARAM to their constructors.
48 
49  Note that TPRODUCT is the object type that
50  the factory knows how to create. The TPARAM type is the type
51  of parameters accepted by the objects being constructed. In order
52  to create your abstract factory, the concrete
53  classes will have to extend the build method.
54  When an object from a concrete factory is created, it is automatically
55  registered in the abstract factory.
56 
57  If you do not need to initialize the objects created by the factories, try to use
58  a derived class from AbstractFactory instead.
59 
60  \ingroup common
61 
62  \sa AbstractFactory, FactoryDictionary
63  */
64  template<class TPRODUCT, class TFACTORYKEY, class TPARAM, class TKEYCOMPARE=std::less<TFACTORYKEY> >
66  {
67  public:
68 
70 
71  friend class FactoryDictionary<ParameterizedAbstractFactory<TPRODUCT, TFACTORYKEY, TPARAM, TKEYCOMPARE>, TFACTORYKEY, TKEYCOMPARE>;
72 
73  /*!
74  \brief It returns the factory key associated to the concreate factory.
75 
76  \return The factory key associated to the concreate factory.
77  */
78  const TFACTORYKEY& getKey() const;
79 
80  /*!
81  \brief It creates an object with the appropriated factory.
82 
83  \param factoryKey A key that identifies the factory used to build the object.
84  \param p The parameter to be passed to the object being created.
85 
86  \return It returns a new object created by the given factory.
87 
88  \exception Exception If the concrete factory is not specified or the object can not be built for any reason this methiod may throws an exception.
89 
90  \note The caller of this method must release the memory pointed by the returned pointer.
91  */
92  static TPRODUCT* make(const TFACTORYKEY& factoryKey, TPARAM p);
93 
94  /*!
95  \brief It returns a reference to the internal dictionary.
96 
97  \return A reference to the internal dictionary.
98  */
99  static dictionary_type& getDictionary();
100 
101  protected:
102 
103  /*!
104  \brief Concrete factories (derived from this one) must implement this method in order to create objects.
105 
106  \param p The parameter to be passed to the object being created.
107 
108  \return It returns an object created by the concrete factory.
109  */
110  virtual TPRODUCT* build(TPARAM p) = 0;
111 
112  /*!
113  \brief It creates the factory.
114 
115  \param factoryKey The key that identifies the factory.
116  */
117  ParameterizedAbstractFactory(const TFACTORYKEY& factoryKey);
118 
119  /*!
120  \brief Virtual destructor.
121 
122  \note It will automatically unregister the factory from the dictionary.
123  */
125 
126  private:
127 
128  /** @name Copy Constructor and Assignment Operator
129  * Copy constructor and assignment operator not allowed.
130  */
131  //@{
132 
133  /*!
134  \brief Copy constructor not allowed.
135 
136  \param rhs The right-hand-side copy that would be used to copy from.
137  */
139 
140  /*!
141  \brief Assignment operator not allowed.
142 
143  \param rhs The right-hand-side copy that would be used to copy from.
144  */
146 
147  //@}
148 
149  protected:
150 
151  TFACTORYKEY m_factoryKey; //!< The key that identifies the concrete factory: it will be used for unregistering the factory during destruction.
152  };
153 
154  template<class TPRODUCT, class TFACTORYKEY, class TPARAM, class TKEYCOMPARE> inline
156  {
157  return m_factoryKey;
158  }
159 
160  template<class TPRODUCT, class TFACTORYKEY, class TPARAM, class TKEYCOMPARE> inline
162  TPARAM p)
163  {
164  ParameterizedAbstractFactory<TPRODUCT, TFACTORYKEY, TPARAM, TKEYCOMPARE>* factory = getDictionary().find(factoryKey);
165 
166  if(factory)
167  return factory->build(p);
168 
169  throw Exception(TE_TR("Concrete factory not found!"));
170  }
171 
172  template<class TPRODUCT, class TFACTORYKEY, class TPARAM, class TKEYCOMPARE> inline
174  {
175  static dictionary_type factoryDictionary; // Internal factory dictionary.
176  return factoryDictionary;
177  }
178 
179  template<class TPRODUCT, class TFACTORYKEY, class TPARAM, class TKEYCOMPARE> inline
181  : m_factoryKey(factoryKey)
182  {
183  getDictionary().insert(factoryKey, this);
184  }
185 
186  template<class TPRODUCT, class TFACTORYKEY, class TPARAM, class TKEYCOMPARE> inline
188  {
189  getDictionary().remove(m_factoryKey);
190  }
191 
192  } // end namespace common
193 } // end namespace te
194 
195 #endif // __TERRALIB_COMMON_INTERNAL_PARAMETERIZEDABSTRACTFACTORY_H
TFACTORYKEY m_factoryKey
The key that identifies the concrete factory: it will be used for unregistering the factory during de...
This class defines the basic interface of an abstract factory that makes use of parameters to constru...
void insert(const TFACTORYKEY &factoryKey, TFACTORY *factory)
It inserts a pointer to a factory and makes it associated to the factory key.
#define TE_TR(message)
It marks a string in order to get translated.
Definition: Translator.h:346
static dictionary_type & getDictionary()
It returns a reference to the internal dictionary.
FactoryDictionary< ParameterizedAbstractFactory< TPRODUCT, TFACTORYKEY, TPARAM, TKEYCOMPARE >, TFACTORYKEY, TKEYCOMPARE > dictionary_type
A dictionary for a Factory.
URI C++ Library.
ParameterizedAbstractFactory & operator=(const ParameterizedAbstractFactory &rhs)
Assignment operator not allowed.
virtual TPRODUCT * build(TPARAM p)=0
Concrete factories (derived from this one) must implement this method in order to create objects...
This class is designed to declare objects to be thrown as exceptions by TerraLib. ...
Definition: Exception.h:58
static TPRODUCT * make(const TFACTORYKEY &factoryKey, TPARAM p)
It creates an object with the appropriated factory.
const TFACTORYKEY & getKey() const
It returns the factory key associated to the concreate factory.
virtual ~ParameterizedAbstractFactory()
Virtual destructor.
This class represents a dictionary of factories.
ParameterizedAbstractFactory(const TFACTORYKEY &factoryKey)
It creates the factory.