YARP
Yet Another Robot Platform
SharedLibraryClass.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_SHAREDLIBRARYCLASS_H
10 #define YARP_OS_SHAREDLIBRARYCLASS_H
11 
12 #include <yarp/os/Network.h>
14 
15 namespace yarp {
16 namespace os {
17 
23 template <class T>
25 {
26 private:
27  T* content{nullptr};
28  SharedLibraryClassFactory<T>* pfactory{nullptr};
29 
30 public:
34  SharedLibraryClass() = default;
35 
43  {
44  open(factory);
45  }
46 
56  {
57  close();
58  content = factory.create();
59  pfactory = &factory;
60  factory.addRef();
61 
62  return content != nullptr;
63  }
64 
70  virtual bool close()
71  {
72  if (content != nullptr) {
73  pfactory->destroy(content);
75  if (pfactory->removeRef() == 0) {
76  delete pfactory;
77  }
79  }
80 
81  content = nullptr;
82  pfactory = nullptr;
83 
84  return true;
85  }
86 
91  {
92  close();
93  }
94 
104  {
105  return *content;
106  }
107 
116  const T& getContent() const
117  {
118  return *content;
119  }
120 
126  bool isValid() const
127  {
128  return content != nullptr;
129  }
130 
137  {
138  return *content;
139  }
140 
146  const T& operator*() const
147  {
148  return *content;
149  }
150 
158  {
159  return content;
160  }
161 
168  const T* operator->() const
169  {
170  return content;
171  }
172 };
173 
174 } // namespace os
175 } // namespace yarp
176 
177 #endif // YARP_OS_SHAREDLIBRARYCLASS_H
yarp::os::SharedLibraryClassFactory::create
T * create()
Definition: SharedLibraryClassFactory.h:34
Network.h
yarp::os::SharedLibraryClass::getContent
const T & getContent() const
Gives access to the created instance (const version).
Definition: SharedLibraryClass.h:116
yarp::os::NetworkBase::lock
static void lock()
Call wait() on a global mutual-exclusion semaphore allocated by YARP.
Definition: Network.cpp:1462
yarp::os::SharedLibraryClass::SharedLibraryClass
SharedLibraryClass(SharedLibraryClassFactory< T > &factory)
Constructor for valid instance of a class from a shared library.
Definition: SharedLibraryClass.h:42
yarp::os::SharedLibraryClass::operator->
const T * operator->() const
A pointer version of SharedLibraryClass::getContent (const version)
Definition: SharedLibraryClass.h:168
yarp::os::SharedLibraryClass::close
virtual bool close()
Destroy an instance if one has been created.
Definition: SharedLibraryClass.h:70
yarp::os::SharedLibraryClass::open
bool open(SharedLibraryClassFactory< T > &factory)
Construct an instance using the specified factory.
Definition: SharedLibraryClass.h:55
yarp::os::SharedLibraryClass::operator*
const T & operator*() const
Shorthand for SharedLibraryClass::getContent (const version)
Definition: SharedLibraryClass.h:146
yarp::os::SharedLibraryClass::SharedLibraryClass
SharedLibraryClass()=default
Constructor for empty instance.
SharedLibraryClassFactory.h
yarp::os::SharedLibraryClass::~SharedLibraryClass
virtual ~SharedLibraryClass()
Destructor.
Definition: SharedLibraryClass.h:90
yarp::os::SharedLibraryClass::isValid
bool isValid() const
Check whether a valid instance has been created.
Definition: SharedLibraryClass.h:126
yarp::os::SharedLibraryClassFactory
A type-safe wrapper for SharedLibraryFactory, committing to creation/destruction of instances of a pa...
Definition: SharedLibraryClassFactory.h:25
yarp::os::SharedLibraryClass::getContent
T & getContent()
Gives access to the created instance.
Definition: SharedLibraryClass.h:103
yarp::os::NetworkBase::unlock
static void unlock()
Call post() on a global mutual-exclusion semaphore allocated by YARP.
Definition: Network.cpp:1467
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::SharedLibraryClass::operator->
T * operator->()
A pointer version of SharedLibraryClass::getContent.
Definition: SharedLibraryClass.h:157
yarp::os::SharedLibraryFactory::addRef
int addRef()
Increment the reference count of this factory.
Definition: SharedLibraryFactory.cpp:117
yarp::os::SharedLibraryClass::operator*
T & operator*()
Shorthand for SharedLibraryClass::getContent.
Definition: SharedLibraryClass.h:136