YARP
Yet Another Robot Platform
YarpPlugin.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
3  * SPDX-License-Identifier: BSD-3-Clause
4  */
5 
6 #ifndef YARP_OS_YARPPLUGIN_H
7 #define YARP_OS_YARPPLUGIN_H
8 
11 
12 namespace yarp {
13 namespace os {
14 
20 template <class T>
22 {
23 private:
25  SharedLibraryClass<T> content;
26  YarpPluginSettings settings;
27 
28 public:
35  {
36  factory = nullptr;
37  }
38 
44  virtual ~YarpPlugin()
45  {
46  close();
47  }
48 
59  bool open(YarpPluginSettings& settings)
60  {
61  close();
62  factory = new SharedLibraryClassFactory<T>();
63  if (!factory) {
64  return false;
65  }
66  if (!settings.open(*factory)) {
67  settings.reportStatus(*factory);
68  close();
69  return false;
70  }
71  this->settings = settings;
72  return true;
73  }
74 
82  bool close()
83  {
84  if (!factory) {
85  return true;
86  }
87  factory->removeRef();
88  if (factory->getReferenceCount() <= 0) {
89  delete factory;
90  factory = nullptr;
91  }
92  return true;
93  }
94 
100  bool isValid() const
101  {
102  return (factory != nullptr);
103  }
104 
113  T* create()
114  {
115  if (!factory) {
116  return nullptr;
117  }
118  return factory->create();
119  }
120 
128  void destroy(T* obj)
129  {
130  if (!factory) {
131  return;
132  }
133  factory->destroy(obj);
134  }
135 
141  std::string getName()
142  {
143  if (!factory) {
144  return {};
145  }
146  return factory->getName();
147  }
148 
154  std::string getClassName()
155  {
156  if (!factory) {
157  return {};
158  }
159  return factory->getClassName();
160  }
161 
167  std::string getBaseClassName()
168  {
169  if (!factory) {
170  return {};
171  }
172  return factory->getBaseClassName();
173  }
174 
181  {
182  return factory;
183  }
184 };
185 
186 } // namespace os
187 } // namespace yarp
188 
189 #endif // YARP_OS_YARPPLUGIN_H
A type-safe wrapper for SharedLibraryFactory, committing to creation/destruction of instances of a pa...
Container for an object created using a factory provided by a shared library.
std::string getBaseClassName() const
Get the base type associated with this factory.
int getReferenceCount() const
Get the current reference count of this factory.
std::string getClassName() const
Get the type associated with this factory.
std::string getName() const
Get the name associated with this factory.
int removeRef()
Decrement the reference count of this factory.
Collect hints for finding a particular plugin.
bool open(SharedLibraryFactory &factory)
Initialize a factory object based on the hints available.
Definition: YarpPlugin.cpp:80
void reportStatus(SharedLibraryFactory &factory) const
Give a human-readable report of the status of a factory.
Definition: YarpPlugin.cpp:154
Type-safe access to a plugin.
Definition: YarpPlugin.h:22
bool close()
End this use of the plugin.
Definition: YarpPlugin.h:82
bool open(YarpPluginSettings &settings)
Load a library and prepare an object factory, based on the hints supplied.
Definition: YarpPlugin.h:59
std::string getName()
Definition: YarpPlugin.h:141
T * create()
Create an object using the plugin.
Definition: YarpPlugin.h:113
void destroy(T *obj)
Destroy an object previously created using the plugin.
Definition: YarpPlugin.h:128
std::string getBaseClassName()
Definition: YarpPlugin.h:167
YarpPlugin()
Constructor.
Definition: YarpPlugin.h:34
std::string getClassName()
Definition: YarpPlugin.h:154
virtual ~YarpPlugin()
Destructor.
Definition: YarpPlugin.h:44
SharedLibraryClassFactory< T > * getFactory() const
Definition: YarpPlugin.h:180
bool isValid() const
Definition: YarpPlugin.h:100
The main, catch-all namespace for YARP.
Definition: dirs.h:16