PLCnext API Documentation 25.0.2.69
SharedModuleLibrary.hxx
1
2//
3// Copyright Phoenix Contact GmbH & Co. KG
4//
6#pragma once
8#include "Arp/System/Core/CommonTypeName.hxx"
9#include "Arp/System/Commons/Runtime/SharedLibrary.hpp"
10#include "Arp/System/Commons/Extensibility/SharedModuleLibraryLoader.hpp"
11#include "Arp/System/Commons/Extensibility/ModuleLibraryBase.hxx"
12#include "Arp/System/Commons/Exceptions/InvalidOperationException.hpp"
13
14namespace Arp { namespace System { namespace Commons { namespace Extensibility
15{
16
21template<typename TModuleInterface>
23{
24public: // typedefs
26 using ModuleInterfacePtr = std::unique_ptr<TModuleInterface>;
27
28private: // typedefs
30 using ModuleLibraryFactoryFunction = const ModuleLibrary & (*)();
31
32public: // construction/destruction
33 explicit SharedModuleLibrary(const String& path, SharedLibraryMemoryLockOption sharedLibraryMemoryLockOption = SharedLibraryMemoryLockOption::Nothing);
43 virtual ~SharedModuleLibrary(void) = default;
44
45public:
46
47 template<typename TModule>
49
50 ModuleInterfacePtr CreateModule(const String& moduleName) const;
51 const String& GetFullPath() const;
52 String GetFileName() const;
53 bool Exists() const;
54 bool IsLoaded() const;
55 void Load();
56 void Unload();
57
58protected:
62
63private:
64 ModuleLibraryFactoryFunction GetModuleLibraryFactoryFunction(SharedLibrary& sharedLibrary) const;
65
66private:
68 const ModuleLibrary* libraryBase = nullptr;
69
70};
71
73template<typename TModuleInterface>
74SharedModuleLibrary<TModuleInterface>::SharedModuleLibrary(const String& path, SharedLibraryMemoryLockOption sharedLibraryMemoryLockOption)
75 : loader(path, sharedLibraryMemoryLockOption)
76{
77}
78
83template<typename TModuleInterface>
84template<typename TModule>
86{
87 return this->CreateModule(CommonTypeName<TModule>());
88}
89
94template<typename TModuleInterface>
97{
98 if (this->libraryBase == nullptr)
99 {
100 return{};
101 }
102 else
103 {
104 return this->libraryBase->GetFactory().Create(moduleName);
105 }
106}
107
109template<typename TModuleInterface>
111{
112 return loader.GetFullPath();
113}
114
116template<typename TModuleInterface>
118{
119 return loader.GetFileName();
120}
121
123template<typename TModuleInterface>
125{
126 return loader.Exists();
127}
128
130template<typename TModuleInterface>
132{
133 return loader.IsLoaded();
134}
135
139template<typename TModuleInterface>
141{
142 this->loader.Load();
143 if (this->loader.IsLoaded())
144 {
145 auto f = GetModuleLibraryFactoryFunction(this->loader.GetSharedLibrary());
146 this->libraryBase = &f();
147 }
148}
149
151template<typename TModuleInterface>
153{
154 if (this->loader.IsLoaded())
155 {
156 this->libraryBase = nullptr;
157 }
158 this->loader.Unload();
159}
160
161template<typename TModuleInterface>
162typename SharedModuleLibrary<TModuleInterface>::ModuleLibraryFactoryFunction
164{
165 auto result = reinterpret_cast<ModuleLibraryFactoryFunction>(
166 sharedLibrary.GetFunctionAddress(GetModuleLibraryFactoryFunctionName()));
167 if (result == nullptr)
168 {
170 "Could not get module library factory function from '{0}'", this->GetFileName());
171 }
172 return result;
173}
174
175}}}} // end of namespace Arp::System::Commons::Extensibility
This exception is thrown when an operation cannot be executed because the related state is invalid.
Definition: InvalidOperationException.hpp:16
This (meta programming) class provides the type-name according the CLS (common language specification...
Definition: CommonTypeName.hxx:33
This class represents the Arp String. The implementation is based on std::string.
Definition: String.hpp:39
Base class for a library providing extension modules
Definition: ModuleLibraryBase.hxx:29
Definition: SharedModuleLibraryLoader.hpp:17
Base class for loading a library with modules
Definition: SharedModuleLibrary.hxx:23
SharedModuleLibrary(SharedModuleLibrary &&arg)=delete
Move constructor.
void Unload()
Unload the library
Definition: SharedModuleLibrary.hxx:152
const String & GetFullPath() const
Returns the full path to the library file
Definition: SharedModuleLibrary.hxx:110
bool IsLoaded() const
Returns true if the library is loaded
Definition: SharedModuleLibrary.hxx:131
String GetFileName() const
Returns the file name of the library (without directory)
Definition: SharedModuleLibrary.hxx:117
SharedModuleLibrary & operator=(SharedModuleLibrary &&arg)=delete
Move-assignment operator.
SharedModuleLibrary(const SharedModuleLibrary &arg)=delete
Copy constructor.
SharedModuleLibrary(const String &path, SharedLibraryMemoryLockOption sharedLibraryMemoryLockOption=SharedLibraryMemoryLockOption::Nothing)
Constructs an SharedModuleLibrary instance.
Definition: SharedModuleLibrary.hxx:74
virtual ~SharedModuleLibrary(void)=default
Destructs this instance and frees all resources.
virtual String GetModuleLibraryFactoryFunctionName() const =0
Returns the name of the factory function for the module library
void Load()
Loads the library
Definition: SharedModuleLibrary.hxx:140
SharedModuleLibrary & operator=(const SharedModuleLibrary &arg)=delete
Copy-assignment operator.
ModuleInterfacePtr CreateModule() const
Creates a module by type
Definition: SharedModuleLibrary.hxx:85
std::unique_ptr< TModuleInterface > ModuleInterfacePtr
owning pointer to a module
Definition: SharedModuleLibrary.hxx:26
bool Exists() const
Returns true if the library file exists.
Definition: SharedModuleLibrary.hxx:124
High level API to enable work with dynamically loadable shared libraries.
Definition: SharedLibrary.hpp:21
void * GetFunctionAddress(const String &symbolName)
Returns the address where specified symbol is located in the memory.
Definition: SharedLibrary.cpp:104
Root namespace for the PLCnext API