LibraryManager.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/lib/LibraryManager.cpp
23 
24  \brief A singleton that can be used to observe the available libraries in the system.
25 
26  \author Gilberto Ribeiro de Queiroz
27  \author Matheus Cavassan Zaglia
28  */
29 
30 // TerraLib
31 #include "LibraryManager.h"
32 #include "Exception.h"
33 #include "../translator/Translator.h"
34 
35 // STL
36 #include <vector>
37 
38 // Boost
39 #include <boost/format.hpp>
40 
42 {
43  std::vector<te::core::LibraryEntry > library_entries;
44 };
45 
46 void
48 {
49  if(exists(entry.m_name))
50  {
51  boost::format err_msg(TE_TR("There is already a library registered with the name: %1%."));
52 
53  throw te::InvalidArgumentException() << te::ErrorDescription((err_msg % entry.m_name).str());
54  }
55 
56  m_pimpl->library_entries.push_back(entry);
57 }
58 
59 void
60 te::core::LibraryManager::remove(const std::string &name)
61 {
62  std::vector<LibraryEntry>::iterator it = std::find_if(m_pimpl->library_entries.begin(),
63  m_pimpl->library_entries.end(),
64  [&name](const LibraryEntry& le)
65  { return le.m_name == name; });
66 
67  if(it != m_pimpl->library_entries.end())
68  {
69  boost::format err_msg(TE_TR("There is no library registered with the name: %1%."));
70 
71  throw te::OutOfRangeException() << te::ErrorDescription((err_msg % name).str());
72  }
73 
74  if(it->m_initialized)
75  {
76  boost::format err_msg(TE_TR("The library cannot be initialized in order to remove it."));
77 
78  throw te::Exception() << te::ErrorDescription((err_msg % name).str());
79  }
80 
81  m_pimpl->library_entries.erase(it);
82 }
83 
84 
86 te::core::LibraryManager::get(const std::string& name)
87 {
88  for(const LibraryEntry& le: m_pimpl->library_entries)
89  {
90  if(le.m_name == name)
91  return le;
92  }
93 
94  boost::format err_msg(TE_TR("There is no library registered with the name: %1%."));
95 
96  throw te::OutOfRangeException() << te::ErrorDescription((err_msg % name).str());
97 }
98 
99 bool
101 {
102  for(const LibraryEntry& le: m_pimpl->library_entries)
103  {
104  if(le.m_name == name)
105  return le.m_initialized;
106  }
107 
108  boost::format err_msg(TE_TR("There is no library registered with the name: %1%."));
109 
110  throw te::OutOfRangeException() << te::ErrorDescription((err_msg % name).str());
111 }
112 
113 bool te::core::LibraryManager::exists(const std::string &name)
114 {
115  return std::find_if(m_pimpl->library_entries.begin(),
116  m_pimpl->library_entries.end(),
117  [&name](const LibraryEntry& le)
118  { return le.m_name == name; }) != m_pimpl->library_entries.end();
119 }
120 
123 {
124  static LibraryManager instance;
125 
126  return instance;
127 }
128 
130  : m_pimpl(nullptr)
131 {
132  m_pimpl = new Impl;
133 }
134 
136 {
137  delete m_pimpl;
138 }
139 
Base exception class for plugin module.
Specific exception types for Library Manager.
#define TE_TR(message)
It marks a string in order to get translated.
Definition: Translator.h:242
static LibraryManager & instance()
It returns a reference to the singleton instance.
boost::error_info< struct tag_error_description, std::string > ErrorDescription
The base type for error report messages.
A singleton that can be used to observe the available libraries in the system.
bool isInitialized(const std::string &name)
The current state of the LibraryEntry.
std::vector< te::core::LibraryEntry > library_entries
LibraryManager()
Singleton constructor must be private or protected.
A singleton that can be used to observe the available shared libraries in the system.
const LibraryEntry & get(const std::string &name)
Return a null pointer if a library doesnt&#39;t exist.
An exception indicating that a given item was not found in a collection (or range).
~LibraryManager()
Singleton destructor must be private or protected.
An exception indicating that a given argument is not valid, for instance if a given item already exis...
void insert(const LibraryEntry &entry)
It inserts a LibraryEntry to the manager.
bool exists(const std::string &name)
Checks if a LibraryEntry exists from a given name.
void remove(const std::string &name)
It removes a LibraryEntry from the manager.