-
Notifications
You must be signed in to change notification settings - Fork 8
C++ API for loading plugins #267
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
Open
RMeli
wants to merge
6
commits into
metatomic-core
Choose a base branch
from
rmeli-cpp-plugin-load
base: metatomic-core
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,51 @@ | ||
| #pragma once | ||
|
|
||
| #include <string> | ||
|
|
||
| #include <metatomic.h> | ||
| #include <metatomic/errors.hpp> | ||
|
|
||
| namespace metatomic { | ||
| /// Load the shared library at `path` and register the plugin contained | ||
| /// within. The library must export the symbols generated by the | ||
| /// `MTA_REGISTER_PLUGIN` macro. | ||
| /// | ||
| /// @param path path to the plugin shared library | ||
| inline void load_plugin(const std::string& path) { | ||
| auto status = mta_load_plugin(path.c_str()); | ||
| details::check_status(status); | ||
| } | ||
|
|
||
| /// Load a model from `load_from` with the given options. | ||
| /// | ||
| /// If `plugin_name` is empty, metatomic will try to determine the correct | ||
| /// plugin to use by checking the `load_from` parameter. If we can not | ||
| /// determine the correct plugin, we then try to load the model with each | ||
| /// registered plugin until one succeeds. | ||
| /// | ||
| /// If `plugin_name` is given, then we only try to load the model with the | ||
| /// specified plugin, and return an error if the plugin can not load the | ||
| /// model. | ||
| /// | ||
| /// @param load_from where to load the model from (e.g. a file path, a | ||
| /// model name, etc.) | ||
| /// @param plugin_name optional name of the plugin to use for loading the | ||
| /// model, or empty to let metatomic search | ||
| /// @param options_json optional JSON object containing string keys and | ||
| /// string values for loading the model | ||
| /// @return the loaded model | ||
| inline mta_model_t load_model( | ||
| const std::string& load_from, | ||
| const std::string& options_json = "", | ||
| const std::string& plugin_name = "" | ||
| ) { | ||
| mta_model_t model; | ||
| const char* plugin_name_ptr = plugin_name.empty() ? nullptr : plugin_name.c_str(); | ||
| const char* options_json_ptr = options_json.empty() ? nullptr : options_json.c_str(); | ||
|
|
||
| auto status = mta_load_model(load_from.c_str(), options_json_ptr, plugin_name_ptr, &model); | ||
| details::check_status(status); | ||
|
|
||
| return model; | ||
| } | ||
| } // namespace metatomic | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,45 +1,71 @@ | ||
| #include <catch.hpp> | ||
|
|
||
| #include "metatomic.h" | ||
| #include "metatomic.hpp" | ||
|
|
||
|
|
||
| TEST_CASE("Load plugins") { | ||
| auto status = mta_load_plugin(PLUGIN_DIR "/test-c-plugin.so"); | ||
| CHECK(status == MTA_SUCCESS); | ||
| SECTION("C API") { | ||
| auto status = mta_load_plugin(PLUGIN_DIR "/test-c-plugin.so"); | ||
| CHECK(status == MTA_SUCCESS); | ||
|
|
||
| // try to load the model with an explicit plugin name | ||
| struct mta_model_t model; | ||
| status = mta_load_model("test-c-plugin", "some_model", "{}", &model); | ||
| CHECK(status == MTA_MODEL_NOT_SUPPORTED_ERROR); | ||
| const char* error_message; | ||
| const char* error_origin; | ||
|
|
||
| // load the plugin without specifying the plugin name | ||
| status = mta_load_model(nullptr, "some_model", "{}", &model); | ||
| CHECK(status == MTA_INVALID_PARAMETER_ERROR); | ||
| struct mta_model_t model; | ||
| status = mta_load_model("some_model", "{}", "test-c-plugin", &model); | ||
| CHECK(status == MTA_INVALID_PARAMETER_ERROR); | ||
|
|
||
| const char* error_message; | ||
| const char* error_origin; | ||
| status = mta_last_error(&error_message, &error_origin, nullptr); | ||
| REQUIRE(status == MTA_SUCCESS); | ||
|
|
||
| status = mta_last_error(&error_message, &error_origin, nullptr); | ||
| REQUIRE(status == MTA_SUCCESS); | ||
| CHECK(std::string(error_origin) == "metatomic-core"); | ||
| CHECK(std::string(error_message) == ( | ||
| "invalid parameter: failed to load model from 'some_model': plugin 'test-c-plugin' could not load the model" | ||
| )); | ||
|
|
||
| CHECK(std::string(error_origin) == "metatomic-core"); | ||
| const char* expected_message = ( | ||
| "invalid parameter: failed to load model from 'some_model': tried the " | ||
| "following plugins, but none could load the model: test-c-plugin" | ||
| ); | ||
| CHECK(std::string(error_message) == expected_message); | ||
| status = mta_load_model("some_model", "{}", nullptr, &model); | ||
| CHECK(status == MTA_INVALID_PARAMETER_ERROR); | ||
|
|
||
| status = mta_last_error(&error_message, &error_origin, nullptr); | ||
| REQUIRE(status == MTA_SUCCESS); | ||
|
|
||
| status = mta_load_plugin(PLUGIN_DIR "/bad-abi-plugin.so"); | ||
| CHECK(status == MTA_INVALID_PARAMETER_ERROR); | ||
| CHECK(std::string(error_origin) == "metatomic-core"); | ||
| CHECK(std::string(error_message) == ( | ||
| "invalid parameter: failed to load model from 'some_model': tried the " | ||
| "following plugins, but none could load the model: test-c-plugin" | ||
| )); | ||
|
|
||
| status = mta_last_error(&error_message, &error_origin, nullptr); | ||
| REQUIRE(status == MTA_SUCCESS); | ||
|
|
||
| CHECK(std::string(error_origin) == "metatomic-core"); | ||
| expected_message = ( | ||
| "invalid parameter: can not register plugin 'bad-abi-plugin': " | ||
| "plugin ABI version is 2, but metatomic expects 1" | ||
| ); | ||
| CHECK(std::string(error_message) == expected_message); | ||
| status = mta_load_plugin(PLUGIN_DIR "/bad-abi-plugin.so"); | ||
| CHECK(status == MTA_INVALID_PARAMETER_ERROR); | ||
|
|
||
| status = mta_last_error(&error_message, &error_origin, nullptr); | ||
| REQUIRE(status == MTA_SUCCESS); | ||
|
|
||
| CHECK(std::string(error_origin) == "metatomic-core"); | ||
| CHECK(std::string(error_message) == ( | ||
| "invalid parameter: can not register plugin 'bad-abi-plugin': " | ||
| "plugin ABI version is 2, but metatomic expects 1" | ||
| )); | ||
| } | ||
|
|
||
| SECTION("C++ API") { | ||
| REQUIRE_THROWS_WITH( | ||
| metatomic::load_model("some_model", "{}", "test-c-plugin"), | ||
| "invalid parameter: failed to load model from 'some_model': plugin 'test-c-plugin' could not load the model" | ||
| ); | ||
|
|
||
| REQUIRE_THROWS_WITH( | ||
| metatomic::load_model("some_model"), | ||
| "invalid parameter: failed to load model from 'some_model': tried the " | ||
| "following plugins, but none could load the model: test-c-plugin" | ||
| ); | ||
|
|
||
| REQUIRE_THROWS_WITH( | ||
| metatomic::load_plugin(PLUGIN_DIR "/bad-abi-plugin.so"), | ||
| "invalid parameter: can not register plugin 'bad-abi-plugin': " | ||
| "plugin ABI version is 2, but metatomic expects 1" | ||
| ); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.