PLCnext API Documentation  22.9.0.33
SharedModuleLibrary.hxx
1 //
3 // Copyright PHOENIX CONTACT Electronics GmbH
4 //
6 #pragma once
7 #include "Arp/System/Core/Arp.h"
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 
14 namespace Arp { namespace System { namespace Commons { namespace Extensibility
15 {
16 
21 template<typename TModuleInterface>
23 {
24 public: // typedefs
26  using ModuleInterfacePtr = std::unique_ptr<TModuleInterface>;
27 
28 private: // typedefs
30  using ModuleLibraryFactoryFunction = const ModuleLibrary & (*)(AppDomain& appDomain);
31 
32 public: // construction/destruction
34  explicit SharedModuleLibrary(const String& path);
44  virtual ~SharedModuleLibrary(void) = default;
45 
46 public:
47 
52  template<typename TModule>
54 
59  ModuleInterfacePtr CreateModule(const String& moduleName) const;
60 
62  const String& GetFullPath() const;
63 
65  String GetFileName() const;
66 
68  bool Exists() const;
69 
71  bool IsLoaded() const;
72 
76  void Load();
77 
79  void Unload();
80 
81 protected:
85 
86 private:
87  ModuleLibraryFactoryFunction GetModuleLibraryFactoryFunction(SharedLibrary& sharedLibrary) const;
88 
89 private:
91  const ModuleLibrary* libraryBase = nullptr;
92 
93 };
94 
95 template<typename TModuleInterface>
97  : loader(path)
98 {
99 }
100 
101 template<typename TModuleInterface>
102 template<typename TModule>
104 {
105  return this->CreateModule(CommonTypeName<TModule>().Value);
106 }
107 
108 template<typename TModuleInterface>
111 {
112  if (this->libraryBase == nullptr)
113  {
114  return{};
115  }
116  else
117  {
118  return this->libraryBase->GetFactory().Create(moduleName);
119  }
120 }
121 
122 template<typename TModuleInterface>
124 {
125  return loader.GetFullPath();
126 }
127 
128 template<typename TModuleInterface>
130 {
131  return loader.GetFileName();
132 }
133 
134 template<typename TModuleInterface>
136 {
137  return loader.Exists();
138 }
139 
140 template<typename TModuleInterface>
142 {
143  return loader.IsLoaded();
144 }
145 
146 template<typename TModuleInterface>
148 {
149  this->loader.Load();
150  if (this->loader.IsLoaded())
151  {
152  auto f = GetModuleLibraryFactoryFunction(this->loader.GetSharedLibrary());
153  this->libraryBase = &f(AppDomain::GetCurrent());
154  }
155 }
156 
157 template<typename TModuleInterface>
159 {
160  if (this->loader.IsLoaded())
161  {
162  this->libraryBase = nullptr;
163  }
164  this->loader.Unload();
165 }
166 
167 template<typename TModuleInterface>
168 typename SharedModuleLibrary<TModuleInterface>::ModuleLibraryFactoryFunction
170 {
171  auto result = reinterpret_cast<ModuleLibraryFactoryFunction>(
172  sharedLibrary.GetFunctionAddress(GetModuleLibraryFactoryFunctionName()));
173  if (result == nullptr)
174  {
176  "Could not get module library factory function from '{0}'", this->GetFileName());
177  }
178  return result;
179 }
180 
181 }}}} // end of namespace Arp::System::Commons::Extensibility
This class represents a single application domain for each process and is implemented as singleton.
Definition: AppDomain.hpp:122
static AppDomain & GetCurrent(void)
Gets the current AppDomain instance.
Definition: AppDomain.hpp:233
This (meta programming) class provides the typename according the CLS (common language specification)...
Definition: CommonTypeName.hxx:30
Base class for a library providing extension modules
Definition: ModuleLibraryBase.hxx:34
Definition: SharedModuleLibraryLoader.hpp:16
Base class for loading a library with modules
Definition: SharedModuleLibrary.hxx:23
SharedModuleLibrary(const String &path)
Constructs an SharedModuleLibrary instance.
Definition: SharedModuleLibrary.hxx:96
SharedModuleLibrary(SharedModuleLibrary &&arg)=delete
Move constructor.
void Unload()
Unload the library
Definition: SharedModuleLibrary.hxx:158
SharedModuleLibrary & operator=(const SharedModuleLibrary &arg)=delete
Copy-assignment operator.
const String & GetFullPath() const
Returns the full path to the library file
Definition: SharedModuleLibrary.hxx:123
bool IsLoaded() const
Returns true if the library is loaded
Definition: SharedModuleLibrary.hxx:141
String GetFileName() const
Returns the file name of the library (without directory)
Definition: SharedModuleLibrary.hxx:129
SharedModuleLibrary(const SharedModuleLibrary &arg)=delete
Copy constructor.
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
SharedModuleLibrary & operator=(SharedModuleLibrary &&arg)=delete
Move-assignment operator.
void Load()
Loads the library
Definition: SharedModuleLibrary.hxx:147
ModuleInterfacePtr CreateModule() const
Creates a module by type
Definition: SharedModuleLibrary.hxx:103
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:135
This exception is used when a method call is invalid for object's current state.
Definition: InvalidOperationException.hpp:15
High level API to enable work with dynamically loadable shared libraries.
Definition: SharedLibrary.hpp:20
void * GetFunctionAddress(const String &symbolName)
Returns the address where specified symbol is located in the memory.
@ System
System components used by the System, Device, Plc or Io domains.
Root namespace for the PLCnext API