dictionary gets xfer constructor and transfer() method(s)

This commit is contained in:
Mark Olesen
2009-01-02 15:50:32 +01:00
parent a010121427
commit 5e90a0ddc9
8 changed files with 125 additions and 90 deletions

View File

@ -42,11 +42,20 @@ using namespace Foam;
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
{ {
dictionary dict(IFstream("testDict")()); dictionary dict1(IFstream("testDict")());
Info<< "dict: " << dict << nl Info<< "dict1: " << dict1 << nl
<< "toc: " << dict.toc() << nl << "toc: " << dict1.toc() << nl
<< "keys: " << dict.keys() << nl << "keys: " << dict1.keys() << nl
<< "patterns: " << dict.keys(true) << endl; << "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;
} }

View File

@ -89,6 +89,12 @@ baz
} }
"anynumber.*"
{
$active
}
// this should work // this should work
#remove active #remove active

View File

@ -22,24 +22,15 @@ License
along with OpenFOAM; if not, write to the Free Software Foundation, along with OpenFOAM; if not, write to the Free Software Foundation,
Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 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 "Switch.H"
#include "error.H" #include "error.H"
#include "dictionary.H" #include "dictionary.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
namespace Foam
{
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
Switch Switch::lookupOrAddToDict Foam::Switch Foam::Switch::lookupOrAddToDict
( (
const word& name, const word& name,
dictionary& dict, dictionary& dict,
@ -52,52 +43,39 @@ Switch Switch::lookupOrAddToDict
// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * // // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
word Switch::wordValue(const bool l) const Foam::word Foam::Switch::wordValue(const bool val) const
{ {
word w("off"); return word(val ? "on" : "off");
if (l)
{
w = "on";
}
return w;
} }
bool Switch::boolValue(const word& w) const bool Foam::Switch::boolValue(const word& val) const
{ {
bool l = true; if (val == "on" || val == "true" || val == "yes" || val == "y")
if (w == "on" || w == "yes" || w == "y" || w == "true")
{ {
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 else
{ {
FatalErrorIn("Switch::boolValue(const word& w) const") FatalErrorIn("Switch::boolValue(const word&) const")
<< "unknown switch word " << w << "unknown switch word " << val
<< abort(FatalError); << abort(FatalError);
} }
return l; return false;
} }
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * 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_); return dict.readIfPresent(name, logicalSwitch_);
} }
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
} // End namespace Foam
// ************************************************************************* // // ************************************************************************* //

View File

@ -26,8 +26,8 @@ Class
Foam::Switch Foam::Switch
Description Description
A simple wrapper around bool so that it can be read as A simple wrapper around bool so that it can be read as a word:
on/off, yes/no or y/n. on/off, true/false, yes/no or y/n.
SourceFiles SourceFiles
Switch.C Switch.C
@ -68,6 +68,7 @@ class Switch
// Private member functions // Private member functions
// could be static instead:
word wordValue(const bool) const; word wordValue(const bool) const;
bool boolValue(const word&) const; bool boolValue(const word&) const;

View File

@ -22,23 +22,14 @@ License
along with OpenFOAM; if not, write to the Free Software Foundation, along with OpenFOAM; if not, write to the Free Software Foundation,
Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 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 "Switch.H"
#include "IOstreams.H" #include "IOstreams.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
namespace Foam
{
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
Switch::Switch(Istream& is) Foam::Switch::Switch(Istream& is)
: :
wordSwitch_(is), wordSwitch_(is),
logicalSwitch_(boolValue(wordSwitch_)) logicalSwitch_(boolValue(wordSwitch_))
@ -47,29 +38,25 @@ Switch::Switch(Istream& is)
// * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * // // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
Istream& operator>>(Istream& is, Switch& s) Foam::Istream& Foam::operator>>(Istream& is, Switch& s)
{ {
is >> s.wordSwitch_; is >> s.wordSwitch_;
s.logicalSwitch_ = s.boolValue(s.wordSwitch_); s.logicalSwitch_ = s.boolValue(s.wordSwitch_);
is.check("Istream& operator>>(Istream& is, Switch& s)"); is.check("Istream& operator>>(Istream&, Switch&)");
return is; return is;
} }
Ostream& operator<<(Ostream& os, const Switch& s) Foam::Ostream& Foam::operator<<(Ostream& os, const Switch& s)
{ {
os << s.wordSwitch_; os << s.wordSwitch_;
os.check("Ostream& operator<<(Ostream& os, const Switch& s)"); os.check("Ostream& operator<<(Ostream&, const Switch&)");
return os; return os;
} }
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
} // End namespace Foam
// ************************************************************************* // // ************************************************************************* //

View File

@ -167,6 +167,29 @@ Foam::dictionary::dictionary
} }
Foam::dictionary::dictionary
(
const dictionary& parentDict,
const xfer<dictionary>& dict
)
:
parent_(parentDict)
{
transfer(dict());
}
Foam::dictionary::dictionary
(
const xfer<dictionary>& dict
)
:
parent_(dictionary::null)
{
transfer(dict());
}
Foam::autoPtr<Foam::dictionary> Foam::dictionary::clone() const Foam::autoPtr<Foam::dictionary> Foam::dictionary::clone() const
{ {
return autoPtr<dictionary>(new dictionary(*this)); return autoPtr<dictionary>(new dictionary(*this));
@ -756,17 +779,15 @@ bool Foam::dictionary::merge(const dictionary& dict)
++iter ++iter
) )
{ {
const word& keyword = iter().keyword(); HashTable<entry*>::iterator fnd = hashedEntries_.find(iter().keyword());
HashTable<entry*>::iterator iter2 = hashedEntries_.find(keyword); if (fnd != hashedEntries_.end())
if (iter2 != hashedEntries_.end())
{ {
// Recursively merge sub-dictionaries // Recursively merge sub-dictionaries
// TODO: merge without copying // 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; 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<entry>::transfer(dict);
hashedEntries_.transfer(dict.hashedEntries_);
patternEntries_.transfer(dict.patternEntries_);
patternRegexps_.transfer(dict.patternRegexps_);
}
Foam::xfer<Foam::dictionary> Foam::dictionary::transfer()
{
Foam::xfer<dictionary> xf;
xf().transfer(*this);
return xf;
}
// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
Foam::ITstream& Foam::dictionary::operator[](const word& keyword) const 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 // Check for assignment to self
if (this == &dict) if (this == &rhs)
{ {
FatalErrorIn("dictionary::operator=(const dictionary&)") FatalErrorIn("dictionary::operator=(const dictionary&)")
<< "attempted assignment to self for dictionary " << name() << "attempted assignment to self for dictionary " << name()
<< abort(FatalError); << abort(FatalError);
} }
name_ = dict.name(); name_ = rhs.name();
clear(); clear();
// Create clones of the entries in the given dictionary // Create clones of the entries in the given dictionary
@ -824,8 +866,8 @@ void Foam::dictionary::operator=(const dictionary& dict)
for for
( (
IDLList<entry>::const_iterator iter = dict.begin(); IDLList<entry>::const_iterator iter = rhs.begin();
iter != dict.end(); iter != rhs.end();
++iter ++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 // Check for assignment to self
if (this == &dict) if (this == &rhs)
{ {
FatalErrorIn("dictionary::operator+=(const dictionary&)") FatalErrorIn("dictionary::operator+=(const dictionary&)")
<< "attempted addition assignment to self for dictionary " << name() << "attempted addition assignment to self for dictionary " << name()
@ -846,8 +888,8 @@ void Foam::dictionary::operator+=(const dictionary& dict)
for for
( (
IDLList<entry>::const_iterator iter = dict.begin(); IDLList<entry>::const_iterator iter = rhs.begin();
iter != dict.end(); iter != rhs.end();
++iter ++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 // Check for assignment to self
if (this == &dict) if (this == &rhs)
{ {
FatalErrorIn("dictionary::operator|=(const dictionary&)") FatalErrorIn("dictionary::operator|=(const dictionary&)")
<< "attempted assignment to self for dictionary " << name() << "attempted assignment to self for dictionary " << name()
@ -868,8 +910,8 @@ void Foam::dictionary::operator|=(const dictionary& dict)
for for
( (
IDLList<entry>::const_iterator iter = dict.begin(); IDLList<entry>::const_iterator iter = rhs.begin();
iter != dict.end(); iter != rhs.end();
++iter ++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 // Check for assignment to self
if (this == &dict) if (this == &rhs)
{ {
FatalErrorIn("dictionary::operator<<=(const dictionary&)") FatalErrorIn("dictionary::operator<<=(const dictionary&)")
<< "attempted assignment to self for dictionary " << name() << "attempted assignment to self for dictionary " << name()
@ -893,8 +935,8 @@ void Foam::dictionary::operator<<=(const dictionary& dict)
for for
( (
IDLList<entry>::const_iterator iter = dict.begin(); IDLList<entry>::const_iterator iter = rhs.begin();
iter != dict.end(); iter != rhs.end();
++iter ++iter
) )
{ {

View File

@ -166,11 +166,17 @@ public:
//- Construct top-level dictionary as copy //- Construct top-level dictionary as copy
dictionary(const dictionary&); dictionary(const dictionary&);
//- Construct by transferring parameter contents given parent dictionary
dictionary(const dictionary& parentDict, const xfer<dictionary>&);
//- Construct top-level dictionary by transferring parameter contents
dictionary(const xfer<dictionary>&);
//- Construct and return clone //- Construct and return clone
Foam::autoPtr<dictionary> clone() const; autoPtr<dictionary> clone() const;
//- Construct top-level dictionary on freestore from Istream //- Construct top-level dictionary on freestore from Istream
static Foam::autoPtr<dictionary> New(Istream&); static autoPtr<dictionary> New(Istream&);
// Destructor // Destructor
@ -385,6 +391,12 @@ public:
//- Clear the dictionary //- Clear the dictionary
void clear(); void clear();
//- Transfer the contents of the argument and annull the argument.
void transfer(dictionary&);
//- Transfer the contents to the xfer container
xfer<dictionary> transfer();
// Write // Write

View File

@ -71,22 +71,22 @@ bool Foam::dictionary::substituteKeyword(const word& keyword)
{ {
word varName = keyword(1, keyword.size()-1); 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); 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) if (ePtr != NULL)
{ {
const dictionary& addDict = ePtr->dict(); const dictionary& addDict = ePtr->dict();
for for
( (
IDLList<entry>::const_iterator addIter = addDict.begin(); IDLList<entry>::const_iterator iter = addDict.begin();
addIter != addDict.end(); iter != addDict.end();
++addIter ++iter
) )
{ {
add(addIter()); add(iter());
} }
return true; return true;