Skip to content

Commit 2464325

Browse files
committed
Refactoring creation of plugin
1 parent 98f5e8d commit 2464325

File tree

2 files changed

+11
-9
lines changed

2 files changed

+11
-9
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ These are required when you include related headers to access internal data.
3838

3939
## Tested Platforms
4040
I tested it in the following platforms:
41-
- Intel CPU, NVIDIA GPU, Windows 10 64-bit, Visual Studio 2013 / Visual Studio 2017
41+
- Intel CPU, NVIDIA GPU, Windows 10 64-bit, Visual Studio 2017
4242
- Intel CPU, Intel GPU (UHD Graphics 620), Windows 10 64-bit, Visual Studio 2017
4343

4444
Visual Studio 2015 may be able to compile it, but I did not test.

src/rpcore/pluginbase/manager.cpp

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ class PluginManager::Impl
9696

9797
bool requires_daytime_settings_;
9898

99-
std::unordered_map<std::string, std::shared_ptr<BasePlugin>> instances_;
99+
std::unordered_map<std::string, std::unique_ptr<BasePlugin>> instances_;
100100

101101
std::unordered_set<std::string> enabled_plugins_;
102102
std::unordered_map<std::string, std::function<PluginCreatorType>> plugin_creators_;
@@ -124,10 +124,6 @@ void PluginManager::Impl::unload()
124124

125125
for (auto&& id_handle: instances_)
126126
{
127-
const auto count = id_handle.second.use_count();
128-
if (count != 1)
129-
self_.warn(fmt::format("Plugin ({}) is used as {} on somewhere before unloading.", id_handle.first, count));
130-
131127
// delete plugin instance.
132128
id_handle.second.reset();
133129
}
@@ -154,12 +150,18 @@ std::unique_ptr<BasePlugin> PluginManager::Impl::load_plugin(const std::string&
154150

155151
try
156152
{
157-
plugin_creators_[plugin_id] = boost::dll::import_alias<PluginCreatorType>(
153+
auto result = plugin_creators_.insert({plugin_id, boost::dll::import_alias<PluginCreatorType>(
158154
plugin_path,
159155
"create_plugin",
160-
boost::dll::load_mode::append_decorations);
156+
boost::dll::load_mode::rtld_global | boost::dll::load_mode::append_decorations)});
157+
158+
if (!result.second)
159+
{
160+
self_.error(fmt::format("Plugin '{}' was already loaded.", plugin_id));
161+
return nullptr;
162+
}
161163

162-
auto instance = plugin_creators_.at(plugin_id)(pipeline_);
164+
auto instance = result.first->second(pipeline_);
163165

164166
for (const auto& required_plugin: instance->get_required_plugins())
165167
{

0 commit comments

Comments
 (0)