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
12namespace yarp::os {
13
19template <class T>
21{
22private:
25 YarpPluginSettings settings;
26
27public:
34 {
35 factory = nullptr;
36 }
37
43 virtual ~YarpPlugin()
44 {
45 close();
46 }
47
58 bool open(YarpPluginSettings& settings)
59 {
60 close();
61 factory = new SharedLibraryClassFactory<T>();
62 if (!factory) {
63 return false;
64 }
65 if (!settings.open(*factory)) {
66 settings.reportStatus(*factory);
67 close();
68 return false;
69 }
70 this->settings = settings;
71 return true;
72 }
73
81 bool close()
82 {
83 if (!factory) {
84 return true;
85 }
86 factory->removeRef();
87 if (factory->getReferenceCount() <= 0) {
88 delete factory;
89 factory = nullptr;
90 }
91 return true;
92 }
93
99 bool isValid() const
100 {
101 return (factory != nullptr);
102 }
103
113 {
114 if (!factory) {
115 return nullptr;
116 }
117 return factory->create();
118 }
119
127 void destroy(T* obj)
128 {
129 if (!factory) {
130 return;
131 }
132 factory->destroy(obj);
133 }
134
140 std::string getName()
141 {
142 if (!factory) {
143 return {};
144 }
145 return factory->getName();
146 }
147
153 std::string getClassName()
154 {
155 if (!factory) {
156 return {};
157 }
158 return factory->getClassName();
159 }
160
166 std::string getBaseClassName()
167 {
168 if (!factory) {
169 return {};
170 }
171 return factory->getBaseClassName();
172 }
173
180 {
181 return factory;
182 }
183};
184
185} // namespace yarp::os
186
187#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:21
SharedLibraryClassFactory< T > * getFactory() const
Definition: YarpPlugin.h:179
bool close()
End this use of the plugin.
Definition: YarpPlugin.h:81
T * create()
Create an object using the plugin.
Definition: YarpPlugin.h:112
bool open(YarpPluginSettings &settings)
Load a library and prepare an object factory, based on the hints supplied.
Definition: YarpPlugin.h:58
std::string getName()
Definition: YarpPlugin.h:140
void destroy(T *obj)
Destroy an object previously created using the plugin.
Definition: YarpPlugin.h:127
std::string getBaseClassName()
Definition: YarpPlugin.h:166
YarpPlugin()
Constructor.
Definition: YarpPlugin.h:33
std::string getClassName()
Definition: YarpPlugin.h:153
virtual ~YarpPlugin()
Destructor.
Definition: YarpPlugin.h:43
bool isValid() const
Definition: YarpPlugin.h:99
An interface to the operating system, including Port based communication.