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
class_loader::MultiLibraryClassLoader::getClassLoaderForLibrary
ClassLoader * getClassLoaderForLibrary(const std::string &library_path)
Gets a handle to the class loader corresponding to a specific runtime library.
Definition: multi_library_class_loader.cpp:60
multi_library_class_loader.hpp
class_loader
Definition: class_loader.hpp:50
class_loader::MultiLibraryClassLoader::active_class_loaders_
LibraryToClassLoaderMap active_class_loaders_
Definition: multi_library_class_loader.hpp:357
class_loader::MultiLibraryClassLoader::getRegisteredLibraries
std::vector< std::string > getRegisteredLibraries()
Gets a list of all libraries opened by this class loader .
Definition: multi_library_class_loader.cpp:49
class_loader::MultiLibraryClassLoader::MultiLibraryClassLoader
MultiLibraryClassLoader(bool enable_ondemand_loadunload)
Constructor for the class.
Definition: multi_library_class_loader.cpp:39
class_loader::MultiLibraryClassLoader::loadLibrary
void loadLibrary(const std::string &library_path)
Loads a library into memory for this class loader.
Definition: multi_library_class_loader.cpp:82
class_loader::ClassLoaderVector
std::vector< ClassLoader * > ClassLoaderVector
Definition: multi_library_class_loader.hpp:50
class_loader::ClassLoader::unloadLibrary
CLASS_LOADER_PUBLIC int unloadLibrary()
Attempts to unload a library loaded within scope of the ClassLoader. If the library is not opened,...
Definition: class_loader.cpp:105
class_loader::ClassLoader
This class allows loading and unloading of dynamically linked libraries which contain class definitio...
Definition: class_loader.hpp:78
class_loader::MultiLibraryClassLoader::unloadLibrary
int unloadLibrary(const std::string &library_path)
Unloads a library for this class loader.
Definition: multi_library_class_loader.cpp:99
class_loader::MultiLibraryClassLoader::isLibraryAvailable
bool isLibraryAvailable(const std::string &library_path)
Indicates if a library has been loaded into memory.
Definition: multi_library_class_loader.cpp:77
class_loader::MultiLibraryClassLoader::isOnDemandLoadUnloadEnabled
bool isOnDemandLoadUnloadEnabled()
Indicates if on-demand (lazy) load/unload is enabled so libraries are loaded/unloaded automatically a...
Definition: multi_library_class_loader.hpp:316
class_loader::MultiLibraryClassLoader::~MultiLibraryClassLoader
virtual ~MultiLibraryClassLoader()
Virtual destructor for class.
Definition: multi_library_class_loader.cpp:44
class_loader::MultiLibraryClassLoader::getAllAvailableClassLoaders
ClassLoaderVector getAllAvailableClassLoaders()
Gets all class loaders loaded within scope.
Definition: multi_library_class_loader.cpp:68
class_loader::MultiLibraryClassLoader::shutdownAllClassLoaders
void shutdownAllClassLoaders()
Destroys all ClassLoaders.
Definition: multi_library_class_loader.cpp:90


class_loader
Author(s): Mirza Shah, Steven! Ragnarök
autogenerated on Fri Jan 12 2024 04:01:32