diff --git a/exercises/README.md b/exercises/README.md index 5143d71..bf058b5 100644 --- a/exercises/README.md +++ b/exercises/README.md @@ -224,3 +224,30 @@ Take your vector implementation from Task 14 and instantiate it with a big numbe Inspect the relationship between the number of unique instantiates and compile time. Furthermore, look at the compiled object file using `nm`. + +## Task 16 + +In this task you have to create a rudimentary plugin system. + +You are given `plugin.hpp` which contains an interface for your plugins, as well as the function name of the constructor function and its type. +Note that the constructor function returns an `std::unique_ptr`. + +- create an executable which *dynamically* loads plugins and executes their `run` member function +- create two different plugins (`foo` and `bar`) showing off the plugin system + +It could look like this: + + $ ./main ./libFoo.so + Creation of first plugin + Running the first plugin + Destruction of first plugin + + $ ./main ./libFoo.so ./libBar.so + Creation of first plugin + Running the first plugin + Destruction of first plugin + Creation of second plugin + Running the second plugin + Destruction of second plugin + +*Hint:* Have a look at the related man-pages *dlopen(3)* and *dlsym(3)*. diff --git a/exercises/task16/plugin.hpp b/exercises/task16/plugin.hpp new file mode 100644 index 0000000..a0f75ca --- /dev/null +++ b/exercises/task16/plugin.hpp @@ -0,0 +1,19 @@ +#ifndef PLUGIN_HPP +#define PLUGIN_HPP + +#include + +/// Interface for plugins. +class Plugin { + public: + virtual void run() = 0; + virtual ~Plugin(){}; +}; + +/// Symbol of the plugin constructor function. +const auto PLUGIN_CONSTRUCTOR = "create_plugin"; + +/// Type of the plugin constructor function. +using PLUGIN_CONSTRUCTOR_T = std::unique_ptr (*)(); + +#endif // PLUGIN_HPP