examples/core/lib/main.cpp
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/examples/core/lib/main.cpp
23 
24  \brief Examples for the TerraLib Library Module
25 
26  \author Frederico Augusto BedĂȘ
27 */
28 
29 // TerraLib
33 
34 // STL
35 #include <cstdlib>
36 #include <iostream>
37 #include <string>
38 
39 int main()
40 {
41  try
42  {
43 // Get the shared library file name according to the operational system.
44  std::string lName = te::core::Library::getNativeName("terralib_example_core_lib_function");
45  std::string lPath = te::core::FindInTerraLibPath("example");
46 
47 #if TE_PLATFORM == TE_PLATFORMCODE_MSWINDOWS
48  if(!lPath.empty())
50 #else
51  lName = lPath + "/" + lName;
52 #endif
53 
54 // The library is located at the same directory as the executable. */
55  te::core::Library l1(lName);
56 
57 // Testing if the shared library was loaded.
58  if(l1.isLoaded())
59  std::cout << std::endl << "Shared library " << l1.getFileName() << " loaded!" << std::endl;
60 
61 // Getting a pointer to a function in the library.
62  void* t = l1.getAddress("fatorial");
63 
64 // Testing the pointer.
65  if(t != nullptr)
66  {
67  std::cout << "Function fatorial found!" << std::endl;
68  }
69  else
70  {
71  std::cout << "Function fatorial not found!" << std::endl;
72  return EXIT_FAILURE;
73  }
74 
75  typedef int (*fatorial_fptr)(int);
76 
77  fatorial_fptr fat = reinterpret_cast<fatorial_fptr>(t);
78 
79  std::cout << "Fatorial 5: " << fat(5) << std::endl;
80 
81 // Unloading the shared library.
82  l1.unload();
83 
84  std::cout << "Library unloaded!" << std::endl << std::endl;
85  }
86  catch(const te::core::LibraryLoadException& e)
87  {
88  if(const std::string* d = boost::get_error_info<te::ErrorDescription>(e))
89  std::cout << std::endl << "Fail to load library: " << *d;
90 
91  return EXIT_FAILURE;
92  }
93  catch(const te::core::LibraryUnloadException& e)
94  {
95  if(const std::string* d = boost::get_error_info<te::ErrorDescription>(e))
96  std::cout << std::endl << "Fail to unload library: " << *d;
97 
98  return EXIT_FAILURE;
99  }
101  {
102  if(const std::string* d = boost::get_error_info<te::ErrorDescription>(e))
103  std::cout << std::endl << "Fail to load symbol: " << *d;
104 
105  return EXIT_FAILURE;
106  }
107  catch(const te::Exception& e)
108  {
109  if(const std::string* d = boost::get_error_info<te::ErrorDescription>(e))
110  std::cout << std::endl << "Unknown error: " << *d;
111 
112  return EXIT_FAILURE;
113  }
114  catch(const std::exception& e)
115  {
116  std::cout << std::endl << "Unknown error: " << e.what();
117  return EXIT_FAILURE;
118  }
119  catch(...)
120  {
121  std::cout << std::endl << "Unknown error!";
122  return EXIT_FAILURE;
123  }
124 
125  return EXIT_SUCCESS;
126 }
A class for handling shared libraries.
An exception indicating an error when loading a shared library.
Base exception class for plugin module.
An exception indicating an error when releasing a shared library.
This file is a wrapper around platform specific include files.
Specific exception types for Library Manager.
static void addSearchDir(const std::string &dir_name)
Add the informed dir to the path used by the operational system to lookup for shared libraries...
Definition: Library.cpp:279
void unload()
Force the unload of the shared library from memory.
Definition: Library.cpp:180
A class for handling shared libraries (DLLs, SO, DyLibs).
Definition: Library.h:73
static te::dt::DateTime d(2010, 8, 9, 15, 58, 39)
void * getAddress(const char *symbol) const
Return the address where the given symbol is loaded into memory.
Definition: Library.cpp:227
const std::string & getFileName() const
Return the shared library file name as informed in the constructor.
Definition: Library.cpp:221
TECOREEXPORT std::string FindInTerraLibPath(const std::string &path)
Returns the path relative to a directory or file in the context of TerraLib.
te::gm::LineString * l1
An exception indicating an error when searching for a given symbol in a shared library.
bool isLoaded() const
Return true if the shared library is loaded otherwise return false.
Definition: Library.cpp:215
int main()
static std::string getNativeName(const std::string &name)
Given a shared library name without file extensions, prefixes and nor suffixes it will construct a li...
Definition: Library.cpp:256