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>
43 {
44 
45  if(i >= size() ) return nullptr;
46 
47  auto iter = list_.begin();
48  std::advance(iter, i);
49 
50  return *iter;
51 }
52 
53 template<typename T>
55 (
56  label i
57 ) const
58 {
59 
60  if(i >= size() ) return nullptr;
61 
62  auto iter = list_.cbegin();
63  std::advance(iter, i);
64 
65  return *iter;
66 }
67 
68 template<typename T>
70 (
71  label i
72 )
73 {
74  if(i >= size() )
75  {
77  "out of range access to container element. \n";
78  fatalExit;
79  }
80 
81  auto iter = list_.begin();
82  std::advance(iter, i);
83  return iter;
84 }
85 
86 template<typename T>
88 (
89  label i
90 )const
91 {
92  if(i >= size() )
93  {
95  "out of range access to container element. \n";
96  fatalExit;
97  }
98 
99  auto iter = list_.cbegin();
100  std::advance(iter, i);
101  return iter;
102 }
103 
104 template<typename T>
106 (
107  const ListPtrType& src
108 )
109 {
110 
111  if( !copy(src) )
112  {
114  "cannot copy new item into ListPtr \n" <<
115  "ListPtr type is "<< typeid(T).name() <<endl;
116  fatalExit;
117  }
118 }
119 
120 
121 template<typename T>
123 (
124  const ListPtrType& rhs
125 )
126 {
127  if (this == &rhs)
128  {
129  return *this; // Self-assignment
130  }
131 
132  // clears the content of this
133  clear();
134 
135  if( !copy(rhs) )
136  {
138  "cannot perform assignment from rhs into MapPtr \n" <<
139  "MapPtr type is "<< typeid(T).name() <<endl;
140  fatalExit;
141  }
142 
143  return *this;
144 }
145 
146 template<typename T>
148 (
149  label i, T* ptr
150 )
151 {
152  uniquePtr<T> uptr(ptr);
153  return set(i, uptr);
154 }
155 
156 
157 template<typename T>
159 (
160  label i,
161  uniquePtr<T>& ptr
162 )
163 {
164  if( i > size() )
165  {
167  "Out of range access of list members. PtrList size is "<<
168  size() << "and you are accessing "<< i << "\n";
169  fatalExit;
170  }
171 
172  auto iter = list_.begin();
173  std::advance(iter, i);
174  auto oldIter = iter;
175  *iter = ptr.release();
176  return *oldIter;
177 
178 }
179 
180 
181 template<typename T>
182 template<typename... Args>
184 (
185  label i,
186  Args&&... args
187 )
188 {
189  auto ptr(uniquePtr<T>::makeUnique(std::forward<Args>(args)...) );
190  return set(i,ptr);
191 }
192 
193 template<typename T>
195 (
196  T* ptr
197 )
198 {
199  list_.push_back(ptr);
200 }
201 
202 template<typename T>
204 {
205  list_.push_back( ptr.release() );
206 }
207 
208 template<typename T>
209 template<typename... Args>
211 {
212  auto ptr=makeUnique<T>(std::forward<Args>(args)...) ;
213  push_back(ptr);
214 }
215 
216 template<typename T>
218 (
219  label i
220 )
221 {
222  T* p = ptr(i);
223 
224  if( !p )
225  {
227  "trying to reach the reference of a nullptr or out of range access. \n";
228  fatalExit;
229  }
230 
231  return *p;
232 }
233 
234 template<typename T>
236 (
237  label i
238 ) const
239 {
240  const T* p = ptr(i);
241 
242  if(!p)
243  {
245  "trying to reach the reference of a nullptr or out of range access. \n";
246  fatalExit;
247  }
248  return *p;
249 }
250 
251 template<typename T>
253 {
254  return list_.size();
255 }
256 
257 template<typename T>
259 {
260  return list_.emtpy();
261 }
262 
263 template<typename T>
265 (
266  label i
267 )
268 {
269  auto p = ptr(i);
270  list_.erase(pos(i));
271  return p;
272 }
273 
274 template<typename T>
276 {
277  for( auto iter = list_.begin(); iter != list_.end(); ++iter )
278  {
279  if(*iter != nullptr)
280  {
281  delete *iter;
282  *iter = nullptr;
283  }
284  }
285  list_.clear();
286 }
287 
288 template<typename T>
290 (
291  label i
292 )
293 {
294  T* p = ptr(i);
295 
296  if( p )
297  {
298  delete p;
299  list_.erase(pos(i));
300  }
301 }
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)