RobotTestingFramework  2.0.1
Robot Testing Framework
SharedLibraryClass.h
Go to the documentation of this file.
1 /*
2  * Robot Testing Framework
3  *
4  * Copyright (C) 2015-2019 Istituto Italiano di Tecnologia (IIT)
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 
22 #ifndef ROBOTTESTINGFRAMEWORK_SHAREDLIBRARYCLASS_H
23 #define ROBOTTESTINGFRAMEWORK_SHAREDLIBRARYCLASS_H
24 
26 
27 namespace shlibpp {
28 template <class T>
30 }
31 
32 
38 template <class T>
40 {
41 private:
42  T* content;
44 
45 public:
50  content(nullptr),
51  pfactory(nullptr)
52  {
53  }
54 
62  content(nullptr),
63  pfactory(nullptr)
64  {
65  open(factory);
66  }
67 
77  {
78  close();
79  content = factory.create();
80  pfactory = &factory;
81  factory.addRef();
82 
83  return content != nullptr;
84  }
85 
91  virtual bool close()
92  {
93  if (content != nullptr) {
94  pfactory->destroy(content);
95  //NetworkBase::lock();
96  if (pfactory->removeRef() == 0) {
97  delete pfactory;
98  }
99  //NetworkBase::unlock();
100  }
101 
102  content = nullptr;
103  pfactory = nullptr;
104 
105  return true;
106  }
107 
112  {
113  close();
114  }
115 
124  {
125  return *content;
126  }
127 
133  bool isValid() const
134  {
135  return content != nullptr;
136  }
137 
144  {
145  return (*content);
146  }
147 
154  {
155  return (content);
156  }
157 };
158 
159 
160 #endif // ROBOTTESTINGFRAMEWORK_SHAREDLIBRARYCLASS_H
T & getContent()
Gives access to the created instance.
bool isValid() const
Check whether a valid instance has been created.
T * operator->()
A pointer version of SharedLibraryClass::getContent.
virtual ~SharedLibraryClass()
Destructor.
int addRef()
Increment the reference count of this factory.
SharedLibraryClassFactory< T > * pfactory
bool open(SharedLibraryClassFactory< T > &factory)
Construct an instance using the specified factory.
virtual bool close()
Destroy an instance if one has been created.
Container for an object created using a factory provided by a shared library.
A type-safe wrapper for SharedLibraryFactory, committing to creation/destruction of instances of a pa...
T & operator*()
Shorthand for SharedLibraryClass::getContent.
SharedLibraryClass(SharedLibraryClassFactory< T > &factory)
Constructor for valid instance of a class from a shared library.
SharedLibraryClass()
Constructor for empty instance.
int removeRef()
Decrement the reference count of this factory.