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 
34 #include "Poco/SharedLibrary.h"
35 
36 namespace class_loader
37 {
38 
40 
42 {
44 }
45 
46 std::string systemLibraryPrefix()
47 {
48 #ifndef _WIN32
49  return "lib";
50 #endif
51  return "";
52 }
53 
54 std::string systemLibrarySuffix()
55 {
56  return Poco::SharedLibrary::suffix();
57 }
58 
59 
60 std::string systemLibraryFormat(const std::string & library_name)
61 {
62  return systemLibraryPrefix() + library_name + systemLibrarySuffix();
63 }
64 
65 ClassLoader::ClassLoader(const std::string & library_path, bool ondemand_load_unload)
66 : ondemand_load_unload_(ondemand_load_unload),
67  library_path_(library_path),
68  load_ref_count_(0),
70 {
71  CONSOLE_BRIDGE_logDebug(
72  "class_loader.ClassLoader: "
73  "Constructing new ClassLoader (%p) bound to library %s.",
74  this, library_path.c_str());
76  loadLibrary();
77  }
78 }
79 
81 {
82  CONSOLE_BRIDGE_logDebug("%s",
83  "class_loader.ClassLoader: "
84  "Destroying class loader, unloading associated library...\n");
85  unloadLibrary(); // TODO(mikaelarguedas): while(unloadLibrary() > 0){} ??
86 }
87 
89 {
91 }
92 
94 {
96 }
97 
99 {
100  boost::recursive_mutex::scoped_lock lock(load_ref_count_mutex_);
103 }
104 
106 {
107  return unloadLibraryInternal(true);
108 }
109 
110 int ClassLoader::unloadLibraryInternal(bool lock_plugin_ref_count)
111 {
112  boost::recursive_mutex::scoped_lock load_ref_lock(load_ref_count_mutex_);
113  boost::recursive_mutex::scoped_lock plugin_ref_lock;
114  if (lock_plugin_ref_count) {
115  plugin_ref_lock = boost::recursive_mutex::scoped_lock(plugin_ref_count_mutex_);
116  }
117 
118  if (plugin_ref_count_ > 0) {
119  CONSOLE_BRIDGE_logWarn("class_loader.ClassLoader: SEVERE WARNING!!!\n"
120  "Attempting to unload %s\n"
121  "while objects created by this library still exist in the heap!\n"
122  "You should delete your objects before destroying the ClassLoader. "
123  "The library will NOT be unloaded.", library_path_.c_str());
124  } else {
126  if (0 == load_ref_count_) {
128  } else if (load_ref_count_ < 0) {
129  load_ref_count_ = 0;
130  }
131  }
132  return load_ref_count_;
133 }
134 
135 } // namespace class_loader
CLASS_LOADER_PUBLIC void loadLibrary(const std::string &library_path, ClassLoader *loader)
Loads a library into memory if it has not already been done so. Attempting to load an already loaded ...
CLASS_LOADER_PUBLIC bool isLibraryLoaded()
Indicates if a library is loaded within the scope of this ClassLoader. Note that the library may alre...
boost::recursive_mutex plugin_ref_count_mutex_
CLASS_LOADER_PUBLIC std::string systemLibraryFormat(const std::string &library_name)
Returns a platform specific version of a basic library name.
CLASS_LOADER_PUBLIC void unloadLibrary(const std::string &library_path, ClassLoader *loader)
Unloads a library if it loaded in memory and cleans up its corresponding class factories. If it is not loaded, the function has no effect.
CLASS_LOADER_PUBLIC bool isOnDemandLoadUnloadEnabled()
Indicates if the library is to be loaded/unloaded on demand...meaning that only to load a lib when th...
CLASS_LOADER_PUBLIC std::string systemLibrarySuffix()
Returns runtime library extension for native os.
CLASS_LOADER_PUBLIC ClassLoader(const std::string &library_path, bool ondemand_load_unload=false)
Constructor for ClassLoader.
CLASS_LOADER_PUBLIC int unloadLibraryInternal(bool lock_plugin_ref_count)
As the library may be unloaded in "on-demand load/unload" mode, unload maybe called from createInstan...
CLASS_LOADER_PUBLIC bool isLibraryLoadedByAnyClassloader()
Indicates if a library is loaded by some entity in the plugin system (another ClassLoader), but not necessarily loaded by this ClassLoader.
CLASS_LOADER_PUBLIC bool isLibraryLoaded(const std::string &library_path, ClassLoader *loader)
Indicates if passed library loaded within scope of a ClassLoader. The library maybe loaded in memory...
CLASS_LOADER_PUBLIC int unloadLibrary()
Attempts to unload a library loaded within scope of the ClassLoader. If the library is not opened...
CLASS_LOADER_PUBLIC std::string getLibraryPath()
Gets the full-qualified path and name of the library associated with this class loader.
virtual CLASS_LOADER_PUBLIC ~ClassLoader()
Destructor for ClassLoader. All libraries opened by this ClassLoader are unloaded automatically...
static CLASS_LOADER_PUBLIC bool has_unmananged_instance_been_created_
CLASS_LOADER_PUBLIC std::string systemLibraryPrefix()
Returns the default library prefix for the native os.
CLASS_LOADER_PUBLIC void loadLibrary()
Attempts to load a library on behalf of the ClassLoader. If the library is already opened...
static CLASS_LOADER_PUBLIC bool hasUnmanagedInstanceBeenCreated()
Getter for if an unmanaged (i.e. unsafe) instance has been created flag.
CLASS_LOADER_PUBLIC bool isLibraryLoadedByAnybody(const std::string &library_path)
Indicates if passed library has been loaded by ANY ClassLoader.
boost::recursive_mutex load_ref_count_mutex_


class_loader
Author(s): Mirza Shah, Steven! Ragnarök
autogenerated on Mon Feb 28 2022 22:02:03