Chi-Tech
PlugIn.cc
Go to the documentation of this file.
1#include "PlugIn.h"
2
3#include "ChiObjectFactory.h"
4
5#include "chi_runtime.h"
6#include "chi_log.h"
7#include "utils/chi_utils.h"
9
10#include <dlfcn.h>
11
12namespace chi
13{
14
16
18{
20
21 // clang-format off
23 "Object to handle the loading of shared libraries as plug-ins");
24 // clang-format on
25 params.SetDocGroup("DocInterfaces");
26
27 params.AddRequiredParameter<std::string>(
28 "plugin_path", "Path to the shared library containing the plug-in.");
29 params.AddOptionalParameter("entry_function", "", "Entry function to call.");
30
31 return params;
32}
33
35 : ChiObject(params),
36 plugin_path_(params.GetParamValue<std::string>("plugin_path"))
37{
38 Chi::log.Log0Verbose1() << "Loading plugin \"" << plugin_path_ << "\"";
40
42 library_handle_ = dlopen(plugin_path_.c_str(), RTLD_LAZY);
43
45 "Failure loading \"" + plugin_path_ + "\"");
46
47 const auto& user_params = params.ParametersAtAssignment();
48 if (user_params.Has("entry_function"))
49 {
50 const auto& entry_function =
51 user_params.GetParamValue<std::string>("entry_function");
52 typedef void(some_func)();
53
54 auto func = (some_func*)dlsym(library_handle_, entry_function.c_str());
55
57 not func, "Failed to call entry function \"" + entry_function + "\"");
58
59 // Calling the function
60 func();
61 }
62
63 Chi::console.UpdateConsoleBindings(registry_statuses);
64}
65
67
68} // namespace chi
#define ChiLogicalErrorIf(condition, message)
static chi::RegistryStatuses GetStatusOfRegistries()
Definition: chi_runtime.cc:350
static chi::ChiLog & log
Definition: chi_runtime.h:81
static chi::Console & console
Definition: chi_runtime.h:80
static chi::InputParameters GetInputParameters()
Definition: ChiObject.cc:4
LogStream Log0Verbose1()
Definition: chi_log.h:234
void UpdateConsoleBindings(const chi::RegistryStatuses &old_statuses)
void SetDocGroup(const std::string &doc_group)
void AddRequiredParameter(const std::string &name, const std::string &doc_string)
const ParameterBlock & ParametersAtAssignment() const
void AddOptionalParameter(const std::string &name, T value, const std::string &doc_string)
void SetGeneralDescription(const std::string &description)
T GetParamValue(const std::string &param_name) const
void * library_handle_
Definition: PlugIn.h:19
Plugin(const InputParameters &params)
Definition: PlugIn.cc:34
const std::string plugin_path_
Definition: PlugIn.h:18
static InputParameters GetInputParameters()
Definition: PlugIn.cc:17
void AssertReadibleFile(const std::string &file_name)
RegisterChiObject(chi, KBAGraphPartitioner)