# PhasicFlow: src/phasicFlow/smartPointers/uniquePtr.hpp Source File

uniquePtr.hpp
Go to the documentation of this file.
/*------------------------------- phasicFlow ---------------------------------
O C enter of
O O E ngineering and
O O M ultiscale modeling of
OOOOOOO F luid flow
------------------------------------------------------------------------------
Copyright (C): www.cemf.ir
email: hamid.r.norouzi AT gmail.com
------------------------------------------------------------------------------
Licence:
This file is part of phasicFlow code. It is a free software for simulating
granular and multiphase flows. You can redistribute it and/or modify it under
the terms of GNU General Public License v3 or any other later versions.
phasicFlow is distributed to help others in their research in the field of
granular and multiphase flows, but WITHOUT ANY WARRANTY; without even the
implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
-----------------------------------------------------------------------------*/
#ifndef __uniquePtr_hpp__
#define __uniquePtr_hpp__
#include <memory>
#include <typeinfo>
#include "pFlowMacros.hpp"
#include "error.hpp"
// just for preventing the use of std namespace and adding some minor functionalities
namespace pFlow
{
template<
typename T,
typename Deleter = std::default_delete<T>
>
class uniquePtr
:
public std::unique_ptr<T, Deleter>
{
public:
typedef std::unique_ptr<T,Deleter> uniquePtrType;
// using base constructors
using uniquePtrType::unique_ptr;
template<typename... Args>
inline static uniquePtr<T> makeUnique(Args&&... args)
{
return uniquePtr<T>(new T(std::forward<Args>(args)...));
}
void clear()
{
if( ! (*this) )
{
auto p = this->release();
delete p;
}
}
// ref to the object of type T
T& operator ()()
{
if(!this->get())
{
fatalErrorInFunction <<
"uniquePtr is empty, and you are trying to get its reference. \n" <<
"Type name is "<< typeid(T).name()<<"\n";
fatalExit;
}
return *this->get();
}
// const ref to the object of type T
const T& operator() () const
{
if(!this->get())
{
fatalErrorInFunction <<
"uniquePtr is empty, and you are trying to get its reference. \n" <<
"Type name is "<< typeid(T).name()<<"\n";
fatalExit;
}
return const_cast<T&>(*this->get());
}
};
template<class T, class... Args>
inline uniquePtr<T> makeUnique(Args&&... args)
{
return uniquePtr<T>(new T(std::forward<Args>(args)...));
}
}
#endif
fatalExit
#define fatalExit
Definition: error.hpp:57
pFlow::uniquePtr::uniquePtrType
std::unique_ptr< T, Deleter > uniquePtrType
Definition: uniquePtr.hpp:50
pFlow
Definition: demComponent.hpp:28
pFlowMacros.hpp
fatalErrorInFunction
#define fatalErrorInFunction
Definition: error.hpp:42
pFlow::makeUnique
uniquePtr< T > makeUnique(Args &&... args)
Definition: uniquePtr.hpp:100
pFlow::uniquePtr::clear
void clear()
Definition: uniquePtr.hpp:62
pFlow::uniquePtr
Definition: uniquePtr.hpp:44
pFlow::uniquePtr::makeUnique
static uniquePtr< T > makeUnique(Args &&... args)
Definition: uniquePtr.hpp:57
pFlow::uniquePtr::operator()
T & operator()()
Definition: uniquePtr.hpp:72
error.hpp