www.cemf.ir
dictionary.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 // based on OpenFOAM dictionary, with some modifications/simplifications
21 // to be tailored to our needs
22 
23 
24 #ifndef __dictionary_hpp__
25 #define __dictionary_hpp__
26 
27 #include "types.hpp"
28 #include "iEntry.hpp"
29 #include "dataEntry.hpp"
30 #include "MapPtr.hpp"
31 #include "List.hpp"
32 #include "fileSystem.hpp"
33 
34 namespace pFlow
35 {
36 
68 :
69  public iEntry
70 {
71 protected:
72 
74 
77 
80 
83 
86 
87  bool isGlobal_ = false;
88 
89 
91 
94  iEntry* findEntry(const word& keyword);
95 
98  iEntry* findEntry(const word& keyword)const;
99 
101  template<typename T>
102  bool readDataEntry( const word& keyword, T& val)const;
103 
105  bool readDictionary(iIstream & is);
106 
108  bool writeDictionary(iOstream& os, bool withBlock = true)const;
109 
110 
111 
112 
113 public:
114 
115  // null dictionary object, to be used for references
117 
118  TypeInfo("dictionary");
119 
122  dictionary(const word& keyword, bool global);
123 
125  dictionary(const word& keyword, const fileSystem& file);
126 
128  dictionary();
129 
131  dictionary(const word& keyword);
132 
134  dictionary(const word& keyword, const dictionary& parDict);
135 
137  dictionary( const word& keyword, const dictionary& parDict, iIstream& is);
138 
141  dictionary(const word& keyword, const dictionary& parDict, const dictionary& dict);
142 
145  dictionary(const dictionary& );
146 
147  dictionary(const dictionary& src, bool global);
148 
149 
152  dictionary& operator=(const dictionary& rhs);
153 
154 
156 
158  virtual dictionary* dictPtr();
159 
161  virtual const dictionary* dictPtr() const;
162 
164  virtual bool isDictionary() const;
165 
167  virtual word globalName()const;
168 
170  virtual const dictionary& parrentDict() const;
171 
173  virtual dictionary& dict();
174 
176  virtual const dictionary& dict() const;
177 
179  virtual bool isFileDict()const;
180 
183  bool addPtr(const word& keyword, uniquePtr<iEntry>& etry, bool warning = true );
184 
185 
187  bool add(const word& keyword, const float& v);
188 
190  bool add(const word& keyword, const double& v);
191 
193  bool add(const word& keyword, const word& v);
194 
196  bool add(const word& keyword, const int64& v);
197 
199  bool add(const word& keyword, const int32& v);
200 
202  bool add(const word& keyword, const int8& v);
203 
205  bool add(const word& keyword, const uint64& v);
206 
208  bool add(const word& keyword, const uint32& v);
209 
211  bool add(const word& keyword, const uint8& v);
212 
215  bool addDict(const word& keyword, const dictionary& dict);
216 
218  template<typename T>
219  bool add(const word& keyword, const T& v );
220 
221  template<typename T>
222  bool addOrKeep(const word& keyword, const T& v);
223 
224  template<typename T>
225  bool addOrReplace(const word& keyword, const T& v);
226 
227  void clear();
228 
232 
235  dictionary& subDict(const word& keyword);
236 
239  const dictionary& subDict(const word& keyword) const;
240 
244 
248 
251  const dataEntry& dataEntryRef(const word& keyword)const;
252 
257 
259  template<typename T>
260  T getVal(const word& keyword) const;
261 
263  template<typename T>
264  T getValMax(const word& keyword, const T& maxVal)const;
265 
267  template<typename T>
268  T getValMin(const word& keyword, const T& minVal)const;
269 
272  template<typename T>
273  T getValOrSet(const word& keyword, const T& setVal)const;
274 
277  template<typename T>
278  T getValOrSetMax(const word& keyword, const T& setMaxVal)const;
279 
282  template<typename T>
283  T getValOrSetMin(const word& keyword, const T& setMinVal)const;
284 
286  size_t numEntries()const;
287 
289  size_t numDataEntries()const;
290 
292  size_t numDictionaries()const;
293 
295  wordList allKeywords()const;
296 
299 
302 
304  bool containsDictionay(const word& name)const;
305 
307  bool containsDataEntry(const word& name)const;
308 
310  virtual uniquePtr<iEntry> clone() const;
311 
312  virtual iEntry* clonePtr() const;
313 
315  virtual uniquePtr<iEntry> clone(const dictionary& parDict)const;
316 
317  virtual iEntry* clonePtr(const dictionary& parDict) const;
318 
319 
321 
323  bool read(iIstream& is) override;
324 
326  bool write(iOstream& os) const override;
327 
328 };
329 
330 
331 
332 template<typename T>
333 bool dictionary::add(const word& keyword, const T& v )
334 {
335 
336  uniquePtr<iEntry> ptr = makeUnique<dataEntry>(keyword, *this ,v);
337  return addPtr(keyword, ptr);
338 
339 }
340 
341 
342 template<typename T>
343 bool dictionary::addOrKeep(const word& keyword, const T& v )
344 {
346  {
347  uniquePtr<iEntry> ptr = makeUnique<dataEntry>(keyword, *this ,v);
348  return addPtr(keyword, ptr);
349  }
350 
351  return true;
352 }
353 
354 
355 template<typename T>
357 (
358  const word& keyword,
359  T& val
360 )const
361 {
362 
363  if( auto entry = findEntry(keyword); entry!= nullptr && !entry->isDictionary() )
364  {
365  iTstream& is = dynamic_cast<dataEntry&>(*entry).stream();
366 
367  is >> val;
368  return true;
369  }
370  else
371  {
372  return false;
373  }
374 
375 }
376 
377 template<typename T>
379 (
380  const word& keyword
381 )const
382 {
383  T val{};
384  if(!readDataEntry(keyword, val))
385  {
387  "cannot find dataEntry "<< keyword <<" in dictionary "<< globalName()<<endl;
388  fatalExit;
389  }
390  return val;
391 }
392 
393 template<typename T>
395 (
396  const word& keyword,
397  const T& maxVal
398 )const
399 {
400  return max(getVal<T>(keyword), maxVal);
401 }
402 
403 template<typename T>
405 (
406  const word& keyword,
407  const T& minVal
408 )const
409 {
410  return min(getVal<T>(keyword), minVal);
411 }
412 
413 template<typename T>
415 (
416  const word& keyword,
417  const T& setVal
418 )const
419 {
420  T val{};
421  if( readDataEntry(keyword, val) )
422  {
423  return val;
424  }
425  else
426  {
427  return setVal;
428  }
429 }
430 
431 template<typename T>
432 T dictionary::getValOrSetMax(const word& keyword, const T& setMaxVal)const
433 {
434  return max(getValOrSet(keyword, setMaxVal), setMaxVal);
435 }
436 
437 template<typename T>
438 T dictionary::getValOrSetMin(const word& keyword, const T& setMinVal)const
439 {
440  return min(getValOrSet(keyword, setMinVal), setMinVal);
441 }
442 
443 template <typename T>
444 inline bool dictionary::addOrReplace
445 (
446  const word &keyword,
447  const T &v
448 )
449 {
450  uniquePtr<iEntry> ptr = makeUnique<dataEntry>(keyword, *this ,v);
451  return addPtr(keyword, ptr, false);
452 }
453 
454 
455 } // pFlow
456 
457 #endif // __dictionary_hpp__
pFlow::dictionary::orderedEntries_
List< iEntry * > orderedEntries_
entries in order of insertion
Definition: dictionary.hpp:82
pFlow::List
Definition: List.hpp:39
pFlow::dictionary::getValOrSet
T getValOrSet(const word &keyword, const T &setVal) const
get the value of data entry or if not found, set the value to setVal
Definition: dictionary.hpp:415
fatalExit
#define fatalExit
Fatal exit.
Definition: error.hpp:98
pFlow::dictionary::name_
word name_
global name of dictionary, separated with dots
Definition: dictionary.hpp:76
pFlow::dataEntry::stream
virtual iTstream & stream()
access to token stream
Definition: dataEntry.cpp:265
pFlow::dictionary::read
bool read(iIstream &is) override
read from stream
Definition: dictionary.cpp:772
types.hpp
pFlow::dictionary::allKeywords
wordList allKeywords() const
return all keywords (non-nullptr) in this dictionary
Definition: dictionary.cpp:708
pFlow::dictionary::findEntry
iEntry * findEntry(const word &keyword)
find an entry based on keyword return nullptr if not found
Definition: dictionary.cpp:106
List.hpp
pFlow::dictionary::parDict_
const dictionary & parDict_
ref to parrent dictionary
Definition: dictionary.hpp:85
pFlow::uint32
unsigned int uint32
Definition: builtinTypes.hpp:56
pFlow::dictionary::isGlobal_
bool isGlobal_
Definition: dictionary.hpp:87
pFlow::dictionary::operator=
dictionary & operator=(const dictionary &rhs)
assignment preserve name of this dictionary only entries are transfered with ownership
Definition: dictionary.cpp:310
pFlow::word
std::string word
Definition: builtinTypes.hpp:64
pFlow::max
T max(const internalField< T, MemorySpace > &iField)
Definition: internalFieldAlgorithms.hpp:79
pFlow::dictionary::addOrKeep
bool addOrKeep(const word &keyword, const T &v)
Definition: dictionary.hpp:343
pFlow::int64
long long int int64
Definition: builtinTypes.hpp:52
pFlow::dictionary::globalName
virtual word globalName() const
global name of entry, separated with dots
Definition: dictionary.cpp:356
pFlow::dictionary::subDictOrCreate
dictionary & subDictOrCreate(const word &keyword)
search for a sub-dict with keyword create a new sub-dict if not found and return a ref to it fatalExi...
Definition: dictionary.cpp:647
pFlow::dictionary::add
bool add(const word &keyword, const float &v)
add a float dataEntry
Definition: dictionary.cpp:435
pFlow::iEntry::keyword
virtual const word & keyword() const
return keyword
Definition: iEntry.hpp:88
pFlow::dictionary::isDictionary
virtual bool isDictionary() const
if this is a dictionary
Definition: dictionary.cpp:351
pFlow::endl
iOstream & endl(iOstream &os)
Add newline and flush stream.
Definition: iOstream.hpp:341
pFlow::dictionary::nullDict
static dictionary nullDict
Definition: dictionary.hpp:116
fileSystem.hpp
pFlow::dictionary::clonePtr
virtual iEntry * clonePtr() const
clone the object
Definition: dictionary.cpp:810
pFlow
Definition: demGeometry.hpp:27
pFlow::dictionary::containsDataEntry
bool containsDataEntry(const word &name) const
check if a data entry exist
Definition: dictionary.cpp:761
pFlow::dictionary::dictionaryKeywords
wordList dictionaryKeywords() const
return a list of all dictionary (non-null) keywords
Definition: dictionary.cpp:734
pFlow::dictionary::dictPtr
virtual dictionary * dictPtr()
pointer to this dictionary
Definition: dictionary.cpp:339
pFlow::dictionary::entries_
wordOrderedMapPtr< iEntry > entries_
all the entries (data and dictionary) of the current dictionary
Definition: dictionary.hpp:79
pFlow::dictionary::TypeInfo
TypeInfo("dictionary")
pFlow::fileSystem
Manages file pathes, manupulate and combines them.
Definition: fileSystem.hpp:71
pFlow::dictionary::containsDictionay
bool containsDictionay(const word &name) const
check if a sub-dictionary exists
Definition: dictionary.cpp:749
pFlow::dictionary::getValMin
T getValMin(const word &keyword, const T &minVal) const
get the value of data entry and return min(value, minVal)
Definition: dictionary.hpp:405
pFlow::MapPtr
Definition: MapPtr.hpp:39
pFlow::dataEntry
Data entry to be used in dictionries.
Definition: dataEntry.hpp:48
pFlow::dictionary::getValMax
T getValMax(const word &keyword, const T &maxVal) const
get the value of data entry and return max(value, maxVal)
Definition: dictionary.hpp:395
pFlow::dictionary::numEntries
size_t numEntries() const
return number of entris in this dictionary
Definition: dictionary.cpp:677
pFlow::iIstream
Interface class for any input stream
Definition: iIstream.hpp:37
pFlow::dictionary::clone
virtual uniquePtr< iEntry > clone() const
clone polymorphic object (here dictionary)
Definition: dictionary.cpp:804
pFlow::dictionary::dataEntryPtr
dataEntry * dataEntryPtr(const word &keyword)
pointer to a dataEntry fatalExit if not found/not a dataEntry
Definition: dictionary.cpp:598
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::dictionary::isFileDict
virtual bool isFileDict() const
if dictionary is file dictionary, return false
Definition: dictionary.cpp:380
pFlow::dictionary::dict
virtual dictionary & dict()
ref to this dictionary, if it is a dictionary
Definition: dictionary.cpp:369
pFlow::dictionary::readDictionary
bool readDictionary(iIstream &is)
read dictionary from stream - without keyword
Definition: dictionary.cpp:37
pFlow::dictionary::writeDictionary
bool writeDictionary(iOstream &os, bool withBlock=true) const
write dictionary to stream - with keyword
Definition: dictionary.cpp:87
pFlow::dictionary::numDataEntries
size_t numDataEntries() const
return number of non-nullptr dataEntries
Definition: dictionary.cpp:682
pFlow::dictionary::dataEntryRef
dataEntry & dataEntryRef(const word &keyword)
ref to a subdictioanry fatalExit if not found/not a dataEntry
Definition: dictionary.cpp:613
pFlow::dictionary::write
bool write(iOstream &os) const override
write to stream
Definition: dictionary.cpp:793
pFlow::min
T min(const internalField< T, MemorySpace > &iField)
Definition: internalFieldAlgorithms.hpp:28
pFlow::dictionary::subDictPtr
dictionary * subDictPtr(const word &keyword)
pointer to a subdictionary fatalExit if not found
Definition: dictionary.cpp:542
pFlow::dictionary::readDataEntry
bool readDataEntry(const word &keyword, T &val) const
reads a dataEntry with keyword from dictionary
Definition: dictionary.hpp:357
pFlow::dictionary::subDict
dictionary & subDict(const word &keyword)
ref to a subdictioanry fatalExit if not found
Definition: dictionary.cpp:560
iEntry.hpp
pFlow::iEntry::name
virtual word name() const
name/keyword of entry
Definition: iEntry.hpp:100
pFlow::dictionary::getVal
T getVal(const word &keyword) const
get the value of data entry
Definition: dictionary.hpp:379
pFlow::dictionary::addPtr
bool addPtr(const word &keyword, uniquePtr< iEntry > &etry, bool warning=true)
add a pointer entry (dictionary/dataEntry) replaces this entry with existing one and issue a warning
Definition: dictionary.cpp:386
pFlow::iTstream
Input token stream.
Definition: iTstream.hpp:27
pFlow::dictionary::addOrReplace
bool addOrReplace(const word &keyword, const T &v)
Definition: dictionary.hpp:445
pFlow::uniquePtr
Definition: uniquePtr.hpp:42
pFlow::dictionary::getValOrSetMin
T getValOrSetMin(const word &keyword, const T &setMinVal) const
get the value of data entry anf return max(setMinVal, value) if not found, set the value to setMinVal
Definition: dictionary.hpp:438
dataEntry.hpp
pFlow::uint64
unsigned long long int uint64
Definition: builtinTypes.hpp:58
pFlow::iEntry
Interface calss for data entry and dictionary
Definition: iEntry.hpp:42
pFlow::dictionary::dictionary
dictionary()
cunstructs a null dictionary
Definition: dictionary.cpp:135
pFlow::dictionary::clear
void clear()
Definition: dictionary.cpp:535
pFlow::int8
signed char int8
Definition: builtinTypes.hpp:48
pFlow::dictionary::addDict
bool addDict(const word &keyword, const dictionary &dict)
add a dictionary with the specifiedd keyword, if it exists, replace it.
Definition: dictionary.cpp:526
pFlow::dictionary::numDictionaries
size_t numDictionaries() const
return number of non-nullptr dictionaries
Definition: dictionary.cpp:695
pFlow::dictionary::getValOrSetMax
T getValOrSetMax(const word &keyword, const T &setMaxVal) const
get the value of data entry anf return max(setMaxVal, value) if not found, set the value to setMaxVal
Definition: dictionary.hpp:432
pFlow::uint8
unsigned char uint8
Definition: builtinTypes.hpp:54
pFlow::dictionary::parrentDict
virtual const dictionary & parrentDict() const
const ref to parrent dictionary
Definition: dictionary.cpp:362
pFlow::iOstream
Interface class for any output stream.
Definition: iOstream.hpp:59
pFlow::dictionary::dataEntryKeywords
wordList dataEntryKeywords() const
return a list of all dataEntries (non-nullptr) keywords
Definition: dictionary.cpp:719
pFlow::dictionary
Dictionary holds a set of data entries or sub-dictionaries that are enclosed in a curely braces or ar...
Definition: dictionary.hpp:67
MapPtr.hpp