All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
DataSourceFactory.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/dataaccess/core/datasource/DataSourceFactory.cpp
22 
23  \brief A factory of data sources.
24 */
25 
26 // TerraLib
27 #include "../../common/Translator.h"
28 #include "DataSource.h"
29 #include "DataSourceFactory.h"
30 
31 // Boost
32 #include <boost/format.hpp>
33 
34 std::map<std::string, te::da::DataSourceFactory::FactoryFnctType>
36 
37 std::auto_ptr<te::da::DataSource> te::da::DataSourceFactory::make(const std::string& dsType)
38 {
39  std::map<std::string, FactoryFnctType>::const_iterator it = sm_factories.find(dsType);
40 
41  if(it == sm_factories.end())
42  throw Exception((boost::format(TE_TR("Could not find a data source factory named: %1!")) % dsType).str());
43 
44  std::auto_ptr<DataSource> ds(sm_factories[dsType]());
45 
46  return ds;
47 }
48 
49 bool te::da::DataSourceFactory::find(const std::string& dsType)
50 {
51  std::map<std::string, FactoryFnctType>::const_iterator it = sm_factories.find(dsType);
52 
53  return (it != sm_factories.end());
54 }
55 
56 void te::da::DataSourceFactory::add(const std::string& dsType, FactoryFnctType f)
57 {
58  std::map<std::string, FactoryFnctType>::const_iterator it = sm_factories.find(dsType);
59 
60  if(it != sm_factories.end())
61  throw Exception((boost::format(TE_TR("A data source factory with name '%1' is already registered.")) % dsType).str());
62 
63  sm_factories[dsType] = f;
64 }
65 
66 void te::da::DataSourceFactory::remove(const std::string& dsType)
67 {
68  std::map<std::string, FactoryFnctType>::iterator it = sm_factories.find(dsType);
69 
70  if(it == sm_factories.end())
71  throw Exception((boost::format(TE_TR("There is no registered data source factory named '%1'.")) % dsType).str());
72 
73  sm_factories.erase(it);
74 }
static bool find(const std::string &dsType)
static std::map< std::string, FactoryFnctType > sm_factories
An abstract class for data providers like a DBMS, Web Services or a regular file. ...
#define TE_TR(message)
It marks a string in order to get translated.
Definition: Translator.h:347
boost::function< DataSource *()> FactoryFnctType
static std::auto_ptr< DataSource > make(const std::string &dsType)
A factory for data sources.
static void add(const std::string &dsType, FactoryFnctType f)
static void remove(const std::string &dsType)