# PhasicFlow: src/phasicFlow/containers/List/ListPtr/ListPtrI.hpp Source File

ListPtrI.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.
-----------------------------------------------------------------------------*/
template<typename T>
inline bool pFlow::ListPtr<T>::copy(const ListPtrType& src)
{
for( auto& iter : src.list_)
{
if( iter != nullptr)
{
push_back( iter->clone().release());
}
else
{
push_back(nullptr);
}
}
return true;
}
template<typename T>
T* pFlow::ListPtr<T>::ptr(label i)
{
if(i >= size() ) return nullptr;
auto iter = list_.begin();
std::advance(iter, i);
return *iter;
}
template<typename T>
const T* pFlow::ListPtr<T>::ptr
(
label i
) const
{
if(i >= size() ) return nullptr;
auto iter = list_.cbegin();
std::advance(iter, i);
return *iter;
}
template<typename T>
auto pFlow::ListPtr<T>::pos
(
label i
)
{
if(i >= size() )
{
fatalErrorInFunction<<
"out of range access to container element. \n";
fatalExit;
}
auto iter = list_.begin();
std::advance(iter, i);
return iter;
}
template<typename T>
auto pFlow::ListPtr<T>::pos
(
label i
)const
{
if(i >= size() )
{
fatalErrorInFunction<<
"out of range access to container element. \n";
fatalExit;
}
auto iter = list_.cbegin();
std::advance(iter, i);
return iter;
}
template<typename T>
pFlow::ListPtr<T>::ListPtr
(
const ListPtrType& src
)
{
if( !copy(src) )
{
fatalErrorInFunction<<
"cannot copy new item into ListPtr \n" <<
"ListPtr type is "<< typeid(T).name() <<endl;
fatalExit;
}
}
template<typename T>
pFlow::ListPtr<T>& pFlow::ListPtr<T>::operator=
(
const ListPtrType& rhs
)
{
if (this == &rhs)
{
return *this; // Self-assignment
}
// clears the content of this
clear();
if( !copy(rhs) )
{
fatalErrorInFunction<<
"cannot perform assignment from rhs into MapPtr \n" <<
"MapPtr type is "<< typeid(T).name() <<endl;
fatalExit;
}
return *this;
}
template<typename T>
T* pFlow::ListPtr<T>::set
(
label i, T* ptr
)
{
uniquePtr<T> uptr(ptr);
return set(i, uptr);
}
template<typename T>
pFlow::uniquePtr<T> pFlow::ListPtr<T>::set
(
label i,
uniquePtr<T>& ptr
)
{
if( i > size() )
{
fatalErrorInFunction<<
"Out of range access of list members. PtrList size is "<<
size() << "and you are accessing "<< i << "\n";
fatalExit;
}
auto iter = list_.begin();
std::advance(iter, i);
auto oldIter = iter;
*iter = ptr.release();
return *oldIter;
}
template<typename T>
template<typename... Args>
pFlow::uniquePtr<T> pFlow::ListPtr<T>::setSafe
(
label i,
Args&&... args
)
{
auto ptr(uniquePtr<T>::makeUnique(std::forward<Args>(args)...) );
return set(i,ptr);
}
template<typename T>
void pFlow::ListPtr<T>::push_back
(
T* ptr
)
{
list_.push_back(ptr);
}
template<typename T>
void pFlow::ListPtr<T>::push_back(uniquePtr<T>& ptr)
{
list_.push_back( ptr.release() );
}
template<typename T>
template<typename... Args>
void pFlow::ListPtr<T>::push_backSafe(Args&&... args)
{
auto ptr=makeUnique<T>(std::forward<Args>(args)...) ;
push_back(ptr);
}
template<typename T>
T& pFlow::ListPtr<T>::operator[]
(
label i
)
{
T* p = ptr(i);
if( !p )
{
fatalErrorInFunction<<
"trying to reach the reference of a nullptr or out of range access. \n";
fatalExit;
}
return *p;
}
template<typename T>
const T& pFlow::ListPtr<T>::operator[]
(
label i
) const
{
const T* p = ptr(i);
if(!p)
{
fatalErrorInFunction<<
"trying to reach the reference of a nullptr or out of range access. \n";
fatalExit;
}
return *p;
}
template<typename T>
size_t pFlow::ListPtr<T>::size()const
{
return list_.size();
}
template<typename T>
auto pFlow::ListPtr<T>::empty() const
{
return list_.emtpy();
}
template<typename T>
pFlow::uniquePtr<T> pFlow::ListPtr<T>::release
(
label i
)
{
auto p = ptr(i);
list_.erase(pos(i));
return p;
}
template<typename T>
void pFlow::ListPtr<T>::clear()
{
for( auto iter = list_.begin(); iter != list_.end(); ++iter )
{
if(*iter != nullptr)
{
delete *iter;
*iter = nullptr;
}
}
list_.clear();
}
template<typename T>
void pFlow::ListPtr<T>::clear
(
label i
)
{
T* p = ptr(i);
if( p )
{
delete p;
list_.erase(pos(i));
}
}
pFlow::ListPtr::size
size_t size() const
Definition: ListPtrI.hpp:252
pFlow::ListPtr< pFlow::processField >
fatalExit
#define fatalExit
Definition: error.hpp:57
pFlow::ListPtr::copy
bool copy(const ListPtrType &src)
Definition: ListPtrI.hpp:22
pFlow::copy
INLINE_FUNCTION_H void copy(const ViewType1D< dType, dProperties... > &dst, const ViewType1D< sType, sProperties... > &src)
Definition: ViewAlgorithms.hpp:296
pFlow::ListPtr::ptr
T * ptr(label i)
Definition: ListPtrI.hpp:42
pFlow::ListPtr::push_backSafe
void push_backSafe(Args &&... args)
Definition: ListPtrI.hpp:210
pFlow::ListPtr::release
uniquePtr< T > release(label i)
Definition: ListPtrI.hpp:265
pFlow::ListPtr::clear
void clear()
Definition: ListPtrI.hpp:275
pFlow::endl
iOstream & endl(iOstream &os)
Definition: iOstream.hpp:312
pFlow::ListPtr::pos
auto pos(label i)
Definition: ListPtrI.hpp:70
pFlow::ListPtr::push_back
void push_back(T *ptr)
Definition: ListPtrI.hpp:195
fatalErrorInFunction
#define fatalErrorInFunction
Definition: error.hpp:42
pFlow::uniquePtr
Definition: uniquePtr.hpp:44
pFlow::ListPtr::list_
std::list< T * > list_
Definition: ListPtr.hpp:57
pFlow::label
std::size_t label
Definition: builtinTypes.hpp:61
pFlow::ListPtr::set
T * set(label i, T *ptr)
Definition: ListPtrI.hpp:148
pFlow::ListPtr::ListPtr
ListPtr()
Definition: ListPtr.hpp:86
pFlow::ListPtr::empty
auto empty() const
Definition: ListPtrI.hpp:258
pFlow::ListPtr::setSafe
uniquePtr< T > setSafe(label i, Args &&... args)