From 9039c956906cc8241d795759a5866f46d936f821 Mon Sep 17 00:00:00 2001 From: Alex Hirsch Date: Thu, 19 Nov 2020 23:47:47 +0100 Subject: [PATCH] Exercises: Add plugin system task --- exercises/README.md | 27 +++++++++++++++++++++++++++ exercises/task16/plugin.hpp | 19 +++++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 exercises/task16/plugin.hpp 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