PluginEngineManager.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 National Institute For Space Research (INPE) - Brazil.
3 
4  This file is part of the TerraLib - a Framework for building GIS enabled applications.
5 
6  TerraLib is free software: you can redistribute it and/or modify
7  it under the terms of the GNU Lesser General Public License as published by
8  the Free Software Foundation, either version 3 of the License,
9  or (at your option) any later version.
10 
11  TerraLib is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU Lesser General Public License for more details.
15 
16  You should have received a copy of the GNU Lesser General Public License
17  along with TerraLib. See COPYING. If not, write to
18  TerraLib Team at <terralib-team@terralib.org>.
19  */
20 
21 /*!
22  \file terralib/core/plugin/PluginEngineManager.cpp
23 
24  \brief A singleton that can be used to register plugin engines.
25 
26  \author Gilberto Ribeiro de Queiroz
27  \author Matheus Cavassan Zaglia
28  */
29 
30 // TerraLib
31 #include "PluginEngineManager.h"
32 #include "../translator/Translator.h"
33 #include "AbstractPluginEngine.h"
34 #include "Exception.h"
35 
36 // Boost
37 #include <boost/format.hpp>
38 
40 {
41  std::map<std::string, std::shared_ptr<AbstractPluginEngine> > engines;
42 };
43 
44 
45 void
46 te::core::PluginEngineManager::insert(std::unique_ptr<AbstractPluginEngine> engine)
47 {
48  if(engine == nullptr)
49  throw te::InvalidArgumentException() << ErrorDescription(TE_TR("Attempting to register a null plugin engine in PluginEngineManager."));
50 
51  const std::string& engine_id = engine->id();
52 
53  if(m_pimpl->engines.find(engine_id) != m_pimpl->engines.end())
54  {
55  boost::format err_msg(TE_TR("There is already a plugin engine registered with name: %1%."));
56 
57  throw InvalidArgumentException() << ErrorDescription((err_msg % engine_id).str());
58  }
59 
60  std::shared_ptr<AbstractPluginEngine> new_engine(engine.release());
61 
62  m_pimpl->engines.insert(std::make_pair(engine_id, new_engine));
63 }
64 
65 void
66 te::core::PluginEngineManager::remove(const std::string& engine_id)
67 {
68  std::map<std::string, std::shared_ptr<AbstractPluginEngine> >::iterator it = m_pimpl->engines.find(engine_id);
69 
70  if(it == m_pimpl->engines.end())
71  {
72  boost::format err_msg(TE_TR("Could not remove plugin engine '%1%': not found."));
73 
74  throw OutOfRangeException() << ErrorDescription((err_msg % engine_id).str());
75  }
76 
77  m_pimpl->engines.erase(it);
78 }
79 
81 te::core::PluginEngineManager::get(const std::string& engine_id) const
82 {
83  std::map<std::string, std::shared_ptr<AbstractPluginEngine> >::iterator it = m_pimpl->engines.find(engine_id);
84 
85  if(it == m_pimpl->engines.end())
86  {
87  boost::format err_msg(TE_TR("Could not retrieve plugin engine '%1%': not found."));
88 
89  throw OutOfRangeException() << ErrorDescription((err_msg % engine_id).str());
90  }
91 
92  return *(it->second);
93 }
94 
95 void
97 {
98  m_pimpl->engines.clear();
99 }
100 
103 {
104  static PluginEngineManager inst;
105 
106  return inst;
107 }
108 
110  : m_pimpl(nullptr)
111 {
112  m_pimpl = new Impl;
113 }
114 
116 {
117  delete m_pimpl;
118 }
void insert(std::unique_ptr< AbstractPluginEngine > engine)
Register a new plugin engine.
std::map< std::string, std::shared_ptr< AbstractPluginEngine > > engines
A singleton that can be used to register plugin engines.
#define TE_TR(message)
It marks a string in order to get translated.
Definition: Translator.h:242
void remove(const std::string &engine_id)
Unregister the plugin engine.
void clear()
Unregister every plugin engine.
boost::error_info< struct tag_error_description, std::string > ErrorDescription
The base type for error report messages.
The base class for plugin engines.
A singleton that can be used to register plugin engines.
AbstractPluginEngine & get(const std::string &engine_id) const
Find a plugin engine with the given id.
An exception indicating that a given item was not found in a collection (or range).
An exception indicating that a given argument is not valid, for instance if a given item already exis...
static PluginEngineManager & instance()
Access the singleton.