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

dictionary.hpp
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
#ifndef __dictionary_hpp__
#define __dictionary_hpp__
#include "types.hpp"
#include "iEntry.hpp"
#include "dataEntry.hpp"
#include "MapPtr.hpp"
#include "List.hpp"
#include "fileSystem.hpp"
namespace pFlow
{
class dictionary
:
public iEntry
{
protected:
// global name of dictionary, separated with dots
word name_;
// all the entries (data and dictionary) of the current dictionary
wordOrderedMapPtr<iEntry> entries_;
// entries in order of insertion
List<iEntry*> orderedEntries_;
// - ref to parrent dictionary
const dictionary& parDict_;
bool isGlobal_;
// - find an entry based on keyword
// return nullptr if not found
iEntry* findEntry(const word& keyword);
// - find and entry based on keyword
// return nullptr if not found
iEntry* findEntry(const word& keyword)const;
// - reads a dataEntry with keyword from dictionary
template<typename T>
bool readDataEntry( const word& keyword, T& val)const;
// - read dictionary from stream - without keyword
bool readDictionary(iIstream & is);
// - write dictionary to stream - with keyword
bool writeDictionary(iOstream& os, bool withBlock = true)const;
public:
// null dictionary object, to be used for references
static dictionary nullDict;
TypeInfo("dictionary");
// - cunstructs a null dictionary
dictionary();
// - construct an empty dictionary with keyword
dictionary(const word& keyword);
// - construct an empty dictionary with keyword and make it global/fileDictionary (if true)
dictionary(const word& keyword, bool global);
// - construct a dictionary with name and read it from file
dictionary(const word& keyword, const fileSystem& file);
// - cunstruct an empty dictionary with keyword and parDict
dictionary(const word& keyword, const dictionary& parDict);
// - cunstruct a dictionary with keyword and parDict and from stream
dictionary( const word& keyword, const dictionary& parDict, iIstream& is);
// - copy construct with keyword and new parrent dict
// discard the keyword and parDict of dict
dictionary(const word& keyword, const dictionary& parDict, const dictionary& dict);
// - copy construct as default behavior
// entries_ are copied smoothly. set parrent dict to nullDict
dictionary(const dictionary& );
// - assignment preserve name of this dictionary
// - only entries are transfered with ownership
dictionary& operator=(const dictionary& rhs);
// - pointer to this dictionary
virtual dictionary* dictPtr();
// - pointer to this dictionary
virtual const dictionary* dictPtr() const;
// - if this is a dictionary
virtual bool isDictionary() const;
// - global name of entry, separated with dots
virtual word globalName()const;
// - const ref to parrent dictionary
virtual const dictionary& parrentDict() const;
// - ref to this dictionary, if it is a dictionary
virtual dictionary& dict();
// - const ref to this dictionary, if it is a dictionary
virtual const dictionary& dict() const;
// - if dictionary is file dictionary, return false
virtual bool isFileDict()const;
// - add a pointer entry (dictionary/dataEntry)
// replaces this entry with existing one
bool addPtr(const word& keyword, uniquePtr<iEntry>& etry );
// - add a float dataEntry
bool add(const word& keyword, const float& v);
// - add a double dataEntry
bool add(const word& keyword, const double& v);
// - add a word dataEntry
bool add(const word& keyword, const word& v);
// - add a int64 dataEntry
bool add(const word& keyword, const int64& v);
// - add a int32 dataEntry
bool add(const word& keyword, const int32& v);
// - add a int16 dataEntry
bool add(const word& keyword, const int16& v);
// - add a int8 dataEntry
bool add(const word& keyword, const int8& v);
// - add a label dataEntry
bool add(const word& keyword, const label& v);
// - add a uint32 dataEntry
bool add(const word& keyword, const uint32& v);
// add a dictionary with the specifiedd keyword
bool addDict(const word& keyword, const dictionary& dict);
// - add a dataEntry of type T
template<typename T>
bool add(const word& keyword, const T& v );
void clear();
// - pointer to a subdictionary
// fatalExit if not found
dictionary* subDictPtr(const word& keyword);
// - ref to a subdictioanry
// fatalExit if not found
dictionary& subDict(const word& keyword);
// - const ref to a subdictioanry
// fatalExit if not found
const dictionary& subDict(const word& keyword) const;
// - pointer to a dataEntry
// fatalExit if not found/not a dataEntry
dataEntry* dataEntryPtr(const word& keyword);
// - ref to a subdictioanry
// fatalExit if not found/not a dataEntry
dataEntry& dataEntryRef(const word& keyword);
// - const ref to a subdictioanry
// fatalExit if not found/not a dataEntry
const dataEntry& dataEntryRef(const word& keyword)const;
// - search for a sub-dict with keyword
// create a new sub-dict if not found and return a ref to it
// fatalExit if fails
dictionary& subDictOrCreate(const word& keyword);
// - get the value of data entry
template<typename T>
T getVal(const word& keyword) const;
// - get the value of data entry or
// if not found, set the value to setVal
template<typename T>
T getValOrSet(const word& keyword, const T& setVal)const;
// return number of entris in this dictionary
size_t numEntries()const;
// return number of non-nullptr dataEntries
size_t numDataEntries()const;
// return number of non-nullptr dictionaries
size_t numDictionaries()const;
// return all keywords (non-nullptr) in this dictionary
wordList allKeywords()const;
// return a list of all dataEntries (non-nullptr) keywords
wordList dataEntryKeywords()const;
// return a list of all dictionary (non-null) keywords
wordList dictionaryKeywords()const;
// check if a sub-dictionary exists
bool containsDictionay(const word& name)const;
// check if a data entry exist
bool containsDataEntry(const word& name)const;
// clone polymorphic object (here dictionary)
virtual uniquePtr<iEntry> clone() const;
virtual iEntry* clonePtr() const;
// - clone the polymorhpic object with parDict as the new parrent dictionary
virtual uniquePtr<iEntry> clone(const dictionary& parDict)const;
virtual iEntry* clonePtr(const dictionary& parDict) const;
// - read from stream
virtual bool read(iIstream& is);
// - write to stream
virtual bool write(iOstream& os) const;
};
template<typename T>
bool dictionary::add(const word& keyword, const T& v )
{
uniquePtr<iEntry> ptr = makeUnique<dataEntry>(keyword, *this ,v);
return addPtr(keyword, ptr);
}
template<typename T>
bool dictionary::readDataEntry
(
const word& keyword,
T& val
)const
{
if( auto entry = findEntry(keyword); entry!= nullptr && !entry->isDictionary() )
{
iTstream& is = dynamic_cast< dataEntry&>(*entry).stream();
is >> val;
return true;
}
else
{
return false;
}
}
template<typename T>
T dictionary::getVal
(
const word& keyword
)const
{
T val{};
if(!readDataEntry(keyword, val))
{
fatalErrorInFunction <<
"cannot find dataEntry "<< keyword <<" in dictionary "<< globalName()<<endl;
fatalExit;
}
return val;
}
template<typename T>
T dictionary::getValOrSet
(
const word& keyword,
const T& setVal
)const
{
T val{};
if( readDataEntry(keyword, val) )
{
return val;
}
else
{
return setVal;
}
}
}
#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