Skip to content

Commit 6a800a4

Browse files
committed
Use exceptions
Signed-off-by: ahcorde <ahcorde@gmail.com>
1 parent 58d57ed commit 6a800a4

File tree

2 files changed

+10
-14
lines changed

2 files changed

+10
-14
lines changed

include/class_loader/class_loader.hpp

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -122,16 +122,14 @@ class ClassLoader : public std::enable_shared_from_this<ClassLoader>
122122
* @return A std::shared_ptr<Base> to newly created plugin object
123123
*/
124124
template<class Base>
125-
std::shared_ptr<Base> createInstance(
126-
const std::string & derived_class_name,
127-
bool is_shared_ptr = false)
125+
std::shared_ptr<Base> createInstance(const std::string & derived_class_name)
128126
{
129-
if (is_shared_ptr) {
127+
try {
130128
return std::shared_ptr<Base>(
131129
createRawInstance<Base>(derived_class_name, true),
132130
std::bind(&ClassLoader::onPluginDeletion<Base>, shared_from_this(), std::placeholders::_1)
133131
);
134-
} else {
132+
} catch (std::bad_weak_ptr & e) { // This is not a shared_ptr
135133
return std::shared_ptr<Base>(
136134
createRawInstance<Base>(derived_class_name, true),
137135
std::bind(&ClassLoader::onPluginDeletion<Base>, this, std::placeholders::_1)
@@ -153,17 +151,15 @@ class ClassLoader : public std::enable_shared_from_this<ClassLoader>
153151
* @return A std::unique_ptr<Base> to newly created plugin object.
154152
*/
155153
template<class Base>
156-
UniquePtr<Base> createUniqueInstance(
157-
const std::string & derived_class_name,
158-
bool is_shared_ptr = false)
154+
UniquePtr<Base> createUniqueInstance(const std::string & derived_class_name)
159155
{
160156
Base * raw = createRawInstance<Base>(derived_class_name, true);
161-
if (is_shared_ptr) {
157+
try {
162158
return std::unique_ptr<Base, DeleterType<Base>>(
163159
raw,
164160
std::bind(&ClassLoader::onPluginDeletion<Base>, shared_from_this(), std::placeholders::_1)
165161
);
166-
} else {
162+
} catch (std::bad_weak_ptr & e) { // This is not a shared_ptr
167163
return std::unique_ptr<Base, DeleterType<Base>>(
168164
raw,
169165
std::bind(&ClassLoader::onPluginDeletion<Base>, this, std::placeholders::_1)

include/class_loader/multi_library_class_loader.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ class CLASS_LOADER_PUBLIC MultiLibraryClassLoader
105105
"was explicitly loaded through MultiLibraryClassLoader::loadLibrary()");
106106
}
107107

108-
return loader->createInstance<Base>(class_name, true);
108+
return loader->createInstance<Base>(class_name);
109109
}
110110

111111
/**
@@ -128,7 +128,7 @@ class CLASS_LOADER_PUBLIC MultiLibraryClassLoader
128128
"MultiLibraryClassLoader bound to library " + library_path +
129129
" Ensure you called MultiLibraryClassLoader::loadLibrary()");
130130
}
131-
return loader->createInstance<Base>(class_name, true);
131+
return loader->createInstance<Base>(class_name);
132132
}
133133

134134
/// Creates an instance of an object of given class name with ancestor class Base
@@ -154,7 +154,7 @@ class CLASS_LOADER_PUBLIC MultiLibraryClassLoader
154154
"Make sure that the library exists and was explicitly loaded through "
155155
"MultiLibraryClassLoader::loadLibrary()");
156156
}
157-
return loader->createUniqueInstance<Base>(class_name, true);
157+
return loader->createUniqueInstance<Base>(class_name);
158158
}
159159

160160
/// Creates an instance of an object of given class name with ancestor class Base
@@ -177,7 +177,7 @@ class CLASS_LOADER_PUBLIC MultiLibraryClassLoader
177177
"MultiLibraryClassLoader bound to library " + library_path +
178178
" Ensure you called MultiLibraryClassLoader::loadLibrary()");
179179
}
180-
return loader->createUniqueInstance<Base>(class_name, true);
180+
return loader->createUniqueInstance<Base>(class_name);
181181
}
182182

183183
/**

0 commit comments

Comments
 (0)