Skip to content

Commit

Permalink
Plugin: Change appendDefinitions into replaceDefinitions
Browse files Browse the repository at this point in the history
  • Loading branch information
Pagghiu committed Jun 16, 2024
1 parent 8ad2aca commit 9466040
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
25 changes: 22 additions & 3 deletions Libraries/Plugin/Plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -617,13 +617,32 @@ SC::Result SC::PluginDynamicLibrary::load(const PluginCompiler& compiler, const
return Result(true);
}

SC::Result SC::PluginRegistry::appendDefinitions(Vector<PluginDefinition>&& definitions)
SC::Result SC::PluginRegistry::replaceDefinitions(Vector<PluginDefinition>&& definitions)
{
for (auto& definition : definitions)
SmallVector<String, 16> librariesToUnload;
// Unload libraries that have no match in the definitions
for (auto& item : libraries.items)
{
StringView libraryId = item.key.view();
if (not definitions.find([&](const PluginDefinition& it)
{ return it.identity.identifier.view() == libraryId; }))
{
SC_TRY(librariesToUnload.push_back(libraryId));
}
}

for (String& identifier : librariesToUnload)
{
SC_TRY(unloadPlugin(identifier.view()));
SC_TRY(libraries.remove(identifier));
}

for (PluginDefinition& definition : definitions)
{
PluginDynamicLibrary pdl;
pdl.definition = move(definition);
SC_TRY(libraries.insertIfNotExists({pdl.definition.identity.identifier, move(pdl)}));
// If the plugin already exists, it's fine, there's no need to return error
(void)libraries.insertIfNotExists({pdl.definition.identity.identifier, move(pdl)});
}
definitions.clear();
return Result(true);
Expand Down
4 changes: 2 additions & 2 deletions Libraries/Plugin/Plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,8 @@ struct SC::PluginRegistry
{
/// @brief Appends the definitions to registry
/// @param definitions found plugin definitions
/// @return Valid Result if the given definitions can be added to the libraries registry
[[nodiscard]] Result appendDefinitions(Vector<PluginDefinition>&& definitions);
/// @return Valid Result if definitions have been replaced successfully
Result replaceDefinitions(Vector<PluginDefinition>&& definitions);

/// @brief Instructs loadPlugin to Load or Reload the plugin
enum class LoadMode
Expand Down
2 changes: 1 addition & 1 deletion Libraries/Plugin/Tests/PluginTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ struct SC::PluginTest : public SC::TestCase

// Setup registry
PluginRegistry registry;
SC_TEST_EXPECT(registry.appendDefinitions(move(definitions)));
SC_TEST_EXPECT(registry.replaceDefinitions(move(definitions)));
SC_TEST_EXPECT(registry.loadPlugin(identifierChild, compiler, sysroot, report.executableFile));

// Check that plugins have been compiled and are valid
Expand Down

0 comments on commit 9466040

Please sign in to comment.