dictionary.cpp
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 #include "dictionary.hpp"
25 #include "uniquePtr.hpp"
26 #include "error.hpp"
27 #include "streams.hpp"
28 
29 
30 
32 
33 
34 // better to move it to the abstract class
35 // with an static member function.
37 (
38  iIstream & is
39 )
40 {
41  // ensure that the entry list of new the dicrionary is empty
42  entries_.clear();
43 
44 
45  // empty dictionary
46  if (is.eof())
47  {
48  return true;
49  }
50 
51  if (!is.good())
52  {
53  ioErrorInFile( is.name(), is.lineNumber() )<<
54  "iIstream is not good for reading tokens .... \n";
55  return false;
56  }
57 
58 
59  // reads tokens one-by-one
60  token nextTok(is);
61  if( nextTok == token::END_BLOCK)
62  {
63  ioErrorInFile( is.name(), is.lineNumber() )<<
64  "Not expecting a } for the start of a dictionray \n";
65  return false;
66  }
67 
68  bool hasBlockToken = true;
69 
70  // this is a sub-dict, so it should start with {
71  if(nextTok != token::BEGIN_BLOCK)
72  {
73  hasBlockToken = false;
74  is.putBack(nextTok);
75  }
76 
77 
78  //read all entries in ths dictionary
79  while ( !is.eof() && iEntry::createEntry(*this, is, hasBlockToken) )
80  {}
81 
82 
83  return true;
84 }
85 
87 (
88  iOstream& os,
89  bool withBlock
90 )const
91 {
92  if(withBlock) os.beginBlock(keyword());
93 
94  for( const auto& e:orderedEntries_)
95  {
96  if(e != nullptr )
97  e->write(os);
98  }
99  if(withBlock) os.endBlock();
100 
101  return true;
102 
103 }
104 
106 (
107  const word& keyword
108 )
109 {
110  if( auto [ptr, exist] = entries_.find(keyword); exist)
111  {
112 
113  return ptr;
114 
115  }
116 
117  return nullptr;
118 }
119 
121 (
122  const word& keyword
123 )const
124 {
125  if( auto [ptr, exist] = entries_.find(keyword); exist)
126  {
127  return const_cast<iEntry*> (ptr);
128  }
129 
130  return nullptr;
131 }
132 
133 
135 ()
136 :
137  iEntry("NULL_DICT"),
138  name_("NULL_DICT"),
139  entries_(),
140  orderedEntries_(),
141  parDict_(dictionary::nullDict),
142  isGlobal_(false)
143 {}
144 
146 (
147  const word& keyword
148 )
149 :
150  iEntry(keyword),
151  name_(keyword),
152  entries_(),
153  orderedEntries_(),
154  parDict_(dictionary::nullDict),
155  isGlobal_(false)
156 {}
157 
158 
160 (
161  const word& keyword,
162  bool global
163 )
164 :
165  iEntry(keyword),
166  name_(keyword),
167  entries_(),
168  orderedEntries_(),
169  parDict_(dictionary::nullDict),
170  isGlobal_(global)
171 {
172 }
173 
175 (
176  const word& keyword,
177  const fileSystem& file
178 )
179 :
180  iEntry(keyword),
181  name_(file.wordPath()),
182  entries_(),
183  orderedEntries_(),
184  parDict_(dictionary::nullDict),
185  isGlobal_(true)
186 {
187  iFstream dictStream(file);
188 
189  if(!read(dictStream))
190  {
191  ioErrorInFile(dictStream.name(), dictStream.lineNumber())<<
192  "error in reading dictionary from file "<< file <<endl;
193  fatalExit;
194  }
195 
196 }
197 
199 (
200  const word& keyword,
201  const dictionary& parDict
202 )
203 :
204  iEntry(keyword),
205  name_(groupNames(parDict.globalName(), keyword)),
206  entries_(),
207  orderedEntries_(),
208  parDict_(parDict),
209  isGlobal_(false)
210 {
211 
212 }
213 
215 (
216  const word& keyword,
217  const dictionary& parDict,
218  iIstream& is
219 )
220 :
221  iEntry(keyword),
222  name_(groupNames(parDict.globalName(), keyword)),
223  entries_(),
224  orderedEntries_(),
225  parDict_(parDict),
226  isGlobal_(false)
227 {
228 
229  if( !readDictionary(is) )
230  {
231  ioErrorInFile(is.name(), is.lineNumber()) <<
232  "error in reading dictionary " << name_ <<endl;
233  fatalExit;
234  }
235 }
236 
238 (
239  const word& keyword,
240  const dictionary& parDict,
241  const dictionary& dict
242 )
243 :
244  iEntry(keyword),
245  name_(groupNames(parDict.globalName(),keyword)),
246  entries_(),
247  orderedEntries_(),
248  parDict_(parDict),
249  isGlobal_(false)
250 {
251 
252  for( auto& entry : dict.orderedEntries_ )
253  {
254  if(entry)
255  {
256  auto ptr = entry->clone(*this);
257  auto key = entry->name();
258 
259  if( !addPtr(key, ptr) )
260  {
262  " error in cloning dicrionary / dataEntry " << entry->globalName() <<endl;
263  fatalExit;
264  }
265  }
266  }
267 
268 }
269 
271 (
272  const dictionary& src
273 )
274 :
275  iEntry(src.keyword()),
276  name_(src.keyword()),
277  entries_(),
278  orderedEntries_(),
279  parDict_(dictionary::nullDict),
280  isGlobal_(src.isGlobal_)
281 {
282 
283  for( auto& entry: src.orderedEntries_)
284  {
285 
286  if(entry)
287  {
288  auto ptr = entry->clone(*this);
289  auto key = entry->name();
290 
291  if( !addPtr(key, ptr) )
292  {
294  " error in cloning dicrionary / dataEntry " << entry->globalName() <<endl;
295  fatalExit;
296  }
297  }
298  }
299 
300 }
301 
302 pFlow::dictionary& pFlow::dictionary::operator=
303 (
304  const dictionary& rhs
305 )
306 {
307 
308  if( &rhs == this)return *this;
309 
310  clear();
311 
312  for( auto& entry: rhs.orderedEntries_)
313  {
314 
315  if(entry)
316  {
317  auto ptr = entry->clone(*this);
318  auto key = entry->name();
319 
320  if( !addPtr(key, ptr) )
321  {
323  " error in cloning dicrionary / dataEntry " << entry->globalName() <<endl;
324  fatalExit;
325  }
326  }
327  }
328 
329  return *this;
330 }
331 
333 {
334  return this;
335 }
336 
337 
339 {
340  return this;
341 }
342 
343 
345 {
346  return true;
347 }
348 
350 {
351  return name_;
352 }
353 
354 
356 {
357  return parDict_;
358 }
359 
360 
361 
363 {
364  return *this;
365 }
366 
367 
369 {
370  return *this;
371 }
372 
374 {
375  return isGlobal_;
376 }
377 
379 (
380  const word& keyword,
381  uniquePtr<iEntry>& entry
382 )
383 {
384 
385  if(entry == nullptr) return false;
386 
387  iEntry* oldEntryPtr = nullptr;
388  iEntry* newEntryPtr = entry.get();
389 
390  // search all entries for repeated keyword
391  if(auto [ptr, exist] = entries_.find(keyword); exist )
392  {
394  "keyword " << keyword << " already exists in the dicrionary " <<
395  this->globalName() << ". The old entry will be replaced by the new one. \n";
396  // store the old pointer to entry
397  oldEntryPtr = ptr;
398  }
399 
400 
401  if( entries_.insertReplace(keyword, entry) )
402  {
403  if(oldEntryPtr)
404  {
405  // this should be replaced
406  auto oIter = orderedEntries_.find(oldEntryPtr);
407  *oIter = newEntryPtr;
408  }
409  else
410  {
411  orderedEntries_.push_back(newEntryPtr);
412  }
413  return true;
414  }else
415  {
416  return false;
417  }
418 
419 }
420 
422 (
423  const word& keyword,
424  const float& v
425 )
426 {
427  uniquePtr<iEntry> ptr = makeUnique<dataEntry>(keyword, *this, token(v));
428  return addPtr(keyword, ptr);
429 }
430 
432 (
433  const word& keyword,
434  const double& v
435 )
436 {
437  uniquePtr<iEntry> ptr = makeUnique<dataEntry>(keyword, *this, token(v));
438  return addPtr(keyword, ptr);
439 }
440 
442 (
443  const word& keyword,
444  const word& v
445 )
446 {
447  uniquePtr<iEntry> ptr = makeUnique<dataEntry>(keyword, *this, token(v));
448  return addPtr(keyword, ptr);
449 }
450 
452 (
453  const word& keyword,
454  const int64& v
455 )
456 {
457  uniquePtr<iEntry> ptr = makeUnique<dataEntry>(keyword, *this, token(v));
458  return addPtr(keyword, ptr);
459 }
460 
462 (
463  const word& keyword,
464  const int32& v
465 )
466 {
467  uniquePtr<iEntry> ptr = makeUnique<dataEntry>(keyword, *this, token(v));
468  return addPtr(keyword, ptr);
469 }
470 
472 (
473  const word& keyword,
474  const int16& v
475 )
476 {
477  uniquePtr<iEntry> ptr = makeUnique<dataEntry>(keyword, *this, token(v));
478  return addPtr(keyword, ptr);
479 }
480 
482 (
483  const word& keyword,
484  const int8& v
485 )
486 {
487  uniquePtr<iEntry> ptr = makeUnique<dataEntry>(keyword, *this, token(v));
488  return addPtr(keyword, ptr);
489 }
490 
492 (
493  const word& keyword,
494  const label& v
495 )
496 {
497  uniquePtr<iEntry> ptr = makeUnique<dataEntry>(keyword, *this, token(v));
498  return addPtr(keyword, ptr);
499 }
500 
502 (
503  const word& keyword,
504  const uint32& v
505 )
506 {
507  uniquePtr<iEntry> ptr = makeUnique<dataEntry>(keyword, *this, token(v));
508  return addPtr(keyword, ptr);
509 }
510 
511 
513 (
514  const word& keyword,
515  const dictionary& dict
516 )
517 {
518  uniquePtr<iEntry> ptr = makeUnique<dictionary>(keyword, *this, dict);
519  return addPtr(keyword, ptr);
520 }
521 
523 {
524  orderedEntries_.clear();
525  entries_.clear();
526 }
527 
529 (
530  const word& keyword
531 )
532 {
533  if( auto entry = findEntry(keyword); entry!= nullptr && entry->isDictionary() )
534  {
535  return dynamic_cast<dictionary*>(entry);
536  }
537  else
538  {
540  "keyword " << keyword<< " is not an sub-dictionary of " << this->globalName()<<endl;
541  fatalExit;
542  return this;
543  }
544 }
545 
547 (
548  const word& keyword
549 )
550 {
551 
552  if( auto entry = findEntry(keyword); entry!= nullptr && entry->isDictionary() )
553  {
554  return dynamic_cast<dictionary&>(*entry);
555  }
556  else
557  {
559  "keyword " << keyword<< " is not an sub-dictionary of " << this->globalName()<<endl;
560  fatalExit;
561  return *this;
562  }
563 
564 }
565 
567 (
568  const word& keyword
569 ) const
570 {
571 
572  if( auto entry = findEntry(keyword); entry!= nullptr && entry->isDictionary() )
573  {
574  return dynamic_cast<dictionary&>(*entry);
575  }
576  else
577  {
579  "keyword " << keyword<< " is not an sub-dictionary of " << this->globalName()<<endl;
580  fatalExit;
581  return *this;
582  }
583 }
584 
586 {
587  if( auto entry = findEntry(keyword); entry && !entry->isDictionary() )
588  {
589  return dynamic_cast<dataEntry*>(entry);
590  }
591  else
592  {
594  "keyword " << keyword<< " is not a dataEntry of " << this->globalName()<<endl;
595  fatalExit;
596  return nullptr;
597  }
598 }
599 
601 {
602  if( auto entry = findEntry(keyword); entry && !entry->isDictionary() )
603  {
604  return dynamic_cast<dataEntry&>(*entry);
605  }
606  else
607  {
609  "keyword " << keyword<< " is not a dataEntry of " << this->globalName()<<endl;
610  fatalExit;
611  return dataEntry::nullDataEntry;
612  }
613 }
614 
616 (
617  const word& keyword
618 )const
619 {
620  if( auto entry = findEntry(keyword); entry && !entry->isDictionary() )
621  {
622  return dynamic_cast<dataEntry&>(*entry);
623  }
624  else
625  {
627  "keyword " << keyword<< " is not a dataEntry of " << this->globalName()<<endl;
628  fatalExit;
629  return dataEntry::nullDataEntry;
630  }
631 }
632 
634 (
635  const word& keyword
636 )
637 {
638  if( auto entry = findEntry(keyword); entry!= nullptr && entry->isDictionary() )
639  {
640  return dynamic_cast<dictionary&>(*entry);
641  }
642  else
643  {
644  uniquePtr<iEntry> ptr = makeUnique<dictionary>(keyword, *this);
645  if( addPtr
646  (
647  keyword,
648  ptr
649  )
650  )
651  {
652  return subDictOrCreate(keyword);
653  }
654  else
655  {
657  "Unable to create sub-dictionary "<< keyword << " in dictionary " << globalName() <<endl;
658  fatalExit;
659  }
660  }
661  return *this;
662 }
663 
665 {
666  return entries_.size();
667 }
668 
670 {
671  size_t num = 0;
672  for(auto& e:entries_)
673  {
674  if( e.second && !e.second->isDictionary())
675  {
676  num++;
677  }
678  }
679  return num;
680 }
681 
683 {
684  size_t num = 0;
685  for(auto& e:entries_)
686  {
687  if( e.second && e.second->isDictionary())
688  {
689  num++;
690  }
691  }
692  return num;
693 }
694 
696 {
697  wordList wl;
698 
699  for(auto oe:orderedEntries_)
700  {
701  if(oe) wl.push_back( oe->keyword() );
702  }
703  return wl;
704 }
705 
707 {
708  wordList wl;
709 
710  for(auto oe:orderedEntries_)
711  {
712  if( oe && !oe->isDictionary())
713  {
714  wl.push_back(oe->keyword());
715  }
716  }
717  return wl;
718 }
719 
720 // return a list of all dictionary keywords
722 {
723  wordList wl;
724 
725  for(auto& oe:orderedEntries_)
726  {
727  if( oe && oe->isDictionary())
728  {
729  wl.push_back(oe->keyword());
730  }
731  }
732  return wl;
733 }
734 
736 (
737  const word& name
738 )const
739 {
740  if( auto ptr = findEntry(name); ptr)
741  {
742  return ptr->isDictionary();
743  }
744  return false;
745 }
746 
748 (
749  const word& name
750 )const
751 {
752  if( auto ptr = findEntry(name); ptr)
753  {
754  return !ptr->isDictionary();
755  }
756  return false;
757 }
758 
760 {
761  token tok;
762  if(!isFileDict() && !readKeyword(is, keyword_, tok))
763  {
764  ioErrorInFile(is.name(), is.lineNumber()) <<
765  "expected a valid keyword for dictionary but found " << tok <<endl;
766  fatalExit;
767  }
768 
769  if( !readDictionary(is) )
770  {
771  ioErrorInFile(is.name(), is.lineNumber()) <<
772  "error in reading dictionary " << keyword_ <<endl;
773  fatalExit;
774  }
775 
776  return true;
777 }
778 
779 
781 {
782  if(! writeDictionary(os, !isFileDict()) )
783  {
784  ioErrorInFile( os.name(), os.lineNumber());
785  fatalExit;
786  }
787  return true;
788 }
789 
790 
792 {
793  return makeUnique<dictionary>(*this);
794 }
795 
796 
798 {
799 
800  auto ptr = makeUnique<dictionary>(*this);
801  return ptr.release();
802 }
803 
805 (
806  const dictionary& parDict
807 ) const
808 {
809  return makeUnique<dictionary>(this->keyword(), parDict, *this);
810 }
811 
813 (
814  const dictionary& parDict
815 ) const
816 {
817  auto ptr = makeUnique<dictionary>(this->keyword(), parDict, *this);
818  return ptr.release();
819 }
pFlow::IOstream::eof
bool eof() const
Definition: IOstream.hpp:156
pFlow::iOstream::beginBlock
virtual iOstream & beginBlock(const word &kw)
Definition: iOstream.cpp:70
pFlow::dictionary::orderedEntries_
List< iEntry * > orderedEntries_
Definition: dictionary.hpp:53
pFlow::List< word >
pFlow::iOstream::write
virtual bool write(const token &tok)=0
pFlow::iEntry::clone
virtual uniquePtr< iEntry > clone() const =0
fatalExit
#define fatalExit
Definition: error.hpp:57
pFlow::token
Definition: token.hpp:42
pFlow::dictionary::write
virtual bool write(iOstream &os) const
Definition: dictionary.cpp:780
pFlow::iOstream::endBlock
virtual iOstream & endBlock()
Definition: iOstream.cpp:88
pFlow::dictionary::allKeywords
wordList allKeywords() const
Definition: dictionary.cpp:695
warningInFunction
#define warningInFunction
Definition: error.hpp:55
pFlow::dictionary::findEntry
iEntry * findEntry(const word &keyword)
Definition: dictionary.cpp:106
pFlow::dictionary::read
virtual bool read(iIstream &is)
Definition: dictionary.cpp:759
pFlow::uint32
unsigned int uint32
Definition: builtinTypes.hpp:59
pFlow::dictionary::isGlobal_
bool isGlobal_
Definition: dictionary.hpp:58
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::Istream::name
virtual const word & name() const
Definition: Istream.hpp:78
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
pFlow::iFstream
Definition: iFstream.hpp:35
pFlow::dictionary::clonePtr
virtual iEntry * clonePtr() const
Definition: dictionary.cpp:797
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::fileSystem
Definition: fileSystem.hpp:63
pFlow::dictionary::containsDictionay
bool containsDictionay(const word &name) const
Definition: dictionary.cpp:736
uniquePtr.hpp
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
dictionary.hpp
pFlow::dictionary::writeDictionary
bool writeDictionary(iOstream &os, bool withBlock=true) const
Definition: dictionary.cpp:87
pFlow::iIstream::putBack
void putBack(const token &tok)
Definition: iIstream.cpp:5
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::IOstream::name
virtual const word & name() const
Definition: IOstream.cpp:31
pFlow::dictionary::subDict
dictionary & subDict(const word &keyword)
Definition: dictionary.cpp:547
streams.hpp
pFlow::IOstream::good
bool good() const
Definition: IOstream.hpp:150
pFlow::dataEntry::nullDataEntry
static dataEntry nullDataEntry
Definition: dataEntry.hpp:66
pFlow::uniquePtr
Definition: uniquePtr.hpp:44
pFlow::dictionary::addPtr
bool addPtr(const word &keyword, uniquePtr< iEntry > &etry)
Definition: dictionary.cpp:379
pFlow::groupNames
word groupNames(const word &bw, const word &tw, char sep='.')
Definition: bTypesFunctions.cpp:151
ioErrorInFile
#define ioErrorInFile(fileName, lineNumber)
Definition: error.hpp:49
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::IOstream::lineNumber
int32 lineNumber() const
Definition: IOstream.hpp:187
pFlow::fileSystem::wordPath
word wordPath() const
Definition: fileSystem.hpp:126
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
error.hpp