www.cemf.ir
ListPtr.hpp
Go to the documentation of this file.
1 /*------------------------------- phasicFlow ---------------------------------
2  O C enter of
3  O O E ngineering and
4  O O M ultiscale modeling of
5  OOOOOOO F luid flow
6 ------------------------------------------------------------------------------
7  Copyright (C): www.cemf.ir
8  email: hamid.r.norouzi AT gmail.com
9 ------------------------------------------------------------------------------
10 Licence:
11  This file is part of phasicFlow code. It is a free software for simulating
12  granular and multiphase flows. You can redistribute it and/or modify it under
13  the terms of GNU General Public License v3 or any other later versions.
14 
15  phasicFlow is distributed to help others in their research in the field of
16  granular and multiphase flows, but WITHOUT ANY WARRANTY; without even the
17  implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
18 
19 -----------------------------------------------------------------------------*/
20 
21 #ifndef __ListPtr_hpp__
22 #define __ListPtr_hpp__
23 
24 
25 #include <list>
26 
27 
28 #include "types.hpp"
29 #include "uniquePtr.hpp"
30 #include "error.hpp"
31 #include "iOstream.hpp"
32 
33 namespace pFlow
34 {
35 
36 template<typename T >
37 class ListPtr
38 {
39 public:
40 
42 
43  using listType = std::list<T*>;
44 
45  using iterator = typename listType::iterator;
46 
47  using const_iterator = typename listType::const_iterator;
48 
49  template<typename... Args>
50  inline static uniquePtr<T> makeSafe(Args&&... args)
51  {
52  return uniquePtr<T>(new T(std::forward<Args>(args)...));
53  }
54 
55 protected:
56 
58 
59  // - list of pointers
60  std::list<T*> list_;
61 
62 
63 
65 
66  // - copy the content to this list
67  bool copy(const ListPtrType& src);
68 
69  // - return ith pointer
70  T* ptr(size_t i);
71 
72  // - return ith const poiter
73  const T* ptr(size_t i)const;
74 
75  // - iterator position of ith element
76  auto pos(size_t i);
77 
78  // - const iterator position of ith element
79  auto pos(size_t i) const;
80 
81 public:
82 
83  // - Type info
84  TypeInfoTemplateNV11("ListPtr", T);
85 
87 
88  // - empty list
90  :
91  list_()
92  {}
93 
94  // - a list with initial length of len
95  ListPtr(size_t len)
96  :
97  list_(len, nullptr)
98  {}
99 
100  // - copy construct, create new objects out of the pointers in the src
101  ListPtr(const ListPtrType& src);
102 
103 
104  //- copy assignment, create new objects out of he pointers in the src
105  ListPtrType& operator=(const ListPtrType& rhs);
106 
107 
108  // - move construct
109  // Simply move the pointers, so the new object takes the
110  // ownership of the pointers.
112  :
113  list_(std::move(src))
114  {}
115 
116  // - move assignment
117  // the lhs object takes the ownership of pointers and rhs loses the ownership
119  {
120  // clears the content of this
121  clear();
122 
123  list_.operator=(std::move(rhs));
124  return *this;
125  }
126 
128  {
129  auto ptr = makeUnique<ListPtrType>(*this);
130  return ptr.release();
131  }
132 
134  {
135  return makeUnique<ListPtrType>(*this);
136  }
137 
138  // - remove /delete all the objectes associated with pointers
140  {
141  clear();
142  }
143 
145 
146  // - set the ith element
147  uniquePtr<T> set(size_t i, T* ptr);
148 
149  // - set the ith element and take the ownership from uniquePtr
150  uniquePtr<T> set(size_t i, uniquePtr<T>&& ptr );
151 
152  // - create the object in-place and set the pointer in ith position
153  // if oject creation fails, uniquePtr deletes the memeory
154  template<typename... Args>
155  uniquePtr<T> setSafe(size_t i, Args&&... args);
156 
157  // - put the pointer at the end
158  void push_back(T* ptr);
159 
160  // - put the pointer at the end
161  void push_back(uniquePtr<T>&& ptr);
162 
163  // - safely create (in-place) and put the pointer at the end
164  template<typename... Args>
165  void push_backSafe(Args&&... args);
166 
167  // - access to ith element
168  // fatalexit if out of range or nullptr
169  T& operator[](size_t i);
170 
171  // - const access to ith element
172  // fatalexit if out of range or nullptr
173  const T& operator[](size_t i) const;
174 
175  // size of container
176  size_t size()const;
177 
178  // check if the container empty
179  auto empty() const;
180 
181  // release the ownership of ith pointer
182  uniquePtr<T> release(size_t i);
183 
184  // - clear the content of list and delete objects
185  void clear();
186 
187  // - clear the ith element
188  void clear(size_t i);
189 
191  {
192  return list_.begin();
193  }
194 
196  {
197  return list_.begin();
198  }
199 
201  {
202  return list_.end();
203  }
204 
206  {
207  return list_.end();
208  }
209 
210 };
211 
212 
213 } // pFlow
214 
215 
216 #include "ListPtrI.hpp"
217 
218 #endif
pFlow::ListPtr::ListPtrType
ListPtr< T > ListPtrType
Definition: ListPtr.hpp:41
pFlow::ListPtr::size
size_t size() const
Definition: ListPtrI.hpp:266
pFlow::ListPtr
Definition: ListPtr.hpp:37
pFlow::ListPtr< boundaryIntegration >::const_iterator
typename listType::const_iterator const_iterator
Definition: ListPtr.hpp:47
pFlow::ListPtr::~ListPtr
~ListPtr()
Definition: ListPtr.hpp:139
pFlow::ListPtr::end
const_iterator end() const
Definition: ListPtr.hpp:205
types.hpp
pFlow::ListPtr::copy
bool copy(const ListPtrType &src)
Definition: ListPtrI.hpp:22
pFlow::ListPtr::pos
auto pos(size_t i)
Definition: ListPtrI.hpp:73
pFlow::ListPtr::push_backSafe
void push_backSafe(Args &&... args)
Definition: ListPtrI.hpp:221
pFlow::ListPtr::ListPtr
ListPtr(ListPtrType &&src)
Definition: ListPtr.hpp:111
pFlow::ListPtr::TypeInfoTemplateNV11
TypeInfoTemplateNV11("ListPtr", T)
pFlow::ListPtr::setSafe
uniquePtr< T > setSafe(size_t i, Args &&... args)
pFlow::ListPtr::clear
void clear()
Definition: ListPtrI.hpp:294
pFlow
Definition: demGeometry.hpp:27
pFlow::ListPtr::push_back
void push_back(T *ptr)
Definition: ListPtrI.hpp:204
pFlow::ListPtr::ListPtr
ListPtr(size_t len)
Definition: ListPtr.hpp:95
pFlow::ListPtr::clone
uniquePtr< ListPtrType > clone() const
Definition: ListPtr.hpp:133
uniquePtr.hpp
pFlow::ListPtr::begin
const_iterator begin() const
Definition: ListPtr.hpp:195
pFlow::ListPtr::clonePtr
ListPtrType * clonePtr() const
Definition: ListPtr.hpp:127
pFlow::ListPtr::operator=
ListPtrType & operator=(ListPtrType &&rhs)
Definition: ListPtr.hpp:118
pFlow::ListPtr::operator=
ListPtrType & operator=(const ListPtrType &rhs)
Definition: ListPtrI.hpp:129
pFlow::ListPtr::release
uniquePtr< T > release(size_t i)
Definition: ListPtrI.hpp:281
pFlow::ListPtr::end
iterator end()
Definition: ListPtr.hpp:200
pFlow::ListPtr< boundaryIntegration >::listType
std::list< boundaryIntegration * > listType
Definition: ListPtr.hpp:43
pFlow::ListPtr::makeSafe
static uniquePtr< T > makeSafe(Args &&... args)
Definition: ListPtr.hpp:50
pFlow::uniquePtr
Definition: uniquePtr.hpp:42
pFlow::ListPtr::ptr
T * ptr(size_t i)
Definition: ListPtrI.hpp:43
pFlow::ListPtr::list_
std::list< T * > list_
Definition: ListPtr.hpp:60
pFlow::ListPtr::operator[]
T & operator[](size_t i)
Definition: ListPtrI.hpp:230
iOstream.hpp
pFlow::ListPtr::begin
iterator begin()
Definition: ListPtr.hpp:190
pFlow::ListPtr::ListPtr
ListPtr()
Definition: ListPtr.hpp:89
pFlow::ListPtr::empty
auto empty() const
Definition: ListPtrI.hpp:273
pFlow::ListPtr< boundaryIntegration >::iterator
typename listType::iterator iterator
Definition: ListPtr.hpp:45
error.hpp
pFlow::ListPtr::set
uniquePtr< T > set(size_t i, T *ptr)
Definition: ListPtrI.hpp:155
ListPtrI.hpp