Library.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/Library.h
23 
24  \brief A class for handling shared libraries.
25 
26  \author Gilberto Ribeiro de Queiroz
27  */
28 
29 #ifndef __TERRALIB_CORE_LIB_LIBRARY_H__
30 #define __TERRALIB_CORE_LIB_LIBRARY_H__
31 
32 // TerraLib
33 #include "../Config.h"
34 
35 // STL
36 #include <string>
37 
38 // Boost
39 #include <boost/noncopyable.hpp>
40 
41 namespace te
42 {
43  namespace core
44  {
45 
46  /*!
47  \class Library
48 
49  \brief A class for handling shared libraries (DLLs, SO, DyLibs).
50 
51  You can use this class to load any shared library and acces any address
52  inside it.
53 
54  If the shared library you are attempting to load has dependencies
55  the other shared libraries must be in a folder reachable by the operational
56  system.
57 
58  On Mac OS X the environment variables PATH, DYLD_LIBRARY_PATH,
59  DYLD_FALLBACK_LIBRARY_PATH and DYLD_FRAMEWORK_FALLBACK_PATH may be set to
60  indicate the list of folders containing shared libraries.
61 
62  On Linux the environment variables PATH and LD_LIBRARY_PATH may be used
63  to indicate the folders searchable by the operational system.
64 
65  On Windows you can set the environment variable PATH.
66 
67  Note that this class has a special static method (or class method)
68  named addSearchDir that can be used to control the behaviour of
69  operational system when loading the shared library dependencies.
70 
71  \warning Shared libraries should not be loaded concurrently by multiple threads.
72  */
73  class TECOREEXPORT Library : public boost::noncopyable
74  {
75  public:
76 
77  //! Load a new shared library.
78  /*!
79  The file name may be a relative or absolute path.
80 
81  If just the name of the shared library is given the class will look for the library in the operational system
82  specific locations.
83 
84  \param slib_file_name The library file name. The file name may contain the full library path.
85  \param delay_load If true the client object must call explicitly the load method before trying to access any symbol in the library.
86 
87  \exception LibraryLoadException It can throw an exception if delay_load is set to false and the library can not be loaded.
88  \exception LibraryNameException It throws an exception if library name is empty or just white-spaces.
89  */
90  Library(const std::string& slib_file_name, const bool& delay_load = false);
91 
92  //! The destructor automatically unloads from memory the shared library if it was not unloaded explicitly.
93  ~Library();
94 
95  //! Load the shared library to memory.
96  /*!
97  If the shared library was already loaded this method doesn't perform operations.
98 
99  \exception LibraryLoadException If the shared library can not be loaded to memory.
100 
101  \note Not thread-safe.
102  */
103  void load();
104 
105  //! Force the unload of the shared library from memory.
106  /*!
107  If the library wasn't previously loaded or if it was already unloaded
108  this method doesn't perform operations.
109 
110  \exception LibraryUnloadException If the library can not be
111  unload from memory this method raises an exception.
112 
113  \note Not thread-safe.
114  */
115  void unload();
116 
117  //! Return true if the shared library is loaded otherwise return false.
118  bool isLoaded() const;
119 
120  //! Return the shared library file name as informed in the constructor.
121  const std::string& getFileName() const;
122 
123  //! Return the address where the given symbol is loaded into memory.
124  /*!
125  \param symbol The name of the symbol inside the library you are searching for.
126  It may be for instance a function name.
127 
128  \return The address where the symbol is loaded.
129 
130  \exception LibrarySymbolNotFoundException It throws an exception if it is not possible to locate the given symbol.
131 
132  \note Not thread-safe.
133  */
134  void* getAddress(const char* symbol) const;
135 
136  //! Given a shared library name without file extensions, prefixes and nor suffixes it will construct a library name according to the specifc platform.
137  static std::string getNativeName(const std::string& name);
138 
139  //! Add the informed dir to the path used by the operational system to lookup for shared libraries.
140  /*!
141  \param dir_name A directory to be added to the lookup of shared libraries.
142 
143  \exception LibraryInvalidSearchPathException It throws an exception if the path couldn't be added to the search path.
144 
145  \note Not thread-safe.
146 
147  \note For Linux and Mac OS X this method doesn't perform operations.
148  */
149  static void addSearchDir(const std::string& dir_name);
150 
151  //! Comes back the application lookup path to the original state, before any add_search_dir has been called.
152  /*!
153  \exception LibraryResetSearchPathException It throws an exception if the path couldn't be reset.
154 
155  \note Not thread-safe.
156 
157  \note For Linux and Mac OS X this method doesn't perform operations.
158  */
159  static void resetSearchPath();
160 
161  //! Returns the system lookup path.
162  /*!
163  \exception LibrarySearchPathException It throws an exception if the path couldn't be reset.
164 
165  \note Not thread-safe.
166  */
167  static std::string getSearchPath();
168 
169  private:
170 
171  struct Impl;
172 
173  Impl* m_pimpl;
174  };
175 
176  } // end namespace core
177 } // end namespace te
178 
179 #endif // __TERRALIB_CORE_LIB_LIBRARY_H__
Impl * m_pimpl
Definition: Library.h:171
A class for handling shared libraries (DLLs, SO, DyLibs).
Definition: Library.h:73
#define TECOREEXPORT
Definition: Config.h:40
URI C++ Library.