JeVois  1.20
JeVois Smart Embedded Machine Vision Toolkit
Share this page:
DynamicLoader.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 // This file has been modified / imported from the Neuromorphic Robotics Toolkit (NRT). Original copyright is:
19 
20 /* author Randolph Voorhies
21  // ////////////////////////////////////////////////////////////////////////
22  // The iLab Neuromorphic Robotics Toolkit (NRT) //
23  // Copyright 2010-2012 by the University of Southern California (USC) //
24  // and the iLab at USC. //
25  // //
26  // iLab - University of Southern California //
27  // Hedco Neurociences Building, Room HNB-10 //
28  // Los Angeles, Ca 90089-2520 - USA //
29  // //
30  // See http://ilab.usc.edu for information about this project. //
31  // ////////////////////////////////////////////////////////////////////////
32  // This file is part of The iLab Neuromorphic Robotics Toolkit. //
33  // //
34  // The iLab Neuromorphic Robotics Toolkit is free software: you can //
35  // redistribute it and/or modify it under the terms of the GNU General //
36  // Public License as published by the Free Software Foundation, either //
37  // version 3 of the License, or (at your option) any later version. //
38  // //
39  // The iLab Neuromorphic Robotics Toolkit is distributed in the hope //
40  // that it will be useful, but WITHOUT ANY WARRANTY; without even the //
41  // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR //
42  // PURPOSE. See the GNU General Public License for more details. //
43  // //
44  // You should have received a copy of the GNU General Public License //
45  // along with The iLab Neuromorphic Robotics Toolkit. If not, see //
46  // <http://www.gnu.org/licenses/>. //
47  // ////////////////////////////////////////////////////////////////////////
48 */
49 
50 #pragma once
51 
52 #include <functional>
53 #include <string>
54 #include <exception>
55 #include <dlfcn.h>
56 #include <jevois/Debug/Log.H>
57 
58 namespace jevois
59 {
60  //! Class to open shared object (.so) files and load functions contained in them
61  /*! This class allows one to open a shared library (.so file) and to invoke functions or classes that are in that
62  library.
63 
64  Engine uses DynamicLoader to load the appropriate vision processing Module each time the user selects a new
65  VideoMapping (i.e., changes image resolution in a USB webcam program running on a host computer, or invokes the
66  \c setmapping command over Serial port).
67 
68  This class is modified from DynamicLoader in the Neuromorphic Robotics Toolkit (NRT). \ingroup core */
70  {
71  public:
72  //! Open the shared object located at filename, throws if fail
73  /*! If closeOnDestroy is true, the .so file will be closed when the DynamicLoader object is destroyed. If you want
74  to maintain access to the functions and classes in a .so file after the loader has run out of scope, set
75  closeOnDestroy to false (and you will then have no way of ever unloading that .so file until termination of
76  your program). */
77  DynamicLoader(std::string const & filename, bool closeOnDestroy);
78 
79  //! Retrieve the path to the .so file of this loader, useful to avoid closing and re-loading the same .so file
80  std::string const & sopath() const;
81 
82  //! Destructor, may close the object on destroy (see constructor arguments)
84 
85  //! Close the shared object file
86  /*! Beware that closing the object file will remove all links to any objects/functions/etc that have been created
87  from the shared object. Use this function only if you want nothing more to do with anything created from this
88  object file. */
89  void close();
90 
91  //! Load the symbol named functionName
92  /*! This method returns an std::function that points to the requested name in the shared object opened by this
93  DynamicLoader. For example, to load the function <code>int foo(double x, bool y)</code> from the file
94  "mySharedObject.so" you should write:
95 
96  @code
97  jevois::DynamicLoader loader("./mySharedObject.so");
98  try
99  {
100  std::function<int(double,bool)> foo = loader.load<int(double,bool)>("foo");
101  int result = foo(3.0, true);
102  }
103  catch (std::exception const & e)
104  {
105  LFATAL(e.what());
106  }
107  @endcode
108 
109  You should not try to use anything returned by load() after the DynamicLoader has been close()'d (or
110  destroyed, see constructor arguments).
111 
112  Throws if the requested symbol could not be found in the opened shared object. */
113  template <class Signature>
114  std::function<Signature> load(std::string const & functionName);
115 
116  private:
117  bool itsCloseOnDestroy;
118  void * itsLibraryHandle;
119  std::string itsFilename;
120  };
121 }
122 
123 
124 // Implementation details:
125 #include <jevois/Core/details/DynamicLoaderImpl.H>
jevois::DynamicLoader::~DynamicLoader
~DynamicLoader()
Destructor, may close the object on destroy (see constructor arguments)
jevois::DynamicLoader::DynamicLoader
DynamicLoader(std::string const &filename, bool closeOnDestroy)
Open the shared object located at filename, throws if fail.
jevois::DynamicLoader::sopath
const std::string & sopath() const
Retrieve the path to the .so file of this loader, useful to avoid closing and re-loading the same ....
jevois::DynamicLoader
Class to open shared object (.so) files and load functions contained in them.
Definition: DynamicLoader.H:69
jevois::DynamicLoader::load
std::function< Signature > load(std::string const &functionName)
Load the symbol named functionName.
jevois
Definition: Concepts.dox:1
Log.H
jevois::DynamicLoader::close
void close()
Close the shared object file.