Loading...
Searching...
No Matches
UnitsOfMeasureManager.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/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
38namespace 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 */
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 */
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 Check if two units of measure are compatible.
132
133 Check if a unit can be converted to another.
134
135 \param unitFromName A unit of measure name.
136 \param unitToName The name of unit to be converted to.
137
138 \return True if the UnitFrom can be converted to UnitTo false otherwise.
139 */
140 bool areConvertible(const std::string& unitFromName, const std::string& unitToName) const;
141
142 /*!
143 \brief Calculates a multiplicative factor to convert from a given unit to its base unit and vice-versa.
144
145 \param unitFromName A unit of measure name.
146 \param unitToName The name of unit to be converted to.
147
148 \return A multiplicative factor to convert from a given unit to another.
149
150 \exception Exception If the conversion is not possible.
151 */
152 double getConversion(const std::string& unitFromName, const std::string& unitToName) const;
153
154 /*!
155 \brief It initializes the list of well kown units of measure.
156
157 The list is read from the JSON file TERRALIB_DIR/resources/json/uom.json.
158
159 \exception Exception It throws an exception if it is not possible to initialize the UOM list.
160 */
161 void init();
162
163 /*!
164 \brief It returns an iterator to the beginning of the conteiner.
165
166 \return An iterator to the beginning of the conteiner.
167 */
168 const_iterator begin() const;
169
170 /*!
171 \brief It returns an iterator to the beginning of the conteiner.
172
173 \return An iterator to the beginning of the conteiner.
174 */
175 iterator begin();
176
177 /*!
178 \brief It returns an iterator to the end of the conteiner.
179
180 \return An iterator to the beginning of the conteiner.
181 */
182 const_iterator end() const;
183
184 /*!
185 \brief It returns an iterator to the end of the conteiner.
186
187 \return An iterator to the beginning of the conteiner.
188 */
189 iterator end();
190
191 /*!
192 \brief It returns the number of units in the manager.
193
194 \return The number of units in the manager.
195 */
196 std::size_t size() const;
197 /*
198 \brief Checks if the Manager is already initialized.
199 */
201
202 protected:
203
204 /*! \brief It initializes the Singleton. */
206
207 /*! \brief Destructor. */
209
210 private:
211
212 std::map<unsigned int, UnitOfMeasurePtr> m_uoms;
213 std::map<std::string, unsigned int> m_uomsIdxByName;
214
215 };
216
217 inline std::size_t UnitsOfMeasureManager::size() const
218 {
219 return m_uoms.size();
220 }
221
223 {
224 return m_uoms.begin();
225 }
226
228 {
229 return m_uoms.begin();
230 }
231
233 {
234 return m_uoms.end();
235 }
236
238 {
239 return m_uoms.end();
240 }
241
242
243 } // end namespace common
244} // end namespace te
245
246#endif // __TERRALIB_COMMON_INTERNAL_UNITSOFMEASUREMANAGER_H
247
Template support for singleton pattern.
A class for representing a unit of measure.
Template support for singleton pattern.
Definition: Singleton.h:101
A singleton class for dealing with units of measure in the TerraLib environment.
std::size_t size() const
It returns the number of units in the manager.
UnitOfMeasurePtr findBySymbol(const std::string &symbol) const
Returns a unit of measure identified by its symbol.
void remove(UnitOfMeasurePtr &uom)
Removes a unit of measure from the manager.
const_iterator end() const
It returns an iterator to the end of the conteiner.
UnitOfMeasurePtr find(const std::string &name) const
Returns a unit of measure identified by its name.
std::map< unsignedint, UnitOfMeasurePtr >::iterator iterator
UnitOfMeasurePtr find(unsigned int id) const
Returns a unit of measure identified by its identificaton.
const_iterator begin() const
It returns an iterator to the beginning of the conteiner.
void getNames(UnitOfMeasurePtr &uom, std::vector< std::string > &names) const
Retrieves the alternative names for a unit of measure.
std::map< unsigned int, UnitOfMeasurePtr > m_uoms
std::map< unsignedint, UnitOfMeasurePtr >::const_iterator const_iterator
void insert(UnitOfMeasurePtr &uom, const std::vector< std::string > &alternativeNames)
Inserts a new unit of measure to be managed and its alternative names.
UnitsOfMeasureManager()
It initializes the Singleton.
void insert(UnitOfMeasurePtr &uom)
Inserts a new unit of measure to be managed.
void init()
It initializes the list of well kown units of measure.
std::map< std::string, unsigned int > m_uomsIdxByName
double getConversion(const std::string &unitFromName, const std::string &unitToName) const
Calculates a multiplicative factor to convert from a given unit to its base unit and vice-versa.
bool areConvertible(const std::string &unitFromName, const std::string &unitToName) const
Check if two units of measure are compatible.
void clear()
Removes all units from the catalogue.
boost::shared_ptr< UnitOfMeasure > UnitOfMeasurePtr
TerraLib.
#define TECOMMONEXPORT
You can use this macro in order to export/import classes and functions from this module.
Definition: Config.h:66