multi_library_class_loader.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2012, Willow Garage, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * * Neither the name of the Willow Garage, Inc. nor the names of its
14  * contributors may be used to endorse or promote products derived from
15  * this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */
29 
31 
32 #include <string>
33 #include <vector>
34 
35 namespace class_loader
36 {
37 
38 MultiLibraryClassLoader::MultiLibraryClassLoader(bool enable_ondemand_loadunload)
39 : enable_ondemand_loadunload_(enable_ondemand_loadunload)
40 {
41 }
42 
44 {
46 }
47 
49 {
50  std::vector<std::string> libraries;
51  for (
52  LibraryToClassLoaderMap::iterator itr = active_class_loaders_.begin();
53  itr != active_class_loaders_.end(); itr++)
54  {
55  libraries.push_back(itr->first);
56  }
57  return libraries;
58 }
59 
61 {
62  LibraryToClassLoaderMap::iterator itr = active_class_loaders_.find(library_path);
63  if (itr != active_class_loaders_.end()) {
64  return itr->second;
65  } else {return NULL;}
66 }
67 
69 {
70  ClassLoaderVector loaders;
71  for (
72  LibraryToClassLoaderMap::iterator itr = active_class_loaders_.begin();
73  itr != active_class_loaders_.end(); itr++)
74  {
75  loaders.push_back(itr->second);
76  }
77  return loaders;
78 }
79 
80 bool MultiLibraryClassLoader::isLibraryAvailable(const std::string & library_name)
81 {
82  return getClassLoaderForLibrary(library_name) != NULL;
83 }
84 
85 void MultiLibraryClassLoader::loadLibrary(const std::string & library_path)
86 {
87  if (!isLibraryAvailable(library_path)) {
88  active_class_loaders_[library_path] =
90  }
91 }
92 
94 {
95  std::vector<std::string> available_libraries = getRegisteredLibraries();
96  for (unsigned int c = 0; c < available_libraries.size(); c++) {
97  unloadLibrary(available_libraries.at(c));
98  }
99 }
100 
101 int MultiLibraryClassLoader::unloadLibrary(const std::string & library_path)
102 {
103  int remaining_unloads = 0;
104  LibraryToClassLoaderMap::iterator itr = active_class_loaders_.find(library_path);
105  if (itr != active_class_loaders_.end()) {
106  ClassLoader * loader = itr->second;
107  if (0 == (remaining_unloads = loader->unloadLibrary())) {
108  delete (loader);
109  active_class_loaders_.erase(itr);
110  }
111  }
112  return remaining_unloads;
113 }
114 
115 } // namespace class_loader
bool isLibraryAvailable(const std::string &library_path)
Indicates if a library has been loaded into memory.
int unloadLibrary(const std::string &library_path)
Unloads a library for this class loader.
This class allows loading and unloading of dynamically linked libraries which contain class definitio...
Definition: class_loader.h:64
std::vector< ClassLoader * > ClassLoaderVector
void loadLibrary(const std::string &library_path)
Loads a library into memory for this class loader.
void shutdownAllClassLoaders()
Destroys all ClassLoaders.
virtual ~MultiLibraryClassLoader()
Virtual destructor for class.
int unloadLibrary()
Attempts to unload a library loaded within scope of the ClassLoader. If the library is not opened...
std::vector< std::string > getRegisteredLibraries()
Gets a list of all libraries opened by this class loader .
MultiLibraryClassLoader(bool enable_ondemand_loadunload)
Constructor for the class.
ClassLoaderVector getAllAvailableClassLoaders()
Gets all class loaders loaded within scope.
bool isOnDemandLoadUnloadEnabled()
Indicates if on-demand (lazy) load/unload is enabled so libraries are loaded/unloaded automatically a...
ClassLoader * getClassLoaderForLibrary(const std::string &library_path)
Gets a handle to the class loader corresponding to a specific runtime library.


class_loader
Author(s): Mirza Shah
autogenerated on Thu Dec 7 2017 03:43:35