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 
37 
39 :
40  public iEntry
41 {
42 protected:
43 
45 
46  // global name of dictionary, separated with dots
48 
49  // all the entries (data and dictionary) of the current dictionary
51 
52  // entries in order of insertion
54 
55  // - ref to parrent dictionary
57 
58  bool isGlobal_;
59 
60 
62 
63  // - find an entry based on keyword
64  // return nullptr if not found
65  iEntry* findEntry(const word& keyword);
66 
67  // - find and entry based on keyword
68  // return nullptr if not found
69  iEntry* findEntry(const word& keyword)const;
70 
71  // - reads a dataEntry with keyword from dictionary
72  template<typename T>
73  bool readDataEntry( const word& keyword, T& val)const;
74 
75  // - read dictionary from stream - without keyword
76  bool readDictionary(iIstream & is);
77 
78  // - write dictionary to stream - with keyword
79  bool writeDictionary(iOstream& os, bool withBlock = true)const;
80 
81 
82 public:
83 
84  // null dictionary object, to be used for references
86 
87  TypeInfo("dictionary");
88 
90 
91  // - cunstructs a null dictionary
92  dictionary();
93 
94  // - construct an empty dictionary with keyword
95  dictionary(const word& keyword);
96 
97  // - construct an empty dictionary with keyword and make it global/fileDictionary (if true)
98  dictionary(const word& keyword, bool global);
99 
100  // - construct a dictionary with name and read it from file
101  dictionary(const word& keyword, const fileSystem& file);
102 
103  // - cunstruct an empty dictionary with keyword and parDict
104  dictionary(const word& keyword, const dictionary& parDict);
105 
106  // - cunstruct a dictionary with keyword and parDict and from stream
107  dictionary( const word& keyword, const dictionary& parDict, iIstream& is);
108 
109  // - copy construct with keyword and new parrent dict
110  // discard the keyword and parDict of dict
111  dictionary(const word& keyword, const dictionary& parDict, const dictionary& dict);
112 
113  // - copy construct as default behavior
114  // entries_ are copied smoothly. set parrent dict to nullDict
115  dictionary(const dictionary& );
116 
117 
118  // - assignment preserve name of this dictionary
119  // - only entries are transfered with ownership
120  dictionary& operator=(const dictionary& rhs);
121 
122 
124 
125  // - pointer to this dictionary
126  virtual dictionary* dictPtr();
127 
128  // - pointer to this dictionary
129  virtual const dictionary* dictPtr() const;
130 
131  // - if this is a dictionary
132  virtual bool isDictionary() const;
133 
134  // - global name of entry, separated with dots
135  virtual word globalName()const;
136 
137  // - const ref to parrent dictionary
138  virtual const dictionary& parrentDict() const;
139 
140  // - ref to this dictionary, if it is a dictionary
141  virtual dictionary& dict();
142 
143  // - const ref to this dictionary, if it is a dictionary
144  virtual const dictionary& dict() const;
145 
146  // - if dictionary is file dictionary, return false
147  virtual bool isFileDict()const;
148 
149  // - add a pointer entry (dictionary/dataEntry)
150  // replaces this entry with existing one
151  bool addPtr(const word& keyword, uniquePtr<iEntry>& etry );
152 
153  // - add a float dataEntry
154  bool add(const word& keyword, const float& v);
155 
156  // - add a double dataEntry
157  bool add(const word& keyword, const double& v);
158 
159  // - add a word dataEntry
160  bool add(const word& keyword, const word& v);
161 
162  // - add a int64 dataEntry
163  bool add(const word& keyword, const int64& v);
164 
165  // - add a int32 dataEntry
166  bool add(const word& keyword, const int32& v);
167 
168  // - add a int16 dataEntry
169  bool add(const word& keyword, const int16& v);
170 
171  // - add a int8 dataEntry
172  bool add(const word& keyword, const int8& v);
173 
174  // - add a label dataEntry
175  bool add(const word& keyword, const label& v);
176 
177  // - add a uint32 dataEntry
178  bool add(const word& keyword, const uint32& v);
179 
180  // add a dictionary with the specifiedd keyword
181  bool addDict(const word& keyword, const dictionary& dict);
182 
183  // - add a dataEntry of type T
184  template<typename T>
185  bool add(const word& keyword, const T& v );
186 
187 
188  void clear();
189 
190  // - pointer to a subdictionary
191  // fatalExit if not found
193 
194  // - ref to a subdictioanry
195  // fatalExit if not found
196  dictionary& subDict(const word& keyword);
197 
198  // - const ref to a subdictioanry
199  // fatalExit if not found
200  const dictionary& subDict(const word& keyword) const;
201 
202  // - pointer to a dataEntry
203  // fatalExit if not found/not a dataEntry
205 
206  // - ref to a subdictioanry
207  // fatalExit if not found/not a dataEntry
209 
210  // - const ref to a subdictioanry
211  // fatalExit if not found/not a dataEntry
212  const dataEntry& dataEntryRef(const word& keyword)const;
213 
214  // - search for a sub-dict with keyword
215  // create a new sub-dict if not found and return a ref to it
216  // fatalExit if fails
218 
219  // - get the value of data entry
220  template<typename T>
221  T getVal(const word& keyword) const;
222 
223  // - get the value of data entry or
224  // if not found, set the value to setVal
225  template<typename T>
226  T getValOrSet(const word& keyword, const T& setVal)const;
227 
228  // return number of entris in this dictionary
229  size_t numEntries()const;
230 
231  // return number of non-nullptr dataEntries
232  size_t numDataEntries()const;
233 
234  // return number of non-nullptr dictionaries
235  size_t numDictionaries()const;
236 
237  // return all keywords (non-nullptr) in this dictionary
238  wordList allKeywords()const;
239 
240  // return a list of all dataEntries (non-nullptr) keywords
242 
243  // return a list of all dictionary (non-null) keywords
245 
246  // check if a sub-dictionary exists
247  bool containsDictionay(const word& name)const;
248 
249  // check if a data entry exist
250  bool containsDataEntry(const word& name)const;
251 
252  // clone polymorphic object (here dictionary)
253  virtual uniquePtr<iEntry> clone() const;
254 
255  virtual iEntry* clonePtr() const;
256 
257  // - clone the polymorhpic object with parDict as the new parrent dictionary
258  virtual uniquePtr<iEntry> clone(const dictionary& parDict)const;
259 
260  virtual iEntry* clonePtr(const dictionary& parDict) const;
261 
262 
264 
265  // - read from stream
266  virtual bool read(iIstream& is);
267 
268  // - write to stream
269  virtual bool write(iOstream& os) const;
270 
271 };
272 
273 
274 
275 template<typename T>
276 bool dictionary::add(const word& keyword, const T& v )
277 {
278 
279  uniquePtr<iEntry> ptr = makeUnique<dataEntry>(keyword, *this ,v);
280  return addPtr(keyword, ptr);
281 
282 }
283 
284 
285 template<typename T>
287 (
288  const word& keyword,
289  T& val
290 )const
291 {
292 
293  if( auto entry = findEntry(keyword); entry!= nullptr && !entry->isDictionary() )
294  {
295  iTstream& is = dynamic_cast<dataEntry&>(*entry).stream();
296 
297  is >> val;
298  return true;
299  }
300  else
301  {
302  return false;
303  }
304 
305 }
306 
307 template<typename T>
309 (
310  const word& keyword
311 )const
312 {
313  T val{};
314  if(!readDataEntry(keyword, val))
315  {
317  "cannot find dataEntry "<< keyword <<" in dictionary "<< globalName()<<endl;
318  fatalExit;
319  }
320  return val;
321 }
322 
323 template<typename T>
325 (
326  const word& keyword,
327  const T& setVal
328 )const
329 {
330  T val{};
331  if( readDataEntry(keyword, val) )
332  {
333  return val;
334  }
335  else
336  {
337  return setVal;
338  }
339 }
340 
341 }
342 
343 
344 #endif // __dictionary_hpp__
pFlow::dictionary::orderedEntries_
List< iEntry * > orderedEntries_
Definition: dictionary.hpp:53
pFlow::List
Definition: List.hpp:39
pFlow::dictionary::getValOrSet
T getValOrSet(const word &keyword, const T &setVal) const
Definition: dictionary.hpp:325
fatalExit
#define fatalExit
Definition: error.hpp:57
pFlow::dictionary::name_
word name_
Definition: dictionary.hpp:47
pFlow::dataEntry::stream
virtual iTstream & stream()
Definition: dataEntry.cpp:261
pFlow::dictionary::write
virtual bool write(iOstream &os) const
Definition: dictionary.cpp:780
types.hpp
pFlow::dictionary::allKeywords
wordList allKeywords() const
Definition: dictionary.cpp:695
pFlow::dictionary::findEntry
iEntry * findEntry(const word &keyword)
Definition: dictionary.cpp:106
List.hpp
pFlow::dictionary::read
virtual bool read(iIstream &is)
Definition: dictionary.cpp:759
pFlow::dictionary::parDict_
const dictionary & parDict_
Definition: dictionary.hpp:56
pFlow::uint32
unsigned int uint32
Definition: builtinTypes.hpp:59
pFlow::dictionary::isGlobal_
bool isGlobal_
Definition: dictionary.hpp:58
pFlow::dictionary::operator=
dictionary & operator=(const dictionary &rhs)
Definition: dictionary.cpp:303
pFlow::word
std::string word
Definition: builtinTypes.hpp:63
pFlow::int64
long long int int64
Definition: builtinTypes.hpp:55
pFlow::dictionary::globalName
virtual word globalName() const
Definition: dictionary.cpp:349
pFlow::dictionary::subDictOrCreate
dictionary & subDictOrCreate(const word &keyword)
Definition: dictionary.cpp:634
pFlow::dictionary::add
bool add(const word &keyword, const float &v)
Definition: dictionary.cpp:422
pFlow::iEntry::keyword
virtual const word & keyword() const
Definition: iEntry.hpp:84
pFlow::dictionary::isDictionary
virtual bool isDictionary() const
Definition: dictionary.cpp:344
pFlow::endl
iOstream & endl(iOstream &os)
Definition: iOstream.hpp:312
pFlow::dictionary::nullDict
static dictionary nullDict
Definition: dictionary.hpp:85
fileSystem.hpp
pFlow::dictionary::clonePtr
virtual iEntry * clonePtr() const
Definition: dictionary.cpp:797
pFlow
Definition: demComponent.hpp:28
pFlow::dictionary::containsDataEntry
bool containsDataEntry(const word &name) const
Definition: dictionary.cpp:748
pFlow::dictionary::dictionaryKeywords
wordList dictionaryKeywords() const
Definition: dictionary.cpp:721
pFlow::dictionary::dictPtr
virtual dictionary * dictPtr()
Definition: dictionary.cpp:332
pFlow::dictionary::entries_
wordOrderedMapPtr< iEntry > entries_
Definition: dictionary.hpp:50
pFlow::dictionary::TypeInfo
TypeInfo("dictionary")
pFlow::fileSystem
Definition: fileSystem.hpp:63
pFlow::dictionary::containsDictionay
bool containsDictionay(const word &name) const
Definition: dictionary.cpp:736
pFlow::MapPtr
Definition: MapPtr.hpp:39
pFlow::dataEntry
Definition: dataEntry.hpp:40
pFlow::int16
short int int16
Definition: builtinTypes.hpp:51
pFlow::dictionary::numEntries
size_t numEntries() const
Definition: dictionary.cpp:664
pFlow::iIstream
Definition: iIstream.hpp:33
pFlow::dictionary::clone
virtual uniquePtr< iEntry > clone() const
Definition: dictionary.cpp:791
pFlow::dictionary::dataEntryPtr
dataEntry * dataEntryPtr(const word &keyword)
Definition: dictionary.cpp:585
fatalErrorInFunction
#define fatalErrorInFunction
Definition: error.hpp:42
pFlow::int32
int int32
Definition: builtinTypes.hpp:53
pFlow::dictionary::isFileDict
virtual bool isFileDict() const
Definition: dictionary.cpp:373
pFlow::dictionary::dict
virtual dictionary & dict()
Definition: dictionary.cpp:362
pFlow::dictionary::readDictionary
bool readDictionary(iIstream &is)
Definition: dictionary.cpp:37
pFlow::dictionary::writeDictionary
bool writeDictionary(iOstream &os, bool withBlock=true) const
Definition: dictionary.cpp:87
pFlow::dictionary::numDataEntries
size_t numDataEntries() const
Definition: dictionary.cpp:669
pFlow::dictionary::dataEntryRef
dataEntry & dataEntryRef(const word &keyword)
Definition: dictionary.cpp:600
pFlow::dictionary::subDictPtr
dictionary * subDictPtr(const word &keyword)
Definition: dictionary.cpp:529
pFlow::dictionary::readDataEntry
bool readDataEntry(const word &keyword, T &val) const
Definition: dictionary.hpp:287
pFlow::dictionary::subDict
dictionary & subDict(const word &keyword)
Definition: dictionary.cpp:547
iEntry.hpp
pFlow::iEntry::name
virtual word name() const
Definition: iEntry.hpp:95
pFlow::dictionary::getVal
T getVal(const word &keyword) const
Definition: dictionary.hpp:309
pFlow::iTstream
Definition: iTstream.hpp:21
pFlow::uniquePtr
Definition: uniquePtr.hpp:44
dataEntry.hpp
pFlow::dictionary::addPtr
bool addPtr(const word &keyword, uniquePtr< iEntry > &etry)
Definition: dictionary.cpp:379
pFlow::iEntry
Definition: iEntry.hpp:38
pFlow::label
std::size_t label
Definition: builtinTypes.hpp:61
pFlow::dictionary::dictionary
dictionary()
Definition: dictionary.cpp:135
pFlow::dictionary::clear
void clear()
Definition: dictionary.cpp:522
pFlow::int8
signed char int8
Definition: builtinTypes.hpp:49
pFlow::dictionary::addDict
bool addDict(const word &keyword, const dictionary &dict)
Definition: dictionary.cpp:513
pFlow::dictionary::numDictionaries
size_t numDictionaries() const
Definition: dictionary.cpp:682
pFlow::dictionary::parrentDict
virtual const dictionary & parrentDict() const
Definition: dictionary.cpp:355
pFlow::iOstream
Definition: iOstream.hpp:53
pFlow::dictionary::dataEntryKeywords
wordList dataEntryKeywords() const
Definition: dictionary.cpp:706
pFlow::dictionary
Definition: dictionary.hpp:38
MapPtr.hpp