# PhasicFlow: src/phasicFlow/dictionary/dictionary.cpp Source File

dictionary.cpp
Go to the documentation of this file.
/*------------------------------- phasicFlow ---------------------------------
O C enter of
O O E ngineering and
O O M ultiscale modeling of
OOOOOOO F luid flow
------------------------------------------------------------------------------
Copyright (C): www.cemf.ir
email: hamid.r.norouzi AT gmail.com
------------------------------------------------------------------------------
Licence:
This file is part of phasicFlow code. It is a free software for simulating
granular and multiphase flows. You can redistribute it and/or modify it under
the terms of GNU General Public License v3 or any other later versions.
phasicFlow is distributed to help others in their research in the field of
granular and multiphase flows, but WITHOUT ANY WARRANTY; without even the
implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
-----------------------------------------------------------------------------*/
// based on OpenFOAM dictionary, with some modifications/simplifications
// to be tailored to our needs
#include "dictionary.hpp"
#include "uniquePtr.hpp"
#include "error.hpp"
#include "streams.hpp"
pFlow::dictionary pFlow::dictionary::nullDict;
// better to move it to the abstract class
// with an static member function.
bool pFlow::dictionary::readDictionary
(
iIstream & is
)
{
// ensure that the entry list of new the dicrionary is empty
entries_.clear();
// empty dictionary
if (is.eof())
{
return true;
}
if (!is.good())
{
ioErrorInFile( is.name(), is.lineNumber() )<<
"iIstream is not good for reading tokens .... \n";
return false;
}
// reads tokens one-by-one
token nextTok(is);
if( nextTok == token::END_BLOCK)
{
ioErrorInFile( is.name(), is.lineNumber() )<<
"Not expecting a } for the start of a dictionray \n";
return false;
}
bool hasBlockToken = true;
// this is a sub-dict, so it should start with {
if(nextTok != token::BEGIN_BLOCK)
{
hasBlockToken = false;
is.putBack(nextTok);
}
//read all entries in ths dictionary
while ( !is.eof() && iEntry::createEntry(*this, is, hasBlockToken) )
{}
return true;
}
bool pFlow::dictionary::writeDictionary
(
iOstream& os,
bool withBlock
)const
{
if(withBlock) os.beginBlock(keyword());
for( const auto& e:orderedEntries_)
{
if(e != nullptr )
e->write(os);
}
if(withBlock) os.endBlock();
return true;
}
pFlow::iEntry* pFlow::dictionary::findEntry
(
const word& keyword
)
{
if( auto [ptr, exist] = entries_.find(keyword); exist)
{
return ptr;
}
return nullptr;
}
pFlow::iEntry* pFlow::dictionary::findEntry
(
const word& keyword
)const
{
if( auto [ptr, exist] = entries_.find(keyword); exist)
{
return const_cast< iEntry*> (ptr);
}
return nullptr;
}
pFlow::dictionary::dictionary
()
:
iEntry("NULL_DICT"),
name_("NULL_DICT"),
entries_(),
orderedEntries_(),
parDict_(dictionary::nullDict),
isGlobal_(false)
{}
pFlow::dictionary::dictionary
(
const word& keyword
)
:
iEntry(keyword),
name_(keyword),
entries_(),
orderedEntries_(),
parDict_(dictionary::nullDict),
isGlobal_(false)
{}
pFlow::dictionary::dictionary
(
const word& keyword,
bool global
)
:
iEntry(keyword),
name_(keyword),
entries_(),
orderedEntries_(),
parDict_(dictionary::nullDict),
isGlobal_(global)
{
}
pFlow::dictionary::dictionary
(
const word& keyword,
const fileSystem& file
)
:
iEntry(keyword),
name_(file.wordPath()),
entries_(),
orderedEntries_(),
parDict_(dictionary::nullDict),
isGlobal_(true)
{
iFstream dictStream(file);
if(!read(dictStream))
{
ioErrorInFile(dictStream.name(), dictStream.lineNumber())<<
"error in reading dictionary from file "<< file <<endl;
fatalExit;
}
}
pFlow::dictionary::dictionary
(
const word& keyword,
const dictionary& parDict
)
:
iEntry(keyword),
name_(groupNames(parDict.globalName(), keyword)),
entries_(),
orderedEntries_(),
parDict_(parDict),
isGlobal_(false)
{
}
pFlow::dictionary::dictionary
(
const word& keyword,
const dictionary& parDict,
iIstream& is
)
:
iEntry(keyword),
name_(groupNames(parDict.globalName(), keyword)),
entries_(),
orderedEntries_(),
parDict_(parDict),
isGlobal_(false)
{
if( !readDictionary(is) )
{
ioErrorInFile(is.name(), is.lineNumber()) <<
"error in reading dictionary " << name_ <<endl;
fatalExit;
}
}
pFlow::dictionary::dictionary
(
const word& keyword,
const dictionary& parDict,
const dictionary& dict
)
:
iEntry(keyword),
name_(groupNames(parDict.globalName(),keyword)),
entries_(),
orderedEntries_(),
parDict_(parDict),
isGlobal_(false)
{
for( auto& entry : dict.orderedEntries_ )
{
if(entry)
{
auto ptr = entry->clone(*this);
auto key = entry->name();
if( !addPtr(key, ptr) )
{
fatalErrorInFunction <<
" error in cloning dicrionary / dataEntry " << entry->globalName() <<endl;
fatalExit;
}
}
}
}
pFlow::dictionary::dictionary
(
const dictionary& src
)
:
iEntry(src.keyword()),
name_(src.keyword()),
entries_(),
orderedEntries_(),
parDict_(dictionary::nullDict),
isGlobal_(src.isGlobal_)
{
for( auto& entry: src.orderedEntries_)
{
if(entry)
{
auto ptr = entry->clone(*this);
auto key = entry->name();
if( !addPtr(key, ptr) )
{
fatalErrorInFunction <<
" error in cloning dicrionary / dataEntry " << entry->globalName() <<endl;
fatalExit;
}
}
}
}
pFlow::dictionary& pFlow::dictionary::operator=
(
const dictionary& rhs
)
{
if( &rhs == this)return *this;
clear();
for( auto& entry: rhs.orderedEntries_)
{
if(entry)
{
auto ptr = entry->clone(*this);
auto key = entry->name();
if( !addPtr(key, ptr) )
{
fatalErrorInFunction <<
" error in cloning dicrionary / dataEntry " << entry->globalName() <<endl;
fatalExit;
}
}
}
return *this;
}
pFlow::dictionary* pFlow::dictionary::dictPtr()
{
return this;
}
const pFlow::dictionary* pFlow::dictionary::dictPtr() const
{
return this;
}
bool pFlow::dictionary::isDictionary() const
{
return true;
}
pFlow::word pFlow::dictionary::globalName()const
{
return name_;
}
const pFlow::dictionary& pFlow::dictionary::parrentDict() const
{
return parDict_;
}
pFlow::dictionary& pFlow::dictionary::dict()
{
return *this;
}
const pFlow::dictionary& pFlow::dictionary::dict() const
{
return *this;
}
bool pFlow::dictionary::isFileDict()const
{
return isGlobal_;
}
bool pFlow::dictionary::addPtr
(
const word& keyword,
uniquePtr<iEntry>& entry
)
{
if(entry == nullptr) return false;
iEntry* oldEntryPtr = nullptr;
iEntry* newEntryPtr = entry.get();
// search all entries for repeated keyword
if(auto [ptr, exist] = entries_.find(keyword); exist )
{
warningInFunction <<
"keyword " << keyword << " already exists in the dicrionary " <<
this->globalName() << ". The old entry will be replaced by the new one. \n";
// store the old pointer to entry
oldEntryPtr = ptr;
}
if( entries_.insertReplace(keyword, entry) )
{
if(oldEntryPtr)
{
// this should be replaced
auto oIter = orderedEntries_.find(oldEntryPtr);
*oIter = newEntryPtr;
}
else
{
orderedEntries_.push_back(newEntryPtr);
}
return true;
}else
{
return false;
}
}
bool pFlow::dictionary::add
(
const word& keyword,
const float& v
)
{
uniquePtr<iEntry> ptr = makeUnique<dataEntry>(keyword, *this, token(v));
return addPtr(keyword, ptr);
}
bool pFlow::dictionary::add
(
const word& keyword,
const double& v
)
{
uniquePtr<iEntry> ptr = makeUnique<dataEntry>(keyword, *this, token(v));
return addPtr(keyword, ptr);
}
bool pFlow::dictionary::add
(
const word& keyword,
const word& v
)
{
uniquePtr<iEntry> ptr = makeUnique<dataEntry>(keyword, *this, token(v));
return addPtr(keyword, ptr);
}
bool pFlow::dictionary::add
(
const word& keyword,
const int64& v
)
{
uniquePtr<iEntry> ptr = makeUnique<dataEntry>(keyword, *this, token(v));
return addPtr(keyword, ptr);
}
bool pFlow::dictionary::add
(
const word& keyword,
const int32& v
)
{
uniquePtr<iEntry> ptr = makeUnique<dataEntry>(keyword, *this, token(v));
return addPtr(keyword, ptr);
}
bool pFlow::dictionary::add
(
const word& keyword,
const int16& v
)
{
uniquePtr<iEntry> ptr = makeUnique<dataEntry>(keyword, *this, token(v));
return addPtr(keyword, ptr);
}
bool pFlow::dictionary::add
(
const word& keyword,
const int8& v
)
{
uniquePtr<iEntry> ptr = makeUnique<dataEntry>(keyword, *this, token(v));
return addPtr(keyword, ptr);
}
bool pFlow::dictionary::add
(
const word& keyword,
const label& v
)
{
uniquePtr<iEntry> ptr = makeUnique<dataEntry>(keyword, *this, token(v));
return addPtr(keyword, ptr);
}
bool pFlow::dictionary::add
(
const word& keyword,
const uint32& v
)
{
uniquePtr<iEntry> ptr = makeUnique<dataEntry>(keyword, *this, token(v));
return addPtr(keyword, ptr);
}
bool pFlow::dictionary::addDict
(
const word& keyword,
const dictionary& dict
)
{
uniquePtr<iEntry> ptr = makeUnique<dictionary>(keyword, *this, dict);
return addPtr(keyword, ptr);
}
void pFlow::dictionary::clear()
{
orderedEntries_.clear();
entries_.clear();
}
pFlow::dictionary* pFlow::dictionary::subDictPtr
(
const word& keyword
)
{
if( auto entry = findEntry(keyword); entry!= nullptr && entry->isDictionary() )
{
return dynamic_cast< dictionary*>(entry);
}
else
{
fatalErrorInFunction<<
"keyword " << keyword<< " is not an sub-dictionary of " << this->globalName()<<endl;
fatalExit;
return this;
}
}
pFlow::dictionary& pFlow::dictionary::subDict
(
const word& keyword
)
{
if( auto entry = findEntry(keyword); entry!= nullptr && entry->isDictionary() )
{
return dynamic_cast< dictionary&>(*entry);
}
else
{
fatalErrorInFunction<<
"keyword " << keyword<< " is not an sub-dictionary of " << this->globalName()<<endl;
fatalExit;
return *this;
}
}
const pFlow::dictionary& pFlow::dictionary::subDict
(
const word& keyword
) const
{
if( auto entry = findEntry(keyword); entry!= nullptr && entry->isDictionary() )
{
return dynamic_cast< dictionary&>(*entry);
}
else
{
fatalErrorInFunction<<
"keyword " << keyword<< " is not an sub-dictionary of " << this->globalName()<<endl;
fatalExit;
return *this;
}
}
pFlow::dataEntry* pFlow::dictionary::dataEntryPtr(const word& keyword)
{
if( auto entry = findEntry(keyword); entry && !entry->isDictionary() )
{
return dynamic_cast< dataEntry*>(entry);
}
else
{
fatalErrorInFunction<<
"keyword " << keyword<< " is not a dataEntry of " << this->globalName()<<endl;
fatalExit;
return nullptr;
}
}
pFlow::dataEntry& pFlow::dictionary::dataEntryRef(const word& keyword)
{
if( auto entry = findEntry(keyword); entry && !entry->isDictionary() )
{
return dynamic_cast< dataEntry&>(*entry);
}
else
{
fatalErrorInFunction<<
"keyword " << keyword<< " is not a dataEntry of " << this->globalName()<<endl;
fatalExit;
return dataEntry::nullDataEntry;
}
}
const pFlow::dataEntry& pFlow::dictionary::dataEntryRef
(
const word& keyword
)const
{
if( auto entry = findEntry(keyword); entry && !entry->isDictionary() )
{
return dynamic_cast< dataEntry&>(*entry);
}
else
{
fatalErrorInFunction<<
"keyword " << keyword<< " is not a dataEntry of " << this->globalName()<<endl;
fatalExit;
return dataEntry::nullDataEntry;
}
}
pFlow::dictionary& pFlow::dictionary::subDictOrCreate
(
const word& keyword
)
{
if( auto entry = findEntry(keyword); entry!= nullptr && entry->isDictionary() )
{
return dynamic_cast< dictionary&>(*entry);
}
else
{
uniquePtr<iEntry> ptr = makeUnique<dictionary>(keyword, *this);
if( addPtr
(
keyword,
ptr
)
)
{
return subDictOrCreate(keyword);
}
else
{
fatalErrorInFunction<<
"Unable to create sub-dictionary "<< keyword << " in dictionary " << globalName() <<endl;
fatalExit;
}
}
return *this;
}
size_t pFlow::dictionary::numEntries()const
{
return entries_.size();
}
size_t pFlow::dictionary::numDataEntries()const
{
size_t num = 0;
for(auto& e:entries_)
{
if( e.second && !e.second->isDictionary())
{
num++;
}
}
return num;
}
size_t pFlow::dictionary::numDictionaries()const
{
size_t num = 0;
for(auto& e:entries_)
{
if( e.second && e.second->isDictionary())
{
num++;
}
}
return num;
}
pFlow::wordList pFlow::dictionary::allKeywords()const
{
wordList wl;
for(auto oe:orderedEntries_)
{
if(oe) wl.push_back( oe->keyword() );
}
return wl;
}
pFlow::wordList pFlow::dictionary::dataEntryKeywords()const
{
wordList wl;
for(auto oe:orderedEntries_)
{
if( oe && !oe->isDictionary())
{
wl.push_back(oe->keyword());
}
}
return wl;
}
// return a list of all dictionary keywords
pFlow::wordList pFlow::dictionary::dictionaryKeywords()const
{
wordList wl;
for(auto& oe:orderedEntries_)
{
if( oe && oe->isDictionary())
{
wl.push_back(oe->keyword());
}
}
return wl;
}
bool pFlow::dictionary::containsDictionay
(
const word& name
)const
{
if( auto ptr = findEntry(name); ptr)
{
return ptr->isDictionary();
}
return false;
}
bool pFlow::dictionary::containsDataEntry
(
const word& name
)const
{
if( auto ptr = findEntry(name); ptr)
{
return !ptr->isDictionary();
}
return false;
}
bool pFlow::dictionary::read(iIstream& is)
{
token tok;
if(!isFileDict() && !readKeyword(is, keyword_, tok))
{
ioErrorInFile(is.name(), is.lineNumber()) <<
"expected a valid keyword for dictionary but found " << tok <<endl;
fatalExit;
}
if( !readDictionary(is) )
{
ioErrorInFile(is.name(), is.lineNumber()) <<
"error in reading dictionary " << keyword_ <<endl;
fatalExit;
}
return true;
}
bool pFlow::dictionary::write(iOstream& os) const
{
if(! writeDictionary(os, !isFileDict()) )
{
ioErrorInFile( os.name(), os.lineNumber());
fatalExit;
}
return true;
}
pFlow::uniquePtr<pFlow::iEntry> pFlow::dictionary::clone() const
{
return makeUnique<dictionary>(*this);
}
pFlow::iEntry* pFlow::dictionary::clonePtr() const
{
auto ptr = makeUnique<dictionary>(*this);
return ptr.release();
}
pFlow::uniquePtr<pFlow::iEntry> pFlow::dictionary::clone
(
const dictionary& parDict
) const
{
return makeUnique<dictionary>(this->keyword(), parDict, *this);
}
pFlow::iEntry* pFlow::dictionary::clonePtr
(
const dictionary& parDict
) const
{
auto ptr = makeUnique<dictionary>(this->keyword(), parDict, *this);
return ptr.release();
}
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