SharedLibraryClass.h
Go to the documentation of this file.
/*
* Robot Testing Framework
*
* Copyright (C) 2015-2019 Istituto Italiano di Tecnologia (IIT)
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef ROBOTTESTINGFRAMEWORK_SHAREDLIBRARYCLASS_H
#define ROBOTTESTINGFRAMEWORK_SHAREDLIBRARYCLASS_H
#include <robottestingframework/dll/SharedLibraryClassFactory.h>
namespace shlibpp {
template <class T>
class SharedLibraryClass;
}
template <class T>
class shlibpp::SharedLibraryClass
{
private:
T* content;
SharedLibraryClassFactory<T>* pfactory;
public:
SharedLibraryClass() :
content(nullptr),
pfactory(nullptr)
{
}
SharedLibraryClass(SharedLibraryClassFactory<T>& factory) :
content(nullptr),
pfactory(nullptr)
{
open(factory);
}
bool open(SharedLibraryClassFactory<T>& factory)
{
close();
content = factory.create();
pfactory = &factory;
factory.addRef();
return content != nullptr;
}
virtual bool close()
{
if (content != nullptr) {
pfactory->destroy(content);
//NetworkBase::lock();
if (pfactory->removeRef() == 0) {
delete pfactory;
}
//NetworkBase::unlock();
}
content = nullptr;
pfactory = nullptr;
return true;
}
virtual ~SharedLibraryClass()
{
close();
}
T& getContent()
{
return *content;
}
bool isValid() const
{
return content != nullptr;
}
T& operator*()
{
return (*content);
}
T* operator->()
{
return (content);
}
};
#endif // ROBOTTESTINGFRAMEWORK_SHAREDLIBRARYCLASS_H
SharedLibraryClassFactory.h
shlibpp::SharedLibraryClassFactory
A type-safe wrapper for SharedLibraryFactory, committing to creation/destruction of instances of a pa...
Definition: SharedLibraryClassFactory.h:43
shlibpp::SharedLibraryClassFactory::create
T * create()
Definition: SharedLibraryClassFactory.h:54
shlibpp::SharedLibraryClass
Container for an object created using a factory provided by a shared library.
Definition: SharedLibraryClass.h:40
shlibpp::SharedLibraryClass::open
bool open(SharedLibraryClassFactory< T > &factory)
Construct an instance using the specified factory.
Definition: SharedLibraryClass.h:76
shlibpp::SharedLibraryClass::close
virtual bool close()
Destroy an instance if one has been created.
Definition: SharedLibraryClass.h:91
shlibpp::SharedLibraryClass::pfactory
SharedLibraryClassFactory< T > * pfactory
Definition: SharedLibraryClass.h:43
shlibpp::SharedLibraryClass::SharedLibraryClass
SharedLibraryClass()
Constructor for empty instance.
Definition: SharedLibraryClass.h:49
shlibpp::SharedLibraryClass::isValid
bool isValid() const
Check whether a valid instance has been created.
Definition: SharedLibraryClass.h:133
shlibpp::SharedLibraryClass::content
T * content
Definition: SharedLibraryClass.h:42
shlibpp::SharedLibraryClass::~SharedLibraryClass
virtual ~SharedLibraryClass()
Destructor.
Definition: SharedLibraryClass.h:111
shlibpp::SharedLibraryClass::getContent
T & getContent()
Gives access to the created instance.
Definition: SharedLibraryClass.h:123
shlibpp::SharedLibraryClass::operator*
T & operator*()
Shorthand for SharedLibraryClass::getContent.
Definition: SharedLibraryClass.h:143
shlibpp::SharedLibraryClass::operator->
T * operator->()
A pointer version of SharedLibraryClass::getContent.
Definition: SharedLibraryClass.h:153
shlibpp::SharedLibraryClass::SharedLibraryClass
SharedLibraryClass(SharedLibraryClassFactory< T > &factory)
Constructor for valid instance of a class from a shared library.
Definition: SharedLibraryClass.h:61
shlibpp::SharedLibraryFactory::addRef
int addRef()
Increment the reference count of this factory.
shlibpp
Definition: SharedLibrary.h:27