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

ListPtr.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 __ListPtr_hpp__
#define __ListPtr_hpp__
#include <list>
#include "types.hpp"
#include "uniquePtr.hpp"
#include "error.hpp"
#include "iOstream.hpp"
namespace pFlow
{
template<typename T >
class ListPtr
{
public:
using ListPtrType = ListPtr<T> ;
using listType = std::list<T*>;
template<typename... Args>
inline static uniquePtr<T> makeSafe(Args&&... args)
{
return uniquePtr<T>(new T(std::forward<Args>(args)...));
}
protected:
// - list of pointers
std::list<T*> list_;
// - copy the content to this list
bool copy(const ListPtrType& src);
// - return ith pointer
T* ptr(label i);
// - return ith const poiter
const T* ptr(label i)const;
// - iterator position of ith element
auto pos(label i);
// - const iterator position of ith element
auto pos(label i) const;
public:
// - Type info
TypeInfoTemplateNV("ListPtr", T);
// - empty list
ListPtr()
:
list_()
{}
// - a list with initial length of len
ListPtr(size_t len)
:
list_(len)
{}
// - copy construct, create new objects out of the pointers in the src
ListPtr(const ListPtrType& src);
//- copy assignment, create new objects out of he pointers in the src
ListPtrType& operator=(const ListPtrType& rhs);
// - move construct
// Simply move the pointers, so the new object takes the
// ownership of the pointers.
ListPtr( ListPtrType&& src )
:
list_(std::move(src))
{}
// - move assignment
// the lhs object takes the ownership of pointers and rhs loses the ownership
ListPtrType& operator=(ListPtrType&& rhs)
{
// clears the content of this
clear();
list_.operator=(std::move(rhs));
return *this;
}
ListPtrType* clonePtr()const
{
auto ptr = makeUnique<ListPtrType>(*this);
return ptr.release();
}
uniquePtr<ListPtrType> clone()const
{
return makeUnique<ListPtrType>(*this);
}
// - remove /delete all the objectes associated with pointers
~ListPtr()
{
clear();
}
// - set the ith element
T* set(label i, T* ptr);
// - set the ith element and take the ownership from uniquePtr
uniquePtr<T> set(label i, uniquePtr<T>& ptr );
// - create the object in-place and set the pointer in ith position
// if oject creation fails, uniquePtr deletes the memeory
template<typename... Args>
uniquePtr<T> setSafe(label i, Args&&... args);
// - put the pointer at the end
void push_back(T* ptr);
// - put the pointer at the end
void push_back(uniquePtr<T>& ptr);
// - safely create (in-place) and put the pointer at the end
template<typename... Args>
void push_backSafe(Args&&... args);
// - access to ith element
// fatalexit if out of range or nullptr
T& operator[](label i);
// - const access to ith element
// fatalexit if out of range or nullptr
const T& operator[](label i) const;
// size of container
size_t size()const;
// check if the container empty
auto empty() const;
// release the ownership of ith pointer
uniquePtr<T> release(label i);
// - clear the content of list and delete objects
void clear();
// - clear the ith element
void clear(label i);
// - clone the object
};
} // pFlow
#include "ListPtrI.hpp"
#endif
pFlow::ListPtr::ListPtrType
ListPtr< T > ListPtrType
Definition: ListPtr.hpp:42
pFlow::ListPtr::size
size_t size() const
Definition: ListPtrI.hpp:252
pFlow::ListPtr
Definition: ListPtr.hpp:38
pFlow::ListPtr::~ListPtr
~ListPtr()
Definition: ListPtr.hpp:136
types.hpp
pFlow::ListPtr::copy
bool copy(const ListPtrType &src)
Definition: ListPtrI.hpp:22
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::ListPtr
ListPtr(ListPtrType &&src)
Definition: ListPtr.hpp:108
pFlow::ListPtr::release
uniquePtr< T > release(label i)
Definition: ListPtrI.hpp:265
pFlow::ListPtr::clear
void clear()
Definition: ListPtrI.hpp:275
pFlow::ListPtr::pos
auto pos(label i)
Definition: ListPtrI.hpp:70
pFlow
Definition: demComponent.hpp:28
pFlow::ListPtr::push_back
void push_back(T *ptr)
Definition: ListPtrI.hpp:195
pFlow::ListPtr::ListPtr
ListPtr(size_t len)
Definition: ListPtr.hpp:92
pFlow::ListPtr::clone
uniquePtr< ListPtrType > clone() const
Definition: ListPtr.hpp:130
uniquePtr.hpp
pFlow::ListPtr::operator[]
T & operator[](label i)
Definition: ListPtrI.hpp:218
pFlow::ListPtr::clonePtr
ListPtrType * clonePtr() const
Definition: ListPtr.hpp:124
pFlow::ListPtr::TypeInfoTemplateNV
TypeInfoTemplateNV("ListPtr", T)
pFlow::ListPtr::operator=
ListPtrType & operator=(ListPtrType &&rhs)
Definition: ListPtr.hpp:115
pFlow::ListPtr::operator=
ListPtrType & operator=(const ListPtrType &rhs)
Definition: ListPtrI.hpp:123
pFlow::ListPtr< pFlow::processField >::listType
std::list< pFlow::processField * > listType
Definition: ListPtr.hpp:43
pFlow::ListPtr::makeSafe
static uniquePtr< T > makeSafe(Args &&... args)
Definition: ListPtr.hpp:47
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
iOstream.hpp
pFlow::ListPtr::ListPtr
ListPtr()
Definition: ListPtr.hpp:86
pFlow::ListPtr::empty
auto empty() const
Definition: ListPtrI.hpp:258
error.hpp
pFlow::ListPtr::setSafe
uniquePtr< T > setSafe(label i, Args &&... args)
ListPtrI.hpp