www.cemf.ir
anyList.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 __anyList_hpp__
22 #define __anyList_hpp__
23 
24 #include <any>
25 
26 #include "typeInfo.hpp"
27 #include "types.hpp"
28 #include "List.hpp"
29 
30 
31 namespace pFlow
32 {
33 
34 
35 class anyList
36 {
37 
38 public:
39 
41 
43 
44  using iterator = typename anyListType::iterator;
45 
47 
48 private:
49 
52 
55 
57 
58 public:
59 
60  // - Type info
61  TypeInfoNV("anyList");
62 
64 
65  // All five constructors are created by compiler
66 
67 
69 
71  size_t size()const;
72 
74  bool empty()const;
75 
77  bool contains(const word& name)const;
78 
80  const wordList& names()const;
81 
83  template<typename T, typename... Args>
84  reference emplaceBack(const word& name, Args&&... args)
85  {
86  if( contains(name))
87  {
89  "variable name "<< name << " already exists in the anyList."<<endl<<
90  "list of variables is "<<names_<<endl;
91  fatalExit;
92  }
93  names_.push_back(name);
94  types_.push_back(getTypeName<T>());
95  return anyList_.emplace_back(
96  std::in_place_type<T>,
97  std::forward<Args>(args)...);
98  }
99 
101  template<typename T>
102  reference emplaceBack(const word& name, const T & other)
103  {
104  if( contains(name) )
105  {
107  "variable name "<< name << " already exists in the anyList."<<endl<<
108  "list of variables is "<<names_<<endl;
109  fatalExit;
110  }
111  names_.push_back(name);
112  types_.push_back(getTypeName(other));
113  return anyList_.emplace_back(std::in_place_type<T>, other);
114  }
115 
117  template<typename T>
118  reference emplaceBack(const word& name, T&& other)
119  {
120  if( contains(name) )
121  {
123  "variable name "<< name << " already exists in the anyList."<<endl<<
124  "list of variables is "<<names_<<endl;
125  fatalExit;
126  }
127  names_.push_back(name);
128  types_.push_back(getTypeName(other));
129  return anyList_.emplace_back(std::in_place_type<T>, std::forward<T>(other));
130  }
131 
133  template<typename T>
134  T& getObject(size_t i)
135  {
136  if(getTypeName<T>() != types_[i])
137  {
139  "requested object type is "<<getTypeName<T>()<<
140  " while strored type is "<<types_[i]<<
141  " for object "<< names_[i]<<endl;
142  fatalExit;
143  }
144 
145  std::any *a = &(*anyList_.pos(i));
146  if(!a->has_value())
147  {
149  "any does not have a value for dereferencing. "<<
150  "index in anyList is "<<i<<"."<<endl;
151  fatalExit;
152  }
153  return *std::any_cast<T>(a);
154  }
155 
157  template<typename T>
158  T& getObject(const word& name)
159  {
160  int32 i = names_.findi(name);
161  if(i == -1 )
162  {
164  "variable name "<< name << " does not exist in the anyList."<<endl<<
165  "list of variables is "<<names_<<endl;
166  fatalExit;
167  }
168  return getObject<T>(static_cast<size_t>(i));
169  }
170 
172  template<typename T>
173  const T& getObject(const word& name)const
174  {
175  int32 i = names_.findi(name);
176  if(i == -1 )
177  {
179  "variable name "<< name << " does not exist in the anyList."<<endl<<
180  "list of variables is "<<names_<<endl;
181  fatalExit;
182  }
183  return getObject<T>(static_cast<size_t>(i));
184  }
185 
187  template<typename T>
188  const T& getObject(size_t i)const
189  {
190  if(getTypeName<T>() != types_[i])
191  {
193  "requested object type is "<<getTypeName<T>()<<
194  " while strored type is "<<types_[i]<<
195  " for object "<< names_[i]<<endl;
196  fatalExit;
197  }
198 
199  const std::any *a = &(*anyList_.pos(i));
200  if(!a->has_value())
201  {
203  "any does not have a value for dereferencing. "<<
204  "index in anyList is "<<i<<"."<<endl;
205  fatalExit;
206  }
207  return *std::any_cast<const T>(a);
208  }
209 
211  template<typename T>
212  T* getObjectPtr(size_t i)
213  {
214  if(i>= size())return nullptr;
215 
216  if(getTypeName<T>() != types_[i])
217  {
219  "requested object type is "<<getTypeName<T>()<<
220  " while strored type is "<<types_[i]<<
221  " for object "<< names_[i]<<endl;
222  return nullptr;
223  }
224 
225  std::any *a = &(*anyList_.pos(i));
226  if( a->has_value())
227  {
228  return std::any_cast<T>(a);
229  }
230  else
231  {
232  return nullptr;
233  }
234  }
235 
237  template<typename T>
238  const T* getObjectPtr(size_t i)const
239  {
240  if(i>=size())return nullptr;
241  if(getTypeName<T>() != types_[i])
242  {
244  "requested object type is "<<getTypeName<T>()<<
245  " while strored type is "<<types_[i]<<
246  " for object "<< names_[i]<<endl;
247  return nullptr;
248  }
249 
250  const std::any *a = &(*anyList_.pos(i));
251  if( a->has_value())
252  {
253  return std::any_cast<const T>(a);
254  }
255  else
256  {
257  return nullptr;
258  }
259  }
260 
261 };
262 
263 
264 
265 } // pFlow
266 
267 
268 #endif //__anyList_hpp__
pFlow::List< std::any >
pFlow::anyList::getObject
const T & getObject(const word &name) const
Get the const reference to variable by name.
Definition: anyList.hpp:173
pFlow::anyList::names_
wordList names_
List of variable names in anyList_.
Definition: anyList.hpp:54
fatalExit
#define fatalExit
Fatal exit.
Definition: error.hpp:98
pFlow::anyList::getObject
T & getObject(size_t i)
Get the reference to variable by index.
Definition: anyList.hpp:134
pFlow::anyList::types_
wordList types_
Definition: anyList.hpp:56
pFlow::anyList::getObjectPtr
T * getObjectPtr(size_t i)
Get the pointer to variable by index.
Definition: anyList.hpp:212
types.hpp
warningInFunction
#define warningInFunction
Report a warning.
Definition: error.hpp:95
pFlow::List< std::any >::const_iterator
typename listType::const_iterator const_iterator
Definition: List.hpp:52
List.hpp
pFlow::word
std::string word
Definition: builtinTypes.hpp:64
pFlow::anyList::contains
bool contains(const word &name) const
Does container contain this variable name?
Definition: anyList.cpp:35
pFlow::anyList::empty
bool empty() const
Is container empty.
Definition: anyList.cpp:30
pFlow::endl
iOstream & endl(iOstream &os)
Add newline and flush stream.
Definition: iOstream.hpp:341
pFlow::anyList::emplaceBack
reference emplaceBack(const word &name, T &&other)
Create variable using move constructor.
Definition: anyList.hpp:118
pFlow
Definition: demGeometry.hpp:27
pFlow::anyList::reference
typename anyListType::reference reference
Definition: anyList.hpp:42
pFlow::List::findi
int32 findi(const T &val) const
Definition: ListI.hpp:125
pFlow::anyList
Definition: anyList.hpp:35
pFlow::anyList::TypeInfoNV
TypeInfoNV("anyList")
fatalErrorInFunction
#define fatalErrorInFunction
Report a fatal error and function name and exit the application.
Definition: error.hpp:77
pFlow::int32
int int32
Definition: builtinTypes.hpp:50
pFlow::List< std::any >::iterator
typename listType::iterator iterator
Definition: List.hpp:50
pFlow::anyList::const_iterator
typename anyListType::const_iterator const_iterator
Definition: anyList.hpp:46
pFlow::List< std::any >::reference
typename listType::reference reference
Definition: List.hpp:54
pFlow::anyList::iterator
typename anyListType::iterator iterator
Definition: anyList.hpp:44
pFlow::anyList::getObjectPtr
const T * getObjectPtr(size_t i) const
Get the const pointer to variable by index.
Definition: anyList.hpp:238
pFlow::getTypeName
word getTypeName()
Definition: typeInfo.hpp:122
pFlow::List::pos
auto pos(size_t i, bool noError=false)
Definition: ListI.hpp:23
typeInfo.hpp
pFlow::anyList::getObject
const T & getObject(size_t i) const
Get the const reference to object by index.
Definition: anyList.hpp:188
pFlow::anyList::emplaceBack
reference emplaceBack(const word &name, const T &other)
Create variable using copy constructor.
Definition: anyList.hpp:102
pFlow::anyList::size
size_t size() const
Size of container.
Definition: anyList.cpp:25
pFlow::anyList::emplaceBack
reference emplaceBack(const word &name, Args &&... args)
Create variable using constructor in-place.
Definition: anyList.hpp:84
pFlow::anyList::anyList_
anyListType anyList_
Contains a list of variables with any type.
Definition: anyList.hpp:51
pFlow::anyList::getObject
T & getObject(const word &name)
Get the reference to variable by name.
Definition: anyList.hpp:158
pFlow::anyList::names
const wordList & names() const
List of varibales names.
Definition: anyList.cpp:40