Skip to content

Commit

Permalink
Use exceptions
Browse files Browse the repository at this point in the history
Signed-off-by: ahcorde <ahcorde@gmail.com>
  • Loading branch information
ahcorde committed Jun 25, 2021
1 parent 58d57ed commit 6a800a4
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 14 deletions.
16 changes: 6 additions & 10 deletions include/class_loader/class_loader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,16 +122,14 @@ class ClassLoader : public std::enable_shared_from_this<ClassLoader>
* @return A std::shared_ptr<Base> to newly created plugin object
*/
template<class Base>
std::shared_ptr<Base> createInstance(
const std::string & derived_class_name,
bool is_shared_ptr = false)
std::shared_ptr<Base> createInstance(const std::string & derived_class_name)
{
if (is_shared_ptr) {
try {
return std::shared_ptr<Base>(
createRawInstance<Base>(derived_class_name, true),
std::bind(&ClassLoader::onPluginDeletion<Base>, shared_from_this(), std::placeholders::_1)
);
} else {
} catch (std::bad_weak_ptr & e) { // This is not a shared_ptr
return std::shared_ptr<Base>(
createRawInstance<Base>(derived_class_name, true),
std::bind(&ClassLoader::onPluginDeletion<Base>, this, std::placeholders::_1)
Expand All @@ -153,17 +151,15 @@ class ClassLoader : public std::enable_shared_from_this<ClassLoader>
* @return A std::unique_ptr<Base> to newly created plugin object.
*/
template<class Base>
UniquePtr<Base> createUniqueInstance(
const std::string & derived_class_name,
bool is_shared_ptr = false)
UniquePtr<Base> createUniqueInstance(const std::string & derived_class_name)
{
Base * raw = createRawInstance<Base>(derived_class_name, true);
if (is_shared_ptr) {
try {
return std::unique_ptr<Base, DeleterType<Base>>(
raw,
std::bind(&ClassLoader::onPluginDeletion<Base>, shared_from_this(), std::placeholders::_1)
);
} else {
} catch (std::bad_weak_ptr & e) { // This is not a shared_ptr
return std::unique_ptr<Base, DeleterType<Base>>(
raw,
std::bind(&ClassLoader::onPluginDeletion<Base>, this, std::placeholders::_1)
Expand Down
8 changes: 4 additions & 4 deletions include/class_loader/multi_library_class_loader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ class CLASS_LOADER_PUBLIC MultiLibraryClassLoader
"was explicitly loaded through MultiLibraryClassLoader::loadLibrary()");
}

return loader->createInstance<Base>(class_name, true);
return loader->createInstance<Base>(class_name);
}

/**
Expand All @@ -128,7 +128,7 @@ class CLASS_LOADER_PUBLIC MultiLibraryClassLoader
"MultiLibraryClassLoader bound to library " + library_path +
" Ensure you called MultiLibraryClassLoader::loadLibrary()");
}
return loader->createInstance<Base>(class_name, true);
return loader->createInstance<Base>(class_name);
}

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

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

/**
Expand Down

0 comments on commit 6a800a4

Please sign in to comment.