All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
UnitsOfMeasureManager.h
Go to the documentation of this file.
1 /* Copyright (C) 2001-2009 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/UnitsOfMeasureManager.h
22 
23  \brief A singleton class for dealing with units-of-measure.
24  */
25 
26 #ifndef __TERRALIB_COMMON_INTERNAL_UNITSOFMEASUREMANAGER_H
27 #define __TERRALIB_COMMON_INTERNAL_UNITSOFMEASUREMANAGER_H
28 
29 // TerraLib
30 #include "Singleton.h"
31 #include "UnitOfMeasure.h"
32 
33 // STL
34 #include <map>
35 #include <string>
36 #include <vector>
37 
38 namespace te
39 {
40  namespace common
41  {
42 
43  /*!
44  \class UnitsOfMeasureManager
45 
46  \brief A singleton class for dealing with units of measure in the TerraLib environment.
47 
48  This class manages a unique catalogue of known units of measure within the TerraLib environment.
49 
50  \ingroup common
51 
52  \sa UnitOfMeasure
53  */
54  class TECOMMONEXPORT UnitsOfMeasureManager : public te::common::Singleton<UnitsOfMeasureManager>
55  {
57 
58  public:
59 
60  typedef std::map<unsigned int, UnitOfMeasurePtr>::const_iterator const_iterator;
61  typedef std::map<unsigned int, UnitOfMeasurePtr>::iterator iterator;
62 
63  /*!
64  \brief Inserts a new unit of measure to be managed.
65 
66  \param uom Pointer to unit of measure to be managed. Shouldn't be null. The manager takes the pointer ownership.
67 
68  \exception Exception If the unit is already managed.
69  */
70  void insert(UnitOfMeasurePtr& uom);
71 
72  /*!
73  \brief Inserts a new unit of measure to be managed and its alternative names.
74 
75  \param uom Pointer to unit of measure to be managed. Shouldn't be null. The manager takes the pointer ownership.
76  \param alternativeNames The list of alternative (or auxiliary) names for identifying an unit of measure.
77 
78  \exception Exception If the unit is already managed.
79  */
80  void insert(UnitOfMeasurePtr& uom, const std::vector<std::string>& alternativeNames);
81 
82  /*!
83  \brief Removes a unit of measure from the manager.
84 
85  \param uom Pointer to unit of measure to be managed. Shouldn't be null. The manager will delete the pointer.
86 
87  \exception te::common::Exception If the unit is unknown to the manager.
88  */
89  void remove(UnitOfMeasurePtr& uom);
90 
91  /*!
92  \brief Returns a unit of measure identified by its identificaton.
93 
94  \param id The unit identification.
95 
96  \return A pointer unit of measure if found; null pointer otherwise.
97  */
98  UnitOfMeasurePtr find(unsigned int id) const;
99 
100  /*!
101  \brief Returns a unit of measure identified by its name.
102 
103  \param id The unit identification.
104 
105  \return A unit of measure pointer if found; null pointer otherwise.
106  */
107  UnitOfMeasurePtr find(const std::string& name) const;
108 
109  /*!
110  \brief Returns a unit of measure identified by its symbol.
111 
112  \param id The unit symbol.
113 
114  \return A unit of measure pointer if found; null pointer otherwise.
115  */
116  UnitOfMeasurePtr findBySymbol(const std::string& symbol) const;
117 
118  /*! \brief Removes all units from the catalogue. */
119  void clear();
120 
121  /*!
122  \brief Retrieves the alternative names for a unit of measure.
123 
124  \param uom Pointer to the unit of measure. Shouldn't be null;
125 
126  \param altNames Reference to a vector of strings to return the alternative names (output).
127  */
128  void getNames(UnitOfMeasurePtr& uom,std::vector<std::string>& names) const;
129 
130  /*!
131  \brief Calculates a multiplicative factor to convert from a given unit to its base unit and vice-versa.
132 
133  \param unitFromName A unit of measure name.
134  \param unitToName The name of unit to be converted to.
135 
136  \return A multiplicative factor to convert from a given unit to another.
137 
138  \exception Exception If the conversion is not possible.
139  */
140  double getConversion(const std::string& unitFromName, const std::string& unitToName) const;
141 
142  /*!
143  \brief It initializes the list of well kown units of measure.
144 
145  The list is read from the JSON file TERRALIB_DIR/resources/json/uom.json.
146 
147  \exception Exception It throws an exception if it is not possible to initialize the UOM list.
148  */
149  void init();
150 
151  /*!
152  \brief It returns an iterator to the beginning of the conteiner.
153 
154  \return An iterator to the beginning of the conteiner.
155  */
156  const_iterator begin() const;
157 
158  /*!
159  \brief It returns an iterator to the beginning of the conteiner.
160 
161  \return An iterator to the beginning of the conteiner.
162  */
163  iterator begin();
164 
165  /*!
166  \brief It returns an iterator to the end of the conteiner.
167 
168  \return An iterator to the beginning of the conteiner.
169  */
170  const_iterator end() const;
171 
172  /*!
173  \brief It returns an iterator to the end of the conteiner.
174 
175  \return An iterator to the beginning of the conteiner.
176  */
177  iterator end();
178 
179  /*!
180  \brief It returns the number of units in the manager.
181 
182  \return The number of units in the manager.
183  */
184  std::size_t size() const;
185 
186  protected:
187 
188  /*! \brief It initializes the Singleton. */
190 
191  /*! \brief Destructor. */
193 
194  private:
195 
196  std::map<unsigned int, UnitOfMeasurePtr> m_uoms;
197  std::map<std::string, unsigned int> m_uomsIdxByName;
198 
199  };
200 
201  inline std::size_t UnitsOfMeasureManager::size() const
202  {
203  return m_uoms.size();
204  }
205 
207  {
208  return m_uoms.begin();
209  }
210 
212  {
213  return m_uoms.begin();
214  }
215 
217  {
218  return m_uoms.end();
219  }
220 
222  {
223  return m_uoms.end();
224  }
225 
226 
227  } // end namespace common
228 } // end namespace te
229 
230 #endif // __TERRALIB_COMMON_INTERNAL_UNITSOFMEASUREMANAGER_H
231 
Template support for singleton pattern.
Definition: Singleton.h:100
std::map< unsigned int, UnitOfMeasurePtr >::iterator iterator
mydialect insert("+", new te::da::BinaryOpEncoder("+"))
A singleton class for dealing with units of measure in the TerraLib environment.
#define TECOMMONEXPORT
You can use this macro in order to export/import classes and functions from this module.
Definition: Config.h:455
std::map< std::string, unsigned int > m_uomsIdxByName
A class for representing a unit of measure.
boost::shared_ptr< UnitOfMeasure > UnitOfMeasurePtr
const_iterator begin() const
It returns an iterator to the beginning of the conteiner.
std::map< unsigned int, UnitOfMeasurePtr >::const_iterator const_iterator
const_iterator end() const
It returns an iterator to the end of the conteiner.
Template support for singleton pattern.
std::map< unsigned int, UnitOfMeasurePtr > m_uoms
std::size_t size() const
It returns the number of units in the manager.