Ring Daemon 16.0.0
Loading...
Searching...
No Matches
pluginloader.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2004-2025 Savoir-faire Linux Inc.
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17#pragma once
18
19#include "jamiplugin.h"
20#include <dlfcn.h>
21#include <string>
22#include <memory>
23
24namespace jami {
25
30class Plugin
31{
32public:
33 virtual ~Plugin() = default;
34
39 static Plugin* load(const std::string& path, std::string& error);
40 virtual void* getSymbol(const char* name) const = 0;
41
47 {
48 return reinterpret_cast<JAMI_PluginInitFunc>(getSymbol(JAMI_DYN_INIT_FUNC_NAME));
49 }
50
51protected:
52 Plugin() = default;
53};
54
59class DLPlugin : public Plugin
60{
61public:
62 DLPlugin(void* handle, const std::string& path)
63 : handle_(handle, ::dlclose)
64 , path_ {path}
65 {
66 api_.context = this;
67 }
68
69 virtual ~DLPlugin() { unload(); }
70
75 bool unload()
76 {
77 if (!handle_) {
78 return false;
79 }
80 return !(::dlclose(handle_.release()));
81 }
82
88 void* getSymbol(const char* name) const
89 {
90 if (!handle_)
91 return nullptr;
92
93 return ::dlsym(handle_.get(), name);
94 }
95
96 const std::string& getPath() const { return path_; }
97
98public:
101
102private:
103 // Pointer to the loaded library returned by dlopen
104 std::unique_ptr<void, int (*)(void*)> handle_;
105 // Plugin's data path
106 const std::string path_;
107};
108
109} // namespace jami
This class is used after a plugin library is successfully loaded.
const std::string & getPath() const
DLPlugin(void *handle, const std::string &path)
bool unload()
Unload plugin's library.
void * getSymbol(const char *name) const
Searchs for symbol in library.
JAMI_PluginAPI api_
virtual ~DLPlugin()
This class is used to attempt loading a plugin library.
virtual JAMI_PluginInitFunc getInitFunction() const
Search loaded library for its initialization function.
static Plugin * load(const std::string &path, std::string &error)
Load plugin's library.
virtual ~Plugin()=default
virtual void * getSymbol(const char *name) const =0
Plugin()=default
#define JAMI_DYN_INIT_FUNC_NAME
Definition jamiplugin.h:111
JAMI_PluginExitFunc(* JAMI_PluginInitFunc)(const JAMI_PluginAPI *api)
Definition jamiplugin.h:107
void emitSignal(Args... args)
Definition ring_signal.h:64
This structure is filled by the PluginManager.
Definition jamiplugin.h:93