All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ColorSchemeCatalogManager.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2008-2011 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/color/ColorSchemeCatalogManager.cpp
22 
23  \brief Implementation of the singleton to manage color scheme catalogs.
24 */
25 
26 // TerraLib
27 #include "../common/Exception.h"
28 #include "../common/STLUtils.h"
29 #include "../common/Translator.h"
30 #include "ColorSchemeCatalog.h"
32 
33 // STL
34 #include <cassert>
35 
37 {
38  assert(c);
39 
40  if(findByName(c->getName()))
41  throw te::common::Exception(TR_COLOR("There is already a color scheme catalog with the given name!"));
42 
43  m_catalogs.push_back(c);
44  m_catalogIdxByName.insert(std::map<std::string, ColorSchemeCatalog*>::value_type(c->getName(), c));
45 }
46 
48 {
49  assert(c);
50 
51 // first, find candidates for deletion... if one of then is not found, raise an exception
52  std::map<std::string, ColorSchemeCatalog*>::iterator itProjectIdxByName = m_catalogIdxByName.find(c->getName());
53 
54  if(itProjectIdxByName != m_catalogIdxByName.end())
55  throw te::common::Exception(TR_COLOR("Couldn't find the catalog with the given name!"));
56 
57  size_t i = 0;
58 
59  for(; i < m_catalogs.size(); ++i)
60  {
61  if(m_catalogs[i]->getName() == c->getName())
62  break;
63  }
64 
65  if(i == m_catalogs.size())
66  throw te::common::Exception(TR_COLOR("Couldn't find the catalog with the given name!"));
67 
68 // if we are here, so all entries are ok... just remove them
69  m_catalogs.erase(m_catalogs.begin() + i);
70  m_catalogIdxByName.erase(itProjectIdxByName);
71 }
72 
74 {
75  disconnect(c);
76 
77 // and delete the projet from main memory
78  delete (c);
79 }
80 
82 {
83  std::map<std::string, ColorSchemeCatalog*>::const_iterator it = m_catalogIdxByName.find(name);
84 
85  if(it != m_catalogIdxByName.end())
86  return it->second;
87 
88  return 0;
89 }
90 
91 std::pair<std::vector<te::color::ColorSchemeCatalog*>::const_iterator,
92  std::vector<te::color::ColorSchemeCatalog*>::const_iterator> te::color::ColorSchemeCatalogManager::getIterator() const
93 {
94  return std::pair<std::vector<te::color::ColorSchemeCatalog*>::const_iterator,
95  std::vector<te::color::ColorSchemeCatalog*>::const_iterator>(m_catalogs.begin(), m_catalogs.end());
96 }
97 
98 const std::vector<te::color::ColorSchemeCatalog*>& te::color::ColorSchemeCatalogManager::getCatalogs() const
99 {
100  return m_catalogs;
101 }
102 
104 {
105  return m_catalogs.empty();
106 }
107 
109 {
110  te::common::FreeContents(m_catalogs);
111 
112  m_catalogs.clear();
113 
114  m_catalogIdxByName.clear();
115 }
116 
118 {
119  clear();
120 }
121 
123 {
124 }
125 
void clear()
It unloads all catalogs managed by ColorSchemeCatalogManager.
This class is designed to declare objects to be thrown as exceptions by TerraLib. ...
Definition: Exception.h:58
std::vector< ColorSchemeCatalog * > m_catalogs
This is the list of all system&#39;s loaded catalogs.
ColorSchemeCatalogManager()
It initializes the Singleton.
std::pair< std::vector< ColorSchemeCatalog * >::const_iterator, std::vector< ColorSchemeCatalog * >::const_iterator > getIterator() const
It returns a pair of iterators over the catalogs of this manager.
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
#define TR_COLOR(message)
It marks a string in order to get translated. This is a special mark used in the Color module of Terr...
Definition: Config.h:72
ColorSchemeCatalog * findByName(const std::string &name) const
It returns the catalog identified by a given name or NULL if none is found.
std::map< std::string, ColorSchemeCatalog * > m_catalogIdxByName
An index from catalog&#39;s name to catalog&#39;s instance (note: we can not have duplicated names)...
void disconnect(ColorSchemeCatalog *c)
It removes the internal reference to the catalog.
A catalog for color schemes groups.
const std::string & getName() const
It returns the catalog name.
const std::vector< ColorSchemeCatalog * > & getCatalogs() const
It returns the list of catalogs available in the system.
void insert(ColorSchemeCatalog *c)
It inserts a new catalog that will be managed by ColorSchemeCatalogManager.
bool isEmpty() const
It returns true if the manager contains at least one catalog. If no catalog exists, it returns false.
A catalog for color schemes.
The ColorSchemeCatalogManager is a singleton that can be used to manage all loaded color scheme catal...
void erase(ColorSchemeCatalog *c)
It removes the catalog from the manager and clears it resources.