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 namespace class_loader
35 {
36 
38 
40 {
42 }
43 
44 std::string systemLibrarySuffix()
45 {
46  return Poco::SharedLibrary::suffix();
47 }
48 
49 ClassLoader::ClassLoader(const std::string & library_path, bool ondemand_load_unload)
50 : ondemand_load_unload_(ondemand_load_unload),
51  library_path_(library_path),
52  load_ref_count_(0),
54 {
56  "class_loader.ClassLoader: "
57  "Constructing new ClassLoader (%p) bound to library %s.",
58  this, library_path.c_str());
60  loadLibrary();
61  }
62 }
63 
65 {
67  "class_loader.ClassLoader: "
68  "Destroying class loader, unloading associated library...\n");
69  unloadLibrary(); // TODO(mikaelarguedas): while(unloadLibrary() > 0){} ??
70 }
71 
73 {
75 }
76 
78 {
80 }
81 
83 {
84  boost::recursive_mutex::scoped_lock lock(load_ref_count_mutex_);
87 }
88 
90 {
91  return unloadLibraryInternal(true);
92 }
93 
94 int ClassLoader::unloadLibraryInternal(bool lock_plugin_ref_count)
95 {
96  boost::recursive_mutex::scoped_lock load_ref_lock(load_ref_count_mutex_);
97  boost::recursive_mutex::scoped_lock plugin_ref_lock;
98  if (lock_plugin_ref_count) {
99  plugin_ref_lock = boost::recursive_mutex::scoped_lock(plugin_ref_count_mutex_);
100  }
101 
102  if (plugin_ref_count_ > 0) {
104  "class_loader.ClassLoader: "
105  "SEVERE WARNING!!! Attempting to unload library while objects created by this loader "
106  "exist in the heap! "
107  "You should delete your objects before attempting to unload the library or "
108  "destroying the ClassLoader. The library will NOT be unloaded.");
109  } else {
111  if (0 == load_ref_count_) {
113  } else if (load_ref_count_ < 0) {
114  load_ref_count_ = 0;
115  }
116  }
117  return load_ref_count_;
118 }
119 
120 } // namespace class_loader
#define CONSOLE_BRIDGE_logWarn(fmt,...)
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_
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 ...
#define CONSOLE_BRIDGE_logDebug(fmt,...)
std::string systemLibrarySuffix()
ClassLoader(const std::string &library_path, bool ondemand_load_unload=false)
Constructor for ClassLoader.
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.
int unloadLibraryInternal(bool lock_plugin_ref_count)
As the library may be unloaded in "on-demand load/unload" mode, unload maybe called from createInstan...
bool isLibraryLoadedByAnyClassloader()
Indicates if a library is loaded by some entity in the plugin system (another ClassLoader), but not necessarily loaded by this ClassLoader.
bool isOnDemandLoadUnloadEnabled()
Indicates if the library is to be loaded/unloaded on demand...meaning that only to load a lib when th...
int unloadLibrary()
Attempts to unload a library loaded within scope of the ClassLoader. If the library is not opened...
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...
virtual ~ClassLoader()
Destructor for ClassLoader. All libraries opened by this ClassLoader are unloaded automatically...
static bool has_unmananged_instance_been_created_
std::string getLibraryPath()
Gets the full-qualified path and name of the library associated with this class loader.
bool isLibraryLoadedByAnybody(const std::string &library_path)
Indicates if passed library has been loaded by ANY ClassLoader.
void loadLibrary()
Attempts to load a library on behalf of the ClassLoader. If the library is already opened...
static bool hasUnmanagedInstanceBeenCreated()
Getter for if an unmanaged (i.e. unsafe) instance has been created flag.
boost::recursive_mutex load_ref_count_mutex_


class_loader
Author(s): Mirza Shah
autogenerated on Mon Jun 10 2019 12:51:50