Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide stl api #95

Merged
merged 6 commits into from
Apr 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion include/class_loader/class_loader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,20 @@ class ClassLoader
* if the library is not yet loaded (which typically happens when in "On Demand Load/Unload" mode).
*
* @param derived_class_name The name of the class we want to create (@see getAvailableClasses())
* @return A boost::shared_ptr<Base> to newly created plugin object
* @return A std::shared_ptr<Base> to newly created plugin object
*/
template<class Base>
std::shared_ptr<Base> createSharedInstance(const std::string & derived_class_name)
{
return std::shared_ptr<Base>(
createRawInstance<Base>(derived_class_name, true),
boost::bind(&ClassLoader::onPluginDeletion<Base>, this, _1));
}

/**
* @brief Generates an instance of loadable classes (i.e. class_loader).
*
* Same as createSharedInstance() except it returns a boost::shared_ptr.
*/
template<class Base>
boost::shared_ptr<Base> createInstance(const std::string & derived_class_name)
Expand Down
60 changes: 47 additions & 13 deletions include/class_loader/multi_library_class_loader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,10 @@ class CLASS_LOADER_PUBLIC MultiLibraryClassLoader
* This version does not look in a specific library for the factory, but rather the first open library that defines the classs
* @param Base - polymorphic type indicating base class
* @param class_name - the name of the concrete plugin class we want to instantiate
* @return A boost::shared_ptr<Base> to newly created plugin
* @return A std::shared_ptr<Base> to newly created plugin
*/
template<class Base>
boost::shared_ptr<Base> createInstance(const std::string & class_name)
std::shared_ptr<Base> createSharedInstance(const std::string & class_name)
{
CONSOLE_BRIDGE_logDebug(
"class_loader::MultiLibraryClassLoader: "
Expand All @@ -90,7 +90,7 @@ class CLASS_LOADER_PUBLIC MultiLibraryClassLoader
"was explicitly loaded through MultiLibraryClassLoader::loadLibrary()");
}

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

/**
Expand All @@ -99,7 +99,48 @@ class CLASS_LOADER_PUBLIC MultiLibraryClassLoader
* @param Base - polymorphic type indicating base class
* @param class_name - the name of the concrete plugin class we want to instantiate
* @param library_path - the library from which we want to create the plugin
* @return A boost::shared_ptr<Base> to newly created plugin
* @return A std::shared_ptr<Base> to newly created plugin
*/
template<class Base>
std::shared_ptr<Base>
createSharedInstance(const std::string & class_name, const std::string & library_path)
{
ClassLoader * loader = getClassLoaderForLibrary(library_path);
if (nullptr == loader) {
throw class_loader::NoClassLoaderExistsException(
"Could not create instance as there is no ClassLoader in "
"MultiLibraryClassLoader bound to library " + library_path +
" Ensure you called MultiLibraryClassLoader::loadLibrary()");
}
return loader->createSharedInstance<Base>(class_name);
}

/**
* @brief Creates an instance of an object of given class name with ancestor class Base
* Same as createSharedInstance() except it returns a boost::shared_ptr.
*/
template<class Base>
boost::shared_ptr<Base> createInstance(const std::string & class_name)
{
CONSOLE_BRIDGE_logDebug(
"class_loader::MultiLibraryClassLoader: "
"Attempting to create instance of class type %s.",
class_name.c_str());
ClassLoader * loader = getClassLoaderForClass<Base>(class_name);
if (nullptr == loader) {
throw class_loader::CreateClassException(
"MultiLibraryClassLoader: Could not create object of class type " +
class_name +
" as no factory exists for it. Make sure that the library exists and "
"was explicitly loaded through MultiLibraryClassLoader::loadLibrary()");
}

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

/**
* @brief Creates an instance of an object of given class name with ancestor class Base
* Same as createSharedInstance() except it returns a boost::shared_ptr.
*/
template<class Base>
boost::shared_ptr<Base>
Expand All @@ -117,10 +158,7 @@ class CLASS_LOADER_PUBLIC MultiLibraryClassLoader

/**
* @brief Creates an instance of an object of given class name with ancestor class Base
* This version does not look in a specific library for the factory, but rather the first open library that defines the classs
* @param Base - polymorphic type indicating base class
* @param class_name - the name of the concrete plugin class we want to instantiate
* @return A unique pointer to newly created plugin
* Same as createSharedInstance() except it returns a std::unique_ptr.
*/
template<class Base>
ClassLoader::UniquePtr<Base> createUniqueInstance(const std::string & class_name)
Expand All @@ -141,11 +179,7 @@ class CLASS_LOADER_PUBLIC MultiLibraryClassLoader

/**
* @brief Creates an instance of an object of given class name with ancestor class Base
* This version takes a specific library to make explicit the factory being used
* @param Base - polymorphic type indicating base class
* @param class_name - the name of the concrete plugin class we want to instantiate
* @param library_path - the library from which we want to create the plugin
* @return A unique pointer to newly created plugin
* Same as createSharedInstance() except it returns a std::unique_ptr.
*/
template<class Base>
ClassLoader::UniquePtr<Base>
Expand Down
6 changes: 6 additions & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ if(TARGET ${PROJECT_NAME}_utest)
add_dependencies(${PROJECT_NAME}_utest ${PROJECT_NAME}_TestPlugins1 ${PROJECT_NAME}_TestPlugins2)
endif()

catkin_add_gtest(${PROJECT_NAME}_shared_ptr_test shared_ptr_test.cpp)
if(TARGET ${PROJECT_NAME}_shared_ptr_test)
target_link_libraries(${PROJECT_NAME}_shared_ptr_test ${Boost_LIBRARIES} ${class_loader_LIBRARIES})
add_dependencies(${PROJECT_NAME}_shared_ptr_test ${PROJECT_NAME}_TestPlugins1 ${PROJECT_NAME}_TestPlugins2)
endif()

catkin_add_gtest(${PROJECT_NAME}_unique_ptr_test unique_ptr_test.cpp)
if(TARGET ${PROJECT_NAME}_unique_ptr_test)
target_link_libraries(${PROJECT_NAME}_unique_ptr_test ${Boost_LIBRARIES} ${class_loader_LIBRARIES})
Expand Down
Loading