www.cemf.ir
ListPtrI.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 template<typename T>
22 inline bool pFlow::ListPtr<T>::copy(const ListPtrType& src)
23 {
24 
25  for( auto& iter : src.list_)
26  {
27 
28  if( iter != nullptr)
29  {
30  push_back( iter->clone().release());
31  }
32  else
33  {
34  push_back(nullptr);
35  }
36  }
37 
38  return true;
39 }
40 
41 template<typename T>
42 inline
44 {
45 
46  if(i >= size() ) return nullptr;
47 
48  auto iter = list_.begin();
49  std::advance(iter, i);
50 
51  return *iter;
52 }
53 
54 template<typename T>
55 inline
57 (
58  size_t i
59 ) const
60 {
61 
62  if(i >= size() ) return nullptr;
63 
64  auto iter = list_.cbegin();
65  std::advance(iter, i);
66 
67  return *iter;
68 }
69 
70 template<typename T>
71 inline
73 (
74  size_t i
75 )
76 {
77  if(i >= size() )
78  {
80  "out of range access to container element. \n";
81  fatalExit;
82  }
83 
84  auto iter = list_.begin();
85  std::advance(iter, i);
86  return iter;
87 }
88 
89 template<typename T>
90 inline
92 (
93  size_t i
94 )const
95 {
96  if(i >= size() )
97  {
99  "out of range access to container element. \n";
100  fatalExit;
101  }
102 
103  auto iter = list_.cbegin();
104  std::advance(iter, i);
105  return iter;
106 }
107 
108 template<typename T>
109 inline
111 (
112  const ListPtrType& src
113 )
114 {
115 
116  if( !copy(src) )
117  {
119  "cannot copy new item into ListPtr \n" <<
120  "ListPtr type is "<< typeid(T).name() <<endl;
121  fatalExit;
122  }
123 }
124 
125 
126 template<typename T>
127 inline
129 (
130  const ListPtrType& rhs
131 )
132 {
133  if (this == &rhs)
134  {
135  return *this; // Self-assignment
136  }
137 
138  // clears the content of this
139  clear();
140 
141  if( !copy(rhs) )
142  {
144  "cannot perform assignment from rhs into MapPtr \n" <<
145  "MapPtr type is "<< typeid(T).name() <<endl;
146  fatalExit;
147  }
148 
149  return *this;
150 }
151 
152 template<typename T>
153 inline
155 (
156  size_t i, T* ptr
157 )
158 {
159  uniquePtr<T> uptr(ptr);
160  return set(i, uptr);
161 }
162 
163 
164 template<typename T>
165 inline
167 (
168  size_t i,
169  uniquePtr<T>&& ptr
170 )
171 {
172  if( i > size() )
173  {
175  "Out of range access of list members. PtrList size is "<<
176  size() << "and you are accessing "<< i << "\n";
177  fatalExit;
178  }
179 
180  auto iter = list_.begin();
181  std::advance(iter, i);
182  T* oldPtr = *iter;
183  *iter = ptr.release();
184  return uniquePtr<T>(oldPtr);
185 }
186 
187 
188 template<typename T>
189 template<typename... Args>
190 inline
192 (
193  size_t i,
194  Args&&... args
195 )
196 {
197  auto ptr(uniquePtr<T>::makeUnique(std::forward<Args>(args)...) );
198  return set(i,ptr);
199 }
200 
201 template<typename T>
202 inline
204 (
205  T* ptr
206 )
207 {
208  list_.push_back(ptr);
209 }
210 
211 template<typename T>
212 inline
214 {
215  list_.push_back( ptr.release() );
216 }
217 
218 template<typename T>
219 template<typename... Args>
220 inline
222 {
223  uniquePtr<T> ptr = makeUnique<T>(std::forward<Args>(args)...) ;
224  push_back(ptr);
225 }
226 
227 template<typename T>
228 inline
230 (
231  size_t i
232 )
233 {
234  T* p = ptr(i);
235 
236  if( !p )
237  {
239  "trying to reach the reference of a nullptr or out of range access. \n";
240  fatalExit;
241  }
242 
243  return *p;
244 }
245 
246 template<typename T>
247 inline
249 (
250  size_t i
251 ) const
252 {
253  const T* p = ptr(i);
254 
255  if(!p)
256  {
258  "trying to reach the reference of a nullptr or out of range access. \n";
259  fatalExit;
260  }
261  return *p;
262 }
263 
264 template<typename T>
265 inline
267 {
268  return list_.size();
269 }
270 
271 template<typename T>
272 inline
274 {
275  return list_.emtpy();
276 }
277 
278 template<typename T>
279 inline
281 (
282  size_t i
283 )
284 {
285  auto p = ptr(i);
286  list_.erase(pos(i));
287  return p;
288 }
289 
290 
291 
292 template<typename T>
293 inline
295 {
296 
297  for( auto iter = list_.begin(); iter != list_.end(); ++iter )
298  {
299  if(*iter != nullptr)
300  {
301  delete *iter;
302  *iter = nullptr;
303  }
304  }
305 
306  list_.clear();
307 }
308 
309 template<typename T>
310 inline
312 (
313  size_t i
314 )
315 {
316  T* p = ptr(i);
317 
318  if( p )
319  {
320  delete p;
321  list_.erase(pos(i));
322  }
323 }
pFlow::ListPtr::size
size_t size() const
Definition: ListPtrI.hpp:266
pFlow::ListPtr< boundaryIntegration >
fatalExit
#define fatalExit
Fatal exit.
Definition: error.hpp:98
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:234
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::setSafe
uniquePtr< T > setSafe(size_t i, Args &&... args)
pFlow::ListPtr::clear
void clear()
Definition: ListPtrI.hpp:294
pFlow::endl
iOstream & endl(iOstream &os)
Add newline and flush stream.
Definition: iOstream.hpp:341
pFlow::ListPtr::push_back
void push_back(T *ptr)
Definition: ListPtrI.hpp:204
fatalErrorInFunction
#define fatalErrorInFunction
Report a fatal error and function name and exit the application.
Definition: error.hpp:77
pFlow::ListPtr::release
uniquePtr< T > release(size_t i)
Definition: ListPtrI.hpp:281
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::ListPtr
ListPtr()
Definition: ListPtr.hpp:89
pFlow::ListPtr::empty
auto empty() const
Definition: ListPtrI.hpp:273
pFlow::ListPtr::set
uniquePtr< T > set(size_t i, T *ptr)
Definition: ListPtrI.hpp:155