YARP
Yet Another Robot Platform
YarpPlugin.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2020 Istituto Italiano di Tecnologia (IIT)
3  * All rights reserved.
4  *
5  * This software may be modified and distributed under the terms of the
6  * BSD-3-Clause license. See the accompanying LICENSE file for details.
7  */
8 
9 #ifndef YARP_OS_YARPPLUGIN_H
10 #define YARP_OS_YARPPLUGIN_H
11 
14 
15 namespace yarp {
16 namespace os {
17 
23 template <class T>
25 {
26 private:
28  SharedLibraryClass<T> content;
29  YarpPluginSettings settings;
30 
31 public:
38  {
39  factory = nullptr;
40  }
41 
47  virtual ~YarpPlugin()
48  {
49  close();
50  }
51 
62  bool open(YarpPluginSettings& settings)
63  {
64  close();
65  factory = new SharedLibraryClassFactory<T>();
66  if (!factory)
67  return false;
68  if (!settings.open(*factory)) {
69  settings.reportStatus(*factory);
70  close();
71  return false;
72  }
73  this->settings = settings;
74  return true;
75  }
76 
84  bool close()
85  {
86  if (!factory) {
87  return true;
88  }
89  factory->removeRef();
90  if (factory->getReferenceCount() <= 0) {
91  delete factory;
92  factory = nullptr;
93  }
94  return true;
95  }
96 
102  bool isValid() const
103  {
104  return (factory != nullptr);
105  }
106 
115  T* create()
116  {
117  if (!factory) {
118  return nullptr;
119  }
120  return factory->create();
121  }
122 
130  void destroy(T* obj)
131  {
132  if (!factory) {
133  return;
134  }
135  factory->destroy(obj);
136  }
137 
143  std::string getName()
144  {
145  if (!factory) {
146  return {};
147  }
148  return factory->getName();
149  }
150 
156  std::string getClassName()
157  {
158  if (!factory) {
159  return {};
160  }
161  return factory->getClassName();
162  }
163 
169  std::string getBaseClassName()
170  {
171  if (!factory) {
172  return {};
173  }
174  return factory->getBaseClassName();
175  }
176 
183  {
184  return factory;
185  }
186 };
187 
188 } // namespace os
189 } // namespace yarp
190 
191 #endif // YARP_OS_YARPPLUGIN_H
yarp::os::SharedLibraryClassFactory::create
T * create()
Definition: SharedLibraryClassFactory.h:34
yarp::os::SharedLibraryClassFactory::destroy
void destroy(T *obj) const
Definition: SharedLibraryClassFactory.h:42
yarp::os::YarpPlugin::close
bool close()
End this use of the plugin.
Definition: YarpPlugin.h:84
yarp::os::YarpPlugin::open
bool open(YarpPluginSettings &settings)
Load a library and prepare an object factory, based on the hints supplied.
Definition: YarpPlugin.h:62
yarp::os::SharedLibraryFactory::removeRef
int removeRef()
Decrement the reference count of this factory.
Definition: SharedLibraryFactory.cpp:123
YarpPluginSettings.h
yarp::os::YarpPluginSettings::reportStatus
void reportStatus(SharedLibraryFactory &factory) const
Give a human-readable report of the status of a factory.
Definition: YarpPlugin.cpp:157
yarp::os::YarpPlugin::getName
std::string getName()
Definition: YarpPlugin.h:143
yarp::os::YarpPlugin::getFactory
SharedLibraryClassFactory< T > * getFactory() const
Definition: YarpPlugin.h:182
yarp::os::SharedLibraryFactory::getName
std::string getName() const
Get the name associated with this factory.
Definition: SharedLibraryFactory.cpp:129
yarp::os::YarpPlugin::create
T * create()
Create an object using the plugin.
Definition: YarpPlugin.h:115
yarp::os::YarpPlugin::getBaseClassName
std::string getBaseClassName()
Definition: YarpPlugin.h:169
yarp::os::YarpPlugin::destroy
void destroy(T *obj)
Destroy an object previously created using the plugin.
Definition: YarpPlugin.h:130
yarp::os::YarpPlugin::~YarpPlugin
virtual ~YarpPlugin()
Destructor.
Definition: YarpPlugin.h:47
yarp::os::YarpPlugin::isValid
bool isValid() const
Definition: YarpPlugin.h:102
yarp::os::YarpPluginSettings::open
bool open(SharedLibraryFactory &factory)
Initialize a factory object based on the hints available.
Definition: YarpPlugin.cpp:83
yarp::os::SharedLibraryFactory::getReferenceCount
int getReferenceCount() const
Get the current reference count of this factory.
Definition: SharedLibraryFactory.cpp:111
yarp::os::SharedLibraryFactory::getClassName
std::string getClassName() const
Get the type associated with this factory.
Definition: SharedLibraryFactory.cpp:134
yarp::os::YarpPlugin::getClassName
std::string getClassName()
Definition: YarpPlugin.h:156
yarp::os::YarpPluginSettings
Collect hints for finding a particular plugin.
Definition: YarpPluginSettings.h:25
yarp::os::SharedLibraryClassFactory
A type-safe wrapper for SharedLibraryFactory, committing to creation/destruction of instances of a pa...
Definition: SharedLibraryClassFactory.h:25
yarp::os::YarpPlugin
Type-safe access to a plugin.
Definition: YarpPlugin.h:25
YarpPluginSelector.h
yarp::os::SharedLibraryFactory::getBaseClassName
std::string getBaseClassName() const
Get the base type associated with this factory.
Definition: SharedLibraryFactory.cpp:139
yarp
The main, catch-all namespace for YARP.
Definition: environment.h:18
yarp::os::SharedLibraryClass
Container for an object created using a factory provided by a shared library.
Definition: SharedLibraryClass.h:25
yarp::os::YarpPlugin::YarpPlugin
YarpPlugin()
Constructor.
Definition: YarpPlugin.h:37