LibraryManager.h
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.h
23 
24  \brief A singleton that can be used to observe the available shared libraries in the system.
25 
26  \author Gilberto Ribeiro de Queiroz
27  \author Matheus Cavassan Zaglia
28  */
29 
30 #ifndef __TERRALIB_CORE_LIB_LIBRARYMANAGER_H__
31 #define __TERRALIB_CORE_LIB_LIBRARYMANAGER_H__
32 
33 // TerraLib
34 #include "../Config.h"
35 
36 // STL
37 #include <string>
38 
39 // Boost
40 #include <boost/function.hpp>
41 
42 namespace te
43 {
44  namespace core
45  {
46  /*!
47  \typedef void (*StartupFptr)(void);
48 
49  \brief This is the type for call back functions that makes the startup of a module.
50  */
51  typedef boost::function0<void> StartupFnct;
52 
53  /*!
54  \typedef void (*CleanupFptr)(void);
55 
56  \brief This is the type for call back functions that makes the cleanup of a module.
57  */
58  typedef boost::function0<void> CleanupFnct;
59 
60 
61  struct LibraryEntry
62  {
63  std::string m_name;
64  StartupFnct m_startFptr;
65  CleanupFnct m_cleanupFptr;
67  };
68 
69  /*!
70  \class LibraryManager
71 
72  \brief A singleton that can be used to observe the available libraries in the system.
73 
74  This singleton doesn't control the libraries lifetime, it just make smart
75  references to them. These references will be automatically removed
76  when a library goes out of scope (or been destroyed). Actually it works
77  like an observer of known libraries.
78  */
80  {
81  public:
82 
83  /*!
84  \brief It inserts a LibraryEntry to the manager.
85 
86  \param entry LibraryEntry to be managed.
87 
88  \exception te::InvalidArgumentException If a LibraryEntry with the given name is already registered.
89  */
90  void insert(const LibraryEntry& entry);
91 
92  /*!
93  \brief It removes a LibraryEntry from the manager.
94 
95  \param name The name of the LibraryEntry.
96 
97  \exception te::OutOfRangeException If a LibraryEntry with the given name is not registered.,
98  \exception te::Exception If trying to remove a initialized library.
99  */
100  void remove(const std::string& name);
101 
102  /*!
103  \brief Return a null pointer if a library doesnt't exist.
104 
105  \param name The LibraryEntry name.
106 
107  \return a const reference to the LibraryEntry
108 
109  \exception te::OutOfRangeException If a LibraryEntry with the given name is not registered.
110  */
111  const LibraryEntry& get(const std::string& name);
112 
113  /*!
114  \brief The current state of the LibraryEntry.
115 
116  \param name The name of the LibraryEntry.
117 
118  \return true if the LibraryEntry is initialized or false if isn't.
119 
120  \exception te::OutOfRangeException If a LibraryEntry with the given name is not registered.
121  */
122  bool isInitialized(const std::string& name);
123 
124  /*!
125  \brief Checks if a LibraryEntry exists from a given name
126 
127  \param name The name of the LibraryEntry
128 
129  \return true if the LibraryEntry exist or false if doesn't.
130  */
131  bool exists(const std::string& name);
132 
133  /*!
134  \brief It returns a reference to the singleton instance.
135 
136  \return A reference to the singleton instance.
137  */
138  static LibraryManager& instance();
139 
140  protected:
141 
142  /*! \brief Singleton constructor must be private or protected. */
143  LibraryManager();
144 
145  /*! \brief Singleton destructor must be private or protected. */
146  ~LibraryManager();
147 
148 // no copy allowed
150  LibraryManager& operator=(const LibraryManager&);
151 
152  private:
153 
154  struct Impl;
155 
156  Impl* m_pimpl;
157  };
158 
159  } // end namespace core
160 } // end namespace te
161 
162 #define TERRALIB_LIBRARY_BEGIN(library_name) \
163 class library_ ## library_name \
164 { \
165  public: \
166 \
167  library_ ## library_name() \
168  { \
169  LibraryEntry le = {#library_name, \
170  startup, \
171  shutdown \
172  }; \
173 \
174  te::core::LibraryManager::instance().insert(le); \
175  } \
176 \
177  ~library_ ## library_name() \
178  {\
179  if(te::core::LibraryManager::instance().exists(#library_name))\
180  {\
181  te::core::LibraryManager::instance().get(#library_name).shutdown();\
182  te::core::LibraryManager::instance().remove(#library_name);\
183  }\
184  }
185 
186 
187 #define TERRALIB_LIBRARY_STARTUP \
188  void startup()
189 
190 #define TERRALIB_LIBRARY_SHUTDOWN \
191  void shutdown()
192 
193 #define TERRALIB_LIBRARY_END(library_name) \
194 }; \
195 \
196 static library_ ## library_name s_lib;
197 
198 #endif // __TERRALIB_CORE_LIB_LIBRARYMANAGER_H__
A singleton that can be used to observe the available libraries in the system.
#define TECOREEXPORT
Definition: Config.h:40
mydialect insert("+", new te::da::BinaryOpEncoder("+"))
URI C++ Library.
boost::function0< void > StartupFnct
boost::function0< void > CleanupFnct