www.cemf.ir
MapPtrI.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 
22 template< template<class, class> class Container, class Key, class T >
24 (
25  const MapPtrType& src
26 )
27 {
28  for(constIterator iter= src.begin(); iter != src.end(); ++iter)
29  {
30 
31  // first make sure that the new key can be inserted
32  auto [nIter, suc] = map_.insert(valueType(iter->first, nullptr));
33 
34  // then insert the new item
35  if( suc )
36  {
37  if(iter->second != nullptr)
38  map_[iter->first] = iter->second->clonePtr();
39 
40  }
41  else
42  {
43  return false;
44  }
45 
46  }
47 
48  return true;
49 }
50 
51 template< template<class, class> class Container, class Key, class T >
53 (
54  const keyType& k
55 )
56 {
57  if( auto iter = map_.find(k); iter!= map_.end() )
58  return iter->second;
59  else
60  return nullptr;
61 }
62 
63 template< template<class, class> class Container, class Key, class T >
65 (
66  const keyType& k
67 )const
68 {
69  if( auto iter = map_.find(k); iter!= map_.cend() )
70  return iter->second;
71  else
72  return nullptr;
73 }
74 
75 template< template<class, class> class Container, class Key, class T >
77 (
78  const MapPtrType& src
79 )
80 {
81 
82  if( !copy(src) )
83  {
85  "cannot copy new item into MapPtr \n" <<
86  "MapPtr type is "<< typeid(T).name() <<endl;
87  fatalExit;
88  }
89 }
90 
91 template< template<class, class> class Container, class Key, class T >
93 (
94  const MapPtrType& rhs
95 )
96 {
97  if (this == &rhs)
98  {
99  return *this; // Self-assignment
100  }
101 
102  // clears the content of this
103  clear();
104 
105  if( !copy(rhs) )
106  {
108  "cannot perform assignment from rhs into MapPtr \n" <<
109  "MapPtr type is "<< typeid(T).name() <<endl;
110  fatalExit;
111  }
112 
113  return *this;
114 
115 }
116 
117 template< template<class, class> class Container, class Key, class T >
119 (
120  MapPtrType&& src
121 )
122 :
123  map_(std::move(src))
124 {}
125 
126 template< template<class, class> class Container, class Key, class T >
128 (
129  MapPtrType&& rhs
130 )
131 {
132  // clears the content of this
133  clear();
134 
135  map_.operator=(std::move(rhs));
136  return *this;
137 }
138 
139 
140 template< template<class, class> class Container, class Key, class T >
142 (
143  const keyType& key,
144  T* ptr
145 )
146 {
147  // delete the current key if it exists
148  erase(key);
149 
150  auto [iter, success] = map_.insert( valueType(key,ptr));
151 
152  return success;
153 }
154 
155 template< template<class, class> class Container, class Key, class T >
157 (
158  const keyType& key,
159  uniquePtr<T>& ptr
160 )
161 {
162  erase(key);
163  if(auto [iter, success] = map_.insert(valueType(key, nullptr)); success)
164  {
165  map_[key] = ptr.release();
166  return true;
167  }
168 
169  return false;
170 }
171 
172 template< template<class, class> class Container, class Key, class T >
173 template<typename... Args>
175 (
176  const keyType& key,
177  Args&&... args
178 )
179 {
180  auto ptr = makeUnique<T>(std::forward<Args>(args)...);
181  return insertReplace(key, ptr);
182 }
183 
184 
185 template< template<class, class> class Container, class Key, class T >
187 (
188  const keyType& key,
189  T* ptr
190 )
191 {
192 
193  auto [optr, exist] = find(key);
194  map_[key] = ptr;
195  return optr;
196 }
197 
198 template< template<class, class> class Container, class Key, class T >
200 (
201  const keyType& key,
202  uniquePtr<T>& ptr)
203 {
204 
205  auto[optr,exist] = find(key);
206  map_[key] = ptr.release();
207  return optr;
208 
209 }
210 
211 template< template<class, class> class Container, class Key, class T >
212 template<typename... Args>
214 (
215  const keyType& key, Args&&... args
216 )
217 {
218  auto ptr = makeUnique<T>(std::forward<Args>(args)...);
219  return set(key, ptr);
220 }
221 
222 
223 template< template<class, class> class Container, class Key, class T >
225 (
226  const keyType& key
227 )
228 {
229  T* p = findPtr(key);
230  if( !p )
231  {
233  "trying to reach the reference of a nullptr or out of range access the element with key "
234  <<key<<endl;
235  fatalExit;
236  }
237  return *p;
238 }
239 
240 template< template<class, class> class Container, class Key, class T >
242 (
243  const keyType& key
244 )const
245 {
246  const T* p = findPtr(key);
247  if( !p )
248  {
250  "trying to reach the reference of a nullptr or out of range access the element with key "
251  <<key<<endl;
252  fatalExit;
253  }
254 
255  return *p;
256 }
257 
258 template< template<class, class> class Container, class Key, class T >
260 (
261  const keyType k
262 ) const
263 {
264  auto [iter, found] = find(k);
265  return found;
266 }
267 
268 
269 template< template<class, class> class Container, class Key, class T >
270 std::pair<const T*, bool> pFlow::MapPtr<Container, Key, T>::find
271 (
272  const keyType& k
273 )const
274 {
275  if( auto iter = map_.find(k); iter!= map_.end() )
276  return {iter->second,true};
277  else
278  return {nullptr,false};
279 }
280 
281 
282 template< template<class, class> class Container, class Key, class T >
283 std::pair<T*, bool> pFlow::MapPtr<Container, Key, T>::find(const keyType& k)
284 {
285  if( auto iter = map_.find(k); iter!= map_.end() )
286  return {iter->second,true};
287  else
288  return {nullptr,false};
289 }
290 
291 
292 template< template<class, class> class Container, class Key, class T >
294 (
295  const keyType& key
296 )
297 {
298  auto p = findPtr(key);
299  map_.erase(key);
300  return p;
301 }
302 
303 
304 template< template<class, class> class Container, class Key, class T >
306 (
307  const keyType& key
308 )
309 {
310  if( auto ptr = findPtr(key); ptr )
311  {
312  delete ptr;
313  ptr = nullptr;
314  }
315 
316  map_.erase(key);
317 }
318 
319 template< template<class, class> class Container, class Key, class T >
321 {
322  for( auto iter = map_.begin(); iter != map_.end(); ++iter )
323  {
324  if(iter->second != nullptr)
325  {
326  delete iter->second;
327  iter->second = nullptr;
328  }
329  }
330  map_.clear();
331 }
332 
333 
pFlow::MapPtr::find
std::pair< const T *, bool > find(const keyType &k) const
Definition: MapPtrI.hpp:271
fatalExit
#define fatalExit
Fatal exit.
Definition: error.hpp:98
pFlow::copy
INLINE_FUNCTION_H void copy(const ViewType1D< dType, dProperties... > &dst, const ViewType1D< sType, sProperties... > &src)
Definition: ViewAlgorithms.hpp:234
pFlow::find
int64 find(Vector< T, Allocator > &vec, const T &val)
Definition: VectorAlgorithm.hpp:69
pFlow::MapPtr::release
uniquePtr< T > release(const keyType &k)
Definition: MapPtrI.hpp:294
pFlow::MapPtr::insertReplaceSafe
bool insertReplaceSafe(const keyType &key, Args &&... args)
Definition: MapPtrI.hpp:175
pFlow::MapPtr::findPtr
T * findPtr(const keyType &k)
Definition: MapPtrI.hpp:53
pFlow::MapPtr::clear
void clear()
Definition: MapPtrI.hpp:320
pFlow::endl
iOstream & endl(iOstream &os)
Add newline and flush stream.
Definition: iOstream.hpp:341
pFlow::MapPtr< pFlow::iEntry >::constIterator
typename mapType::const_iterator constIterator
Definition: MapPtr.hpp:60
pFlow::MapPtr< pFlow::iEntry >::valueType
typename mapType::value_type valueType
Definition: MapPtr.hpp:52
pFlow::MapPtr
Definition: MapPtr.hpp:39
fatalErrorInFunction
#define fatalErrorInFunction
Report a fatal error and function name and exit the application.
Definition: error.hpp:77
pFlow::MapPtr::copy
bool copy(const MapPtrType &src)
Definition: MapPtrI.hpp:24
pFlow::MapPtr::set
T * set(const keyType &key, T *ptr)
Definition: MapPtrI.hpp:187
pFlow::MapPtr::end
iterator end()
Definition: MapPtr.hpp:212
pFlow::MapPtr::MapPtr
MapPtr()
Definition: MapPtr.hpp:92
pFlow::uniquePtr
Definition: uniquePtr.hpp:42
pFlow::MapPtr< pFlow::iEntry >::keyType
typename mapType::key_type keyType
Definition: MapPtr.hpp:48
pFlow::MapPtr::erase
void erase(const keyType &key)
Definition: MapPtrI.hpp:306
pFlow::MapPtr::search
bool search(const keyType k) const
Definition: MapPtrI.hpp:260
pFlow::MapPtr::insertReplace
bool insertReplace(const keyType &key, T *ptr)
Definition: MapPtrI.hpp:142
pFlow::MapPtr::setSafe
uniquePtr< T > setSafe(const keyType &key, Args &&... args)
pFlow::MapPtr::begin
iterator begin()
Definition: MapPtr.hpp:202