common/TerraLib.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/common/TerraLib.cpp
22 
23  \brief An utility class to control the startup and cleanup of the TerraLib Platform and its resources.
24 */
25 
26 // TerraLib
27 #include "TerraLib.h"
28 #include "../core/plugin/Utils.h"
29 
30 // STL
31 #include <algorithm>
32 #include <cassert>
33 
35 {
36 // let's initialize all non-initialized modules
38 
39  const std::size_t size = m_modules.size();
40 
41  for(std::size_t i = 0; i < size; ++i)
42  initialize(i);
43 }
44 
45 void TerraLib::initialize(const std::size_t i)
46 {
47  assert(i < m_modules.size());
48 
49  if(m_initialized[i])
50  return;
51 
52  Module& module = m_modules[i];
53 
54 // invoke startup just for modules that has an init function
55  if(module.m_startFptr != nullptr)
56  (module.m_startFptr)();
57 
58  m_initialized[i] = true;
59 }
60 
61 void TerraLib::initialize(const std::string& moduleName)
62 {
63  const std::size_t i = getModuleIdx(moduleName);
64  initialize(i);
65 }
66 
68 {
70 
71 // let's finalize all modules
72  const std::size_t size = m_modules.size();
73 
74  for(std::size_t i = 0; i < size; ++i)
75  finalize(i);
76 }
77 
78 void TerraLib::finalize(const std::size_t i)
79 {
80  assert(i < m_modules.size());
81 
82  if(!m_initialized[i])
83  return;
84 
85  Module& module = m_modules[i];
86 
87 // invoke cleanup just for modules that has a cleanup function
88  if(module.m_cleanupFptr != nullptr)
89  (module.m_cleanupFptr)();
90 
91  m_initialized[i] = false;
92 }
93 
94 void TerraLib::finalize(const std::string& moduleName)
95 {
96  const std::size_t i = getModuleIdx(moduleName);
97  finalize(i);
98 }
99 
100 void TerraLib::add(const Module& m)
101 {
102  m_modules.push_back(m);
103  m_initialized.push_back(false);
104 }
105 
106 void TerraLib::remove(const std::string& moduleName)
107 {
108 // let's find the module
109  const std::size_t size = m_modules.size();
110 
111  for(std::size_t i = 0; i < size; ++i)
112  {
113  const Module& module = m_modules[i];
114 
115  if(module.m_name == moduleName)
116  {
117 // after finding the module let's remove it from the initialization list and the module list
118  m_initialized.erase(m_initialized.begin() + i);
119  m_modules.erase(m_modules.begin() + i);
120  break;
121  }
122  }
123 }
124 
125 void TerraLib::remove(const std::size_t i)
126 {
127  assert((i < m_initialized.size()) && (i < m_modules.size()));
128 
129  m_initialized.erase(m_initialized.begin() + i);
130  m_modules.erase(m_modules.begin() + i);
131 }
132 
133 std::size_t TerraLib::getNumRegModules() const
134 {
135  return m_modules.size();
136 }
137 
138 std::size_t TerraLib::getModuleIdx(const std::string& moduleName) const
139 {
140  const std::size_t size = m_modules.size();
141 
142  for(std::size_t i = 0; i < size; ++i)
143  {
144  if(moduleName == m_modules[i].m_name)
145  return i;
146  }
147 
148  return (std::size_t)(-1);
149 }
150 
151 const std::string& TerraLib::getModuleName(std::size_t i) const
152 {
153  assert(i < m_modules.size());
154  return m_modules[i].m_name;
155 }
156 
158 {
159  //std::atexit(&TerraLib::sfinalize);
160 }
161 
162 TerraLib::~TerraLib() = default;
163 
164 //void TerraLib::sfinalize()
165 //{
166 // TerraLib::getInstance().finalize();
167 //}
168 
169 
TECOREEXPORT void FinalizePluginSystem()
An utility class to control the startup and cleanup of the TerraLib Platform and its resources...
std::string m_name
The module name: an internal value used to identify the module in the system. Must be a unique value...
const std::string & getModuleName(std::size_t i) const
It return the i-th module name.
CleanupFptr m_cleanupFptr
The module finalization routine.
std::size_t getModuleIdx(const std::string &moduleName) const
It returns the module index inside the internal data structure.
std::size_t getNumRegModules() const
It returns the number of registered modules.
void finalize()
It finalizes the TerraLib Platform.
StartupFptr m_startFptr
The module initialization routine.
std::vector< bool > m_initialized
The list of modules to be initialized when initialize is called.
void remove(const std::string &moduleName)
It removes the module entry identified by the given name.
std::vector< Module > m_modules
This static function is invoked automatically when the application finishes its execution.
void add(const Module &m)
It registers the module in the TerraLib Platform.
void initialize()
It initializes the TerraLib Platform.
TerraLib()
The singleton constructor is not callable outside the class.
This internal structure is used to model the basic information about a TerraLib module.
TECOREEXPORT void InitializePluginSystem()