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 <cstddef>
33 #include <string>
34 #include <vector>
35 
36 namespace class_loader
37 {
38 
39 MultiLibraryClassLoader::MultiLibraryClassLoader(bool enable_ondemand_loadunload)
40 : enable_ondemand_loadunload_(enable_ondemand_loadunload)
41 {
42 }
43 
45 {
47 }
48 
50 {
51  std::vector<std::string> libraries;
52  for (auto & it : active_class_loaders_) {
53  if (it.second != nullptr) {
54  libraries.push_back(it.first);
55  }
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 nullptr;}
66 }
67 
69 {
70  ClassLoaderVector loaders;
71  for (auto & it : active_class_loaders_) {
72  loaders.push_back(it.second);
73  }
74  return loaders;
75 }
76 
77 bool MultiLibraryClassLoader::isLibraryAvailable(const std::string & library_name)
78 {
79  return getClassLoaderForLibrary(library_name) != nullptr;
80 }
81 
82 void MultiLibraryClassLoader::loadLibrary(const std::string & library_path)
83 {
84  if (!isLibraryAvailable(library_path)) {
85  active_class_loaders_[library_path] =
87  }
88 }
89 
91 {
92  std::vector<std::string> available_libraries = getRegisteredLibraries();
93 
94  for (auto & library_path : getRegisteredLibraries()) {
95  unloadLibrary(library_path);
96  }
97 }
98 
99 int MultiLibraryClassLoader::unloadLibrary(const std::string & library_path)
100 {
101  int remaining_unloads = 0;
102  LibraryToClassLoaderMap::iterator itr = active_class_loaders_.find(library_path);
103  if (itr != active_class_loaders_.end()) {
104  ClassLoader * loader = itr->second;
105  if (0 == (remaining_unloads = loader->unloadLibrary())) {
106  delete (loader);
107  active_class_loaders_.erase(itr);
108  }
109  }
110  return remaining_unloads;
111 }
112 
113 } // 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...
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.
CLASS_LOADER_PUBLIC 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, Steven! Ragnarök
autogenerated on Mon Feb 28 2022 22:02:03