YARP
Yet Another Robot Platform
SharedLibraryClass.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_SHAREDLIBRARYCLASS_H
7 #define YARP_OS_SHAREDLIBRARYCLASS_H
8 
9 #include <yarp/os/Network.h>
11 
12 namespace yarp {
13 namespace os {
14 
20 template <class T>
22 {
23 private:
24  T* content{nullptr};
25  SharedLibraryClassFactory<T>* pfactory{nullptr};
26 
27 public:
31  SharedLibraryClass() = default;
32 
40  {
41  open(factory);
42  }
43 
53  {
54  close();
55  content = factory.create();
56  pfactory = &factory;
57  factory.addRef();
58 
59  return content != nullptr;
60  }
61 
67  virtual bool close()
68  {
69  if (content != nullptr) {
70  pfactory->destroy(content);
72  if (pfactory->removeRef() == 0) {
73  delete pfactory;
74  }
76  }
77 
78  content = nullptr;
79  pfactory = nullptr;
80 
81  return true;
82  }
83 
88  {
89  close();
90  }
91 
101  {
102  return *content;
103  }
104 
113  const T& getContent() const
114  {
115  return *content;
116  }
117 
123  bool isValid() const
124  {
125  return content != nullptr;
126  }
127 
134  {
135  return *content;
136  }
137 
143  const T& operator*() const
144  {
145  return *content;
146  }
147 
155  {
156  return content;
157  }
158 
165  const T* operator->() const
166  {
167  return content;
168  }
169 };
170 
171 } // namespace os
172 } // namespace yarp
173 
174 #endif // YARP_OS_SHAREDLIBRARYCLASS_H
static void unlock()
Call post() on a global mutual-exclusion semaphore allocated by YARP.
Definition: Network.cpp:1464
static void lock()
Call wait() on a global mutual-exclusion semaphore allocated by YARP.
Definition: Network.cpp:1459
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.
bool open(SharedLibraryClassFactory< T > &factory)
Construct an instance using the specified factory.
virtual ~SharedLibraryClass()
Destructor.
const T & getContent() const
Gives access to the created instance (const version).
T & getContent()
Gives access to the created instance.
virtual bool close()
Destroy an instance if one has been created.
const T * operator->() const
A pointer version of SharedLibraryClass::getContent (const version)
bool isValid() const
Check whether a valid instance has been created.
const T & operator*() const
Shorthand for SharedLibraryClass::getContent (const version)
T & operator*()
Shorthand for SharedLibraryClass::getContent.
T * operator->()
A pointer version of SharedLibraryClass::getContent.
SharedLibraryClass(SharedLibraryClassFactory< T > &factory)
Constructor for valid instance of a class from a shared library.
SharedLibraryClass()=default
Constructor for empty instance.
int addRef()
Increment the reference count of this factory.
The main, catch-all namespace for YARP.
Definition: dirs.h:16