DSPatch v.11.3.1
Loading...
Searching...
No Matches
Plugin.h
1/******************************************************************************
2DSPatch - The Refreshingly Simple C++ Dataflow Framework
3Copyright (c) 2024, Marcus Tomlinson
4
5BSD 2-Clause License
6
7Redistribution and use in source and binary forms, with or without
8modification, are permitted provided that the following conditions are met:
9
101. Redistributions of source code must retain the above copyright notice, this
11 list of conditions and the following disclaimer.
12
132. Redistributions in binary form must reproduce the above copyright notice,
14 this list of conditions and the following disclaimer in the documentation
15 and/or other materials provided with the distribution.
16
17THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
21FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
24CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27******************************************************************************/
28
29#pragma once
30
31#include "Component.h"
32
33#ifdef _WIN32
34#define WIN32_LEAN_AND_MEAN
35#include <windows.h>
36#undef WIN32_LEAN_AND_MEAN
37#define DLLEXPORT __declspec( dllexport )
38#else
39#include <dlfcn.h>
40#define DLLEXPORT
41#endif
42
43#include <string>
44
45#define EXPORT_PLUGIN( classname, ... ) \
46 extern "C" \
47 { \
48 DLLEXPORT DSPatch::Component* Create() \
49 { \
50 return new classname( __VA_ARGS__ ); \
51 } \
52 }
53
54namespace DSPatch
55{
56
58
69class Plugin final
70{
71public:
72 Plugin( const Plugin& ) = delete;
73 Plugin& operator=( const Plugin& ) = delete;
74
75 explicit Plugin( const std::string& pluginPath );
76 ~Plugin();
77
78 bool IsLoaded() const;
79
80 Component::SPtr Create() const;
81
82private:
83 typedef DSPatch::Component* ( *Create_t )();
84
85 void* _handle = nullptr;
86 Create_t _create = nullptr;
87};
88
89inline Plugin::Plugin( const std::string& pluginPath )
90{
91 // open library
92#ifdef _WIN32
93 _handle = LoadLibrary( pluginPath.c_str() );
94#else
95 _handle = dlopen( pluginPath.c_str(), RTLD_NOW );
96#endif
97
98 if ( _handle )
99 {
100 // load symbols
101#ifdef _WIN32
102 _create = (Create_t)GetProcAddress( (HMODULE)_handle, "Create" );
103#else
104 _create = (Create_t)dlsym( _handle, "Create" );
105#endif
106
107 if ( !_create )
108 {
109#ifdef _WIN32
110 FreeLibrary( (HMODULE)_handle );
111#else
112 dlclose( _handle );
113#endif
114
115 _handle = nullptr;
116 }
117 }
118}
119
120inline Plugin::~Plugin()
121{
122 // close library
123 if ( _handle )
124 {
125#ifdef _WIN32
126 FreeLibrary( (HMODULE)_handle );
127#else
128 dlclose( _handle );
129#endif
130 }
131}
132
133// cppcheck-suppress unusedFunction
134inline bool Plugin::IsLoaded() const
135{
136 return _handle;
137}
138
139// cppcheck-suppress unusedFunction
140inline Component::SPtr Plugin::Create() const
141{
142 if ( _handle )
143 {
144 return Component::SPtr( _create() );
145 }
146 return nullptr;
147}
148
149} // namespace DSPatch
Abstract base class for DSPatch components.
Definition Component.h:65
Component plugin loader.
Definition Plugin.h:70