All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
DataTypeManager.cpp
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/datatype/DataTypeManager.cpp
22 
23  \brief A singleton for managing all data types in the system.
24 */
25 
26 // TerraLib
27 #include "../common/STLUtils.h"
28 #include "../common/Translator.h"
29 #include "DataType.h"
30 #include "DataTypeManager.h"
31 #include "Exception.h"
32 
33 // STL
34 #include <memory>
35 
37 
38 int te::dt::DataTypeManager::add(const DataType& dt) throw(te::dt::Exception)
39 {
40  return add(dt.getName(), dt.getDescription());
41 }
42 
43 int te::dt::DataTypeManager::add(const std::string& name, const std::string& description) throw(te::dt::Exception)
44 {
45  int id = sm_lastId + 1;
46 
47  std::auto_ptr<DataType> dt(new DataType(id, name, description));
48 
49  if(m_types.find(dt.get()) != m_types.end())
50  throw Exception(TE_TR("There is already a data type with the given id!"));
51 
52  if(m_nameIdx.find(dt.get()) != m_nameIdx.end())
53  throw Exception(TE_TR("There is already a data type with the given name!"));
54 
55  m_types.insert(dt.get());
56  m_nameIdx.insert(dt.get());
57 
58  dt.release();
59  ++sm_lastId;
60 
61  return sm_lastId;
62 }
63 
65 {
66  DataType* odt = 0;
67  std::set<DataType*, IdComparer>::iterator it = m_types.find((DataType*)dt);
68 
69  if(it != m_types.end())
70  {
71  odt = *it;
72  m_types.erase(it);
73  std::set<DataType*, NameComparer>::iterator it2 = m_nameIdx.find(odt);
74 
75  if(it2 != m_nameIdx.end())
76  m_nameIdx.erase(it2);
77  }
78 
79  delete odt;
80 }
81 
83 {
84  DataType dummy(id, "", "");
85 
86  std::set<DataType*, IdComparer>::const_iterator it = m_types.find(&dummy);
87 
88  if(it != m_types.end())
89  return *it;
90 
91  return 0;
92 }
93 
94 const te::dt::DataType* te::dt::DataTypeManager::find(const std::string& name) const
95 {
96 
97  DataType dummy(0, name, "");
98 
99  std::set<DataType*, NameComparer>::const_iterator it = m_nameIdx.find(&dummy);
100 
101  if(it != m_nameIdx.end())
102  return *it;
103 
104  return 0;
105 }
106 
108 {
109 }
110 
112 {
113  te::common::FreeContents(m_types);
114 }
115 
It stores information about a data type.
Definition: DataType.h:55
void remove(const DataType *dt)
It removes the data type.
std::set< DataType *, NameComparer > m_nameIdx
The set of data types ordered by name.
An exception class for the DataType module.
#define TE_TR(message)
It marks a string in order to get translated.
Definition: Translator.h:347
DataTypeManager()
Constructor for singletons is protected.
It stores information about a data type.
std::set< DataType *, IdComparer > m_types
The set of data types ordered by id.
static int sm_lastId
The id value of the last inserted data type.
const DataType * find(int id) const
It finds a data type having the given id.
A singleton for managing all data types in the system.
~DataTypeManager()
Destructor for singletons is protected.
int add(const DataType &dt)
It adds a new data type to the system.
void FreeContents(boost::unordered_map< K, V * > &m)
This function can be applied to a map of pointers. It will delete each pointer in the map...
Definition: BoostUtils.h:55