@@ -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)
0 commit comments