From 5e90a0ddc9e4bc1639ce9609ce9b0083e2d337db Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Fri, 2 Jan 2009 15:50:32 +0100 Subject: [PATCH] dictionary gets xfer constructor and transfer() method(s) --- applications/test/dictionary/dictionaryTest.C | 19 ++-- applications/test/dictionary/testDict | 6 ++ src/OpenFOAM/db/Switch/Switch.C | 46 +++------- src/OpenFOAM/db/Switch/Switch.H | 5 +- src/OpenFOAM/db/Switch/SwitchIO.C | 23 ++--- src/OpenFOAM/db/dictionary/dictionary.C | 88 ++++++++++++++----- src/OpenFOAM/db/dictionary/dictionary.H | 16 +++- src/OpenFOAM/db/dictionary/dictionaryIO.C | 12 +-- 8 files changed, 125 insertions(+), 90 deletions(-) diff --git a/applications/test/dictionary/dictionaryTest.C b/applications/test/dictionary/dictionaryTest.C index 7de5cf8037..0ac4c225fa 100644 --- a/applications/test/dictionary/dictionaryTest.C +++ b/applications/test/dictionary/dictionaryTest.C @@ -42,11 +42,20 @@ using namespace Foam; int main(int argc, char *argv[]) { { - dictionary dict(IFstream("testDict")()); - Info<< "dict: " << dict << nl - << "toc: " << dict.toc() << nl - << "keys: " << dict.keys() << nl - << "patterns: " << dict.keys(true) << endl; + dictionary dict1(IFstream("testDict")()); + Info<< "dict1: " << dict1 << nl + << "toc: " << dict1.toc() << nl + << "keys: " << dict1.keys() << nl + << "patterns: " << dict1.keys(true) << endl; + + dictionary dict2(dict1.transfer()); + + Info<< "dict1.toc(): " << dict1.name() << " " << dict1.toc() << nl + << "dict2.toc(): " << dict2.name() << " " << dict2.toc() << endl; + + // copy back + dict1 = dict2; + Info<< "dict1.toc(): " << dict1.name() << " " << dict1.toc() << endl; } diff --git a/applications/test/dictionary/testDict b/applications/test/dictionary/testDict index 9bd58c58ae..809625ee18 100644 --- a/applications/test/dictionary/testDict +++ b/applications/test/dictionary/testDict @@ -89,6 +89,12 @@ baz } +"anynumber.*" +{ + $active +} + + // this should work #remove active diff --git a/src/OpenFOAM/db/Switch/Switch.C b/src/OpenFOAM/db/Switch/Switch.C index 8072939178..5d395e8ff5 100644 --- a/src/OpenFOAM/db/Switch/Switch.C +++ b/src/OpenFOAM/db/Switch/Switch.C @@ -22,24 +22,15 @@ License along with OpenFOAM; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA -Description - A simple wrapper around bool so that it can - be read as on/off, yes/no or y/n. - \*---------------------------------------------------------------------------*/ #include "Switch.H" #include "error.H" #include "dictionary.H" -// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // - -namespace Foam -{ - // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // -Switch Switch::lookupOrAddToDict +Foam::Switch Foam::Switch::lookupOrAddToDict ( const word& name, dictionary& dict, @@ -52,52 +43,39 @@ Switch Switch::lookupOrAddToDict // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * // -word Switch::wordValue(const bool l) const +Foam::word Foam::Switch::wordValue(const bool val) const { - word w("off"); - - if (l) - { - w = "on"; - } - - return w; + return word(val ? "on" : "off"); } -bool Switch::boolValue(const word& w) const +bool Foam::Switch::boolValue(const word& val) const { - bool l = true; - - if (w == "on" || w == "yes" || w == "y" || w == "true") + if (val == "on" || val == "true" || val == "yes" || val == "y") { - l = true; + return true; } - else if (w == "off" || w == "no" || w == "n" || w == "false") + else if (val == "off" || val == "false" || val == "no" || val == "n") { - l = false; + return false; } else { - FatalErrorIn("Switch::boolValue(const word& w) const") - << "unknown switch word " << w + FatalErrorIn("Switch::boolValue(const word&) const") + << "unknown switch word " << val << abort(FatalError); } - return l; + return false; } // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * // -bool Switch::readIfPresent(const word& name, const dictionary& dict) +bool Foam::Switch::readIfPresent(const word& name, const dictionary& dict) { return dict.readIfPresent(name, logicalSwitch_); } -// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // - -} // End namespace Foam - // ************************************************************************* // diff --git a/src/OpenFOAM/db/Switch/Switch.H b/src/OpenFOAM/db/Switch/Switch.H index 3b2bb2aeed..4361d44e3e 100644 --- a/src/OpenFOAM/db/Switch/Switch.H +++ b/src/OpenFOAM/db/Switch/Switch.H @@ -26,8 +26,8 @@ Class Foam::Switch Description - A simple wrapper around bool so that it can be read as - on/off, yes/no or y/n. + A simple wrapper around bool so that it can be read as a word: + on/off, true/false, yes/no or y/n. SourceFiles Switch.C @@ -68,6 +68,7 @@ class Switch // Private member functions + // could be static instead: word wordValue(const bool) const; bool boolValue(const word&) const; diff --git a/src/OpenFOAM/db/Switch/SwitchIO.C b/src/OpenFOAM/db/Switch/SwitchIO.C index a2a4bec316..0f6686e854 100644 --- a/src/OpenFOAM/db/Switch/SwitchIO.C +++ b/src/OpenFOAM/db/Switch/SwitchIO.C @@ -22,23 +22,14 @@ License along with OpenFOAM; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA -Description - A simple wrapper around bool so that it can - be read as on/off, yes/no or y/n. - \*---------------------------------------------------------------------------*/ #include "Switch.H" #include "IOstreams.H" -// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // - -namespace Foam -{ - // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // -Switch::Switch(Istream& is) +Foam::Switch::Switch(Istream& is) : wordSwitch_(is), logicalSwitch_(boolValue(wordSwitch_)) @@ -47,29 +38,25 @@ Switch::Switch(Istream& is) // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * // -Istream& operator>>(Istream& is, Switch& s) +Foam::Istream& Foam::operator>>(Istream& is, Switch& s) { is >> s.wordSwitch_; s.logicalSwitch_ = s.boolValue(s.wordSwitch_); - is.check("Istream& operator>>(Istream& is, Switch& s)"); + is.check("Istream& operator>>(Istream&, Switch&)"); return is; } -Ostream& operator<<(Ostream& os, const Switch& s) +Foam::Ostream& Foam::operator<<(Ostream& os, const Switch& s) { os << s.wordSwitch_; - os.check("Ostream& operator<<(Ostream& os, const Switch& s)"); + os.check("Ostream& operator<<(Ostream&, const Switch&)"); return os; } -// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // - -} // End namespace Foam - // ************************************************************************* // diff --git a/src/OpenFOAM/db/dictionary/dictionary.C b/src/OpenFOAM/db/dictionary/dictionary.C index f5e6ad7c1f..4f7da54361 100644 --- a/src/OpenFOAM/db/dictionary/dictionary.C +++ b/src/OpenFOAM/db/dictionary/dictionary.C @@ -167,6 +167,29 @@ Foam::dictionary::dictionary } +Foam::dictionary::dictionary +( + const dictionary& parentDict, + const xfer& dict +) +: + parent_(parentDict) +{ + transfer(dict()); +} + + +Foam::dictionary::dictionary +( + const xfer& dict +) +: + parent_(dictionary::null) +{ + transfer(dict()); +} + + Foam::autoPtr Foam::dictionary::clone() const { return autoPtr(new dictionary(*this)); @@ -756,17 +779,15 @@ bool Foam::dictionary::merge(const dictionary& dict) ++iter ) { - const word& keyword = iter().keyword(); + HashTable::iterator fnd = hashedEntries_.find(iter().keyword()); - HashTable::iterator iter2 = hashedEntries_.find(keyword); - - if (iter2 != hashedEntries_.end()) + if (fnd != hashedEntries_.end()) { // Recursively merge sub-dictionaries // TODO: merge without copying - if (iter2()->isDict() && iter().isDict()) + if (fnd()->isDict() && iter().isDict()) { - if (iter2()->dict().merge(iter().dict())) + if (fnd()->dict().merge(iter().dict())) { changed = true; } @@ -798,6 +819,27 @@ void Foam::dictionary::clear() } +void Foam::dictionary::transfer(dictionary& dict) +{ + // changing parents probably doesn't make much sense, + // but what about the names? + name_ = dict.name_; + + IDLList::transfer(dict); + hashedEntries_.transfer(dict.hashedEntries_); + patternEntries_.transfer(dict.patternEntries_); + patternRegexps_.transfer(dict.patternRegexps_); +} + + +Foam::xfer Foam::dictionary::transfer() +{ + Foam::xfer xf; + xf().transfer(*this); + return xf; +} + + // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * // Foam::ITstream& Foam::dictionary::operator[](const word& keyword) const @@ -806,17 +848,17 @@ Foam::ITstream& Foam::dictionary::operator[](const word& keyword) const } -void Foam::dictionary::operator=(const dictionary& dict) +void Foam::dictionary::operator=(const dictionary& rhs) { // Check for assignment to self - if (this == &dict) + if (this == &rhs) { FatalErrorIn("dictionary::operator=(const dictionary&)") << "attempted assignment to self for dictionary " << name() << abort(FatalError); } - name_ = dict.name(); + name_ = rhs.name(); clear(); // Create clones of the entries in the given dictionary @@ -824,8 +866,8 @@ void Foam::dictionary::operator=(const dictionary& dict) for ( - IDLList::const_iterator iter = dict.begin(); - iter != dict.end(); + IDLList::const_iterator iter = rhs.begin(); + iter != rhs.end(); ++iter ) { @@ -834,10 +876,10 @@ void Foam::dictionary::operator=(const dictionary& dict) } -void Foam::dictionary::operator+=(const dictionary& dict) +void Foam::dictionary::operator+=(const dictionary& rhs) { // Check for assignment to self - if (this == &dict) + if (this == &rhs) { FatalErrorIn("dictionary::operator+=(const dictionary&)") << "attempted addition assignment to self for dictionary " << name() @@ -846,8 +888,8 @@ void Foam::dictionary::operator+=(const dictionary& dict) for ( - IDLList::const_iterator iter = dict.begin(); - iter != dict.end(); + IDLList::const_iterator iter = rhs.begin(); + iter != rhs.end(); ++iter ) { @@ -856,10 +898,10 @@ void Foam::dictionary::operator+=(const dictionary& dict) } -void Foam::dictionary::operator|=(const dictionary& dict) +void Foam::dictionary::operator|=(const dictionary& rhs) { // Check for assignment to self - if (this == &dict) + if (this == &rhs) { FatalErrorIn("dictionary::operator|=(const dictionary&)") << "attempted assignment to self for dictionary " << name() @@ -868,8 +910,8 @@ void Foam::dictionary::operator|=(const dictionary& dict) for ( - IDLList::const_iterator iter = dict.begin(); - iter != dict.end(); + IDLList::const_iterator iter = rhs.begin(); + iter != rhs.end(); ++iter ) { @@ -881,10 +923,10 @@ void Foam::dictionary::operator|=(const dictionary& dict) } -void Foam::dictionary::operator<<=(const dictionary& dict) +void Foam::dictionary::operator<<=(const dictionary& rhs) { // Check for assignment to self - if (this == &dict) + if (this == &rhs) { FatalErrorIn("dictionary::operator<<=(const dictionary&)") << "attempted assignment to self for dictionary " << name() @@ -893,8 +935,8 @@ void Foam::dictionary::operator<<=(const dictionary& dict) for ( - IDLList::const_iterator iter = dict.begin(); - iter != dict.end(); + IDLList::const_iterator iter = rhs.begin(); + iter != rhs.end(); ++iter ) { diff --git a/src/OpenFOAM/db/dictionary/dictionary.H b/src/OpenFOAM/db/dictionary/dictionary.H index 4528bea04c..5cdbfb56f3 100644 --- a/src/OpenFOAM/db/dictionary/dictionary.H +++ b/src/OpenFOAM/db/dictionary/dictionary.H @@ -166,11 +166,17 @@ public: //- Construct top-level dictionary as copy dictionary(const dictionary&); + //- Construct by transferring parameter contents given parent dictionary + dictionary(const dictionary& parentDict, const xfer&); + + //- Construct top-level dictionary by transferring parameter contents + dictionary(const xfer&); + //- Construct and return clone - Foam::autoPtr clone() const; + autoPtr clone() const; //- Construct top-level dictionary on freestore from Istream - static Foam::autoPtr New(Istream&); + static autoPtr New(Istream&); // Destructor @@ -385,6 +391,12 @@ public: //- Clear the dictionary void clear(); + //- Transfer the contents of the argument and annull the argument. + void transfer(dictionary&); + + //- Transfer the contents to the xfer container + xfer transfer(); + // Write diff --git a/src/OpenFOAM/db/dictionary/dictionaryIO.C b/src/OpenFOAM/db/dictionary/dictionaryIO.C index a5c650e150..243799231f 100644 --- a/src/OpenFOAM/db/dictionary/dictionaryIO.C +++ b/src/OpenFOAM/db/dictionary/dictionaryIO.C @@ -71,22 +71,22 @@ bool Foam::dictionary::substituteKeyword(const word& keyword) { word varName = keyword(1, keyword.size()-1); - // lookup the variable name in the given dictionary.... + // lookup the variable name in the given dictionary const entry* ePtr = lookupEntryPtr(varName, true, true); - // ...if defined insert its entries into this dictionary... + // if defined insert its entries into this dictionary if (ePtr != NULL) { const dictionary& addDict = ePtr->dict(); for ( - IDLList::const_iterator addIter = addDict.begin(); - addIter != addDict.end(); - ++addIter + IDLList::const_iterator iter = addDict.begin(); + iter != addDict.end(); + ++iter ) { - add(addIter()); + add(iter()); } return true;