YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
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
12namespace yarp::os {
13
19template <class T>
21{
22private:
23 T* content{nullptr};
24 SharedLibraryClassFactory<T>* pfactory{nullptr};
25
26public:
30 SharedLibraryClass() = default;
31
42
52 {
53 close();
54 content = factory.create();
55 pfactory = &factory;
56 factory.addRef();
57
58 return content != nullptr;
59 }
60
66 virtual bool close()
67 {
68 if (content != nullptr) {
69 pfactory->destroy(content);
71 if (pfactory->removeRef() == 0) {
72 delete pfactory;
73 }
75 }
76
77 content = nullptr;
78 pfactory = nullptr;
79
80 return true;
81 }
82
87 {
88 close();
89 }
90
100 {
101 return *content;
102 }
103
112 const T& getContent() const
113 {
114 return *content;
115 }
116
122 bool isValid() const
123 {
124 return content != nullptr;
125 }
126
133 {
134 return *content;
135 }
136
142 const T& operator*() const
143 {
144 return *content;
145 }
146
154 {
155 return content;
156 }
157
164 const T* operator->() const
165 {
166 return content;
167 }
168};
169
170} // namespace yarp::os
171
172#endif // YARP_OS_SHAREDLIBRARYCLASS_H
A mini-server for performing network communication in the background.
static void unlock()
Call post() on a global mutual-exclusion semaphore allocated by YARP.
Definition Network.cpp:1423
static void lock()
Call wait() on a global mutual-exclusion semaphore allocated by YARP.
Definition Network.cpp:1418
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 & operator*() const
Shorthand for SharedLibraryClass::getContent (const version)
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.
T * operator->()
A pointer version of SharedLibraryClass::getContent.
T & operator*()
Shorthand for SharedLibraryClass::getContent.
SharedLibraryClass(SharedLibraryClassFactory< T > &factory)
Constructor for valid instance of a class from a shared library.
SharedLibraryClass()=default
Constructor for empty instance.
DumpObj * factory(Bottle &obj)
Definition main.cpp:82
An interface to the operating system, including Port based communication.