www.cemf.ir
MapPtr.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 __MapPtr_hpp__
22 #define __MapPtr_hpp__
23 
24 
25 #include <typeinfo>
26 #include <map>
27 #include <unordered_map>
28 
29 #include "types.hpp"
30 #include "uniquePtr.hpp"
31 #include "error.hpp"
32 #include "iOstream.hpp"
33 
34 
35 namespace pFlow
36 {
37 
38 template< template<class, class> class Container, class Key, class T >
39 class MapPtr
40 
41 {
42 public:
43 
45 
46  using mapType = Container<Key, T*>;
47 
48  using keyType = typename mapType::key_type;
49 
50  using mappedType = typename mapType::mapped_type;
51 
52  using valueType = typename mapType::value_type;
53 
54  using reference = typename mapType::reference;
55 
56  using constReference = typename mapType::const_reference;
57 
58  using iterator = typename mapType::iterator;
59 
60  using constIterator = typename mapType::const_iterator;
61 
62  template<typename... Args>
63  inline static uniquePtr<T> makeSafe(Args&&... args)
64  {
65  return std::make_unique(std::forward<Args>(args)...);
66  }
67 
68 protected:
69 
70  // data members
71  Container<Key, T*> map_;
72 
73 
74 
75  // Methods
76  bool copy(const MapPtrType& src);
77 
78  // - find a key and return the the pointer associated with the key
79  // nullptr if fails
80  T* findPtr(const keyType& k);
81 
82  const T* findPtr(const keyType& k)const ;
83 
84 public:
85 
86  // - type info
87  TypeInfoTemplateNV11("MapPtr", Key);
88 
90 
91  // - empty map
93  :
94  map_()
95  {}
96 
97  // - copy construct, create new objects out of the objects in the src
98  MapPtr(const MapPtrType& src);
99 
100 
101  // - copy assignment, create new objects out of he pointers in the src
102  MapPtrType& operator=(const MapPtrType& rhs);
103 
104 
105  // move construct
106  // it just simply move the pointers, so the new objects takes the
107  // ownership of the pointers.
108  MapPtr( MapPtrType&& src );
109 
110 
111  // move assignment
112  // the lhs object takes the ownership of the new objects and rhs loses the ownership
114 
115  // removes /deletes all the objectes associated with pointers
116 
118  {
119  return makeUnique<MapPtrType>(*this);
120  }
121 
123  {
124  return new MapPtrType(*this);
125  }
126 
128  {
129  clear();
130  }
131 
133 
134  // - insert the object with key and replace if it already exists in the container
135  // return false if fails to insert, no action is taken for object deletion
136  bool insertReplace(const keyType& key, T* ptr);
137 
138  // - insert the object with key and replace if it already exists in the container
139  // return false if fail to insert, no action is taken for object deletion
140  bool insertReplace(const keyType& key, uniquePtr<T>& ptr);
141 
142  // - insert the object (construct in-place)
143  // return false if fail to insert, the object is deleted by default if fails
144  template<typename... Args>
145  bool insertReplaceSafe(const keyType& key, Args&&... args);
146 
147  // - insert the object if key is new and return nullptr
148  // - if key exist, replace the new object and return the pointer to the old object
149  T* set(const keyType& key, T* ptr);
150 
151  // - insert the object if key is new and return nullptr
152  // - if key exist, replace the new object and return the pointer to the old object
153  uniquePtr<T> set(const keyType& key, uniquePtr<T>& ptr );
154 
155  // - insert the object (construct in-place), if key is new and return nullptr
156  // - if key exist, replace the new object and return the pointer to the old object
157  template<typename... Args>
158  uniquePtr<T> setSafe(const keyType& key, Args&&... args);
159 
160  // - reference to the object found by key
161  // fatalError if not found
162  T& operator[] (const keyType& key);
163 
164  // - const reference to the object found by key
165  // fatalError if not found
166  const T& operator[] (const keyType& key)const;
167 
168  // - search if a key exists
169  bool search(const keyType k) const;
170 
171 
172  // - find a key and return the const pointer if successful
173  std::pair<const T*, bool> find(const keyType& k)const;
174 
175  // - find a key and return the pointer if successful
176  std::pair<T*, bool> find(const keyType& k);
177 
178 
179  // - find the key and release the ownership of the object associated with it
180  // delete the key after release
181  uniquePtr<T> release(const keyType& k);
182 
183  // - erase the element and delete the object if it is allocated
184  void erase( const keyType& key );
185 
186  // - clear the content of the map with object deletion
187  void clear();
188 
189  // - size of container
190  size_t size() const
191  {
192  return map_.size();
193  }
194 
195  // - check if the container empty
196  auto empty() const
197  {
198  return map_.emtpy();
199  }
200 
201  // - iterators
203  {
204  return map_.begin();
205  }
206 
208  {
209  return map_.begin();
210  }
211 
213  {
214  return map_.end();
215  }
216 
218  {
219  return map_.end();
220  }
221 
222 };
223 
224 // ordered (sorted) map
225 template<typename key, typename T>
227 
228 // unordered map (hash) map pointer
229 template<typename key, typename T>
231 
232 template<typename T>
234 
235 template<typename T>
237 
238 
239 template<typename T>
241 {
242  if (m.empty())
243  return os<<"wordHashMapPtr is empty"<<endl;
244 
245  for(const auto iter : m )
246  {
247  os << iter->first<<endl;
248  }
249 
250  return os;
251 }
252 
253 template<typename T>
254 inline iOstream& printKeys(iOstream& os, const wordOrderedMapPtr<T> & m)
255 {
256  if (m.empty())
257  return os<<"wordOrderedMapPtr is empty"<<endl;
258 
259  for(const auto iter : m )
260  {
261  os << iter->first<<endl;
262  }
263 
264  return os;
265 }
266 
267 
268 } // pFlow
269 
270 
271 #include "MapPtrI.hpp"
272 
273 #endif
pFlow::MapPtr::clonePtr
MapPtrType * clonePtr() const
Definition: MapPtr.hpp:122
pFlow::MapPtr::find
std::pair< const T *, bool > find(const keyType &k) const
Definition: MapPtrI.hpp:271
pFlow::MapPtr::makeSafe
static uniquePtr< T > makeSafe(Args &&... args)
Definition: MapPtr.hpp:63
pFlow::MapPtr::begin
constIterator begin() const
Definition: MapPtr.hpp:207
pFlow::MapPtr::clone
uniquePtr< MapPtrType > clone() const
Definition: MapPtr.hpp:117
pFlow::MapPtr< pFlow::iEntry >::reference
typename mapType::reference reference
Definition: MapPtr.hpp:54
pFlow::MapPtr::operator[]
T & operator[](const keyType &key)
Definition: MapPtrI.hpp:225
pFlow::MapPtr::end
constIterator end() const
Definition: MapPtr.hpp:217
types.hpp
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::printKeys
iOstream & printKeys(iOstream &os, const wordHashMap< T > &m)
pFlow::MapPtr::clear
void clear()
Definition: MapPtrI.hpp:320
pFlow::endl
iOstream & endl(iOstream &os)
Add newline and flush stream.
Definition: iOstream.hpp:341
MapPtrI.hpp
pFlow::MapPtr< pFlow::iEntry >::constIterator
typename mapType::const_iterator constIterator
Definition: MapPtr.hpp:60
pFlow::MapPtr::~MapPtr
~MapPtr()
Definition: MapPtr.hpp:127
pFlow
Definition: demGeometry.hpp:27
pFlow::MapPtr::size
size_t size() const
Definition: MapPtr.hpp:190
pFlow::MapPtr< pFlow::iEntry >::valueType
typename mapType::value_type valueType
Definition: MapPtr.hpp:52
uniquePtr.hpp
pFlow::MapPtr
Definition: MapPtr.hpp:39
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::MapPtr::operator=
MapPtrType & operator=(const MapPtrType &rhs)
Definition: MapPtrI.hpp:93
pFlow::MapPtr< pFlow::iEntry >::mappedType
typename mapType::mapped_type mappedType
Definition: MapPtr.hpp:50
pFlow::MapPtr::MapPtrType
MapPtr< Container, Key, T > MapPtrType
Definition: MapPtr.hpp:44
pFlow::MapPtr< pFlow::iEntry >::constReference
typename mapType::const_reference constReference
Definition: MapPtr.hpp:56
pFlow::uniquePtr
Definition: uniquePtr.hpp:42
pFlow::MapPtr::TypeInfoTemplateNV11
TypeInfoTemplateNV11("MapPtr", Key)
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::iEntry< Key, T * >
pFlow::MapPtr< pFlow::iEntry >::iterator
typename mapType::iterator iterator
Definition: MapPtr.hpp:58
iOstream.hpp
m
uint32 m
Definition: NBSLoop.hpp:22
pFlow::MapPtr::map_
Container< Key, T * > map_
Definition: MapPtr.hpp:71
pFlow::MapPtr::search
bool search(const keyType k) const
Definition: MapPtrI.hpp:260
pFlow::MapPtr::empty
auto empty() const
Definition: MapPtr.hpp:196
pFlow::iOstream
Interface class for any output stream.
Definition: iOstream.hpp:59
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
error.hpp