All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
DataSourceInfoManager.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2008-2013 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/dataaccess/datasource/DataSourceInfoManager.cpp
22 
23  \brief A singleton to keep all the registered data sources.
24 */
25 
26 // TerraLib
27 #include "../../common/STLUtils.h"
28 #include "../../common/Translator.h"
29 #include "../Exception.h"
30 #include "DataSourceInfo.h"
31 #include "DataSourceInfoManager.h"
32 
33 // STL
34 #include <cassert>
35 
36 // Boost
37 #include <boost/foreach.hpp>
38 #include <boost/format.hpp>
39 
41 {
42  std::map<std::string, DataSourceInfoPtr>::const_iterator it = m_datasources.find(id);
43 
44  return (it != m_datasources.end()) ? it->second : DataSourceInfoPtr();
45 }
46 
48 {
49  if(ds.get() == 0)
50  throw Exception(TR_DATAACCESS("Can not insert a NULL data source into the manager!"));
51 
52  if(m_datasources.find(ds->getId()) != m_datasources.end())
53  throw Exception((boost::format(TR_DATAACCESS("There is already a data source with the given id (%1%) in data source manager!")) % ds->getId()).str());
54 
55  m_datasources[ds->getId()] = ds;
56 }
57 
58 void te::da::DataSourceInfoManager::remove(const std::string& id)
59 {
60  std::map<std::string, DataSourceInfoPtr>::iterator it = m_datasources.find(id);
61 
62  if(it == m_datasources.end())
63  throw Exception((boost::format(TR_DATAACCESS("There isn't a data source with the given id (%1%) in data source manager!")) % id).str());
64 
65  m_datasources.erase(it);
66 }
67 
68 void te::da::DataSourceInfoManager::removeByType(const std::string& dsTypeName)
69 {
70  std::map<std::string, DataSourceInfoPtr>::iterator it = m_datasources.begin();
71 
72  while(it!=m_datasources.end())
73  if(it->second->getType() == dsTypeName)
74  m_datasources.erase(it++);
75  else
76  ++it;
77 }
78 
79 
80 void te::da::DataSourceInfoManager::getByType(const std::string& dsTypeName, std::vector<DataSourceInfoPtr>& datasources) const
81 {
82  std::map<std::string, DataSourceInfoPtr>::const_iterator it = m_datasources.begin();
83  std::map<std::string, DataSourceInfoPtr>::const_iterator itend = m_datasources.end();
84 
85  while(it != itend)
86  {
87  if(it->second->getType() == dsTypeName)
88  datasources.push_back(it->second);
89 
90  ++it;
91  }
92 }
93 
95 {
96  return m_datasources.size();
97 }
98 
100 {
101  return m_datasources.begin();
102 }
103 
105 {
106  return m_datasources.begin();
107 }
108 
110 {
111  return m_datasources.end();
112 }
113 
115 {
116  return m_datasources.end();
117 }
118 
120 {
121 }
122 
124 {
125 }
126 
DataSourceInfoPtr get(const std::string &id) const
std::map< std::string, DataSourceInfoPtr >::iterator iterator
void removeByType(const std::string &dsTypeName)
std::map< std::string, DataSourceInfoPtr >::const_iterator const_iterator
void remove(const std::string &id)
A singleton to keep all the registered data sources.
void add(const DataSourceInfoPtr &ds)
std::map< std::string, DataSourceInfoPtr > m_datasources
#define TR_DATAACCESS(message)
It marks a string in order to get translated. This is a special mark used in the DataAccess module of...
Definition: Config.h:95
void getByType(const std::string &dsTypeName, std::vector< DataSourceInfoPtr > &datasources) const
boost::shared_ptr< DataSourceInfo > DataSourceInfoPtr
A conteiner class for keeping information about a data source.