Library.h
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/Library.h
22 
23  \brief A class for handling shared libraries.
24  */
25 
26 #ifndef __TERRALIB_COMMON_INTERNAL_LIBRARY_H
27 #define __TERRALIB_COMMON_INTERNAL_LIBRARY_H
28 
29 // TerraLib
30 #include "Config.h"
31 #include "Exception.h"
32 
33 // STL
34 #include <string>
35 
36 // Boost
37 #include <boost/noncopyable.hpp>
38 #include <boost/shared_ptr.hpp>
39 
40 namespace te
41 {
42  namespace common
43  {
44  /*!
45  \class Library
46 
47  \brief A class for handling shared libraries (DLLs, SO, DyLibs).
48 
49  \note If the library you are trying to load has dependencies
50  on other shared libraries then these are automatically
51  loaded by the underlying operational system. The dependencies
52  must be in a folder where the system can search for these dependencies.
53 
54  \note This class is based on pimpl idiom.
55 
56  \ingroup common
57  */
58  class TECOMMONEXPORT Library : public boost::noncopyable
59  {
60  public:
61 
62  /*!
63  \brief Loads a new library specified by the file name.
64 
65  File name may be a relative or absolute path. If just the name of the
66  library is given the class will look for the library in the operational system
67  specific locations.
68 
69  \param fileName The library file name. The file name may contain the full library path.
70  \param delayLoad If true the client object must call explicitly the load method before trying to access any symbol in the library.
71 
72  \exception Exception It throws an exception if delayLoad is true and the library can not be loaded.
73  */
74  Library(const std::string& fileName, bool delayLoad = false) throw(Exception);
75 
76  /*! \brief The destructor will automatically unload the library from memory. */
77  ~Library();
78 
79  /*!
80  \brief It loads the shared library to memory.
81 
82  If the library was already loaded this method doesn't perform operations.
83 
84  \exception Exception If the library can not be load to memory this method raises an exception.
85 
86  \note Not thread-safe.
87  */
88  void load() throw(Exception);
89 
90  /*!
91  \brief It forces the unload of the shared library from memory.
92 
93  If the library wasn't previously loaded or if it was already unloaded this method doesn't perform operations.
94 
95  \exception Exception If the library can not be unload from memory this method raises an exception.
96 
97  \note Not thread-safe.
98  */
99  void unload() throw(Exception);
100 
101  /*!
102  \brief It returns true if the shared library is loaded otherwise return false.
103 
104  \return True if the shared library is loaded otherwise returns false.
105 
106  \note Thread-safe.
107  */
108  bool isLoaded() const throw();
109 
110  /*!
111  \brief It returns the library file name as informed in the constructor.
112 
113  \return The library file name as informed in the constructor.
114 
115  \note Thread-safe.
116  */
117  const std::string& getFileName() const throw();
118 
119  /*!
120  \brief It returns the address where the given symbol is loaded into memory.
121 
122  \param symbol The name of the symbol inside the library you are searching for. It may be for instance a function name.
123 
124  \return The address where the symbol is loaded.
125 
126  \exception Exception It throws an exception if it is not possible to locate the given symbol.
127 
128  \note Thread-safe.
129  */
130  void* getAddress(const std::string& symbol) const throw(Exception);
131 
132  /*!
133  \brief Given a library name without file extensions, prefixes and nor suffixes it will construct a library name according to the specifc platform.
134 
135  \return The library file name according to the specifc platform.
136 
137  \note Thread-safe.
138  */
139  static std::string getNativeName(const std::string& name) throw();
140 
141  /*!
142  \brief It adds the informed dir to the path used by the operational system to lookup for shared libraries.
143 
144  \param d A directory to be added to the lookup of shared libraries.
145 
146  \exception Exception It throws an exception if the path couldn't be added to the search path.
147 
148  \note Not thread-safe.
149  */
150  static void addSearchDir(const std::string& d) throw(Exception);
151 
152  /*!
153  \brief It comes back the application lookupo path to the original state, before any addPath has been called.
154 
155  \exception Exception It throws an exception if the search path couldn't be reset.
156 
157  \note Not thread-safe.
158  */
159  static void resetSearchPath() throw(Exception);
160 
161  /*!
162  \brief It returns the system lookup path.
163 
164  \return The system lookup path.
165 
166  \exception Exception It throws an exception if the search path couldn't be reset.
167 
168  \todo This method needs a checkup! Instead of
169  throwing an exception in windows implementation,
170  this methods should take an interative
171  loop to allocate more room for the path string!
172 
173  \note Thread-safe.
174  */
175  static std::string getSearchPath() throw(Exception);
176 
177  private:
178 
179  class Impl;
180 
181  Impl* m_pImpl; //!< A pointer to the real implementation.
182  };
183 
184  typedef boost::shared_ptr<Library> LibraryPtr;
185 
186  } // end namespace common
187 } // end namespace te
188 
189 #endif // __TERRALIB_COMMON_INTERNAL_LIBRARY_H
190 
boost::shared_ptr< Library > LibraryPtr
Definition: Library.h:184
A class for handling shared libraries (DLLs, SO, DyLibs).
Definition: Library.h:58
Configuration flags for the TerraLib Common Runtime module.
Impl * m_pImpl
A pointer to the real implementation.
Definition: Library.h:179
URI C++ Library.
#define TECOMMONEXPORT
You can use this macro in order to export/import classes and functions from this module.
Definition: Config.h:65
This class is designed to declare objects to be thrown as exceptions by TerraLib. ...
Definition: Exception.h:58
This class is designed to declare objects to be thrown as exceptions by TerraLib. ...