JeVois  1.5
JeVois Smart Embedded Machine Vision Toolkit
Share this page:
DynamicLoaderImpl.H
Go to the documentation of this file.
1 // ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2 //
3 // JeVois Smart Embedded Machine Vision Toolkit - Copyright (C) 2016 by Laurent Itti, the University of Southern
4 // California (USC), and iLab at USC. See http://iLab.usc.edu and http://jevois.org for information about this project.
5 //
6 // This file is part of the JeVois Smart Embedded Machine Vision Toolkit. This program is free software; you can
7 // redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software
8 // Foundation, version 2. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
9 // without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
10 // License for more details. You should have received a copy of the GNU General Public License along with this program;
11 // if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
12 //
13 // Contact information: Laurent Itti - 3641 Watt Way, HNB-07A - Los Angeles, CA 90089-2520 - USA.
14 // Tel: +1 213 740 3527 - itti@pollux.usc.edu - http://iLab.usc.edu - http://jevois.org
15 // ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
16 /*! \file */
17 
18 
19 // ##############################################################################################################
20 inline jevois::DynamicLoader::DynamicLoader(std::string const & filename, bool closeOnDestroy) :
21  itsCloseOnDestroy(closeOnDestroy)
22 {
23  // Clear any old errors:
24  dlerror();
25 
26  // Load the shared object file:
27  itsLibraryHandle = dlopen(filename.c_str(), RTLD_LAZY);
28 
29  // Throw an exception if there were any errors:
30  char const * err = dlerror();
31  if (err) LFATAL("Error opening shared library [" << filename << "]: " << err);
32 
33  // Remember the filename for future error messages and for sopath():
34  itsFilename = filename;
35 }
36 
37 // ##############################################################################################################
38 std::string const & jevois::DynamicLoader::sopath() const
39 { return itsFilename; }
40 
41 // ##############################################################################################################
43 {
44  if (itsCloseOnDestroy) close();
45 }
46 
47 // ##############################################################################################################
49 {
50  if (itsLibraryHandle) { dlclose(itsLibraryHandle); itsLibraryHandle = nullptr; }
51 }
52 
53 // ##############################################################################################################
54 template <class Signature> inline
55 std::function<Signature> jevois::DynamicLoader::load(std::string const & functionName)
56 {
57  // Clear any old errors:
58  dlerror();
59 
60  // Try to load the symbol from the shared object:
61  std::function<Signature> f = reinterpret_cast<Signature *>(dlsym(itsLibraryHandle, functionName.c_str()));
62 
63  // Throw an exception if there were any problems:
64  char const * err = dlerror();
65  if (err) LFATAL("Error loading symbol [" << functionName << "] from shared library [" << itsFilename << "]: " << err);
66 
67  return f;
68 }
void close()
Close the shared object file.
DynamicLoader(std::string const &filename, bool closeOnDestroy)
Open the shared object located at filename, throws if fail.
~DynamicLoader()
Destructor, may close the object on destroy (see constructor arguments)
std::function< Signature > load(std::string const &functionName)
Load the symbol named functionName.
#define LFATAL(msg)
Convenience macro for users to print out console or syslog messages, FATAL level. ...
Definition: Log.H:212
std::string const & sopath() const
Retrieve the path to the .so file of this loader, useful to avoid closing and re-loading the same ...